-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.sh
192 lines (157 loc) · 4.39 KB
/
package.sh
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
#!/bin/bash
##
# Helper script to generate a .deb package
# To be completed
#
# Execution Flow
# - Generate folder structure
# - Generate a CONTROL file
# - Generate a systemd .service file
# - Generate preinst script
# - Generate postinst script
# - Generate prerm script
# - Generate postrm script
# - Generate .deb package
##
if [ "$#" -lt 1 ]; then
echo "No arguments provided"
exit 1
elif [ "$#" -gt 1 ]; then
echo "Too much arguments"
exit 1
fi
VERSION="$1"
echo "Generating ethanol package for version: "$VERSION""
# get current dir
ETHANOLDIR=$(pwd)
# setup variables
BUILDDIR="$ETHANOLDIR"/build/ethanol
RELEASEDIR="$ETHANOLDIR"/build/release
DEBIANDIR=/DEBIAN
BINDIR=/opt/ethanol/bin
SYSTEMDFOLDER=/etc/systemd/system
MANPAGESDIR=/usr/share/man
CONFIGDIR=/etc/ethanol/
PLUGINSDIR="$CONFIGDIR"/search_providers
LIBSDIR=/usr/local/lib
WORKINGDIR=/opt/ethanol
# cleanup working directory
rm -rf "$BUILDDIR"
# create directory structure
mkdir "$BUILDDIR"
mkdir -p "$RELEASEDIR"
mkdir -p "$BUILDDIR""$DEBIANDIR"
mkdir -p "$BUILDDIR""$BINDIR"
mkdir -p "$BUILDDIR""$SYSTEMDFOLDER"
mkdir -p "$BUILDDIR""$MANPAGESDIR"
mkdir -p "$BUILDDIR""$CONFIGDIR"
mkdir -p "$BUILDDIR""$LIBSDIR"
mkdir -p "$BUILDDIR""$PLUGINSDIR"
mkdir -p "$BUILDDIR""$WORKINGDIR"
# copy files
cp ./ethanol "$BUILDDIR""$PROJECTBINDIR"
cp ./config.template.yml "$BUILDDIR""$CONFIGDIR"
cp ./config.template.json "$BUILDDIR""$CONFIGDIR"
cp -r ./search_providers/* "$BUILDDIR""$PLUGINSDIR"
# generate control file
echo 'Package: Ethanol
Version: 0.1
Section: MetaSearch Engines
Priority: optional
Architecture: all
Homepage: https://github.com/areYouLazy/ethanol
Maintainer: areYouLazy
Description: Ethanol is a search aggregator,
it is capable of querying multiple applications and return aggregate results.
It is like a MetaSearch Engine but for applications like Jira, MediaWiki,
OTRS, Check_MK and other enterprise applications
' > "$BUILDDIR""$DEBIANDIR"/control
# generate systemd service file
echo '[Unit]
Description=Ethanol
After=network.target
[Service]
Type=simple
User=ethanol
WorkingDirectory=/opt/ethanol
ExecStart=/opt/ethanol/bin/ethanol
Restart=on-failure
[Install]
WantedBy=multi-user.target
' > "$BUILDDIR""$SYSTEMDFOLDER"/ethanol.service
# generate preinst script
echo '#!/bin/bash
if [ "$1" = install ]
then
echo "Setting up ethanol group and user"
# creating ethanol group if he isn t already there
if ! getent group ethanol >/dev/null; then
# Adding system group: ethanol.
addgroup --system ethanol >/dev/null
fi
# creating ethanol user if he isn t already there
if ! getent passwd ethanol >/dev/null; then
# Adding system user: ethanol.
adduser \
--system \
--disabled-login \
--ingroup ethanol \
--no-create-home \
--home /nonexistent \
--gecos "Ethanol Server" \
--shell /bin/false \
ethanol >/dev/null
fi
fi
' > "$BUILDDIR""$DEBIANDIR"/preinst
# generate postinst script
echo '#!/bin/bash
if [ "$1" = configure ]; then
echo "Take ownership of directory..."
chown -R ethanol:ethanol /opt/ethanol
echo "Symlink binary to path..."
ln -s /opt/ethanol/bin/ethanol /usr/local/bin
echo "Reload systemd..."
systemctl daemon-reload
echo "Enable ethanol.service..."
systemctl enable ethanol.service
echo "Start ethanol.service..."
systemctl start ethanol.service
fi
' > "$BUILDDIR""$DEBIANDIR"/postinst
# generate prerm script
echo '#!/bin/bash
if [ -e "/etc/systemd/system/ethanol.service" ]; then
echo "Stopping ethanol.service..."
systemctl stop ethanol.service
echo "Disabling ethanol.service..."
systemctl disable ethanol.service
fi
if pgrep ethanol >/dev/null
then
echo "Stopping ethanol..."
killall ethanol
fi
' > "$BUILDDIR""$DEBIANDIR"/prerm
# generate postrm script
echo '#!/bin/bash
if [ $1 = remove ]; then
echo "Removing ethanol user..."
userdel ethanol -r -f
echo "Deleting /opt/ethanol..."
rm -rf /opt/ethanol
echo "Removing symlinks..."
unlink /usr/local/bin/ethanol
echo "Removing cfg files and service..."
rm /etc/systemd/system/ethanol.service
rm -rf /etc/ethanol
fi
' > "$BUILDDIR""$DEBIANDIR"/postrm
chmod 0755 "$BUILDDIR""$DEBIANDIR"/preinst
chmod 0755 "$BUILDDIR""$DEBIANDIR"/postinst
chmod 0755 "$BUILDDIR""$DEBIANDIR"/prerm
chmod 0755 "$BUILDDIR""$DEBIANDIR"/postrm
# Build
dpkg -b "$BUILDDIR" "$RELEASEDIR"/ethanol_0.0.1_amd64.deb
echo ""
dpkg -I "$RELEASEDIR"/ethanol_0.0.1_amd64.deb