-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.sh
executable file
·171 lines (143 loc) · 3.62 KB
/
util.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
#!/bin/bash
# add your USER to 'group' array
# group[USER]="username"
# run 'echo ${USER}' to find your 'USER'
# use your ssh remote username as 'username'
declare -A group
group[gabriele]="gabriele.fulgaro"
group[mattia]="mattia.polverini"
# add your remote host
# hosts[USER]="host"
# use different host for performance
# http://www.informatica.unibo.it/it/servizi-informatici/accesso-da-remoto
declare -A hosts
hosts[gabriele]="hansel"
hosts[mattia]="marullo"
# don't touch from here
cd ${PWD}
username=${group[${USER}]}
site=${username}
hostname=@${hosts[${USER}]}.cs.unibo.it
login=${username}${hostname}
frontname=`grep -Po '"name": "\K[^"]+' package.json`
docname=documentation
backname=server
webdir=/home/web/${site}/html
dist=${PWD}/dist
frontdir=${dist}/${frontname}
docdir=${dist}/${docname}
backdir=${dist}/${backname}
sshctl=~/.ssh/ctl
sshopts="-C -o ControlPath=${sshctl}/%L-%r@%h:%p"
function help() {
echo "Upload to a webspace front-end and/or back-end."
echo ""
echo "USAGE: util.sh OPTIONS [EXTRA]"
echo "OPTIONS:"
echo -e "\t -h \t show this"
echo -e "\t -f \t front-end ('ng build' if not exist)"
echo -e "\t -b \t back-end (make a symlink in dist if not exist)"
echo "EXTRA:"
echo -e "\t -c \t clean folder/folders on webspace"
echo -e "\t -s \t site1826 (by default is your personal webspace)"
echo -e "\t -k \t keep connection open (don't ask for password the next time)"
echo ""
}
function error() {
help
echo "ERROR: ${BASH_LINENO}: $1"
exit ${LINENO};
}
if [[ "${username}" == "" ]]; then
error "you need to add ${USER} to 'group'!"
fi
if [[ "${hostname}" == "@.cs.unibo.it" ]]; then
error "you need to add ${USER} to 'hosts'!"
fi
while getopts ":hfbcsk" opt; do
case $opt in
h)
help
exit 0
;;
f)
front=1
;;
b)
back=1
;;
c)
clean=1
;;
s)
site=site1826
webdir=/home/web/${site}/html
;;
k)
keep=1
;;
\?)
error "Invalid option: -${OPTARG}"
;;
# ~ :)
# ~ echo "Option -${OPTARG} requires an argument."
# ~ exit 1
# ~ ;;
# ~ *)
# ~ echo "${opt} was triggered, Parameter: ${OPTARG}"
# ~ ;;
esac
done
if [[ ${OPTIND} -eq 1 ]]; then
error "No options were passed!"
elif [[ "${front}" == "" ]] && [[ "${back}" == "" ]]; then
error "few arguments"
fi
if [[ ${front} == 1 ]] && [ ! -d ${frontdir} ]; then
${PWD}/node_modules/@angular/cli/bin/ng build
fi
if [[ ${front} == 1 ]] && [ ! -d ${docdir} ]; then
ln -s ${PWD}/${docname} ${dist}
fi
if [[ ${back} == 1 ]] && [ ! -d ${backdir} ]; then
ln -s ${PWD}/${backname} ${dist}
fi
if [ ! -d ${sshctl} ]; then
mkdir -p ${sshctl}
fi
ssh -q -O check ${sshopts} ${login}
if [[ ${?} != 0 ]]; then
ssh -nNf -o ControlMaster=yes ${sshopts} ${login}
fi
if [[ ${clean} == 1 ]]; then
if [[ ${front} == 1 ]]; then
ssh ${sshopts} ${login} "rm -rf ${webdir}/${frontname}"
ssh ${sshopts} ${login} "rm -rf ${webdir}/${docname}"
fi
if [[ ${back} == 1 ]]; then
ssh ${sshopts} ${login} "rm -rf ${webdir}/${backname}"
fi
fi
if [[ ${front} == 1 ]]; then
echo ""
echo "FRONT-END:"
chmod -R g+w ${frontdir}
scp ${sshopts} -p -r ${frontdir} ${login}:${webdir}/${frontname}
echo ""
echo "DOCS:"
ssh ${sshopts} ${login} "mkdir -p ${webdir}/${docname}"
chmod -R g+w ${docdir}
scp ${sshopts} -p -r ${docdir} ${login}:${webdir}/${docname}/docs
fi
if [[ ${back} == 1 ]]; then
echo ""
echo "BACK-END:"
chmod -R g+w ${backdir}
scp ${sshopts} -p -r ${backdir} ${login}:${webdir}/${backname}
echo ""
echo "WARNING: You have to run 'restart ${site}' on gocker."
fi
if [[ ${keep} != 1 ]]; then
echo ""
ssh -O stop ${sshopts} ${login}
fi