-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·178 lines (153 loc) · 4.58 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·178 lines (153 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env nix
#! nix shell nixpkgs#coreutils nixpkgs#findutils nixpkgs#age nixpkgs#sops nixpkgs#nixos-anywhere --command bash
#/ DESCRIPTION:
#/-
#/ Deploy NixOS configuration to remote hosts using NixOS anywhere.
#/-
#/
#/ USAGE:-
#/ deploy -n|--nixos-configuration NAME -t|--target-host HOST [--master-key-path PATH] [--user-key-spec USERKEY...] [--no-secrets]
#/
#/ SYNOPSIS:
#/ -h|--help: Prints this message.
#/ -n|--nixos-configuration NAME: Name of nixos configuration to deploy to target.
#/ -t|--target-host HOST: SSH username and hostname of server to deploy to (e.g. myuser@myserver).
#/ -m|--master-key-path PATH: Path to secrets master key.
#/ -u|--user-key-spec USERKEY: Path to secret key for a user in the format user=/path/to/keys.txt.
#/ --no-secrets: Skip copying secrets steps to target.
set -euo pipefail
# NOTE: Colors
__STYLE_RESET='\e[0m'
__STYLE_BOLD='\e[1m'
__STYLE_CYAN='\e[36m'
__STYLE_BCYAN='\e[36;1m'
__STYLE_GREEN='\e[32m'
__STYLE_BGREEN='\e[32;1m'
__STYLE_RED='\e[31m'
__STYLE_BRED='\e[31;1m'
__STYLE_YELLOW='\e[33m'
__STYLE_BYELLOW='\e[33;1m'
__OPTION_HELP="0"
__OPTION_NO_SECRETS="0"
__OPTION_TARGET_HOST=""
__OPTION_NIXOS_CONFIGURATION=""
__OPTION_MASTER_KEY_PATH=""
__OPTION_USER_KEY_SPEC=()
__SCRIPT_DIR="$(cd "$(dirname "$(realpath "${0}")")" && pwd)"
__EXTRA_FILES_TMP="$(mktemp -d)"
__errexit() {
printf "${__STYLE_BRED}error:${__STYLE_RESET}${__STYLE_RED} %s${__STYLE_RESET}\n" "${1}"
exit 1
}
__log() {
local _level="${1:-info}"
_level="${_level^^}"
local _msg="${2:-}"
local _level_color="${__STYLE_BOLD}"
local _msg_color=""
case "${_level}" in
INFO)
_level_color="${__STYLE_BCYAN}"
_msg_color="${__STYLE_CYAN}"
;;
SUCCESS)
_level_color="${__STYLE_BGREEN}"
_msg_color="${__STYLE_GREEN}"
;;
WARN | WARNING)
_level_color="${__STYLE_BYELLOW}"
_msg_color="${__STYLE_YELLOW}"
;;
ERROR)
_level_color="${__STYLE_BRED}"
_msg_color="${__STYLE_RED}"
;;
esac
printf "${_level_color}%s, %s:${__STYLE_RESET}${_msg_color} %s${__STYLE_RESET}\n" "$(date '+%F %H:%M:%S')" "${_level}" "${_msg}"
}
__parse_args() {
while (("${#}")); do
local __arg="${1:-}"
local __value="${2:-}"
case "${__arg}" in
-h | --help)
__OPTION_HELP=1
;;
--no-secrets)
__OPTION_NO_SECRETS=1
;;
-n | --nixos-configuration)
__OPTION_NIXOS_CONFIGURATION="${__value}"
shift
;;
-m | --master-key-path)
__OPTION_MASTER_KEY_PATH="${__value}"
shift
;;
-t | --target-host)
__OPTION_TARGET_HOST="${__value}"
shift
;;
-u | --user-key-spec)
__OPTION_USER_KEY_SPEC+=("${__value}")
shift
;;
*)
__errexit "unexpected option: ${__arg}"
;;
esac
shift
done
}
_help() {
grep '^#/' <"${0}" | cut -c 4-
}
_age_genkey() {
local _outdir="${1:-${PWD}}"
age-keygen -o "${_outdir}/keys.txt"
__log success "Host age key generated at: ${_outdir}/keys.txt"
}
_sops_updatekeys() {
if [ -z "${__OPTION_MASTER_KEY_PATH}" ]; then
__errexit "failed to rekey secrets, master key path not specified!"
fi
export SOPS_AGE_KEY_FILE="${__OPTION_MASTER_KEY_PATH}"
find "${__SCRIPT_DIR}/secrets" \( -name "*.yml" -or -name "*.ini" -or -name "*.json" \) -exec sops updatekeys {} \;
__log success "Secrets updated successfully"
}
_prepare_extra_files() {
local _confirm_continue="y"
install -dm755 "${__EXTRA_FILES_TMP}/var/lib/sops-nix"
_age_genkey "${__EXTRA_FILES_TMP}/var/lib/sops-nix"
__log info "Copy the new host key to the .sops.yaml file: $(age-keygen -y "${__EXTRA_FILES_TMP}/var/lib/sops-nix/keys.txt")"
read -rp "Update secrets? [Y/n]: " _confirm_continue
if [ "${_confirm_continue,,}" = "y" ]; then
_sops_updatekeys
fi
for userkey in "${__OPTION_USER_KEY_SPEC[@]}"; do
_username="$(cut -d= -f1 <<<"${userkey}")"
_keypath="$(cut -d= -f2 <<<"${userkey}")"
install -dm755 "${__EXTRA_FILES_TMP}/home/${_username}/.config/sops/age"
install -T "${_keypath}" "${__EXTRA_FILES_TMP}/home/${_username}/.config/sops/age/keys.txt"
done
__log success "Secret keys are ready for deployment"
}
_deploy() {
if [ "${__OPTION_NO_SECRETS}" = "0" ]; then
_prepare_extra_files
fi
nixos-anywhere \
--generate-hardware-config nixos-generate-config "${__SCRIPT_DIR}/hosts/${__OPTION_NIXOS_CONFIGURATION}/hardware.nix" \
--extra-files "${__EXTRA_FILES_TMP}" \
--target-host "${__OPTION_TARGET_HOST}" \
--flake "${__SCRIPT_DIR}#${__OPTION_NIXOS_CONFIGURATION}"
}
_main() {
if [ "${__OPTION_HELP}" = "1" ]; then
_help
exit 0
fi
_deploy
}
__parse_args "${@}"
_main