This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplease
executable file
·308 lines (255 loc) · 6.48 KB
/
please
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env bash
# shellcheck disable=SC1117
JOBSET=./jobset.nix
GET_ATTRS=./scripts/get-attrs.nix
BASH_COMPL_SCRIPT=./scripts/please.complete.bash.sh
NIX_INSTALL_URL="${NIX_INSTALL_URL:-https://nixos.org/nix/install}"
log() {
local args="$*"
local PLEASE="\e[32m[please]:\e[0m"
echo -e "$PLEASE $args"
}
log-error() {
local args="$*"
local PLEASE="\e[31m[please]\e[0m"
echo -e "$PLEASE $args"
}
log-run() {
local cmd="$1"
log "Running \"$cmd\"\n"
eval $cmd
}
_list_attrs() {
nix-instantiate --strict --eval --expr "(import $GET_ATTRS {}).all" | tr -d "[]\""
}
_force_nixpkgs() {
# without evaluating this all our queries will fail
nix-build --quiet ./nixpkgs-fetch.nix >/dev/null
}
_get_name() {
local pkg="$1"
nix-instantiate --strict --eval --expr "(import ./jobset.nix {}).$1.name" | tr -d '"'
}
#
# sanity check functions
#
_isRegularUser() {
test $(id -u) -ne 0
}
_hasKvmSupport() {
test -c /dev/kvm && test -w /dev/kvm && test -r /dev/kvm
}
_isChannelInstalled() {
nix-channel --list | grep contrail >/dev/null
}
_isSubstituterConfigured() {
nix show-config | grep "cache.nix.corp.cloudwatt.com" >/dev/null
}
_isNixInstalled() {
nix --version >/dev/null 2>&1
}
_sourceNix() {
NIX_SH="$HOME/.nix-profile/etc/profile.d/nix.sh"
test -f "$NIX_SH" && source "$NIX_SH"
}
_addCacheConfig() {
if test -f ~/.config/nix/nix.conf
then
log "$HOME/.config/nix/nix.conf exists. Please follow the instructions from the README"
else
mkdir -p "$HOME"/.config/nix/
cat << EOF > "$HOME"/.config/nix/nix.conf
substituters = https://cache.nixos.org https://cache.nix.corp.cloudwatt.com
trusted-substituters = https://cache.nix.corp.cloudwatt.com
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cache.nix.cloudwatt.com:ApPt6XtZeOQ3hNRTSBQx+m6rd8p04G0DQwz0bZfVPL8=
EOF
fi
}
check_args() {
local actual="$1"
local expected="$2"
local cmd="$3"
if [ "$actual" -ne "$expected" ]; then
log-error "'$cmd' requires $expected arguments but $actual were given"
exit 1
fi
}
#
# subcommands
#
register-completions() {
cat "$BASH_COMPL_SCRIPT"
}
build() {
local pkg="$1"
log-run "nix-build default.nix -A $pkg"
if [ $? -eq 0 ]; then
echo ""
log "Your build result is symlinked in ./result"
fi
}
install() {
local pkg="$1"
log-run "nix-env -f default.nix -iA $pkg"
}
uninstall() {
local pkg="$1"
log-run "nix-env -e $pkg"
}
shell() {
local pkg="$1"
log-run "nix-shell default.nix -A $pkg"
}
init() {
log "Initializing setup ..."
if _isNixInstalled ; then
log "Looks like nix is already installed"
else
log "Looks like nix is not installed yet"
log "Running 'curl https://nixos.org/nix/install | sh'"
curl "$NIX_INSTALL_URL" | sh
_sourceNix
fi
if _isChannelInstalled; then
log "channel is already configured"
else
log "adding contrail channel"
log-run "nix-channel --add https://hydra.nix.corp.cloudwatt.com/jobset/nixpkgs-tungsten/trunk/channel/latest contrail"
fi
if _isSubstituterConfigured; then
log "cache is already configured"
else
log "adding contrail binary cache"
_addCacheConfig
fi
}
doctor() {
OK="\e[32mOK\e[0m"
X="\e[31mX\e[0m"
FAIL=""
log "Running sanity checks:\n"
if _isNixInstalled
then
echo -e "- Nix installed : $OK"
else
echo -e "- Nix installed : $X"
FAIL="."
fi
if _isChannelInstalled
then
echo -e "- contrail channel: $OK"
else
echo -e "- contrail channel: $X"
FAIL="."
fi
if _isSubstituterConfigured
then
echo -e "- contrail cache: $OK"
else
echo -e "- contrail cache: $X"
FAIL="."
fi
if _hasKvmSupport
then
echo -e "- kvm support: $OK"
else
echo -e "- kvm support: '/dev/kvm' either not found or you don't have r/w permissions. You probably won't be able to start VMs"
fi
if [[ $FAIL = "" ]]; then
echo -e "\nAll essential tests passed."
else
echo -e "\nSome tests failed. Try running the init command:\n"
echo -e " ./please init\n"
echo -e "Check the 'Manual Configuration' section of the README.md if you continue to experience problems."
exit 1
fi
}
list() {
local args="$*"
if [ -z "$args" ]; then
_list_attrs | tr " " "\n"
else
_list_attrs | tr " " "\n" | grep "$args"
fi
}
run-test(){
local t="$1"
log-run "nix-build default.nix -A $t"
}
run-vm(){
local t="$1"
log-run "nix-build default.nix -A $t.driver"
QEMU_NET_OPTS="hostfwd=tcp::8080-:8080,hostfwd=tcp::8143-:8143,hostfwd=tcp::2222-:22" result/bin/nixos-run-vms
}
usage() {
cat <<EOM
Usage: please <command> [args]
build [artifact] -- build an artifact
completions -- output completion script
doctor -- perform sanity checks
init -- configure initial setup
install [artifact] -- install an artifact
list -- list artifacts and tests
run-test [test] -- run a test
run-vm [test] -- run an interactive vm of a test
shell [artifact] -- enter a dev shell for an artifact
uninstall [artifact] -- uninstall a previously installed artifact
In order to enable context-sensitive completions (bash only!) run:
$ source <(./please completions)
You should add this to your init scripts.
EOM
}
if ! _isRegularUser; then
log-error "root user detected. Run ./please as non-root user!"
exit 1
fi
if [ -z "$1" ]; then
usage
exit 1
fi
_sourceNix
_isNixInstalled && _force_nixpkgs
subcommand="$1"
shift
case "$subcommand" in
build)
check_args $# 1 "build"
build "$@"
;;
install)
check_args $# 1 "install"
install "$@"
;;
uninstall)
check_args $# 1 "uninstall"
uninstall $(_get_name "$@")
;;
shell)
check_args $# 1 "shell"
shell "$@"
;;
init)
init "$@"
;;
list)
list "$@"
;;
run-test)
check_args $# 1 "run-test"
run-test "$@"
;;
run-vm)
check_args $# 1 "run-vm"
run-vm "$@"
;;
doctor)
doctor
;;
completions)
register-completions
;;
*)
echo "Error: unknown command: $subcommand"
usage
exit 1
esac