-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdeploy.sht
280 lines (247 loc) · 5.7 KB
/
mkdeploy.sht
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
#%shebang
#%version 11
#%prolog
################
#%include sys/checkdir
#%include sys/checkfile
#%include sys/cmd
#%include sys/config
#%include sys/findcmd
#%include sys/msg
#%include sys/pathabs
#%include sys/vars
#%include deploy/checksum
#%include deploy/encode
#%include deploy/pack
################
# Print usage information
usage ()
{
cat << EOF
Usage: ${PROG} [-c config] [-d destdir] -f file... -i initfile [-o outfile]
[-s srcdir] [-w workdir] [-P product] [-V version]
EOF
}
# Print help information
usage_help ()
{
usage
cat << EOF
-c config configuration file
-d destdir where to write output file (default: current directory)
-f file file to add to archive (path relative to srcdir)
-i initfile init-file to execute by deploy after unpacking
-o outfile output file name (either absolute name or relative to destdir)
-s srcdir where to look source files (default: current directory)
-w workdir where to place temporary working files (default: /tmp)
-P product product name to write to deploy file
-V version version of product to write to deploy file
EOF
exit 0
}
####
# Parse command line options
get_options ()
{
local _opt=""
case "$1" in ( '-?' | '-help' | '--help' ) usage_help ;; esac
while getopts ":c:d:f:i:o:s:w:P:V:" _opt ; do
case "${_opt}" in
( 'c' ) OPT_CONF_FILE="$(absolute_path "${OPTARG}")" ;;
( 'd' ) OPT_OUTDIR="${OPTARG}" ;;
( 'i' ) OPT_INITFILE="${OPTARG}" ;;
( 'o' ) OPT_OUTFILE="${OPTARG}" ;;
( 's' ) OPT_SRCDIR="${OPTARG}" ;;
( 'w' ) OPT_WORKDIR="${OPTARG}" ;;
( 'P' ) OPT_PRODUCT="${OPTARG}" ;;
( 'V' ) OPT_VERSION="${OPTARG}" ;;
( 'f' ) OPT_FILELIST="${OPT_FILELIST:+"${OPT_FILELIST} "}${OPTARG}" ;;
( ':' )
err "missing argument for option -- '${OPTARG}'"
usage
return 1
;;
( '?' )
err "unknown option -- '${OPTARG}'"
usage
return 1
;;
( * )
err "no handler for option '${_opt}'"
return 1
;;
esac
done
shift $((${OPTIND} - 1))
if test $# -gt 0 ; then
err "too many arguments"
usage
return 1
fi
}
####
# Create archive
mkpack ()
{
if ! pack_${pack_cmd} "${W_ARCH}" "${SRCDIR}" "${FILELIST}" ; then
err "can't pack file '${W_ARCH}'"
return 1
fi
if test -n "${chksum_cmd}" ; then
MD5SUM="$(chksum_${chksum_cmd} "${W_ARCH}")"
MD5SUM="${MD5SUM%%[[:space:]]*}"
fi
}
# Create blob
mkblob ()
{
if ! cat "${W_ARCH}" | encode_${encode_cmd} "${W_BLOB}" ; then
err "can't encode file '${W_ARCH}'"
return 1
fi
}
# Generate deploy script
mkdeploy ()
{
embed_deploy > "${W_OUT}"
}
# Copy generated deploy script
cpdeploy ()
{
cmd cp "${W_OUT}" "${OUTFILE}"
if test $? -ne 0 ; then
err "can't copy file '${W_OUT}' to file '${OUTFILE}'"
return 1
fi
}
####
# Initialization subroutine
init ()
{
set -o noglob # Do not expand pathnames
# Initialize variables
var_init FILELIST INITFILE OUTDIR OUTFILE \
PRODUCT SRCDIR VERSION WORKDIR &&
{
# Set defaults
DEF_OUTDIR="$(pwd)"
DEF_OUTFILE="deploy.sh"
DEF_SRCDIR="$(pwd)"
DEF_WORKDIR="/tmp/${PROG}.$$"
# Define 'internal' variables
MD5SUM=""
} &&
# Set up configuration file variables
var_init CONF_FILE &&
DEF_CONF_FILE="spxshell/mkdeploy.conf" &&
# Get options
get_options "$@" &&
# Read configuration file
var_set CONF_FILE &&
read_config "${CONF_FILE}" &&
# Set variables
var_set FILELIST INITFILE OUTDIR OUTFILE \
PRODUCT SRCDIR VERSION WORKDIR
}
# Startup subroutine
startup ()
{
local _f=""
local _flist=""
# Check binaries
# Encode
encode_cmd="$(find_command uuencode base64 openssl)"
if test -z "${encode_cmd}" ; then
err "can't find encode binary"
return 1
fi
# Pack
pack_cmd="$(find_command pax tar)"
if test -z "${pack_cmd}" ; then
err "can't find pack binary"
return 1
fi
# MD5 (if there is no MD5 binary then do not check MD5 sum)
chksum_cmd="$(find_command md5sum openssl)"
# Normalize output file path
case "${OUTFILE}" in
( /* ) OUTDIR="$(dirname -- "${OUTFILE}")" ;;
( * ) OUTFILE="${OUTDIR}/${OUTFILE}" ;;
esac
# Check directories
if ! test -d "${SRCDIR}" ; then
err "directory '${SRCDIR}' doesn't exist"
return 1
fi
if ! test -d "${OUTDIR}" ; then
err "directory '${OUTDIR}' doesn't exist"
return 1
fi
# Check variables
if test -z "${FILELIST}" ; then
err "no 'file' or list of files are set"
return 1
fi
if test -z "${INITFILE}" ; then
err "no 'initfile' is set"
return 1
fi
# Check files and convert to relative paths
eval set -- ${FILELIST} \"${INITFILE}\"
for _f in "$@" ; do
case "${_f}" in
( /* ) _f="${_f#"${SRCDIR}/"}" ;;
esac
check_file "${SRCDIR}/${_f}" || return 1
case "${_f}" in
( *[[:space:]]* ) _f="\"${_f}\"" ;;
esac
_flist="${_flist:+"${_flist} "}${_f}"
done
FILELIST="${_flist}"
INITFILE="${INITFILE#"${SRCDIR}/"}" # In the case of absolute path
# Check working directory
( umask 0077 ; check_dir "${WORKDIR}" ) || return 1
# Set working filenames
W_ARCH="${WORKDIR}/archive.pax"
W_BLOB="${WORKDIR}/blob"
W_OUT="${WORKDIR}/out.sh"
}
# Clean working directory
cleanup ()
{
if test -n "${WORKDIR}" ; then
cmd rm -f "${W_ARCH}"
cmd rm -f "${W_BLOB}"
cmd rm -f "${W_OUT}"
cmd rmdir "${WORKDIR}"
fi
}
# Clean up the staff and exit with error
clean_fail ()
{
cleanup
fail "${1:-1}"
}
# Exit with error code
fail ()
{
exit "${1:-1}"
}
####
# Main subroutine
main ()
{
init "$@" && startup || fail
trap 'clean_fail 130' HUP INT TERM
{
mkpack &&
mkblob &&
mkdeploy &&
cpdeploy
} || clean_fail
cleanup
}
#%uinclude deploy/deploy-embed
# Call main subroutine
main "$@"