mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect.git
synced 2024-11-29 06:31:28 +00:00
first test
This commit is contained in:
parent
491f836dc6
commit
6cda28182a
146
.drone.yml
Normal file
146
.drone.yml
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: default
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
commands:
|
||||||
|
# Create infect user script and then export it to a remote machine on server creation.
|
||||||
|
- echo '#!/bin/sh' > infect.sh
|
||||||
|
- echo "curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/$DRONE_SOURCE_BRANCH/nixos-infect | PROVIDER=hetzner DOMAIN=$DOMAIN LUSER=cicdcicd ENCODED_PASSWORD=\"$(printf $USER_PASS | base64)\" CF_TOKEN=$CLOUDFLARE_TOKEN DB_PASSWORD=$USER_PASS API_TOKEN=$USER_PASS HOSTNAME=selfprivacy-ci-test DNS_PROVIDER_TYPE=CLOUDFLARE STAGING_ACME=false NIX_VERSION=2.18.2 NIXOS_CONFIG_NAME=sp-nixos CONFIG_URL=https://git.selfprivacy.org/api/v1/repos/alexoundos/selfprivacy-nixos-top-level/archive/master.tar.gz bash 2>&1 | tee /root/infect.log" >> infect.sh
|
||||||
|
- http -v --check-status --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 user_data=@infect.sh automount:=false location=fsn1
|
||||||
|
- echo "Wait for 100 seconds."
|
||||||
|
- sleep 100
|
||||||
|
- false
|
||||||
|
|
||||||
|
- 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: sleep
|
||||||
|
commands:
|
||||||
|
- echo "Wait for 100 seconds."
|
||||||
|
- sleep 100
|
||||||
|
|
||||||
|
- name: test
|
||||||
|
environment:
|
||||||
|
DOMAIN:
|
||||||
|
from_secret: DOMAIN
|
||||||
|
USER_PASS:
|
||||||
|
from_secret: USER_PASS
|
||||||
|
TEST_EMAIL_PASS:
|
||||||
|
from_secret: TEST_EMAIL_PASS
|
||||||
|
commands:
|
||||||
|
- >
|
||||||
|
dig api.$DOMAIN
|
||||||
|
- >
|
||||||
|
http -v --verify=false --check-status --ignore-stdin GET https://api.$DOMAIN/services/status Authorization:"Bearer $USER_PASS"
|
||||||
|
- >
|
||||||
|
swaks --to ci@$DOMAIN -s selfprivacy.org -tls -au test@selfprivacy.org -ap $TEST_EMAIL_PASS
|
||||||
|
- >
|
||||||
|
http -v --verify=false --check-status --ignore-stdin GET https://cloud.$DOMAIN/ocs/v2.php/apps/serverinfo/api/v1/info?format=json -a admin:$USER_PASS
|
||||||
|
- >
|
||||||
|
echo "File to test nextcloud upload" > file
|
||||||
|
- >
|
||||||
|
http -v --verify=false --check-status --ignore-stdin PUT https://cloud.$DOMAIN/remote.php/dav/files/admin/file @file -a admin:$USER_PASS
|
||||||
|
- >
|
||||||
|
http -v --verify=false --check-status --ignore-stdin GET https://cloud.$DOMAIN/remote.php/dav/files/admin/file -a admin:$USER_PASS
|
||||||
|
#- >
|
||||||
|
#http -v --verify=false --check-status --ignore-stdin GET https://password.$DOMAIN/admin/diagnostics
|
||||||
|
|
||||||
|
- 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
|
||||||
|
when:
|
||||||
|
status:
|
||||||
|
- failure
|
||||||
|
- success
|
||||||
|
|
||||||
|
- name: switch
|
||||||
|
environment:
|
||||||
|
CHANNEL_SWITCHER_KEY:
|
||||||
|
from_secret: CHANNEL_SWITCHER_KEY
|
||||||
|
commands:
|
||||||
|
- >
|
||||||
|
curl -X POST -H "Authorization: $CHANNEL_SWITCHER_KEY" 'https://channel.selfprivacy.org/nixos-selfpricacy/upgrade'
|
374
nixos-infect
374
nixos-infect
|
@ -1,205 +1,129 @@
|
||||||
#! /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
|
: "${API_TOKEN:?API_TOKEN variable is not set}"
|
||||||
|
: "${DOMAIN:?DOMAIN variable is not set}"
|
||||||
|
: "${LUSER:?LUSER variable is not set}"
|
||||||
|
: "${HOSTNAME:?HOSTNAME variable is not set}"
|
||||||
|
: "${PROVIDER:?PROVIDER variable is not set}"
|
||||||
|
: "${DNS_PROVIDER_TYPE:?DNS_PROVIDER_TYPE variable is not set}"
|
||||||
|
: "${STAGING_ACME:?STAGING_ACME variable is not set}"
|
||||||
|
: "${CF_TOKEN:?CF_TOKEN variable is not set}"
|
||||||
|
: "${DB_PASSWORD:?DB_PASSWORD variable is not set}"
|
||||||
|
: "${ENCODED_PASSWORD:?ENCODED_PASSWORD variable is not set}"
|
||||||
|
: "${NIX_VERSION:?NIX_VERSION variable is not set}"
|
||||||
|
: "${NIXOS_CONFIG_NAME:?NIXOS_CONFIG_NAME variable is not set}"
|
||||||
|
: "${SUDO_USER:?SUDO_USER variable is not set}"
|
||||||
|
: "${CONFIG_URL:?CONFIG_URL variable is not set}"
|
||||||
|
|
||||||
makeConf() {
|
readonly LOCAL_FLAKE_DIR="/etc/nixos"
|
||||||
# Skip everything if main config already present
|
readonly SECRETS_FILEPATH="/etc/selfprivacy/secrets.json"
|
||||||
[[ -e /etc/nixos/configuration.nix ]] && return 0
|
|
||||||
if [[ -z $PASSWORD ]]; then
|
|
||||||
export PASSWORD=$(printf $ENCODED_PASSWORD | base64 --decode)
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -z $DNS_PROVIDER_TYPE ]]; then
|
# Merge original userdata.json with deployment specific fields and print result.
|
||||||
export DNS_PROVIDER_TYPE='CLOUDFLARE'
|
genUserdata() {
|
||||||
fi
|
local password HASHED_PASSWORD userdata_infect
|
||||||
|
password=$(printf "%s" "$ENCODED_PASSWORD" | base64 --decode)
|
||||||
|
HASHED_PASSWORD=$(mkpasswd -m sha-512 "$password")
|
||||||
|
|
||||||
if [[ -z $STAGING_ACME ]]; then
|
userdata_infect=$(cat << EOF
|
||||||
export STAGING_ACME='false'
|
{
|
||||||
fi
|
"dns": {
|
||||||
|
"provider": "$DNS_PROVIDER_TYPE",
|
||||||
|
"useStagingACME": "$STAGING_ACME"
|
||||||
|
},
|
||||||
|
"server": {
|
||||||
|
"provider": "$PROVIDER"
|
||||||
|
},
|
||||||
|
"domain": "DOMAIN",
|
||||||
|
"hashedMasterPassword": "$HASHED_PASSWORD",
|
||||||
|
"hostname": "HOSTNAME",
|
||||||
|
"username": "$LADMIN"
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
export ESCAPED_PASSWORD=$(printf $ENCODED_PASSWORD | base64 --decode | jq -Rs .)
|
jq -s '.[0] * .[1]' \
|
||||||
export HASHED_PASSWORD=$( mkpasswd -m sha-512 "$PASSWORD" )
|
"${1:?no userdata.json given to merge with}" <(printf "%s" "$userdata_infect")
|
||||||
|
}
|
||||||
|
|
||||||
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
|
genSecrets() {
|
||||||
mkdir -p /etc/nixos
|
local ESCAPED_PASSWORD
|
||||||
|
ESCAPED_PASSWORD=$(printf "%s" "$ENCODED_PASSWORD" | base64 --decode | jq -Rs .)
|
||||||
|
|
||||||
git clone https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git /etc/nixos
|
cat << EOF
|
||||||
|
|
||||||
# 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=""
|
|
||||||
|
|
||||||
cat > /etc/nixos/userdata/userdata.json << EOF
|
|
||||||
{
|
{
|
||||||
"api": {
|
"api": {
|
||||||
"token": "$API_TOKEN",
|
"token": "$API_TOKEN",
|
||||||
"skippedMigrations": ["migrate_to_selfprivacy_channel", "mount_volume"]
|
"skippedMigrations": ["migrate_to_selfprivacy_channel", "mount_volume"]
|
||||||
},
|
},
|
||||||
"backup": {
|
|
||||||
"provider": "BACKBLAZE",
|
|
||||||
"accountId": "$BACKBLAZE_KEY_ID",
|
|
||||||
"accountKey": "$BACKBLAZE_ACCOUNT_KEY",
|
|
||||||
"bucket": "$BACKBLAZE_BUCKET_NAME"
|
|
||||||
},
|
|
||||||
"bitwarden": {
|
|
||||||
"enable": true,
|
|
||||||
"location": "sdb"
|
|
||||||
},
|
|
||||||
"dns": {
|
|
||||||
"provider": "$DNS_PROVIDER_TYPE",
|
|
||||||
"apiKey": "$CF_TOKEN",
|
|
||||||
"useStagingACME": $STAGING_ACME
|
|
||||||
},
|
|
||||||
"server": {
|
|
||||||
"provider": "HETZNER"
|
|
||||||
},
|
|
||||||
"databasePassword": "$DB_PASSWORD",
|
"databasePassword": "$DB_PASSWORD",
|
||||||
"domain": "$DOMAIN",
|
"dns": {
|
||||||
"hashedMasterPassword": "$HASHED_PASSWORD",
|
"apiKey": "$CF_TOKEN"
|
||||||
"hostname": "$HOSTNAME",
|
},
|
||||||
|
"modules": {
|
||||||
"nextcloud": {
|
"nextcloud": {
|
||||||
"enable": true,
|
|
||||||
"adminPassword": $ESCAPED_PASSWORD,
|
"adminPassword": $ESCAPED_PASSWORD,
|
||||||
"databasePassword": $ESCAPED_PASSWORD,
|
"databasePassword": $ESCAPED_PASSWORD,
|
||||||
"location": "sdb"
|
|
||||||
},
|
|
||||||
"gitea": {
|
|
||||||
"enable": true,
|
|
||||||
"location": "sdb"
|
|
||||||
},
|
|
||||||
"jitsi": {
|
|
||||||
"enable": true
|
|
||||||
},
|
|
||||||
"ocserv": {
|
|
||||||
"enable": false
|
|
||||||
},
|
|
||||||
"pleroma": {
|
|
||||||
"enable": false,
|
|
||||||
"location": "sdb"
|
|
||||||
},
|
|
||||||
"timezone": "Europe/Uzhgorod",
|
|
||||||
"resticPassword": $ESCAPED_PASSWORD,
|
|
||||||
"username": "$LUSER",
|
|
||||||
"volumes": [
|
|
||||||
{
|
|
||||||
"device": "/dev/sdb",
|
|
||||||
"mountPoint": "/volumes/sdb",
|
|
||||||
"fsType": "ext4"
|
|
||||||
}
|
}
|
||||||
],
|
},
|
||||||
"useBinds": true
|
"resticPassword": $ESCAPED_PASSWORD
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
chmod 0600 /etc/nixos/userdata/userdata.json
|
}
|
||||||
|
|
||||||
if isEFI; then
|
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
|
cat << EOF
|
||||||
cat > /etc/nixos/hardware-configuration.nix << 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"; };
|
fileSystems."/" = { device = "$ROOTFSDEV"; fsType = "$ROOTFSTYPE"; };
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
[[ -n "$doNetConf" ]] && makeNetworkingConf || true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
makeNetworkingConf() {
|
setupConf() {
|
||||||
# XXX It'd be better if we used procfs for all this...
|
mkdir -p ${LOCAL_FLAKE_DIR}
|
||||||
local IFS=$'\n'
|
if ! curl "${CONFIG_URL}" | tar -xz -C ${LOCAL_FLAKE_DIR} --strip-components=1
|
||||||
eth0_name=$(ip address show | grep '^2:' | awk -F': ' '{print $2}')
|
then
|
||||||
eth0_ip4s=$(ip address show dev "$eth0_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
|
echo "Error downloading/extracting top level flake configuration!"
|
||||||
eth0_ip6s=$(ip address show dev "$eth0_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
|
exit 1
|
||||||
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
|
|
||||||
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
|
|
||||||
$eth1_name = {
|
|
||||||
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
|
|
||||||
$a"; done)
|
|
||||||
];
|
|
||||||
ipv6.addresses = [$(for a in "${eth1_ip6s[@]}"; do echo -n "
|
|
||||||
$a"; done)
|
|
||||||
];
|
|
||||||
EOF
|
|
||||||
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
|
|
||||||
else
|
|
||||||
interfaces1=""
|
|
||||||
extraRules1=""
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
readarray nameservers < <(grep ^nameserver /etc/resolv.conf | sed -r \
|
# generate and write hardware-configuration.nix
|
||||||
-e 's/^nameserver[[:space:]]+([0-9.a-fA-F:]+).*/"\1"/' \
|
genHardwareConfiguration > ${LOCAL_FLAKE_DIR}/hardware-configuration.nix
|
||||||
-e 's/127[0-9.]+/8.8.8.8/' \
|
|
||||||
-e 's/::1/8.8.8.8/' )
|
|
||||||
|
|
||||||
if [[ "$eth0_name" = eth* ]]; then
|
# generate infected userdata based on original
|
||||||
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
|
local userdataInfected
|
||||||
else
|
userdataInfected="$(genUserdata ${LOCAL_FLAKE_DIR}/userdata.json)"
|
||||||
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
|
printf "%s" "$userdataInfected" > ${LOCAL_FLAKE_DIR}/userdata.json
|
||||||
fi
|
|
||||||
cat > /etc/nixos/networking.nix << EOF
|
# generate and write secrets
|
||||||
{ lib, ... }: {
|
local secrets
|
||||||
# This file was populated at runtime with the networking
|
secrets="$(genSecrets)"
|
||||||
# details gathered from the active system.
|
install -m0600 <(printf "%s" "$secrets") -DT ${SECRETS_FILEPATH}
|
||||||
networking = {
|
|
||||||
nameservers = [ ${nameservers[@]} ];
|
|
||||||
defaultGateway = "${gateway}";
|
|
||||||
defaultGateway6 = "${gateway6}";
|
|
||||||
dhcpcd.enable = false;
|
|
||||||
$predictable_inames
|
|
||||||
interfaces = {
|
|
||||||
$eth0_name = {
|
|
||||||
ipv4.addresses = [$(for a in "${eth0_ip4s[@]}"; do echo -n "
|
|
||||||
$a"; done)
|
|
||||||
];
|
|
||||||
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
|
|
||||||
};
|
|
||||||
};
|
|
||||||
services.udev.extraRules = ''
|
|
||||||
ATTR{address}=="${ether0}", NAME="${eth0_name}"
|
|
||||||
$extraRules1
|
|
||||||
'';
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
}
|
}
|
||||||
|
|
||||||
makeSwap() {
|
makeSwap() {
|
||||||
|
@ -216,12 +140,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)" ]] \
|
||||||
|
@ -230,22 +150,26 @@ 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()
|
isEFI=0
|
||||||
if isEFI; then
|
[ -d /sys/firmware/efi ] || isEFI=1
|
||||||
esp="$(findESP)"
|
|
||||||
|
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"
|
||||||
|
@ -295,9 +219,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
|
||||||
|
@ -306,48 +227,100 @@ 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; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Download and execute the nix installer script.
|
||||||
|
installNix() {
|
||||||
|
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 "${installURL}" -o "${tmpNixInstall}" &>/dev/null; then
|
||||||
|
echo "Failure while downloading Nix install script!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! sha="$(curl "${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}"; then
|
||||||
|
echo "Nix is installed"
|
||||||
|
rm "${tmpNixInstall}"
|
||||||
|
else
|
||||||
|
echo "Nix installation script failed!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
infect() {
|
infect() {
|
||||||
# Add nix build users
|
# Add nix build users
|
||||||
# FIXME run only if necessary, rather than defaulting true
|
# FIXME run only if necessary, rather than defaulting true
|
||||||
groupadd nixbld -g 30000 || true
|
# groupadd nixbld -g 30000 || true
|
||||||
for i in {1..10}; do
|
# 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
|
# useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" "nixbld$i" || true
|
||||||
done
|
# done
|
||||||
# TODO use addgroup and adduser as fallbacks
|
# TODO use addgroup and adduser as fallbacks
|
||||||
#addgroup nixbld -g 30000 || true
|
#addgroup nixbld -g 30000 || true
|
||||||
#for i in {1..10}; do adduser -DH -G nixbld nixbld$i || true; done
|
#for i in {1..10}; do adduser -DH -G nixbld nixbld$i || true; done
|
||||||
|
|
||||||
curl -L https://nixos.org/nix/install | $SHELL
|
# install Nix in the current system
|
||||||
|
# (this should create system nixbld users too)
|
||||||
|
# curl -L "https://releases.nixos.org/nix/nix-$NIX_VERSION/install" | $SHELL
|
||||||
|
if ! installNix; then
|
||||||
|
echo "Nix installation failed!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
source ~/.nix-profile/etc/profile.d/nix.sh
|
# is it needed?
|
||||||
|
# source ~/.nix-profile/etc/profile.d/nix.sh
|
||||||
|
|
||||||
nix-channel --remove nixpkgs
|
echo "nix build the configuration flake..."
|
||||||
nix-channel --add "https://channel.selfprivacy.org/nixos-selfpricacy" nixos
|
if ! /root/.nix-profile/bin/nix build \
|
||||||
nix-channel --update
|
--extra-experimental-features "nix-command flakes" \
|
||||||
|
--profile /nix/var/nix/profiles/system \
|
||||||
|
${LOCAL_FLAKE_DIR}/#nixosConfigurations.sp-nixos.config.system.build.toplevel
|
||||||
|
then
|
||||||
|
echo "Failed!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
export NIXOS_CONFIG=/etc/nixos/configuration.nix
|
echo "activate the built configuration..."
|
||||||
|
if ! /nix/var/nix/profiles/system/activate; then
|
||||||
|
echo "Failed!"; exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
nix-env --set \
|
echo "make configuration switch..."
|
||||||
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
|
if /nix/var/nix/profiles/system/bin/switch-to-configuration switch; then
|
||||||
-f '<nixpkgs/nixos>' \
|
echo "Failed!"; exit 1
|
||||||
-p /nix/var/nix/profiles/system \
|
fi
|
||||||
-A system
|
|
||||||
|
|
||||||
# Remove nix installed with curl | bash
|
# Remove nix installed by the "install" script.
|
||||||
rm -fv /nix/var/nix/profiles/default*
|
rm -fv /nix/var/nix/profiles/default*
|
||||||
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
|
/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
|
[[ -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
|
||||||
|
@ -357,24 +330,31 @@ infect() {
|
||||||
echo root/.nix-defexpr/channels >> /etc/NIXOS_LUSTRATE
|
echo root/.nix-defexpr/channels >> /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
|
||||||
}
|
}
|
||||||
|
|
||||||
[ "$PROVIDER" = "digitalocean" ] && doNetConf=y # digitalocean requires detailed network config to be generated
|
set -o pipefail
|
||||||
|
set -o nounset
|
||||||
|
set -o xtrace
|
||||||
|
set -o errexit
|
||||||
|
|
||||||
apt update
|
apt update
|
||||||
apt install -y git tar wget curl whois jq
|
apt install -y git tar wget curl jq
|
||||||
checkEnv
|
checkEnv
|
||||||
prepareEnv
|
prepareEnv
|
||||||
makeSwap # smallest (512MB) droplet needs extra memory!
|
makeSwap # smallest (512MB) droplet needs extra memory!
|
||||||
makeConf
|
setupConf
|
||||||
infect
|
infect
|
||||||
removeSwap
|
removeSwap
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue