-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare-sample-configuration
executable file
·192 lines (149 loc) · 4.93 KB
/
prepare-sample-configuration
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
#!/usr/bin/env sh
# This file is part of libertine. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/libertine-linux/libertine/master/COPYRIGHT. No part of libertine, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2016-2018 The developers of libertine. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/libertine-linux/libertine/master/COPYRIGHT.
set -e
set -u
set -f
program_name="prepare-sample-configuration"
_program_path_find()
{
if [ "${0%/*}" = "$0" ]; then
# We've been invoked by the interpreter as, say, bash program
if [ -r "$0" ]; then
pwd -P
# Clutching at straws; probably run via a download, anonymous script, etc, weird execve, etc
else
printf '\n'
fi
else
# We've been invoked with a relative or absolute path (also when invoked via PATH in a shell)
_program_path_find_parentPath()
{
parentPath="${scriptPath%/*}"
if [ -z "$parentPath" ]; then
parentPath='/'
fi
cd "$parentPath" 1>/dev/null
}
# pdksh / mksh have problems with unsetting a variable that was never set...
if [ "${CDPATH+set}" = 'set' ]; then
unset CDPATH
fi
if command -v realpath 1>/dev/null 2>/dev/null; then
(
scriptPath="$(realpath "$0")"
_program_path_find_parentPath
pwd -P
)
elif command -v readlink 1>/dev/null 2>/dev/null; then
(
local recursionDepth=0
_program_path_resolve_symlinks_recursively()
{
local unresolvedPath="$1"
recursionDepth=$((recursionDepth + 1))
if [ $recursionDepth -gt 10 ]; then
printf '%s\n' 'Recursion to depths greater than 10 is not allowed when resolving links.'
return 1
fi
local potentialLinkDestination="$(readlink -- "$unresolvedPath")"
if [ -z "$potentialLinkDestination" ]; then
scriptPath="$unresolvedPath"
return 0
fi
local linkDestination="$potentialLinkDestination"
local parentFolderPath="${unresolvedPath%/*}"
if [ "$parentFolderPath" = "$unresolvedPath" ]; then
_program_path_resolve_symlinks_recursively "$linkDestination"
else
case "$linkDestination" in
/*)
_program_path_resolve_symlinks_recursively "$linkDestination"
;;
*)
_program_path_resolve_symlinks_recursively "$parentFolderPath"/"$linkDestination"
;;
esac
fi
}
scriptPath="$0"
_program_path_resolve_symlinks_recursively "$scriptPath"
_program_path_find_parentPath
pwd -P
)
else
# This approach will fail in corner cases where the script itself is a symlink in a path not parallel with the concrete script
(
scriptPath="$0"
_program_path_find_parentPath
pwd -P
)
fi
fi
}
fail()
{
local exitCode="$1"
local message="$2"
printf '%s:FAIL:%s\n' "$program_name" "$message" 1>&2
exit $exitCode
}
depends()
{
local binary
for binary in "$@"
do
if ! command -v "$binary" 1>/dev/null 2>/dev/null; then
local EX_OSFILE=72
fail $EX_OSFILE "The binary `$binary` is not on the PATH"
fi
done
}
prepare_sample_configuration_createSshKeyPair()
{
local keyPairType="$1"
case "$keyPairType" in
dsa)
set -- -b 1024
;;
rsa)
set -- -b 4096
;;
ecdsa)
set -- -b 521
;;
ed25519)
set --
;;
esac
local keyFilePath=sample-configuration/machines/example/settings/initramfs/etc/ssh/"ssh_host_${keyPairType}_key"
if [ -s "$keyFilePath" ]; then
return 0
fi
if ! command -v ssh-keygen 1>/dev/null 2>/dev/null; then
printf '%s\n' "Could not generate host key pair for $keyPairType for the example machine as 'ssh-keygen' is not in the PATH; please it in machines/example/settings/initramfs/etc/ssh/ssh_host_${keyPairType}_key and ssh_host_${keyPairType}_key.pub"
return 0
fi
umask 077
set +e
IFS= ssh-keygen -q "$@" -t "$keyPairType" -N '' -C "$keyPairType host key for sample-configuration 'example' machine (in sample-configuration/machines)" -f "$keyFilePath"
set -e
if [ $? -ne 0 ]; then
printf '%s\n' "Could not generate a $keyPairType host key pair for the sample-configuration 'example' machine using 'ssh-keygen'; please create it in machines/example/settings/initramfs/etc/ssh/ssh_host_${keyPairType}_key and ssh_host_${keyPairType}_key.pub"
fi
}
depends git
prepare_sample_configuration_main()
{
cd "$(_program_path_find)" 1>/dev/null 2>/dev/null
local keyPairType
for keyPairType in rsa ecdsa ed25519
do
prepare_sample_configuration_createSshKeyPair "$keyPairType"
done
if [ ! -d sample-configuration/packages/.git ]; then
git -C sample-configuration clone --depth 1 --recurse-submodules https://github.com/libertine-linux/packages
fi
sample-configuration/packages/checkout-submodules-to-master
}
prepare_sample_configuration_main "$@"