-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnspawn
More file actions
executable file
·188 lines (170 loc) · 4.84 KB
/
nspawn
File metadata and controls
executable file
·188 lines (170 loc) · 4.84 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
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env bash
#
# nspawn - nspawn is a wrapper around machinectl pull
#
# Copyright (c) 2021 by Christian Rebischke <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http: #www.gnu.org/licenses/
#
#======================================================================
# Author: Christian Rebischke
# Email : [email protected]
# Github: www.github.com/shibumi
# Contributor: Eduard Tolosa
# Email: [email protected]
# Github: www.github.com/edu4rdshl
set -e
VERSION="0.6"
STATIC_NAME="Nspawn (https://nspawn.org)"
#
# Defaults
#
BASEURL="https://hub.nspawn.org/storage"
LISTURL="https://hub.nspawn.org/storage/list.txt"
KEYLOCATION="https://hub.nspawn.org/storage/masterkey.pgp"
#
# Get vars from local configs if they exist - overriding the defaults
# Last found config file wins
# e.g. can be used for local mirrors without changing this file
#
LOCAL_CONFIGS=( "/etc/nspawn.conf" "$( dirname "${BASH_SOURCE[0]}" )/nspawn.conf")
for local_config in "${LOCAL_CONFIGS[@]}"; do
[[ -f "${local_config}" ]] && source "${local_config}"
done
ctrl_c() {
echo "Keyboard Interrupt detected, leaving."
exit
}
trap ctrl_c 2
version() {
echo "$STATIC_NAME $VERSION"
}
helpout() {
local program="${0##*/}"
echo "Menu usage for $STATIC_NAME $VERSION"
echo
echo "$program {COMMAND} [PARAMETER]"
echo
echo "Wrapper around systemd-machined and https://nspawn.org"
echo
echo "Commands:"
echo -e " -i/--init \tInitializes an image for systemd-machined with the following parameters: <distribution>/<release>/<type>"
echo -e " -l/--list \tLists all available images"
echo -e " -h/--help \tPrints this help message"
echo -e " -v/--version \tPrints version info"
echo
echo "Parameters:"
echo -e " <distribution>\tCheck $program -l/--list for more information and which distribution names to use."
echo -e " <release> \tThe release of the distribution"
echo -e " <type> \tOne out of (raw,tar)"
}
if [[ $# -eq 0 ]]; then
helpout
exit
fi
escalate_privilege() {
if [ "$EUID" -ne 0 ]; then
echo "nspawn needs root privileges for the following action:"
echo " $1"
exit 1
fi
}
check_pubring() {
if ! [ -f "/etc/systemd/import-pubring.gpg" ]; then
echo "/etc/systemd/import-pubring.gpg does not exist"
read -rp "Do you want to create it [y/n]: " choice
case "$choice" in
y | Y)
escalate_privilege "Setting up the GPG keyring"
gpg --no-default-keyring --keyring=/etc/systemd/import-pubring.gpg --fingerprint
tfile=$(mktemp -u /tmp/masterkey.nspawn.org.XXXXXXXXXXX)
curl "$KEYLOCATION" -o "$tfile"
gpg --no-default-keyring --keyring=/etc/systemd/import-pubring.gpg --import "$tfile"
;;
n | N)
exit 2
;;
*)
exit 2
;;
esac
fi
}
list() {
echo
echo -e "\t$LISTURL"
echo
curl "$LISTURL"
}
init() {
check_pubring
distribution=$(echo "$1" | cut -d"/" -f1)
release=$(echo "$1" | cut -d"/" -f2)
type=$(basename "$1")
image_url="$BASEURL/$distribution/$release/$type/image.$type.xz"
output_image="$distribution-$release-$type"
check_image_location=$(curl -o /dev/null -sIw '%{http_code}' "$image_url")
if [[ $check_image_location -eq 200 ]]; then
if machinectl show-image "$output_image" &>/dev/null; then
echo "Machine $output_image already exists. Details:"
echo
machinectl show-image "$output_image"
echo
exit
fi
escalate_privilege "Pulling the image via machinectl-$type..."
if machinectl pull-"$type" "$image_url" "$output_image" 2>/dev/null; then
echo
echo "Removing read-only flag if present from image $output_image..."
if machinectl read-only "$output_image" false &>/dev/null; then
echo "Image deployed locally as $output_image. Details:"
echo
machinectl show-image "$output_image"
echo
fi
else
echo "Error while deploying image."
fi
else
echo "Error: $check_image_location. Wrong type, distribution or release in $output_image. Try 'nspawn --list'."
fi
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-v | --version)
version
exit
;;
-h | --help)
helpout
exit
;;
-l | --list)
list
exit
;;
-i | --init)
init "$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"