-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.sh
192 lines (149 loc) · 4.1 KB
/
builder.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
#!/bin/bash
function program_is_installed {
# set to 1 initially
local return_=1
# set to 0 if not found
type $1 >/dev/null 2>&1 || { local return_=0; }
# return value
echo "$return_"
}
# check if jq is intalled for json read
if [ $(program_is_installed jq ) == 0 ]
then
sudo apt-get install jq
fi
NAME=`cat gen_deb.json | jq -r '."name"'`
VERSION=`cat gen_deb.json | jq -r '."version"'`
DESCRIPTION=`cat gen_deb.json | jq -r '."description"'`
NODE_VERSION=`cat gen_deb.json | jq -r '."node_version"'`
MAIN=`cat gen_deb.json | jq -r '."main"'`
# generate folder structure
mkdir -p debinstall/deb-src/{DEBIAN,sysroot/{etc/init,usr/share/doc/$NAME}}
DEBIAN_DIR=debinstall/deb-src/DEBIAN/
# generate necessary files
# change directory for debian
cd $DEBIAN_DIR
# Control file
cat <<CONTROL > control
Package: $NAME
Version: $VERSION
Section: base
Priority: optional
Architecture: amd64
Installed-Size: SIZE
Depends:
Maintainer: Ginga One Devops <[email protected]>
Description: $DESCRIPTION
CONTROL
# prerm file
cat <<PRERM > prerm
#!/bin/bash
# Stop application before installing
#pm2 stop $NAME
PRERM
cat <<PREINST > preinst
#!/bin/bash
# AddUser
if id ginga >/dev/null 2>&1; then
echo "User ginga already created"
else
sudo useradd ginga -s /sbin/nologin #&& su -s /bin/bash ginga
sudo mkdir -p /home/ginga
sudo chmod -R 0755 /home/ginga
sudo chown -R ginga:ginga /home/ginga
sudo mkdir -p /opt/gingasystems/apps/$NAME
sudo chown -R ginga:ginga /opt/gingasystems/apps/$NAME
sudo echo "ginga ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/ginga
echo "User ginga created"
fi
sudo -H -u ginga /bin/bash -c '
# Donwload, install, and activate nvm
if ! type nvm > /dev/null; then
#su -s /bin/bash ginga
# donwload and install nvm
echo "Installing NVM for \$USER"
#curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
cat <<EOTBASH >> /home/ginga/.bashrc
export NVM_DIR="/home/ginga/.nvm"
[ -s "/home/ginga/.nvm/nvm.sh" ] && . "/home/ginga/.nvm/nvm.sh"
EOTBASH
source /home/ginga/.bashrc
echo "Installing Node for \$USER"
# Installing specific node version for project
nvm install $NODE_VERSION
fi
# Install PM2
if ! type pm2 > /dev/null;
#echo "Pm2 Installed"
then
echo "Installing pm2"
npm install pm2 -g
fi
'
PREINST
cat <<POSTINST > postinst
#!/bin/bash
# Stop application before installing
sudo -H -u ginga /bin/bash -c '
source /home/ginga/.bashrc
echo "Change directory to /opt/gingasystems/apps/$NAME"
cd /opt/gingasystems/apps/$NAME
echo "Current Directory"
pwd
npm install
pm2 start $MAIN --name="$NAME" -i 4
'
POSTINST
# Setting right permission for control files
chmod -R 755 ../DEBIAN/
cd ../sysroot/etc/init/
cat <<CONF > $NAME.conf
# System Service $NAME
description "System Service $NAME"
author "Ginga One Devops"
start on filesystem
stop on runlevel [06]
console output
respawn
script
PATH="/opt/gingasystems/apps/$NAME/:/usr/local/bin:/usr/bin:\$PATH"
#pm2 start $MAIN --name="$NAME" -i 4
pm2 restart /opt/gingasystems/apps/$NAME/server/index.js
end script
CONF
cd ../../../../../
# Generate .deb
set -u
set -e
SRC=/tmp/$NAME-src
DIST=/tmp/$NAME-dist
SYSROOT=$SRC/sysroot
DEBIAN=$SRC/DEBIAN
rm -rf $DIST
mkdir -p $DIST/
rm -rf $SRC
mkdir -p $SRC/
rsync -a debinstall/deb-src/ $SRC/
mkdir -p $SYSROOT/opt/gingasystems/apps/$NAME
rsync -a $NAME/ $SYSROOT/opt/gingasystems/apps/$NAME --delete
find $SRC/ -type d -exec chmod 0755 {} \;
find $SRC/ -type f -exec chmod go-w {} \;
chown -R root:root $SRC/
#SIZE=`du -s $SYSROOT | sed s'/[0-9]+//'` # s'/\s\+.*//'`
SIZE=`du -s $SYSROOT | grep -o -E '[0-9]+' | head -1 | sed -e 's/^0\+//'`
pushd $SYSROOT/
tar czf $DIST/data.tar.gz [a-z]*
popd
sed -i -e "s/SIZE/$SIZE/g" $DEBIAN/control
pushd $DEBIAN
tar czf $DIST/control.tar.gz *
popd
pushd $DIST/
echo 2.0 > ./debian-binary
find $DIST/ -type d -exec chmod 0755 {} \;
find $DIST/ -type f -exec chmod go-w {} \;
chown -R root:root $DIST/
ar r $DIST/$NAME.deb debian-binary control.tar.gz data.tar.gz
popd
rsync -a $DIST/$NAME.deb ./