-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix_bundle.sh.head
124 lines (112 loc) · 3.74 KB
/
unix_bundle.sh.head
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
#!/bin/sh -e
# Unix script to run AnsibleGo on multiple Host platforms
# which have a bundle of binaries for specific systems.
# It detects the Unix platform, unpacks the usable binary
# to temporary folder (if mtime > script) and executes it.
ddFunc() {
# Substitute for dd to avoid slow ibs=1 on BSD systems
# $1 - src, $2 - offset, $3 - count (optional)
if [ "x$3" != "x" ]; then
tail "-c+$(($2+1))" "$1" | head "-c$3"
else
tail "-c+$(($2+1))" "$1"
fi
}
# Look for self path
self_path=$0
exec_path="${self_path}.bin"
if [ ! -f "$self_path" ]; then
# Apparently script is running from PATH
exec_path="/tmp/$self_path.bin"
self_path=$(command -v "$self_path")
if [ ! -f "$self_path" ]; then
echo "AnsibleGo: Unable to locate self path: $0"
exit 1
fi
fi
# Check if the binary already unpacked and older then script
if [ -f "${exec_path}" -a "${self_path}" -ot "${exec_path}" ]; then
# Run AnsibleGo executable
chmod +x "$exec_path"
"$exec_path" "$@"
exit $?
fi
# Removing the old executable to recreate it
rm -f "$exec_path"
# Detect the host platform kernel and arch
kernel=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
[ "x$arch" != 'xx86_64' ] || arch=amd64
# Locating all the embedded binaries
# Notice: Match string is splitted in half to not find itself by fixed grep
embedded_headers=$(grep -F --text --byte-offset -- '--- ''EMBEDDED_BINARY'' ' "${self_path}")
# Find the main kernel-arch binary in self bundle
while read line; do
offset=$(echo "$line" | cut -d: -f 1)
header=$(echo "$line" | cut -d: -f 2-)
if [ "$main_offset" ]; then
# main_offset is set then this line ends the main binary content
main_count=$(($offset - $main_offset - 1))
break
fi
if echo "$header" | cut -d' ' -f 3 | grep -q -- "${kernel}-${arch}"; then
# Found the main binary
main_offset=$(($(echo "$header" | wc -c) + $offset))
main_package=$(echo "$header" | cut -d' ' -f 4)
fi
done <<EOF
${embedded_headers}
EOF
# Extracting the main executable binary (dd is limited by BSD implementation)
ddFunc "${self_path}" "${main_offset}" "${main_count}" > "${exec_path}.${main_package}"
if [ "x${main_package}" = "xxz" ]; then
# Unpack xz main binary
case "$kernel" in
linux)
# Linux needs xz-utils to be installed
xz -d -T0 "$exec_path.xz"
;;
darwin)
# Gzip on modern MacOS can extract XZ
gzip -d "$exec_path.xz"
;;
*)
echo "AnsibleGo: Not supported platform: ${kernel}"
exit 1
;;
esac
elif [ "x${main_package}" = "xgz" ]; then
# Unpack gz main binary
gzip -d "$exec_path.gz"
else
# Just rename to the desired exec name
mv "${exec_path}.${main_package}" "${exec_path}"
fi
# Attach the rest of the execs to the binary
while read line; do
offset=$(echo "$line" | cut -d: -f 1)
header=$(echo "$line" | cut -d: -f 2-)
if [ "$prev_offset" ]; then
# This will append all the parts except for the last one
echo '' >> "$exec_path"
ddFunc "${self_path}" "${prev_offset}" "$(($offset-$prev_offset-1))" >> "$exec_path"
fi
if echo "$header" | cut -d' ' -f 3 | grep -q -- "${kernel}-${arch}"; then
# If it's the main one - skipping it
prev_offset=''
else
prev_offset="$offset"
fi
done <<EOF
${embedded_headers}
EOF
# And processing the last exec if it's not the main one
if [ "$prev_offset" ]; then
echo '' >> "$exec_path"
ddFunc "${self_path}" "${prev_offset}" >> "$exec_path"
fi
# Run AnsibleGo executable
chmod +x "$exec_path"
"$exec_path" "$@"
exit $?
# Rest of the file contains embedded AnsibleGo binaries attached during the build