-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·143 lines (124 loc) · 3.46 KB
/
deploy.sh
File metadata and controls
executable file
·143 lines (124 loc) · 3.46 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
#!/usr/bin/env bash
set -euo pipefail
cdir="$(dirname "$(readlink -f "${0}")")"
function msg {
echo "${@}" >&2
}
function die {
if [[ ${#} -ge 1 ]]; then
msg "${@}"
fi
exit 1
}
function list_hosts {
find "${cdir}"/hosts -mindepth 1 -maxdepth 1 -type d -printf '%f\n'
}
function usage {
msg "usage:"
msg "${0} sync <host> <remote ssh host> # sync this repo to remote host, build on remote host, deploy there locally; does not require nix."
msg "${0} remote <host> # build locally, deploy to remote host"
msg "${0} local <host> # build and deploy on this machine"
msg "${0} image <host> # build a bootable disk image and put it in result/"
msg "${0} image-on <host> <device> # build a bootable disk image and burn it to given device (will call sudo automatically when needed)"
msg
msg "${0} sync-back <remote ssh host> # sync this repo from the remote host (if you did 'sync' and then modified stuff there)"
msg
msg "to see available hosts, look for hostnames inside templates/*/hosts.nix"
die
}
function require_rsync {
if which rsync &>/dev/null; then
return
fi
die "this script requires rsync to be installed. please use your package manager to install it"
}
function user_wants {
read -p "${1} (y/n) " -n 1 -r reply
if [[ "${reply}" =~ ^[Yy]$ ]]; then
return 0
else
return 1
fi
}
function require_nix {
if which nix &>/dev/null; then
return
fi
msg "this script requires nix to be installed."
if user_wants "do you want to install it now? "; then
sh <(curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install) --daemon
require_nix
return
fi
die "please install nix and run me again"
}
function deploy_with_sync {
require_rsync
ssh_to=human@"${1}"
rsync --delete -rvzza --progress --info=progress2 "${cdir}"/ "${ssh_to}":mixos/
ssh "${ssh_to}" "sudo bash ~/mixos/deploy.sh local '${conf_name}'"
}
function sync_back {
ssh_to=human@"${1}"
rsync -rvzza --progress --info=progress2 "${ssh_to}":mixos/ "${cdir}"/
}
function deploy_local {
cd "${cdir}"
require_nix
nix run '.#nixos-rebuild' -- switch --flake ".#${conf_name}"
}
function deploy_remote {
cd "${cdir}"
ssh_to=root@"${1}"
require_nix
nix run '.#deploy' -- --skip-checks ".#${conf_name}"
}
function build_image {
cd "${cdir}"
require_nix
nix build ".#${conf_name}"
}
function burn_image {
local cmd
if [[ -w "${1}" ]]; then
cmd=(dd)
else
cmd=(sudo dd)
fi
"${cmd[@]}" if=./result/nixos.img of="${1}" bs=4M status=progress oflag=direct
}
if [[ ${#} -lt 2 ]]; then
usage
fi
action="${1}"
conf_name="${2}"
case "${action}" in
sync)
[[ ${#} -ne 3 ]] && usage || true
deploy_with_sync "${3}"
;;
sync-back)
[[ ${#} -ne 2 ]] && usage || true
sync_back "${2}"
;;
remote)
[[ ${#} -ne 2 ]] && usage || true
deploy_remote "${2}"
;;
local)
[[ ${#} -ne 2 ]] && usage || true
deploy_local
;;
image)
[[ ${#} -ne 2 ]] && usage || true
build_image
;;
image-on)
[[ ${#} -ne 3 ]] && usage || true
build_image
burn_image "${3}"
;;
*)
usage
;;
esac