blob: 1470c8feb3420536b34f889a5299a84b4b44f085 (
plain)
1 #!/usr/bin/env bash
2
3 # This is a pre-allocated string of 39 chars of whitespace
4 # DO NOT CHANGE THIS without great care - it will break header length
5 # calculations
6 LENS=( )
7 export SELF # Absolute path to the installer script (me!)
8 export TMP # Path to tmp install staging directory
9 export PAYLOAD # Path to the extracted payload directory
10 export NOCLEANUP=0 # Do not clean up deploy resources. Useful for debugging
11 export EXTRACTONLY=0 # Execute extract, skipping deployment and cleanup
12 export DECOMPRESS='{{ DECOMPRESS }}' # Command to decompress data
13
14 # Header: 0
15 # runscript: 1
16 # payload: 2
17 extract_chunk_index() {
18 local src="${1}"
19 local dest="${2}"
20 local index="${3}"
21 local skip=0 # Byte count to skip into source
22 local of # Output path with 'of=' prefix. Only used if dest is not '-'
23
24 for (( i = 0; i < index; i++ )); do
25 skip=$(( skip + LENS[$i] ))
26 done
27
28 [ "${dest}" != '-' ] && of="of=${dest}"
29 # Extract the requested chunk index
30 dd bs=${LENS[$index]} count=1 iflag=skip_bytes skip=${skip} \
31 if=${src} ${of} 2>/dev/null
32 }
33
34 extract_header() {
35 extract_chunk_index "${SELF}" "${TMP}/header.sh" 0
36 }
37
38 extract_libinstall() {
39 extract_chunk_index "${SELF}" '-' 1 | ${DECOMPRESS} | tar -C "${TMP}" -x
40 }
41
42 extract_runscript() {
43 extract_chunk_index "${SELF}" '-' 2 | ${DECOMPRESS} > "${TMP}/run.sh"
44 }
45
46 extract_payload() {
47 extract_chunk_index "${SELF}" '-' 3 | ${DECOMPRESS} | tar -C "${TMP}" -x
48 export PAYLOAD="${TMP}/pkg"
49 }
50
51
52 main() {
53 local args=(${@})
54
55 SELFDIR="$(cd $(dirname ${0}) && pwd)"
56 SELF="${SELFDIR}/$(basename ${0})"
57 TMP="$(mktemp -d /tmp/installer-XXXXXXX)"
58
59 extract_header
60 extract_libinstall
61 extract_runscript
62
63 cd "${TMP}"
64
65 source libinstall/template.sh
66 source libinstall/ensure.sh
67 source libinstall/colorize.sh
68
69 source run.sh
70
71 for (( i = 0; i < ${#args[@]}; i++ )); do
72 # NOTE: Do not support short switches here to reduce the probability of
73 # conflicting with a switch provided by the downstream run.sh
74 if [ "${args[$i]}" = '--extract-only' ]; then
75 export EXTRACTONLY=1
76 export NOCLEANUP=1
77 fi
78 done
79
80 if [ "${EXTRACTONLY}" = '1' ]; then
81 printf 'Extracting...\n'
82 extract_payload
83 printf 'Installation payload available at "%s"\n' "${TMP}/pkg"
84 printf 'Skipping cleanup\n'
85 return 0
86 elif [ "$(type -t deploy)" == 'function' ]; then
87 # Function 'deploy' is provided by run.sh
88 deploy ${@}
89 else
90 printf "ERROR: Deploy function not found.\n"
91 fi
92
93 # Cleanup
94 if [ "${NOCLEANUP}" -ne 0 ]; then
95 rm -rf "${TMP}"
96 fi
97 }
98
99 main ${@}
100 exit 0
|