-
Notifications
You must be signed in to change notification settings - Fork 17
/
create-sensu-asset
executable file
·192 lines (186 loc) · 6.19 KB
/
create-sensu-asset
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 bash
WORK_DIR=/tmp/sensu-assets
BUILD_DIR=$WORK_DIR/build
OUTPUT_DIR=$PWD
GREP_EXCLUDE='(ld.so|ld-linux-x86-64.so|libBrokenLocale.so|libSegFault.so|libanl.so|libc.so|libdl.so|libm.so|libmvec.so|libnss_compat.so|libnss_dns.so|libnss_files.so|libpthread.so|libresolv.so|librt.so|libthread_db.so|libutil.so|vdso.so)'
ASSET_ARCH='amd64'
mkdir -p $BUILD_DIR
# Read arguments
while getopts ha:v:b:l:i:e:o:A: argument;
do
case "${argument}"
in
h)
echo "NAME"
echo " create-sensu-asset -- creates Sensu 2 asset packages (tarballs)."
echo
echo "SYNOPSIS"
echo " create-sensu-asset -a ASSET_NAME -v ASSET_VERSION -b /path/to/executable/script/or/binary"
echo
echo "DESCRIPTION"
echo " The create-sensu-asset utility builds Sensu 2 assets containing"
echo " /bin /lib and /include subdirectories, copying the user-"
echo " provided -b BINARY -l LIBRARIES -i INCLUDES files, and"
echo " compressing them as a gzipped tarball."
echo
echo " The options are as follows:"
echo
echo " -h Prints this help!"
echo " -a ASSET_NAME"
echo " The asset package file name prefix (i.e. tarball file"
echo " name). REQUIRED"
echo " -v ASSET_VERSION"
echo " The asset package file name suffix (i.e. the tarball "
echo " file name suffix). OPTIONAL"
echo " -b BINARIES"
echo " The path(s) to executable scripts or binary files,"
echo " which will be packaged in the asset /bin directory, and"
echo " added to the Sensu Agent \$PATH environment variable at"
echo " runtime. Multiple files may be specified as a"
echo " comma-separated list. OPTIONAL"
echo " -l LIBRARIES"
echo " The path(s) to linked library files, which will be"
echo " packaged in the asset /lib directory, and added to the"
echo " Sensu Agent \$LD_LIBRARY_PATH environment variable at."
echo " runtime. Multiple files may be specified as a comma-"
echo " separated. OPTIONAL"
echo " -i INCLUDES"
echo " The path(s) to any additional files, which will be"
echo " packaged in the asset /include directory and accessible"
echo " Multiple files may be specified as a comma-separated"
echo " list. OPTIONAL"
echo " -e EXTRA"
echo " The path(s) to any additional files or directories,"
echo " which will be packaged in the asset / (root) directory."
echo " Multiple files or directories may be specified as a"
echo " comma-separated list. OPTIONAL"
echo " -o OUTPUT_DIR"
echo " The directory where the asset should be created"
echo " (output). Defaults to \$PWD"
echo " -A ARCH"
echo " Architecture of the asset. Defaults to amd64)"
exit 0;
;;
a)
ASSET_NAME=${OPTARG}
;;
v)
ASSET_VERSION="_${OPTARG}"
;;
b)
BINS=${OPTARG}
;;
l)
LIBS=${OPTARG}
;;
i)
INCLUDES=${OPTARG}
;;
e)
EXTRA=${OPTARG}
;;
o)
OUTPUT_DIR=${OPTARG}
;;
A)
ASSET_ARCH=${OPTARG}
;;
esac
done;
# Build the asset
if [[ -n $ASSET_NAME ]];
then
PKG_DIR="${BUILD_DIR}/${ASSET_NAME}${ASSET_VERSION}";
if [[ -d $PKG_DIR ]]; then
echo "Cleaning up pre-existing build directory: ${PKG_DIR}";
rm -rf $PKG_DIR;
fi
echo "Creating asset build directory: ${PKG_DIR}.";
echo ""
mkdir -p $PKG_DIR/bin $PKG_DIR/lib $PKG_DIR/include;
else
echo "ERROR: no option -a provided. Please provide an asset package name.";
exit 2;
fi
if [[ -n $BINS ]]; then
IFS=',' read -r -a BINARIES <<< "${BINS}"
for INDEX in "${!BINARIES[@]}"
do
BINARY="${BINARIES[$INDEX]}"
if [[ -e $BINARY ]]; then
echo "Copying: $BINARY into the asset bin/$BINARY"
cp $BINARY $PKG_DIR/bin/;
if [[ $(which ldd) ]]; then
LINKED_LIBRARIES=($(ldd $BINARY | grep "=>" | egrep -v "${GREP_EXCLUDE}" | awk '{print $3}'))
for INDEX in "${!LINKED_LIBRARIES[@]}"
do
LINKED_LIBRARY="${LINKED_LIBRARIES[$INDEX]}"
echo "Including linked library: $LINKED_LIBRARY"
cp $LINKED_LIBRARY $PKG_DIR/lib/
done
echo ""
fi;
else
echo "ERROR: file not found: ${BINARY}"
exit 2;
fi
done
fi
if [[ -n $LIBS ]]; then
IFS=',' read -r -a LIBRARIES <<< "${LIBS}"
for INDEX in "${!LIBRARIES[@]}"
do
LIBRARY="${LIBRARIES[$INDEX]}";
if [[ -e $LIBRARY ]]; then
echo "Copying: $LIBRARY into the asset lib/$LIBRARY"
cp $LIBRARY $PKG_DIR/lib/;
else
echo "ERROR: file not found: ${LIBRARY}"
exit 2;
fi
done
fi
if [[ -n $INCLUDES ]]; then
IFS=',' read -r -a INCLUDED <<< "${INCLUDES}"
for INDEX in "${!INCLUDED[@]}"
do
INCLUDE="${INCLUDED[$INDEX]}"
if [[ -e $INCLUDE ]]; then
echo "Copying: $INCLUDE into the asset include/$INCLUDE"
cp $INCLUDE $PKG_DIR/include/;
else
echo "ERROR: file not found: ${INCLUDE}"
exit 2;
fi
done
fi
if [[ -n $EXTRAS ]]; then
IFS=',' read -r -a EXTRAS <<< "${EXTRAS}"
for INDEX in "${!EXTRAS[@]}"
do
EXTRA="${EXTRAS[$INDEX]}"
if [[ -d $EXTRA ]]; then
echo "Copying: $EXTRA (a directory) into the asset $EXTRA"
cp -r $EXTRA $PKG_DIR/;
elif [[ -f $EXTRA ]]; then
echo "Copying: $EXTRA (a file) into the asset $EXTRA"
cp $EXTRA $PKG_DIR/;
else
echo "Extra file not found: $EXTRA"
exit 1
fi
done
fi
export ASSET_OS="_$(uname -s | tr '[:upper:]' '[:lower:]')"
ASSET="${OUTPUT_DIR}/${ASSET_NAME}${ASSET_VERSION}${ASSET_OS}_${ASSET_ARCH}.tar.gz"
echo "";
echo "Packaging contents of ${PKG_DIR} into ${ASSET}";
echo "";
tar -zcf $ASSET -C $PKG_DIR .
if [[ -e $ASSET ]]; then
if [[ $(which sha512sum) ]]; then
sha512sum $ASSET;
elif [[ $(which shasum) ]]; then
shasum -a 512 $ASSET;
fi;
fi