Compare commits
156 Commits
developmen
...
master
Author | SHA1 | Date |
---|---|---|
Inex Code | 0aca77ea71 | |
Inex Code | 7567c69722 | |
Alexander | 62dfb006e4 | |
Alexander | 861a5dbdf8 | |
Alexander Tomokhov | 83c1e4d590 | |
Alexander | 2925c7e41c | |
Alexander | 5d93c8197e | |
Alexander | 77972cd020 | |
Alexander | 4d02caaf7e | |
Alexander | 0c8ada1a1b | |
Alexander | fd0e2fb105 | |
Alexander | 977658fb33 | |
Alexander | 2dd49d3596 | |
Alexander | 8f00617b3d | |
Alexander | 8ce494c490 | |
Alexander | 66f61ce536 | |
Alexander | 4866762afc | |
Alexander | f3696ab0d6 | |
Alexander | 774bea292f | |
Alexander | 743129d6cb | |
Alexander | 15f36551ad | |
Alexander | 4920434aec | |
Alexander | 91c22466b1 | |
Alexander | 6ece80adc2 | |
Alexander | 7cf0a1432d | |
Alexander | 36eb48ab3c | |
Alexander | 374711f324 | |
Alexander | 91073a50a3 | |
Alexander | 18c88f39cf | |
Alexander | eaafba6f65 | |
Alexander | 0ec4fc7d5b | |
Alexander | 2f9903599d | |
Alexander | 83fe55b86e | |
Alexander | c31521e1ca | |
Alexander | 967377f171 | |
Alexander | 659c539f38 | |
Alexander | 4df84db453 | |
Alexander | c95b9d0f62 | |
Alexander | 05e395d6af | |
Alexander | a6713df931 | |
Alexander | eec1c639ec | |
Alexander | 69dd80d065 | |
Alexander | 867396b274 | |
Alexander | 6171589cc3 | |
Alexander | a266532d59 | |
Alexander | b135e52863 | |
Alexander | ab35fc20b6 | |
Alexander | 1494e06c44 | |
Alexander | dcfc5f203e | |
Alexander | ec40186962 | |
Alexander | d099b7bd11 | |
Alexander | 9cd6207238 | |
Alexander | 2d54653924 | |
Alexander | 6f96b9afdf | |
Alexander | ec339bb8d1 | |
Alexander | 55cf8bc0a6 | |
Alexander | 30f5d85acd | |
Alexander | 09ee1f5e4c | |
Alexander | 3b4d75bcdb | |
Alexander | 4380a733a6 | |
Alexander | a717ad6ca5 | |
Alexander | 1344aaa92c | |
Alexander | 0e6d5414f5 | |
Alexander | 0857b96001 | |
Alexander | 7099e2eada | |
Alexander | 7d15460867 | |
Alexander | e3ad189d9a | |
Alexander | efad17f194 | |
Alexander | 0e44c12bef | |
Alexander | f3e19457ee | |
Alexander | d4e33a9639 | |
Alexander | 149d6a8dd2 | |
Alexander | 94421ee3d5 | |
Alexander | c130acf95a | |
Alexander | 1008f2aa51 | |
Alexander | b49b801baa | |
Alexander | fa062875e7 | |
Alexander | 528d5de6dd | |
Alexander | 158c711f66 | |
Alexander | ba181fb162 | |
Alexander | 8de4c0908f | |
Alexander | 9245d6584d | |
Alexander Tomokhov | 55149c1af0 | |
Alexander Tomokhov | ccf32a3172 | |
Alexander Tomokhov | ac5a9c3517 | |
Alexander Tomokhov | 79bb94d7e4 | |
Alexander Tomokhov | 40bade435b | |
Inex Code | 00f9141343 | |
Alexander Tomokhov | fd924f354f | |
Alexander Tomokhov | 66c0e15293 | |
Alexander Tomokhov | 00cf7c40a1 | |
Alexander Tomokhov | 6c0486034d | |
Alexander Tomokhov | aee0eab178 | |
Alexander Tomokhov | 670985f3d5 | |
Alexander Tomokhov | 212f2806f0 | |
Alexander Tomokhov | 347ee5ca12 | |
Alexander Tomokhov | 5fc8a090f7 | |
Alexander Tomokhov | 4cf9f98105 | |
Alexander Tomokhov | 8462fff065 | |
Alexander | cae1b64689 | |
Alexander Tomokhov | 401f6e0789 | |
Alexander Tomokhov | 6645c8e61b | |
Alexander Tomokhov | 7e79a26715 | |
Alexander Tomokhov | 5169969e03 | |
Alexander Tomokhov | bc0f59695c | |
Alexander Tomokhov | 73b5863968 | |
Alexander Tomokhov | ce8d1f8404 | |
Alexander Tomokhov | 31dfa8eb4a | |
Alexander Tomokhov | c58597c4d7 | |
Alexander Tomokhov | daf4390cc9 | |
Alexander Tomokhov | fdfd074e27 | |
Alexander Tomokhov | eeb9b257bf | |
Alexander Tomokhov | 97b9a8421b | |
Alexander Tomokhov | d73fdda3ad | |
Alexander Tomokhov | 5da925ea66 | |
Alexander Tomokhov | 4d62fb57b8 | |
Alexander Tomokhov | c51aece2b9 | |
Alexander Tomokhov | 4c0072fa7c | |
Alexander Tomokhov | 89b1f212ea | |
Alexander Tomokhov | f3aca43b24 | |
Alexander Tomokhov | 3fa57fc77f | |
Alexander Tomokhov | 47f3ba8cee | |
Alexander Tomokhov | 5c8f67f61b | |
Alexander Tomokhov | 8f2d7c8cdb | |
Alexander Tomokhov | 3bf02d9f72 | |
Alexander Tomokhov | 9f23164bda | |
Alexander Tomokhov | 490cd38e4e | |
Alexander Tomokhov | 04260e0852 | |
Alexander Tomokhov | 623856265c | |
Alexander Tomokhov | 5c9a185238 | |
Alexander Tomokhov | db7dd2cff8 | |
Alexander Tomokhov | e7512f3dc6 | |
Alexander Tomokhov | 5131cfe35e | |
Alexander Tomokhov | da56a4625b | |
Alexander Tomokhov | 3b800a7731 | |
Alexander Tomokhov | b2edfdb787 | |
Alexander Tomokhov | 6cda28182a | |
Inex Code | 491f836dc6 | |
Inex Code | 3fda846808 | |
NaiJi ✨ | f0741188b1 | |
Inex Code | 8fc64e74fd | |
sоvд | e05d7a6aa4 | |
sova | 0ca9b7fb75 | |
NaiJi ✨ | d88d75b9b7 | |
NaiJi ✨ | be62d1913e | |
Inex Code | f638192bee | |
Inex Code | f86b76f6bd | |
Inex Code | 6bfe91dee4 | |
NaiJi ✨ | bb59d623c5 | |
inexcode | eca6780983 | |
Inex Code | be3a8485f6 | |
Inex Code | c155f32d5e | |
Inex Code | ecdac11cbe | |
Inex Code | 0f779b5a1a | |
inexcode | 3a224f2111 | |
Inex Code | 6d7a2b9fae |
|
@ -0,0 +1,234 @@
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: default
|
||||||
|
|
||||||
|
clone:
|
||||||
|
disable: true
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: deploy
|
||||||
|
environment:
|
||||||
|
PASSWORD:
|
||||||
|
from_secret: HETZNER_TOKEN
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
CLOUDFLARE_TOKEN:
|
||||||
|
from_secret: CLOUDFLARE_TOKEN
|
||||||
|
USER_PASS:
|
||||||
|
from_secret: USER_PASS
|
||||||
|
INFECT_COMMIT_SHA: ${DRONE_COMMIT_SHA}
|
||||||
|
|
||||||
|
commands:
|
||||||
|
- set -o nounset
|
||||||
|
- > # TODO pass Base64 encoded password from Drone instead of this
|
||||||
|
ENCODED_PASSWORD="$(base64 <<<"$USER_PASS")"
|
||||||
|
# Create infect user script and then push it to a remote machine on server creation.
|
||||||
|
- |
|
||||||
|
cat << EOF > infect.sh
|
||||||
|
#! /usr/bin/env bash
|
||||||
|
|
||||||
|
set -o allexport
|
||||||
|
set -o errtrace
|
||||||
|
set -o pipefail
|
||||||
|
shopt -s inherit_errexit
|
||||||
|
trap 'echo \$LINENO: "\$BASH_COMMAND"; exit 1' ERR
|
||||||
|
|
||||||
|
API_TOKEN="$USER_PASS"
|
||||||
|
DNS_PROVIDER_TOKEN=$CLOUDFLARE_TOKEN
|
||||||
|
DNS_PROVIDER_TYPE=CLOUDFLARE
|
||||||
|
DOMAIN=$DOMAIN
|
||||||
|
ENCODED_PASSWORD="$ENCODED_PASSWORD"
|
||||||
|
HOSTNAME=selfprivacy-ci-test
|
||||||
|
LUSER=cicdcicd
|
||||||
|
NIXOS_CONFIG_ID=default
|
||||||
|
PROVIDER=HETZNER
|
||||||
|
SSH_AUTHORIZED_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMBb3yVhYF4slhf1iQCiGLOVcbGKP/MmkQiEMl2un+4K"
|
||||||
|
STAGING_ACME=true
|
||||||
|
|
||||||
|
curl --fail https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/commit/$INFECT_COMMIT_SHA/nixos-infect \
|
||||||
|
| bash 2>&1 | tee /root/infect.log
|
||||||
|
EOF
|
||||||
|
- cat infect.sh
|
||||||
|
- location=fsn1
|
||||||
|
- set -o pipefail
|
||||||
|
- >
|
||||||
|
volume_id="$(http -d --print=HBb --ignore-stdin POST https://api.hetzner.cloud/v1/volumes Authorization:"Bearer $PASSWORD" format=ext4 size:=10 location=$location name=10gb-by-drone-ci automount:=false | jq -re .volume.id)"
|
||||||
|
# TODO pass linux device volume_id name to infect.sh script
|
||||||
|
- >
|
||||||
|
http -d --print=HBb --ignore-stdin POST https://api.hetzner.cloud/v1/servers Authorization:"Bearer $PASSWORD" name=ci-sibling server_type=cx11 start_after_create:=true image=ubuntu-20.04 volumes:=[$volume_id] user_data=@infect.sh automount:=false location=$location
|
||||||
|
|
||||||
|
- name: dns
|
||||||
|
environment:
|
||||||
|
PASSWORD:
|
||||||
|
from_secret: HETZNER_TOKEN
|
||||||
|
CLOUDFLARE_TOKEN:
|
||||||
|
from_secret: CLOUDFLARE_TOKEN
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
ZONE_ID:
|
||||||
|
from_secret: ZONE_ID
|
||||||
|
|
||||||
|
commands:
|
||||||
|
# Read all available machines and export IP of 'ci-sibling'
|
||||||
|
# Create DNS entires for main domain and subdomains
|
||||||
|
- >
|
||||||
|
curl -s -H "Authorization: Bearer $PASSWORD" 'https://api.hetzner.cloud/v1/servers' > .machine.json
|
||||||
|
- >
|
||||||
|
export machineip=$( for i in {0..24}; do jq 'if .servers['$i'].name == "ci-sibling" then .servers['$i'].public_net.ipv4.ip else null end' .machine.json; done | grep -v null | sed 's/"//' | sed 's/"//' )
|
||||||
|
- echo $machineip
|
||||||
|
- >
|
||||||
|
curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN"
|
||||||
|
-H "Authorization: Bearer $CLOUDFLARE_TOKEN"
|
||||||
|
-H "Content-Type: application/json"
|
||||||
|
- >
|
||||||
|
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
|
||||||
|
-H "Authorization: Bearer $CLOUDFLARE_TOKEN"
|
||||||
|
-H "Content-Type: application/json"
|
||||||
|
--data '{"type":"A","name":"@","content":"'"$$machineip"'","ttl":3600,"priority":10,"proxied":false}'
|
||||||
|
- >
|
||||||
|
http --check-status --ignore-stdin POST https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records Authorization:"Bearer $CLOUDFLARE_TOKEN" type=A name=api content=$machineip ttl:=3600 priority:=10 proxied:=false
|
||||||
|
- >
|
||||||
|
http --check-status --ignore-stdin POST https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records Authorization:"Bearer $CLOUDFLARE_TOKEN" type=A name=cloud content=$machineip ttl:=3600 priority:=10 proxied:=false
|
||||||
|
- >
|
||||||
|
http --check-status --ignore-stdin POST https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records Authorization:"Bearer $CLOUDFLARE_TOKEN" type=A name=password content=$machineip ttl:=3600 priority:=10 proxied:=false
|
||||||
|
- >
|
||||||
|
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
|
||||||
|
-H "Authorization: Bearer $CLOUDFLARE_TOKEN"
|
||||||
|
-H "Content-Type: application/json"
|
||||||
|
--data '{"type":"MX","name":"@","content":"'"$$DOMAIN"'","ttl":3600,"priority":10,"proxied":false}'
|
||||||
|
- >
|
||||||
|
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
|
||||||
|
-H "Authorization: Bearer $CLOUDFLARE_TOKEN"
|
||||||
|
-H "Content-Type: application/json"
|
||||||
|
--data '{"type":"TXT","name":"_dmarc","content":"v=DMARC1; p=none","ttl":18000,"priority":10,"proxied":false}'
|
||||||
|
- >
|
||||||
|
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
|
||||||
|
-H "Authorization: Bearer $CLOUDFLARE_TOKEN"
|
||||||
|
-H "Content-Type: application/json"
|
||||||
|
--data "{\"type\":\"TXT\",\"name\":\"$DOMAIN\",\"content\":\"v=spf1 a mx ip4:$machineip -all\",\"ttl\":18000,\"priority\":10,\"proxied\":false}"
|
||||||
|
|
||||||
|
- name: wait for NixOS boot
|
||||||
|
environment:
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
TIMEOUT: 160
|
||||||
|
commands:
|
||||||
|
- sleep 300
|
||||||
|
- t0="$(date '+%s')"; until nix shell --inputs-from git+https://git.selfprivacy.org/alexoundos/selfprivacy-tests.git nixpkgs#netcat -c nc -zv -w1 "$DOMAIN" 443; do sleep 10; cur_date="$(date '+%s')"; remaining=$((t0 + TIMEOUT - cur_date)); [ "$remaining" -gt 0 ]; echo "$remaining seconds remain before teardown"; done
|
||||||
|
- echo "give time for obtaining ACME certificate"
|
||||||
|
- sleep 60
|
||||||
|
|
||||||
|
- name: basic test
|
||||||
|
environment:
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
USER_PASS:
|
||||||
|
from_secret: USER_PASS
|
||||||
|
TEST_EMAIL_PASS:
|
||||||
|
from_secret: TEST_EMAIL_PASS
|
||||||
|
API_TOKEN:
|
||||||
|
from_secret: USER_PASS
|
||||||
|
TESTS_REPO: git+https://git.selfprivacy.org/alexoundos/selfprivacy-tests.git
|
||||||
|
TIMEOUT_1: 100
|
||||||
|
commands:
|
||||||
|
# ideally it should depend on $STAGING_ACME
|
||||||
|
# CERT_VERIFY=$(if [[ $STAGING_ACME == true ]]; then echo "--cacert root.pem"; else echo ""; fi)
|
||||||
|
- CERT_VERIFY="--cacert root.pem"
|
||||||
|
- source $(nix build $TESTS_REPO#library --print-out-paths)
|
||||||
|
- curl https://letsencrypt.org/certs/staging/letsencrypt-stg-root-x1.pem -sS --fail -o root.pem
|
||||||
|
- dig api.$DOMAIN
|
||||||
|
- >
|
||||||
|
retry $TIMEOUT_1 10 curl https://api.$DOMAIN/api/version -sS --fail-with-body $CERT_VERIFY -H "Authorization: Bearer $USER_PASS"
|
||||||
|
- swaks --to ci@$DOMAIN -s selfprivacy.org -tls -au test@selfprivacy.org -ap $TEST_EMAIL_PASS -ahp
|
||||||
|
- >
|
||||||
|
curl https://cloud.$DOMAIN/ocs/v2.php/apps/serverinfo/api/v1/info?format=json -sS --fail-with-body $CERT_VERIFY -u "admin:$USER_PASS" | jq -re '.ocs.meta.status == "ok"'
|
||||||
|
- echo 'File contents to test nextcloud upload' > file
|
||||||
|
- >
|
||||||
|
curl -X PUT https://cloud.$DOMAIN/remote.php/dav/files/admin/file -sS --fail-with-body $CERT_VERIFY -d @file -u "admin:$USER_PASS"
|
||||||
|
- >
|
||||||
|
curl https://cloud.$DOMAIN/remote.php/dav/files/admin/file -sS --fail-with-body $CERT_VERIFY -u "admin:$USER_PASS"
|
||||||
|
|
||||||
|
- name: API & nixos-rebuild test
|
||||||
|
environment:
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
API_TOKEN:
|
||||||
|
from_secret: USER_PASS
|
||||||
|
TESTS_REPO: git+https://git.selfprivacy.org/alexoundos/selfprivacy-tests.git
|
||||||
|
TIMEOUT_1: 120
|
||||||
|
commands:
|
||||||
|
- source $(nix build $TESTS_REPO#library --print-out-paths)
|
||||||
|
- retry $TIMEOUT_1 10 query-minimum-services
|
||||||
|
- enable-service bitwarden
|
||||||
|
- enable-service gitea
|
||||||
|
- enable-service jitsi-meet
|
||||||
|
- enable-service ocserv
|
||||||
|
- enable-service pleroma
|
||||||
|
- nixos-rebuild Rebuild
|
||||||
|
- sleep 180
|
||||||
|
- retry $TIMEOUT_1 10 query-all-services
|
||||||
|
- nixos-rebuild Rollback
|
||||||
|
- sleep 30
|
||||||
|
- retry $TIMEOUT_1 10 query-minimum-services
|
||||||
|
- enable-service bitwarden
|
||||||
|
- enable-service gitea
|
||||||
|
- enable-service jitsi-meet
|
||||||
|
- enable-service ocserv
|
||||||
|
- enable-service pleroma
|
||||||
|
- nixos-rebuild Rebuild
|
||||||
|
- sleep 60
|
||||||
|
- retry $TIMEOUT_1 10 query-all-services
|
||||||
|
|
||||||
|
- name: sleep after failure
|
||||||
|
environment:
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
TIMEOUT: 1500
|
||||||
|
commands:
|
||||||
|
- timer=0; while [ "$timer" -lt "$TIMEOUT" ]; do nix shell --inputs-from git+https://git.selfprivacy.org/alexoundos/selfprivacy-tests.git nixpkgs#netcat -c nc -zv -w4 "$DOMAIN" 22; echo "$((TIMEOUT - timer)) seconds remain before teardown - login via ssh to shutdown the machine"; ((timer+=10)); sleep 10; done
|
||||||
|
when:
|
||||||
|
status:
|
||||||
|
- failure
|
||||||
|
|
||||||
|
- name: teardown
|
||||||
|
environment:
|
||||||
|
PASSWORD:
|
||||||
|
from_secret: HETZNER_TOKEN
|
||||||
|
CLOUDFLARE_TOKEN:
|
||||||
|
from_secret: CLOUDFLARE_TOKEN
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
ZONE_ID:
|
||||||
|
from_secret: ZONE_ID
|
||||||
|
commands:
|
||||||
|
# Delete our machine called 'ci-sibling' and all the found DNS records on it.
|
||||||
|
- >
|
||||||
|
curl -s "https://api.hetzner.cloud/v1/servers" -H "Authorization: Bearer $PASSWORD" > .hetzner_servers.json
|
||||||
|
- >
|
||||||
|
export machineid=$( for i in {0..24}; do jq 'if .servers['$i'].name == "ci-sibling" then .servers['$i'].id else null end' .hetzner_servers.json; done | grep -v null | sed 's/"//g')
|
||||||
|
- >
|
||||||
|
curl -s -X DELETE "https://api.hetzner.cloud/v1/servers/$machineid" -H "Authorization: Bearer $PASSWORD"
|
||||||
|
- >
|
||||||
|
json=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" -H "Authorization: Bearer $CLOUDFLARE_TOKEN" -H "Content-Type: application/json")
|
||||||
|
- >
|
||||||
|
echo "$json" > .dns-records.json
|
||||||
|
- >
|
||||||
|
result=$(jq -r ".result" <<< "$json")
|
||||||
|
- >
|
||||||
|
echo "$ZONE_ID"
|
||||||
|
- >
|
||||||
|
echo "$result" | jq '.[] | "\(.id)"' | while read id ; do echo "Deleting $id"; http -v --check-status --ignore-stdin DELETE https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$(echo $id | cut -f 2 -d '"') Authorization:"Bearer $CLOUDFLARE_TOKEN"; done
|
||||||
|
- location="fsn1"
|
||||||
|
- volume_name="10gb-by-drone-ci"
|
||||||
|
- >
|
||||||
|
volumes_json="$(http -d --print=HBb --ignore-stdin GET https://api.hetzner.cloud/v1/volumes Authorization:"Bearer $PASSWORD" name=$volume_name)"
|
||||||
|
# as of 2023-12-27 Hetzner ignores "name" query parameter (and others too)!!
|
||||||
|
- >
|
||||||
|
volume_id="$(jq <<<"$volumes_json" -re "(.volumes[] | select(.name == \"$volume_name\")).id")"
|
||||||
|
- sleep 1
|
||||||
|
- >
|
||||||
|
http -d --print=HBb --ignore-stdin DELETE https://api.hetzner.cloud/v1/volumes/$volume_id Authorization:"Bearer $PASSWORD"
|
||||||
|
when:
|
||||||
|
status:
|
||||||
|
- failure
|
||||||
|
- success
|
524
nixos-infect
524
nixos-infect
|
@ -1,118 +1,269 @@
|
||||||
#! /usr/bin/env bash
|
#! /usr/bin/env bash
|
||||||
|
|
||||||
# More info at: https://github.com/elitak/nixos-infect
|
# More info at:
|
||||||
|
# - https://github.com/elitak/nixos-infect
|
||||||
|
# - https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect
|
||||||
|
|
||||||
set -e -o pipefail
|
# mandatory variables
|
||||||
|
: "${API_TOKEN:?API_TOKEN variable is not set}"
|
||||||
|
: "${DNS_PROVIDER_TOKEN:?DNS_PROVIDER_TOKEN variable is not set}"
|
||||||
|
: "${DNS_PROVIDER_TYPE:?DNS_PROVIDER_TYPE variable is not set}"
|
||||||
|
: "${DOMAIN:?DOMAIN variable is not set}"
|
||||||
|
: "${ENCODED_PASSWORD:?ENCODED_PASSWORD variable is not set}"
|
||||||
|
: "${HOSTNAME:?HOSTNAME variable is not set}"
|
||||||
|
: "${LUSER:?LUSER variable is not set}"
|
||||||
|
: "${PROVIDER:?PROVIDER variable is not set}"
|
||||||
|
|
||||||
makeConf() {
|
# optional variables
|
||||||
export HASHED_PASSWORD=$( mkpasswd -m sha-512 "$PASSWORD" )
|
: "${NIXOS_CONFIG_ID:=default}"
|
||||||
# Skip everything if main config already present
|
: "${SSH_AUTHORIZED_KEY:=}"
|
||||||
[[ -e /etc/nixos/configuration.nix ]] && return 0
|
: "${VOLUME_DEV_PATH:=}"
|
||||||
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
|
: "${STAGING_ACME:=false}"
|
||||||
mkdir -p /etc/nixos
|
|
||||||
|
|
||||||
git clone https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git /etc/nixos
|
readonly CONFIG_URL="https://git.selfprivacy.org/api/v1/repos/SelfPrivacy/selfprivacy-nixos-template/archive/c5b16ac16dd328199e8f4f901890fff63f2fb81f.tar.gz"
|
||||||
|
readonly NIX_VERSION="2.18.1"
|
||||||
|
|
||||||
# Prevent grep for sending error code 1 (and halting execution) when no lines are selected : https://www.unix.com/man-page/posix/1P/grep
|
readonly NL=$'\n'
|
||||||
local IFS=$'\n'
|
readonly LOCAL_FLAKE_DIR="/etc/nixos"
|
||||||
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
|
readonly SECRETS_FILEPATH="/etc/selfprivacy/secrets.json"
|
||||||
[[ -r "$trypath" ]] \
|
readonly NIX="/root/.nix-profile/bin/nix"
|
||||||
&& keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") \
|
readonly NIX_OPTS=(--extra-experimental-features "nix-command flakes")
|
||||||
&& break
|
DoNetConf=
|
||||||
done
|
|
||||||
local network_import=""
|
|
||||||
|
|
||||||
cat > /etc/nixos/userdata/userdata.json << EOF
|
genOptionalSsh() {
|
||||||
|
if [ -n "${SSH_AUTHORIZED_KEY}" ]; then
|
||||||
|
cat << EOF
|
||||||
|
"ssh": { "rootKeys": [ "${SSH_AUTHORIZED_KEY}" ] },
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# TODO receive disk device path from outside
|
||||||
|
determine2ndDisk() {
|
||||||
|
local volDevPath rootFsDev
|
||||||
|
if [ -n "$VOLUME_DEV_PATH" ]; then
|
||||||
|
volDevPath="$VOLUME_DEV_PATH"
|
||||||
|
else
|
||||||
|
case "$PROVIDER" in
|
||||||
|
HETZNER)
|
||||||
|
volDevPath="/dev/sdb"
|
||||||
|
;;
|
||||||
|
DIGITALOCEAN)
|
||||||
|
volDevPath="/dev/sda"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
rootFsDev="$(findmnt -no source -T /)"
|
||||||
|
[[ -b "$volDevPath" && "$volDevPath" != "$rootFsDev" ]] && echo "$volDevPath"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Merge original userdata.json with deployment specific fields and print result.
|
||||||
|
genUserdata() {
|
||||||
|
local hashed_password diskDeviceName userdata_infect
|
||||||
|
hashed_password="$(mkpasswd -m sha-512 "$USER_PASS")"
|
||||||
|
diskDevice="$(determine2ndDisk)"
|
||||||
|
diskDeviceName="${diskDevice##/dev/}"
|
||||||
|
|
||||||
|
userdata_infect=$(cat << EOF
|
||||||
{
|
{
|
||||||
"api": {
|
$(genOptionalSsh)
|
||||||
"token": "$API_TOKEN"
|
"dns": {
|
||||||
|
"provider": "$DNS_PROVIDER_TYPE",
|
||||||
|
"useStagingACME": $STAGING_ACME
|
||||||
},
|
},
|
||||||
"backblaze": {
|
"server": {
|
||||||
"accountId": "$BACKBLAZE_KEY_ID",
|
"provider": "$PROVIDER"
|
||||||
"accountKey": "$BACKBLAZE_ACCOUNT_KEY",
|
|
||||||
"bucket": "$BACKBLAZE_BUCKET_NAME"
|
|
||||||
},
|
},
|
||||||
"bitwarden": {
|
|
||||||
"enable": true
|
|
||||||
},
|
|
||||||
"cloudflare": {
|
|
||||||
"apiKey": "$CF_TOKEN"
|
|
||||||
},
|
|
||||||
"databasePassword": "$DB_PASSWORD",
|
|
||||||
"domain": "$DOMAIN",
|
"domain": "$DOMAIN",
|
||||||
"hashedMasterPassword": "$HASHED_PASSWORD",
|
"hashedMasterPassword": "$hashed_password",
|
||||||
"hostname": "$HOSTNAME",
|
"hostname": "$HOSTNAME",
|
||||||
"nextcloud": {
|
"username": "$LUSER",
|
||||||
"enable": true,
|
"volumes": [
|
||||||
"adminPassword": "$PASSWORD",
|
{
|
||||||
"databasePassword": "$PASSWORD"
|
"device": "$diskDevice",
|
||||||
},
|
"mountPoint": "/volumes/$diskDeviceName",
|
||||||
"gitea": {
|
"fsType": "ext4"
|
||||||
"enable": true
|
}
|
||||||
},
|
],
|
||||||
"jitsi": {
|
"modules": {
|
||||||
"enable": true
|
"bitwarden": {
|
||||||
},
|
"location": "$diskDeviceName"
|
||||||
"ocserv": {
|
},
|
||||||
"enable": true
|
"gitea": {
|
||||||
},
|
"location": "$diskDeviceName"
|
||||||
"pleroma": {
|
},
|
||||||
"enable": true
|
"nextcloud": {
|
||||||
},
|
"location": "$diskDeviceName"
|
||||||
"timezone": "Europe/Uzhgorod",
|
},
|
||||||
"resticPassword": "$PASSWORD",
|
"pleroma": {
|
||||||
"username": "$LUSER"
|
"location": "$diskDeviceName"
|
||||||
|
},
|
||||||
|
"simple-nixos-mailserver": {
|
||||||
|
"location": "$diskDeviceName"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
chmod 0600 /etc/nixos/userdata/userdata.json
|
)
|
||||||
|
|
||||||
if isEFI; then
|
jq -s '.[0] * .[1]' \
|
||||||
|
"${1:?no userdata.json given to merge with}" <(printf "%s" "$userdata_infect")
|
||||||
|
}
|
||||||
|
|
||||||
|
genSecrets() {
|
||||||
|
local dbpass
|
||||||
|
dbpass="$(shuf --random-source=/dev/urandom -erz -n32 {A..Z} {a..z} {0..9} | tr -d '\n')"
|
||||||
|
|
||||||
|
cat << EOF
|
||||||
|
{
|
||||||
|
"api": {
|
||||||
|
"token": "$API_TOKEN",
|
||||||
|
"skippedMigrations": ["migrate_to_selfprivacy_channel", "mount_volume"]
|
||||||
|
},
|
||||||
|
"databasePassword": "$dbpass",
|
||||||
|
"dns": {
|
||||||
|
"apiKey": "$DNS_PROVIDER_TOKEN"
|
||||||
|
},
|
||||||
|
"modules": {
|
||||||
|
"nextcloud": {
|
||||||
|
"adminPassword": "$USER_PASS",
|
||||||
|
"databasePassword": "$USER_PASS"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
genHardwareConfiguration() {
|
||||||
|
local bootcfg
|
||||||
|
if ((isEFI)); then
|
||||||
bootcfg=$(cat << EOF
|
bootcfg=$(cat << EOF
|
||||||
boot.loader.grub = {
|
boot.loader.grub = {
|
||||||
efiSupport = true;
|
efiSupport = true;
|
||||||
efiInstallAsRemovable = true;
|
efiInstallAsRemovable = true;
|
||||||
device = "nodev";
|
device = "nodev";
|
||||||
};
|
};
|
||||||
fileSystems."/boot" = { device = "$esp"; fsType = "vfat"; };
|
fileSystems."/boot" = { device = "$ESP"; fsType = "vfat"; };
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
bootcfg=$(cat << EOF
|
bootcfg=$(cat << EOF
|
||||||
boot.loader.grub.device = "$grubdev";
|
boot.loader.grub.device = "$GRUBDEV";
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If you rerun this later, be sure to prune the filesSystems attr
|
local currentSystem
|
||||||
cat > /etc/nixos/hardware-configuration.nix << EOF
|
if ! currentSystem="$($NIX "${NIX_OPTS[@]}" eval --impure --raw --expr builtins.currentSystem)"
|
||||||
|
then
|
||||||
|
echo "cannot determine Nix currentSystem identifier"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# FIXME it's questionable whether these modules are needed at all...
|
||||||
|
declare -a availableKernelModules=()
|
||||||
|
|
||||||
|
[ "$PROVIDER" == "DIGITALOCEAN" ] \
|
||||||
|
&& availableKernelModules+=('"ata_piix"' '"uhci_hcd"' '"xen_blkfront"')
|
||||||
|
|
||||||
|
[ "$(uname -m)" == "x86_64" ] \
|
||||||
|
&& availableKernelModules+=('"vmw_pvscsi"')
|
||||||
|
|
||||||
|
# TODO try nixos-generate-config first, resorting to the way below if failed
|
||||||
|
# FIXME "nvme" is not needed for hetzner?
|
||||||
|
|
||||||
|
cat << EOF
|
||||||
{ modulesPath, ... }:
|
{ modulesPath, ... }:
|
||||||
{
|
{
|
||||||
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
|
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
|
||||||
$bootcfg
|
$bootcfg
|
||||||
boot.initrd.kernelModules = [ "nvme" ];
|
boot.initrd.kernelModules = [ "nvme" ];
|
||||||
fileSystems."/" = { device = "$rootfsdev"; fsType = "$rootfstype"; };
|
boot.initrd.availableKernelModules = [ ${availableKernelModules[@]} ];
|
||||||
|
fileSystems."/" = { device = "$ROOTFSDEV"; fsType = "$ROOTFSTYPE"; };
|
||||||
|
nixpkgs.hostPlatform = "${currentSystem}";
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
[[ -n "$doNetConf" ]] && makeNetworkingConf || true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
makeNetworkingConf() {
|
genDeploymentConfiguration() {
|
||||||
# XXX It'd be better if we used procfs for all this...
|
local release
|
||||||
local IFS=$'\n'
|
|
||||||
eth0_name=$(ip address show | grep '^2:' | awk -F': ' '{print $2}')
|
|
||||||
eth0_ip4s=$(ip address show dev "$eth0_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
|
|
||||||
eth0_ip6s=$(ip address show dev "$eth0_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
|
|
||||||
gateway=$(ip route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9.]+).*|\1|')
|
|
||||||
gateway6=$(ip -6 route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|' || true)
|
|
||||||
ether0=$(ip address show dev "$eth0_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
|
|
||||||
|
|
||||||
eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}')||true
|
if ! release="$($NIX "${NIX_OPTS[@]}" eval --impure --raw --expr "(builtins.getFlake (builtins.toString ${LOCAL_FLAKE_DIR})).inputs.selfprivacy-nixos-config.inputs.nixpkgs.lib.trivial.release")"
|
||||||
if [ -n "$eth1_name" ];then
|
then
|
||||||
eth1_ip4s=$(ip address show dev "$eth1_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
|
echo "cannot determine NixOS release version"
|
||||||
eth1_ip6s=$(ip address show dev "$eth1_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
|
return 1
|
||||||
ether1=$(ip address show dev "$eth1_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
|
fi
|
||||||
interfaces1=<< EOF
|
|
||||||
|
cat << EOF
|
||||||
|
{ lib, ... }: {
|
||||||
|
# The content below is static and belongs to this deployment only!
|
||||||
|
# Do not copy this configuration file to another NixOS installation!
|
||||||
|
|
||||||
|
system.stateVersion = lib.mkDefault "$release";`
|
||||||
|
`$(if [ "$DoNetConf" == "y" ]; then echo -e "$NL"; genNetworkingConf; fi)
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
setupConf() {
|
||||||
|
mkdir -p ${LOCAL_FLAKE_DIR}
|
||||||
|
if ! curl --fail "${CONFIG_URL}" \
|
||||||
|
| tar -xz -C ${LOCAL_FLAKE_DIR} --strip-components=1 --exclude=".*"
|
||||||
|
then
|
||||||
|
echo "Error downloading/extracting top level flake configuration!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# generate and write hardware-configuration.nix
|
||||||
|
if ! genHardwareConfiguration > ${LOCAL_FLAKE_DIR}/hardware-configuration.nix
|
||||||
|
then
|
||||||
|
echo "error generating ${LOCAL_FLAKE_DIR}/hardware-configuration.nix"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# generate and write deployment.nix
|
||||||
|
if ! genDeploymentConfiguration > ${LOCAL_FLAKE_DIR}/deployment.nix
|
||||||
|
then
|
||||||
|
echo "error generating ${LOCAL_FLAKE_DIR}/deployment.nix"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# generate infected userdata based on original
|
||||||
|
local userdataInfected
|
||||||
|
userdataInfected="$(genUserdata ${LOCAL_FLAKE_DIR}/userdata.json)"
|
||||||
|
printf "%s" "$userdataInfected" > ${LOCAL_FLAKE_DIR}/userdata.json
|
||||||
|
|
||||||
|
# generate and write secrets
|
||||||
|
local secrets
|
||||||
|
secrets="$(genSecrets)"
|
||||||
|
install -m0600 <(printf "%s" "$secrets") -DT ${SECRETS_FILEPATH}
|
||||||
|
}
|
||||||
|
|
||||||
|
# shellcheck disable=SC2207
|
||||||
|
genNetworkingConf() {
|
||||||
|
# XXX It'd be better if we used procfs for all this...
|
||||||
|
|
||||||
|
local IFS=$'\n'
|
||||||
|
local eth0_name eth0_ip4s eth0_ip6s gateway gateway6 ether0 eth1_name
|
||||||
|
local interfaces1 extraRules1 predictable_inames
|
||||||
|
|
||||||
|
eth0_name="$(ip address show | grep '^2:' | awk -F': ' '{print $2}')"
|
||||||
|
eth0_ip4s=($(ip address show dev "$eth0_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
|
||||||
|
eth0_ip6s=($(ip address show dev "$eth0_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')) || true
|
||||||
|
gateway="$(ip route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9.]+).*|\1|')"
|
||||||
|
gateway6="$(ip -6 route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|')" || true
|
||||||
|
ether0="$(ip address show dev "$eth0_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')"
|
||||||
|
|
||||||
|
eth1_name="$(ip address show | grep '^3:' | awk -F': ' '{print $2}')" || true
|
||||||
|
if [ -n "$eth1_name" ]; then
|
||||||
|
local eth1_ip4s eth1_ip6s ether1
|
||||||
|
eth1_ip4s="$(ip address show dev "$eth1_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')"
|
||||||
|
eth1_ip6s="$(ip address show dev "$eth1_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')" || true
|
||||||
|
ether1="$(ip address show dev "$eth1_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')"
|
||||||
|
interfaces1=$(cat << EOF
|
||||||
$eth1_name = {
|
$eth1_name = {
|
||||||
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
|
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
|
||||||
$a"; done)
|
$a"; done)
|
||||||
|
@ -120,31 +271,30 @@ makeNetworkingConf() {
|
||||||
ipv6.addresses = [$(for a in "${eth1_ip6s[@]}"; do echo -n "
|
ipv6.addresses = [$(for a in "${eth1_ip6s[@]}"; do echo -n "
|
||||||
$a"; done)
|
$a"; done)
|
||||||
];
|
];
|
||||||
|
};
|
||||||
EOF
|
EOF
|
||||||
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
|
)
|
||||||
|
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
|
||||||
else
|
else
|
||||||
interfaces1=""
|
interfaces1=""
|
||||||
extraRules1=""
|
extraRules1=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
readarray nameservers < <(grep ^nameserver /etc/resolv.conf | sed -r \
|
|
||||||
-e 's/^nameserver[[:space:]]+([0-9.a-fA-F:]+).*/"\1"/' \
|
|
||||||
-e 's/127[0-9.]+/8.8.8.8/' \
|
|
||||||
-e 's/::1/8.8.8.8/' )
|
|
||||||
|
|
||||||
if [[ "$eth0_name" = eth* ]]; then
|
if [[ "$eth0_name" = eth* ]]; then
|
||||||
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
|
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
|
||||||
else
|
else
|
||||||
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
|
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
|
||||||
fi
|
fi
|
||||||
cat > /etc/nixos/networking.nix << EOF
|
|
||||||
{ lib, ... }: {
|
local defaultGateway6=${gateway6:+defaultGateway6 = \{ address = "${gateway6}"; interface = "${eth0_name}"; \};}
|
||||||
# This file was populated at runtime with the networking
|
local ipv6routes=${gateway6:+ipv6.routes = \[ \{ address = "${gateway6}"; prefixLength = 128; \} \];}
|
||||||
# details gathered from the active system.
|
cat << EOF
|
||||||
|
# Networking configuration was populated by nixos-infect with the networking
|
||||||
|
# details gathered from the running system.
|
||||||
networking = {
|
networking = {
|
||||||
nameservers = [ ${nameservers[@]} ];
|
defaultGateway = "${gateway}";`
|
||||||
defaultGateway = "${gateway}";
|
`${defaultGateway6:+
|
||||||
defaultGateway6 = "${gateway6}";
|
defaultGateway6}
|
||||||
dhcpcd.enable = false;
|
dhcpcd.enable = false;
|
||||||
$predictable_inames
|
$predictable_inames
|
||||||
interfaces = {
|
interfaces = {
|
||||||
|
@ -155,17 +305,19 @@ EOF
|
||||||
ipv6.addresses = [$(for a in "${eth0_ip6s[@]}"; do echo -n "
|
ipv6.addresses = [$(for a in "${eth0_ip6s[@]}"; do echo -n "
|
||||||
$a"; done)
|
$a"; done)
|
||||||
];
|
];
|
||||||
ipv4.routes = [ { address = "${gateway}"; prefixLength = 32; } ];
|
ipv4.routes = [ { address = "${gateway}"; prefixLength = 32; } ];`
|
||||||
ipv6.routes = [ { address = "${gateway6}"; prefixLength = 128; } ];
|
`${ipv6routes:+
|
||||||
};
|
$ipv6routes}
|
||||||
$interfaces1
|
};`
|
||||||
|
`${interfaces1:+
|
||||||
|
$interfaces1}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
services.udev.extraRules = ''
|
services.udev.extraRules = ''
|
||||||
ATTR{address}=="${ether0}", NAME="${eth0_name}"
|
ATTR{address}=="${ether0}", NAME="${eth0_name}"`
|
||||||
$extraRules1
|
`${extraRules1:+
|
||||||
|
$extraRules1}
|
||||||
'';
|
'';
|
||||||
}
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,12 +335,8 @@ removeSwap() {
|
||||||
rm -vf /tmp/nixos-infect.*.swp
|
rm -vf /tmp/nixos-infect.*.swp
|
||||||
}
|
}
|
||||||
|
|
||||||
isEFI() {
|
|
||||||
[ -d /sys/firmware/efi ]
|
|
||||||
}
|
|
||||||
|
|
||||||
findESP() {
|
findESP() {
|
||||||
esp=""
|
local esp
|
||||||
for d in /boot/EFI /boot/efi /boot; do
|
for d in /boot/EFI /boot/efi /boot; do
|
||||||
[[ ! -d "$d" ]] && continue
|
[[ ! -d "$d" ]] && continue
|
||||||
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] \
|
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] \
|
||||||
|
@ -197,22 +345,32 @@ findESP() {
|
||||||
done
|
done
|
||||||
[[ -z "$esp" ]] && { echo "ERROR: No ESP mount point found"; return 1; }
|
[[ -z "$esp" ]] && { echo "ERROR: No ESP mount point found"; return 1; }
|
||||||
for uuid in /dev/disk/by-uuid/*; do
|
for uuid in /dev/disk/by-uuid/*; do
|
||||||
[[ $(readlink -f "$uuid") == "$esp" ]] && echo $uuid && return 0
|
[[ $(readlink -f "$uuid") == "$esp" ]] && echo "$uuid" && return 0
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareEnv() {
|
prepareEnv() {
|
||||||
# $esp and $grubdev are used in makeConf()
|
if ! USER_PASS="$(base64 -d <<<"$ENCODED_PASSWORD")"; then
|
||||||
if isEFI; then
|
echo "Error decoding ENCODED_PASSWORD from Base64!"
|
||||||
esp="$(findESP)"
|
exit 1
|
||||||
|
fi
|
||||||
|
readonly USER_PASS
|
||||||
|
|
||||||
|
isEFI=0
|
||||||
|
[ -d /sys/firmware/efi ] && isEFI=1
|
||||||
|
|
||||||
|
if ((isEFI)); then
|
||||||
|
ESP="$(findESP)"
|
||||||
else
|
else
|
||||||
for grubdev in /dev/vda /dev/sda /dev/nvme0n1 ; do [[ -e $grubdev ]] && break; done
|
for GRUBDEV in /dev/vda /dev/sda /dev/nvme0n1; do
|
||||||
|
[[ -e $GRUBDEV ]] && break;
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Retrieve root fs block device
|
# Retrieve root fs block device
|
||||||
# (get root mount) (get partition or logical volume)
|
# (get root mount) (get partition or logical volume)
|
||||||
rootfsdev=$(mount | grep "on / type" | awk '{print $1;}')
|
ROOTFSDEV=$(mount | grep "on / type" | awk '{print $1;}')
|
||||||
rootfstype=$(df $rootfsdev --output=fstype | sed 1d)
|
ROOTFSTYPE=$(df "$ROOTFSDEV" --output=fstype | sed 1d)
|
||||||
|
|
||||||
# DigitalOcean doesn't seem to set USER while running user data
|
# DigitalOcean doesn't seem to set USER while running user data
|
||||||
export USER="root"
|
export USER="root"
|
||||||
|
@ -262,9 +420,6 @@ checkEnv() {
|
||||||
# Perform some easy fixups before checking
|
# Perform some easy fixups before checking
|
||||||
# TODO prevent multiple calls to apt-get update
|
# TODO prevent multiple calls to apt-get update
|
||||||
(which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24
|
(which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24
|
||||||
which bzcat || (which yum && yum install -y bzip2) \
|
|
||||||
|| (which apt-get && apt-get update && apt-get install -y bzip2) \
|
|
||||||
|| true
|
|
||||||
which xzcat || (which yum && yum install -y xz-utils) \
|
which xzcat || (which yum && yum install -y xz-utils) \
|
||||||
|| (which apt-get && apt-get update && apt-get install -y xz-utils) \
|
|| (which apt-get && apt-get update && apt-get install -y xz-utils) \
|
||||||
|| true
|
|| true
|
||||||
|
@ -273,79 +428,124 @@ checkEnv() {
|
||||||
|| true
|
|| true
|
||||||
|
|
||||||
req curl || req wget || { echo "ERROR: Missing both curl and wget"; return 1; }
|
req curl || req wget || { echo "ERROR: Missing both curl and wget"; return 1; }
|
||||||
req bzcat || { echo "ERROR: Missing bzcat"; return 1; }
|
|
||||||
req xzcat || { echo "ERROR: Missing xzcat"; return 1; }
|
req xzcat || { echo "ERROR: Missing xzcat"; return 1; }
|
||||||
req groupadd || { echo "ERROR: Missing groupadd"; return 1; }
|
|
||||||
req useradd || { echo "ERROR: Missing useradd"; return 1; }
|
|
||||||
req ip || { echo "ERROR: Missing ip"; return 1; }
|
|
||||||
req awk || { echo "ERROR: Missing awk"; return 1; }
|
req awk || { echo "ERROR: Missing awk"; return 1; }
|
||||||
req cut || req df || { echo "ERROR: Missing coreutils (cut, df)"; return 1; }
|
req cut || req df || { echo "ERROR: Missing coreutils (cut, df)"; return 1; }
|
||||||
|
req mkpasswd || { echo "ERROR: Missing mkpasswd"; return 1; }
|
||||||
|
req shuf || { echo "ERROR: Missing shuf"; return 1; }
|
||||||
|
}
|
||||||
|
|
||||||
|
# Download and execute the nix installer script.
|
||||||
|
|
||||||
|
installNix() {
|
||||||
|
# install multiuser (system-wide with nix-daemon) Nix in the current system
|
||||||
|
|
||||||
|
local nixReleaseBase='https://releases.nixos.org'
|
||||||
|
local installURL="${nixReleaseBase}/nix/nix-${NIX_VERSION}/install"
|
||||||
|
local shaURL="${installURL}.sha256"
|
||||||
|
local sha tmpNixInstall
|
||||||
|
|
||||||
|
# temporary destination for install script
|
||||||
|
tmpNixInstall="$(mktemp -t nix-install-XXXXXXXXXX)"
|
||||||
|
if [[ ! -f "${tmpNixInstall}" ]]; then
|
||||||
|
echo "Failed creating a temporary file for Nix install script!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Downloading install script from ${installURL}..."
|
||||||
|
if ! curl --fail "${installURL}" -o "${tmpNixInstall}" &>/dev/null; then
|
||||||
|
echo "Failure while downloading Nix install script!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! sha="$(curl --fail "${shaURL}")"; then
|
||||||
|
echo "Failure while downloading Nix install script sha!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Validating Nix install script checksum..."
|
||||||
|
if ! echo "${sha} ${tmpNixInstall}" | sha256sum -c; then
|
||||||
|
echo "Checksum validation failed!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Running nix installer..."
|
||||||
|
if $SHELL "${tmpNixInstall}" \
|
||||||
|
--daemon --no-channel-add --daemon-user-count 4; then
|
||||||
|
echo "Nix is installed"
|
||||||
|
rm "${tmpNixInstall}"
|
||||||
|
else
|
||||||
|
echo "Nix installation script failed!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
infect() {
|
infect() {
|
||||||
# Add nix build users
|
# this is needed solely for accepting the sp-module sub-flake
|
||||||
# FIXME run only if necessary, rather than defaulting true
|
# see https://github.com/NixOS/nix/issues/3978#issuecomment-952418478
|
||||||
groupadd nixbld -g 30000 || true
|
cd /etc/nixos || return 1
|
||||||
for i in {1..10}; do
|
$NIX "${NIX_OPTS[@]}" flake lock ${LOCAL_FLAKE_DIR} \
|
||||||
useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" "nixbld$i" || true
|
--override-input sp-modules path:./sp-modules
|
||||||
done
|
|
||||||
# TODO use addgroup and adduser as fallbacks
|
|
||||||
#addgroup nixbld -g 30000 || true
|
|
||||||
#for i in {1..10}; do adduser -DH -G nixbld nixbld$i || true; done
|
|
||||||
|
|
||||||
curl -L https://nixos.org/nix/install | $SHELL
|
echo "nix build the configuration flake..."
|
||||||
|
if ! $NIX "${NIX_OPTS[@]}" build \
|
||||||
|
--profile /nix/var/nix/profiles/system \
|
||||||
|
${LOCAL_FLAKE_DIR}/#nixosConfigurations."$NIXOS_CONFIG_ID".config.system.build.toplevel
|
||||||
|
then
|
||||||
|
echo "Failed!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# shellcheck disable=SC1090
|
# Reify resolv.conf (???)
|
||||||
source ~/.nix-profile/etc/profile.d/nix.sh
|
|
||||||
|
|
||||||
[[ -z "$NIX_CHANNEL" ]] && NIX_CHANNEL="nixos-21.05"
|
|
||||||
nix-channel --remove nixpkgs
|
|
||||||
nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
|
|
||||||
nix-channel --update
|
|
||||||
|
|
||||||
export NIXOS_CONFIG=/etc/nixos/configuration.nix
|
|
||||||
|
|
||||||
nix-env --set \
|
|
||||||
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
|
|
||||||
-f '<nixpkgs/nixos>' \
|
|
||||||
-p /nix/var/nix/profiles/system \
|
|
||||||
-A system
|
|
||||||
|
|
||||||
# Remove nix installed with curl | bash
|
|
||||||
rm -fv /nix/var/nix/profiles/default*
|
|
||||||
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
|
|
||||||
|
|
||||||
# Reify resolv.conf
|
|
||||||
[[ -L /etc/resolv.conf ]] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk > /etc/resolv.conf
|
[[ -L /etc/resolv.conf ]] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk > /etc/resolv.conf
|
||||||
|
|
||||||
# Stage the Nix coup d'état
|
# Stage the Nix coup d'état
|
||||||
touch /etc/NIXOS
|
touch /etc/NIXOS
|
||||||
echo etc/nixos > /etc/NIXOS_LUSTRATE
|
echo etc/nixos > /etc/NIXOS_LUSTRATE
|
||||||
echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE
|
echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE
|
||||||
echo root/.nix-defexpr/channels >> /etc/NIXOS_LUSTRATE
|
echo ${SECRETS_FILEPATH} >> /etc/NIXOS_LUSTRATE
|
||||||
|
|
||||||
rm -rf /boot.bak
|
rm -rf /boot.bak
|
||||||
isEFI && umount "$esp"
|
((isEFI)) && umount "$ESP"
|
||||||
mv -v /boot /boot.bak
|
mv -v /boot /boot.bak
|
||||||
if isEFI; then
|
if ((isEFI)); then
|
||||||
mkdir /boot
|
mkdir /boot
|
||||||
mount "$esp" /boot
|
mount "$ESP" /boot
|
||||||
find /boot -depth ! -path /boot -exec rm -rf {} +
|
find /boot -depth ! -path /boot -exec rm -rf {} +
|
||||||
fi
|
fi
|
||||||
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
|
||||||
|
echo "make configuration boot by default..."
|
||||||
|
if ! /nix/var/nix/profiles/system/bin/switch-to-configuration boot; then
|
||||||
|
echo "Failed!"; exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove nix installed by the "install" script.
|
||||||
|
rm -fv /nix/var/nix/profiles/default* /root/.nix-profile
|
||||||
|
rm -rv /nix/var/nix/profiles/per-user
|
||||||
|
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
|
||||||
}
|
}
|
||||||
|
|
||||||
[ "$PROVIDER" = "digitalocean" ] && doNetConf=y # digitalocean requires detailed network config to be generated
|
set -o errtrace
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
set -o xtrace
|
||||||
|
shopt -s inherit_errexit
|
||||||
|
trap 'echo ${LINENO}: "$BASH_COMMAND"; exit 1' ERR
|
||||||
|
|
||||||
|
# digitalocean requires detailed network config to be generated
|
||||||
|
[ "$PROVIDER" == "DIGITALOCEAN" ] && DoNetConf="y"
|
||||||
|
|
||||||
apt update
|
apt update
|
||||||
apt install git tar wget curl whois
|
apt install -y git tar curl whois jq
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
prepareEnv
|
prepareEnv
|
||||||
makeSwap # smallest (512MB) droplet needs extra memory!
|
makeSwap # smallest (512MB) droplet needs extra memory!
|
||||||
makeConf
|
installNix
|
||||||
|
setupConf
|
||||||
infect
|
infect
|
||||||
removeSwap
|
removeSwap
|
||||||
|
|
||||||
if [[ -z "$NO_REBOOT" ]]; then
|
if [[ -z "${NO_REBOOT+x}" ]]; then
|
||||||
reboot
|
reboot
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in New Issue