Compare commits

...

154 Commits

Author SHA1 Message Date
Inex Code 7567c69722 infect:config:sha1: update nixos-config
continuous-integration/drone/push Build is passing Details
2024-03-14 18:46:29 +03:00
Alexander 62dfb006e4 infect:config:sha1<- nixpkgs and api update
continuous-integration/drone/push Build is failing Details
2024-03-08 05:34:56 +04:00
Alexander 861a5dbdf8 infect:config:sha1: modules: parameterize all subdomains
continuous-integration/drone/push Build is passing Details
continuous-integration/drone Build is passing Details
2024-02-29 04:56:51 +04:00
Alexander Tomokhov 83c1e4d590 infect:config:sha1: fix no ssh key build
continuous-integration/drone/push Build is passing Details
2024-01-23 18:14:10 +04:00
Alexander 2925c7e41c infect:config:sha1: switch template branch to master
continuous-integration/drone/push Build is failing Details
2024-01-19 10:01:24 +04:00
Alexander 5d93c8197e infect:config:sha1: update selfprivacy-api: small fixes
continuous-integration/drone/push Build is passing Details
2024-01-19 03:10:51 +04:00
Alexander 77972cd020 infect:config:sha1: update selfprivacy-api
continuous-integration/drone/push Build is passing Details
2024-01-18 15:31:08 +04:00
Alexander 4d02caaf7e test: increase timeouts for nixos-rebuild
continuous-integration/drone/push Build is passing Details
2024-01-11 07:00:19 +04:00
Alexander 0c8ada1a1b infect:config:sha1: selfprivacy-api branch change to master
continuous-integration/drone/push Build is failing Details
2024-01-11 00:17:43 +04:00
Alexander fd0e2fb105 infect:config:sha1: update nixpkgs
continuous-integration/drone/push Build is passing Details
2024-01-10 07:23:20 +04:00
Alexander 977658fb33 infect:config:sha1: update selfprivacy-api to 3.0
continuous-integration/drone/push Build is passing Details
2024-01-10 04:17:11 +04:00
Alexander 2dd49d3596 infect:config:sha1: fix nextcloud auth redirection
continuous-integration/drone/push Build is passing Details
2024-01-10 03:13:04 +04:00
Alexander 8f00617b3d infect: remove nix profile in the end
continuous-integration/drone/push Build is passing Details
2024-01-10 00:25:54 +04:00
Alexander 8ce494c490 nixos-infect: update selfprivacy-nixos-config sha1
continuous-integration/drone/push Build is passing Details
2024-01-09 23:34:34 +04:00
Alexander 66f61ce536 fix: no excess slash before disk device name (location)
continuous-integration/drone/push Build is passing Details
2023-12-28 21:06:35 +04:00
Alexander 4866762afc selfprivacy-api git branch: userdata => flakes
continuous-integration/drone/push Build is failing Details
2023-12-28 20:54:26 +04:00
Alexander f3696ab0d6 embed CONFIG_URL and NIX_VERSION inside nixos-infect
continuous-integration/drone/push Build is passing Details
Now you need to edit nixos-infect to update base configuration URL.
2023-12-28 20:25:07 +04:00
Alexander 774bea292f STAGING_ACME is optional (default is false) 2023-12-28 20:25:07 +04:00
Alexander 743129d6cb uppercase PROVIDER variable value 2023-12-28 20:24:40 +04:00
Alexander 15f36551ad fix: absent SSH_AUTHORIZED_KEY does not cause failure
continuous-integration/drone/push Build is passing Details
2023-12-28 20:00:44 +04:00
Alexander 4920434aec ci: sleep 1 before deleting secondary volume 2023-12-28 14:05:36 +04:00
Alexander 91c22466b1 Merge branch 'tests'
continuous-integration/drone/push Build is passing Details
2023-12-28 13:47:19 +04:00
Alexander 6ece80adc2 update: system.autoUpgrade: no verbosity of nixos-rebuild 2023-12-28 13:30:25 +04:00
Alexander 7cf0a1432d update: working system.autoUpgrade
continuous-integration/drone/push Build is passing Details
2023-12-28 13:23:08 +04:00
Alexander 36eb48ab3c nix flake lock /etc/nixos --override-input sp-modules path:./sp-modules
continuous-integration/drone/push Build is passing Details
2023-12-28 05:02:04 +04:00
Alexander 374711f324 optional variables: NIXOS_CONFIG_ID and VOLUME_DEV_PATH
continuous-integration/drone/push Build is passing Details
2023-12-28 03:12:43 +04:00
Alexander 91073a50a3 additional check for 2nd volume
continuous-integration/drone/push Build is passing Details
2023-12-28 01:02:49 +04:00
Alexander 18c88f39cf ci: slightly reformat basic test logging 2023-12-28 00:45:11 +04:00
Alexander eaafba6f65 fix ERR trap in infect.sh 2023-12-28 00:35:36 +04:00
Alexander 0ec4fc7d5b generate volumes and location values (disk device name)
continuous-integration/drone/push Build is passing Details
2023-12-28 00:28:53 +04:00
Alexander 2f9903599d proper ERR trap 2023-12-27 21:01:51 +04:00
Alexander 83fe55b86e nixos-infect: sort variables 2023-12-27 20:59:00 +04:00
Alexander c31521e1ca do not pass DB_PASSWORD, but generate 2023-12-27 20:58:59 +04:00
Alexander 967377f171 pass ENCODED_PASSWORD to nixos-infect instead of USER_PASS 2023-12-27 17:59:09 +04:00
Alexander 659c539f38 update
continuous-integration/drone/push Build is passing Details
2023-12-27 15:17:31 +04:00
Alexander 4df84db453 curl --fail 2023-12-27 14:13:40 +04:00
Alexander c95b9d0f62 /etc/nixos#sp-nixos => /etc/nixos#default
continuous-integration/drone/push Build is passing Details
2023-12-27 14:05:09 +04:00
Alexander 05e395d6af nix flake lock /etc/nixos --update-input sp-modules 2023-12-27 13:20:50 +04:00
Alexander a6713df931 pin selfprivacy-nixos-template commit
continuous-integration/drone/push Build is passing Details
2023-12-26 18:53:28 +04:00
Alexander eec1c639ec reduce sleep after failure time to 1500 seconds 2023-12-26 18:53:28 +04:00
Alexander 69dd80d065 more readable nixos-infect command; DRONE_COMMIT_SHA 2023-12-26 18:43:40 +04:00
Alexander 867396b274 use staging ACME with root CA verification 2023-12-24 16:23:13 +04:00
Alexander 6171589cc3 ci: curl commands in basic test start with `>`
continuous-integration/drone/push Build is failing Details
2023-12-24 07:35:39 +04:00
Alexander a266532d59 remove unnecessary sleeps before API queries 2023-12-24 07:32:09 +04:00
Alexander b135e52863 replace HTTPie with curl in basic test
continuous-integration/drone/push Build is failing Details
2023-12-23 21:41:06 +04:00
Alexander ab35fc20b6 ci: SSL certificate verification in basic test based on STAGING_ACME
continuous-integration/drone/push Build is failing Details
2023-12-23 02:16:33 +04:00
Alexander 1494e06c44 ci: fix typo for starting query-all-services
continuous-integration/drone/push Build is failing Details
2023-12-23 00:17:53 +04:00
Alexander dcfc5f203e ci: no unnecessary sleep and retry in basic test 2023-12-23 00:16:38 +04:00
Alexander ec40186962 ci: enable SSL certificate verification in basic test 2023-12-23 00:16:35 +04:00
Alexander d099b7bd11 system.stateVersion = lib.mkDefault "$release" 2023-12-23 00:14:33 +04:00
Alexander 9cd6207238 add stateVersion to userdata.json 2023-12-22 23:13:04 +04:00
Alexander 2d54653924 ci: retry first request to nextcloud 2023-12-22 21:32:48 +04:00
Alexander 6f96b9afdf add sleep 1 before nixos-rebuild API request
continuous-integration/drone/push Build is failing Details
2023-12-22 14:43:13 +04:00
Alexander ec339bb8d1 use tests library for `enable-service` tests
continuous-integration/drone/push Build is failing Details
2023-12-22 14:19:14 +04:00
Alexander 55cf8bc0a6 wait for NixOS boot 460 seconds at maximum
continuous-integration/drone/push Build is failing Details
2023-12-21 13:21:29 +04:00
Alexander 30f5d85acd fix ip4 value for domain TXT DNS record
continuous-integration/drone/push Build is failing Details
2023-12-21 12:35:59 +04:00
Alexander 09ee1f5e4c 3 times increase sleep after failure timeout
continuous-integration/drone/push Build is failing Details
2023-12-21 12:18:13 +04:00
Alexander 3b4d75bcdb use new retry function; split test into 2
continuous-integration/drone/push Build is failing Details
2023-12-20 21:43:51 +04:00
Alexander 4380a733a6 wait for 443 port instead of sleep
continuous-integration/drone/push Build is failing Details
2023-12-20 20:32:53 +04:00
Alexander a717ad6ca5 STAGING_ACME=true
continuous-integration/drone/push Build is failing Details
2023-12-20 17:39:49 +04:00
Alexander 1344aaa92c `nix flake lock --override-input sp-modules` instead of --update-input
continuous-integration/drone/push Build is failing Details
2023-12-20 16:41:42 +04:00
Alexander 0e6d5414f5 CF_TOKEN => DNS_PROVIDER_TOKEN
continuous-integration/drone/push Build is failing Details
2023-12-20 16:36:14 +04:00
Alexander 0857b96001 STAGING_ACME=false
continuous-integration/drone/push Build is failing Details
2023-12-18 23:57:50 +04:00
Alexander 7099e2eada re-enable all services after rollback
continuous-integration/drone/push Build is failing Details
2023-12-18 17:00:09 +04:00
Alexander 7d15460867 slightly increase retry timeout after nixos-rebuild switch
continuous-integration/drone/push Build is failing Details
2023-12-18 16:31:57 +04:00
Alexander e3ad189d9a add retries for query-minimum-services
continuous-integration/drone/push Build is failing Details
2023-12-18 16:00:06 +04:00
Alexander efad17f194 wait minimal services startup for 1000 seconds
continuous-integration/drone/push Build is failing Details
2023-12-18 15:32:56 +04:00
Alexander 0e44c12bef retries message
continuous-integration/drone/push Build is failing Details
continuous-integration/drone Build is failing Details
2023-12-17 16:41:51 +04:00
Alexander f3e19457ee do not write resticPassword to secrets.json
continuous-integration/drone/push Build is failing Details
2023-12-16 09:44:07 +04:00
Alexander d4e33a9639 notifications during retries
continuous-integration/drone/push Build is failing Details
2023-12-12 20:52:23 +04:00
Alexander 149d6a8dd2 1000 seconds sleep on failure while machine pings 2023-12-12 19:35:37 +04:00
Alexander 94421ee3d5 jitsi => jitsi-meet
continuous-integration/drone/push Build is failing Details
2023-12-12 16:31:52 +04:00
Alexander c130acf95a enable services via API, wait until active or timeout
continuous-integration/drone/push Build is failing Details
2023-12-12 14:24:42 +04:00
Alexander 1008f2aa51 pass API_TOKEN to query-all-services test
continuous-integration/drone/push Build is passing Details
2023-12-12 09:08:04 +04:00
Alexander b49b801baa add test: query-all-services state via GraphQL API
continuous-integration/drone/push Build is failing Details
2023-12-12 08:29:06 +04:00
Alexander fa062875e7 add questionable availableKernelModules for DigitalOcean deployment
continuous-integration/drone/push Build is passing Details
2023-12-11 21:17:42 +04:00
Alexander 528d5de6dd networking configuration for DigitalOcean
continuous-integration/drone/push Build is passing Details
2023-12-11 20:34:09 +04:00
Alexander 158c711f66 fix hashedMasterPassword: install missing `mkpasswd` via `whois` package
continuous-integration/drone/push Build is passing Details
2023-12-11 13:40:47 +04:00
Alexander ba181fb162 generate deployment.nix with system.stateVersion
continuous-integration/drone/push Build is passing Details
2023-12-11 05:35:58 +04:00
Alexander 8de4c0908f pass SSH_AUTHORIZED_KEY and merge with userdata.json 2023-12-05 05:28:15 +04:00
Alexander 9245d6584d drone: clone disable
continuous-integration/drone/push Build is passing Details
2023-12-04 17:09:11 +04:00
Alexander Tomokhov 55149c1af0 infect: fix typo in comment
continuous-integration/drone/push Build is failing Details
2023-12-03 07:08:59 +04:00
Alexander Tomokhov ccf32a3172 drone: pass USER_PASS without base64
continuous-integration/drone/push Build is passing Details
2023-12-03 06:41:19 +04:00
Alexander Tomokhov ac5a9c3517 drone: sleep 600
continuous-integration/drone/push Build is passing Details
2023-11-26 09:40:38 +04:00
Alexander Tomokhov 79bb94d7e4 drone: sleep 420
continuous-integration/drone/push Build is failing Details
2023-11-24 17:04:38 +04:00
Alexander Tomokhov 40bade435b drone: change nixos template URL
continuous-integration/drone/push Build is failing Details
2023-11-24 16:35:14 +04:00
Inex Code 00f9141343 ci: Hide email password
continuous-integration/drone/push Build is failing Details
2023-11-24 13:20:45 +03:00
Alexander Tomokhov fd924f354f Revert "drone: sleep 1000"
continuous-integration/drone/push Build is failing Details
This reverts commit 66c0e15293.
2023-11-24 13:53:20 +04:00
Alexander Tomokhov 66c0e15293 drone: sleep 1000
continuous-integration/drone/push Build is failing Details
2023-11-24 06:57:20 +04:00
Alexander Tomokhov 00cf7c40a1 drone: sleep 360
continuous-integration/drone/push Build is failing Details
2023-11-24 06:49:42 +04:00
Alexander Tomokhov 6c0486034d drone: fix INFECT_SOURCE_BRANCH variable
continuous-integration/drone/push Build is failing Details
2023-11-24 06:21:38 +04:00
Alexander Tomokhov aee0eab178 sleep 999 (dummy commit for CI)
continuous-integration/drone/push Build is failing Details
2023-11-24 05:35:33 +04:00
Alexander Tomokhov 670985f3d5 do not send nixos-infect logs over netcat 2023-11-24 04:09:49 +04:00
Alexander Tomokhov 212f2806f0 sleep 1000
continuous-integration/drone/push Build is failing Details
2023-11-23 03:18:44 +04:00
Alexander Tomokhov 347ee5ca12 fix typo in generated secrets.json
continuous-integration/drone/push Build is failing Details
2023-11-22 23:50:09 +04:00
Alexander Tomokhov 5fc8a090f7 add github issue link to the comment about subflake
continuous-integration/drone/push Build is failing Details
2023-11-22 22:26:58 +04:00
Alexander Tomokhov 4cf9f98105 remove installed Nix after switch-to-configuration boot
continuous-integration/drone/push Build is failing Details
2023-11-22 01:49:08 +04:00
Alexander Tomokhov 8462fff065 drone: sleep duration 360 => 600 seconds 2023-11-22 01:49:08 +04:00
Alexander cae1b64689 fix infect: lustrate /etc/selfprivacy/secrets.json
continuous-integration/drone/push Build is failing Details
2023-11-21 15:28:21 +02:00
Alexander Tomokhov 401f6e0789 do not remove original OS stuff (it will be moved to /old-root anyway)
continuous-integration/drone/push Build is failing Details
2023-11-21 06:31:07 +04:00
Alexander Tomokhov 6645c8e61b Revert "add users.users.root.hashedPassword for testing purposes"
continuous-integration/drone/push Build is failing Details
This reverts commit 31dfa8eb4a.
2023-11-21 03:01:14 +04:00
Alexander Tomokhov 7e79a26715 do not keep root/.nix-defexpr/channels after reboot 2023-11-21 03:01:09 +04:00
Alexander Tomokhov 5169969e03 fix passing HOSTNAME and DOMAIN variables to userdata.json 2023-11-21 01:18:51 +04:00
Alexander Tomokhov bc0f59695c exclude dot files when extracting top-level flake 2023-11-21 00:57:54 +04:00
Alexander Tomokhov 73b5863968 fix isEFI variable assignment
continuous-integration/drone/push Build is failing Details
2023-11-21 00:25:34 +04:00
Alexander Tomokhov ce8d1f8404 drone: DRONE_SOURCE_BRANCH => INFECT_SOURCE_BRANCH
continuous-integration/drone/push Build is failing Details
2023-11-20 19:15:24 +04:00
Alexander Tomokhov 31dfa8eb4a add users.users.root.hashedPassword for testing purposes 2023-11-20 19:15:24 +04:00
Alexander Tomokhov c58597c4d7 comment about flake lock and subflake 2023-11-20 18:59:00 +04:00
Alexander Tomokhov daf4390cc9 do not cd into ${LOCAL_FLAKE_DIR}
continuous-integration/drone/push Build is failing Details
2023-11-18 19:28:33 +04:00
Alexander Tomokhov fdfd074e27 drone: ci-sibling2 => ci-sibling 2023-11-18 19:16:46 +04:00
Alexander Tomokhov eeb9b257bf print hardware-configuration.nix in the end
continuous-integration/drone/push Build is failing Details
2023-11-18 19:04:58 +04:00
Alexander Tomokhov 97b9a8421b ci-sibling => ci-sibling2 2023-11-18 19:00:14 +04:00
Alexander Tomokhov d73fdda3ad Revert "drone: delete all 4 used machines"
This reverts commit 5da925ea66.
2023-11-18 18:59:42 +04:00
Alexander Tomokhov 5da925ea66 drone: delete all 4 used machines
continuous-integration/drone/push Build is failing Details
2023-11-18 18:49:05 +04:00
Alexander Tomokhov 4d62fb57b8 fix NO_REBOOT check in the presense of the "nounset" shell option
continuous-integration/drone/push Build is failing Details
2023-11-18 18:21:38 +04:00
Alexander Tomokhov c51aece2b9 fix switch-to-configuration boot exit code check
continuous-integration/drone/push Build is failing Details
2023-11-18 17:43:36 +04:00
Alexander Tomokhov 4c0072fa7c do not install wget
continuous-integration/drone/push Build is failing Details
2023-11-18 17:36:06 +04:00
Alexander Tomokhov 89b1f212ea do not activate or switch configuration (only boot)
continuous-integration/drone/push Build is failing Details
2023-11-18 17:28:19 +04:00
Alexander Tomokhov f3aca43b24 increase wait time to 360 seconds
continuous-integration/drone/push Build is failing Details
2023-11-18 09:14:02 +04:00
Alexander Tomokhov 3fa57fc77f rm -v -rf /etc/{dbus-1,terminfo,systemd}
continuous-integration/drone/push Build is failing Details
2023-11-18 08:58:34 +04:00
Alexander Tomokhov 47f3ba8cee increase wait time to 300 seconds
continuous-integration/drone/push Build is failing Details
2023-11-18 08:41:14 +04:00
Alexander Tomokhov 5c8f67f61b without double quotes
continuous-integration/drone/push Build is failing Details
2023-11-18 08:35:23 +04:00
Alexander Tomokhov 8f2d7c8cdb name=ci-sibling4 => name=ci-sibling
continuous-integration/drone/push Build is failing Details
2023-11-18 08:31:30 +04:00
Alexander Tomokhov 3bf02d9f72 name=ci-sibling3 => name=ci-sibling4
continuous-integration/drone/push Build is failing Details
2023-11-18 08:21:07 +04:00
Alexander Tomokhov 9f23164bda --extra-experimental-features "nix-command flakes"
continuous-integration/drone/push Build is failing Details
2023-11-18 08:20:26 +04:00
Alexander Tomokhov 490cd38e4e name=ci-sibling2 => name=ci-sibling3
continuous-integration/drone/push Build is failing Details
2023-11-18 08:17:21 +04:00
Alexander Tomokhov 04260e0852 nix flake lock --update-input sp-modules
continuous-integration/drone/push Build is failing Details
2023-11-18 08:15:13 +04:00
Alexander Tomokhov 623856265c name=ci-sibling => name=ci-sibling2
continuous-integration/drone/push Build is failing Details
2023-11-18 08:08:06 +04:00
Alexander Tomokhov 5c9a185238 install multiuser Nix (--daemon)
continuous-integration/drone/push Build is failing Details
2023-11-18 08:04:13 +04:00
Alexander Tomokhov db7dd2cff8 remove unneeded ubuntu repositories to speed up
continuous-integration/drone/push Build is failing Details
2023-11-18 07:54:10 +04:00
Alexander Tomokhov e7512f3dc6 NIX_VERSION=2.18.2 => NIX_VERSION=2.18.1 2023-11-18 07:49:36 +04:00
Alexander Tomokhov 5131cfe35e fix LADMIN => LUSER 2023-11-18 07:49:34 +04:00
Alexander Tomokhov da56a4625b remove SUDO_USER variable
continuous-integration/drone/push Build is failing Details
2023-11-18 07:43:41 +04:00
Alexander Tomokhov 3b800a7731 trying to get any logs
continuous-integration/drone/push Build is failing Details
2023-11-18 07:39:46 +04:00
Alexander Tomokhov b2edfdb787 route logs to another machine using netcat
continuous-integration/drone/push Build is failing Details
2023-11-18 07:21:49 +04:00
Alexander Tomokhov 6cda28182a first test
continuous-integration/drone/push Build is failing Details
2023-11-18 06:47:34 +04:00
Inex Code 491f836dc6 chore: Use master branch of NixOS config 2023-03-17 14:08:23 +03:00
Inex Code 3fda846808 fix: Defaults not being set 2023-02-08 16:27:50 +03:00
NaiJi ✨ f0741188b1 feat: Implement dns provider type variable for infecting 2023-01-17 17:12:53 +04:00
Inex Code 8fc64e74fd Merge pull request 'updates resolvers with quad9' (#24) from sova/selfprivacy-nixos-infect:master into master
Reviewed-on: #24
2023-01-06 11:18:05 +02:00
sоvд e05d7a6aa4 Merge pull request 'changes to quad9 upstream resolvers' (#1) from sova/change-upstream-resolvers-to-quad9 into master
Reviewed-on: sova/selfprivacy-nixos-infect#1
2022-12-14 23:13:07 +02:00
sova 0ca9b7fb75 changes to quad9 upstream resolvers 2022-12-14 21:11:39 +00:00
NaiJi ✨ d88d75b9b7 chore: Remove temporary cloning branch and change back to api-redis
Testing is over :')  now back to normal
2022-11-20 16:39:01 +02:00
NaiJi ✨ be62d1913e chore!: Change to test branch
Please don't forget to overwrite with api-redis again
2022-11-18 07:54:32 +02:00
Inex Code f638192bee fix: userdata.json formatting 2022-11-17 11:19:16 +03:00
Inex Code f86b76f6bd fix: JSON formatting and config branch 2022-11-16 11:11:22 +03:00
Inex Code 6bfe91dee4 feat: Add support for staging ACME 2022-11-16 11:06:15 +03:00
NaiJi ✨ bb59d623c5 chore!: Change to temp branch for test
DON'T MERGE
2022-11-16 03:39:34 +04:00
inexcode eca6780983 feat: Initialize server with binds and resizable volume activated 2022-10-10 17:40:46 +03:00
Inex Code be3a8485f6 Init server with the Selfprivacy channel 2022-07-19 14:56:52 +03:00
Inex Code c155f32d5e Merge pull request 'Encoded Passwords' (#23) from escaped-passwords into master
Reviewed-on: #23
2022-04-06 20:13:41 +03:00
Inex Code ecdac11cbe Fix password escaping in JSON 2022-04-06 19:55:19 +03:00
Inex Code 0f779b5a1a Fix if condition 2022-04-06 19:49:24 +03:00
inexcode 3a224f2111 Decode password from base64 2022-04-06 15:50:45 +03:00
2 changed files with 596 additions and 162 deletions

234
.drone.yml Normal file
View File

@ -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

View File

@ -1,118 +1,269 @@
#! /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() {
export HASHED_PASSWORD=$( mkpasswd -m sha-512 "$PASSWORD" )
# Skip everything if main config already present
[[ -e /etc/nixos/configuration.nix ]] && return 0
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
mkdir -p /etc/nixos
# optional variables
: "${NIXOS_CONFIG_ID:=default}"
: "${SSH_AUTHORIZED_KEY:=}"
: "${VOLUME_DEV_PATH:=}"
: "${STAGING_ACME:=false}"
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/c204ab6da572e28fd5c5e9f6f30c6ed3fb35d709.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
local IFS=$'\n'
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
[[ -r "$trypath" ]] \
&& keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") \
&& break
done
local network_import=""
readonly NL=$'\n'
readonly LOCAL_FLAKE_DIR="/etc/nixos"
readonly SECRETS_FILEPATH="/etc/selfprivacy/secrets.json"
readonly NIX="/root/.nix-profile/bin/nix"
readonly NIX_OPTS=(--extra-experimental-features "nix-command flakes")
DoNetConf=
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": {
"token": "$API_TOKEN"
$(genOptionalSsh)
"dns": {
"provider": "$DNS_PROVIDER_TYPE",
"useStagingACME": $STAGING_ACME
},
"backblaze": {
"accountId": "$BACKBLAZE_KEY_ID",
"accountKey": "$BACKBLAZE_ACCOUNT_KEY",
"bucket": "$BACKBLAZE_BUCKET_NAME"
"server": {
"provider": "$PROVIDER"
},
"bitwarden": {
"enable": true
},
"cloudflare": {
"apiKey": "$CF_TOKEN"
},
"databasePassword": "$DB_PASSWORD",
"domain": "$DOMAIN",
"hashedMasterPassword": "$HASHED_PASSWORD",
"hashedMasterPassword": "$hashed_password",
"hostname": "$HOSTNAME",
"nextcloud": {
"enable": true,
"adminPassword": "$PASSWORD",
"databasePassword": "$PASSWORD"
},
"gitea": {
"enable": true
},
"jitsi": {
"enable": true
},
"ocserv": {
"enable": true
},
"pleroma": {
"enable": true
},
"timezone": "Europe/Uzhgorod",
"resticPassword": "$PASSWORD",
"username": "$LUSER"
"username": "$LUSER",
"volumes": [
{
"device": "$diskDevice",
"mountPoint": "/volumes/$diskDeviceName",
"fsType": "ext4"
}
],
"modules": {
"bitwarden": {
"location": "$diskDeviceName"
},
"gitea": {
"location": "$diskDeviceName"
},
"nextcloud": {
"location": "$diskDeviceName"
},
"pleroma": {
"location": "$diskDeviceName"
},
"simple-nixos-mailserver": {
"location": "$diskDeviceName"
}
}
}
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
boot.loader.grub = {
efiSupport = true;
efiInstallAsRemovable = true;
device = "nodev";
};
fileSystems."/boot" = { device = "$esp"; fsType = "vfat"; };
fileSystems."/boot" = { device = "$ESP"; fsType = "vfat"; };
EOF
)
else
bootcfg=$(cat << EOF
boot.loader.grub.device = "$grubdev";
boot.loader.grub.device = "$GRUBDEV";
EOF
)
fi
# If you rerun this later, be sure to prune the filesSystems attr
cat > /etc/nixos/hardware-configuration.nix << EOF
local currentSystem
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, ... }:
{
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
$bootcfg
boot.initrd.kernelModules = [ "nvme" ];
fileSystems."/" = { device = "$rootfsdev"; fsType = "$rootfstype"; };
boot.initrd.availableKernelModules = [ ${availableKernelModules[@]} ];
fileSystems."/" = { device = "$ROOTFSDEV"; fsType = "$ROOTFSTYPE"; };
nixpkgs.hostPlatform = "${currentSystem}";
}
EOF
[[ -n "$doNetConf" ]] && makeNetworkingConf || true
}
makeNetworkingConf() {
# XXX It'd be better if we used procfs for all this...
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|')
genDeploymentConfiguration() {
local release
eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}')||true
if [ -n "$eth1_name" ];then
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; }|' || '')
ether1=$(ip address show dev "$eth1_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
interfaces1=<< EOF
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")"
then
echo "cannot determine NixOS release version"
return 1
fi
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 = {
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
$a"; done)
@ -120,31 +271,30 @@ makeNetworkingConf() {
ipv6.addresses = [$(for a in "${eth1_ip6s[@]}"; do echo -n "
$a"; done)
];
};
EOF
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
)
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
else
interfaces1=""
extraRules1=""
interfaces1=""
extraRules1=""
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
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
else
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
fi
cat > /etc/nixos/networking.nix << EOF
{ lib, ... }: {
# This file was populated at runtime with the networking
# details gathered from the active system.
local defaultGateway6=${gateway6:+defaultGateway6 = \{ address = "${gateway6}"; interface = "${eth0_name}"; \};}
local ipv6routes=${gateway6:+ipv6.routes = \[ \{ address = "${gateway6}"; prefixLength = 128; \} \];}
cat << EOF
# Networking configuration was populated by nixos-infect with the networking
# details gathered from the running system.
networking = {
nameservers = [ ${nameservers[@]} ];
defaultGateway = "${gateway}";
defaultGateway6 = "${gateway6}";
defaultGateway = "${gateway}";`
`${defaultGateway6:+
defaultGateway6}
dhcpcd.enable = false;
$predictable_inames
interfaces = {
@ -155,17 +305,19 @@ EOF
ipv6.addresses = [$(for a in "${eth0_ip6s[@]}"; do echo -n "
$a"; done)
];
ipv4.routes = [ { address = "${gateway}"; prefixLength = 32; } ];
ipv6.routes = [ { address = "${gateway6}"; prefixLength = 128; } ];
};
$interfaces1
ipv4.routes = [ { address = "${gateway}"; prefixLength = 32; } ];`
`${ipv6routes:+
$ipv6routes}
};`
`${interfaces1:+
$interfaces1}
};
};
services.udev.extraRules = ''
ATTR{address}=="${ether0}", NAME="${eth0_name}"
$extraRules1
ATTR{address}=="${ether0}", NAME="${eth0_name}"`
`${extraRules1:+
$extraRules1}
'';
}
EOF
}
@ -183,12 +335,8 @@ removeSwap() {
rm -vf /tmp/nixos-infect.*.swp
}
isEFI() {
[ -d /sys/firmware/efi ]
}
findESP() {
esp=""
local esp
for d in /boot/EFI /boot/efi /boot; do
[[ ! -d "$d" ]] && continue
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] \
@ -197,22 +345,32 @@ findESP() {
done
[[ -z "$esp" ]] && { echo "ERROR: No ESP mount point found"; return 1; }
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
}
prepareEnv() {
# $esp and $grubdev are used in makeConf()
if isEFI; then
esp="$(findESP)"
if ! USER_PASS="$(base64 -d <<<"$ENCODED_PASSWORD")"; then
echo "Error decoding ENCODED_PASSWORD from Base64!"
exit 1
fi
readonly USER_PASS
isEFI=0
[ -d /sys/firmware/efi ] && isEFI=1
if ((isEFI)); then
ESP="$(findESP)"
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
# Retrieve root fs block device
# (get root mount) (get partition or logical volume)
rootfsdev=$(mount | grep "on / type" | awk '{print $1;}')
rootfstype=$(df $rootfsdev --output=fstype | sed 1d)
ROOTFSDEV=$(mount | grep "on / type" | awk '{print $1;}')
ROOTFSTYPE=$(df "$ROOTFSDEV" --output=fstype | sed 1d)
# DigitalOcean doesn't seem to set USER while running user data
export USER="root"
@ -262,9 +420,6 @@ checkEnv() {
# Perform some easy fixups before checking
# TODO prevent multiple calls to apt-get update
(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 apt-get && apt-get update && apt-get install -y xz-utils) \
|| true
@ -273,79 +428,124 @@ checkEnv() {
|| true
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 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 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() {
# Add nix build users
# FIXME run only if necessary, rather than defaulting true
groupadd nixbld -g 30000 || true
for i in {1..10}; do
useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" "nixbld$i" || true
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
# this is needed solely for accepting the sp-module sub-flake
# see https://github.com/NixOS/nix/issues/3978#issuecomment-952418478
cd /etc/nixos || return 1
$NIX "${NIX_OPTS[@]}" flake lock ${LOCAL_FLAKE_DIR} \
--override-input sp-modules path:./sp-modules
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
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
# Reify 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
touch /etc/NIXOS
echo etc/nixos > /etc/NIXOS_LUSTRATE
echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE
echo root/.nix-defexpr/channels >> /etc/NIXOS_LUSTRATE
echo etc/nixos > /etc/NIXOS_LUSTRATE
echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE
echo ${SECRETS_FILEPATH} >> /etc/NIXOS_LUSTRATE
rm -rf /boot.bak
isEFI && umount "$esp"
((isEFI)) && umount "$ESP"
mv -v /boot /boot.bak
if isEFI; then
if ((isEFI)); then
mkdir /boot
mount "$esp" /boot
mount "$ESP" /boot
find /boot -depth ! -path /boot -exec rm -rf {} +
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 install -y git tar wget curl whois
apt install -y git tar curl whois jq
checkEnv
prepareEnv
makeSwap # smallest (512MB) droplet needs extra memory!
makeConf
installNix
setupConf
infect
removeSwap
if [[ -z "$NO_REBOOT" ]]; then
if [[ -z "${NO_REBOOT+x}" ]]; then
reboot
fi
fi