-
Notifications
You must be signed in to change notification settings - Fork 45
/
setup.sh
executable file
·163 lines (134 loc) · 3.75 KB
/
setup.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
#!/bin/bash
set -eE -o functrace
failure() {
local lineno=$1
local msg=$2
echo "Failed at $lineno: $msg"
}
trap 'failure ${LINENO} "$BASH_COMMAND"' ERR
set -o pipefail
ENV_VALIDATOR=("develop" "qa" "production")
FILE_ENV_CONF="./env.cfg"
FILE_ENV="./.env"
SOURCEPATH="./source"
parser(){
options=$(getopt -l "help,env:,appname:,domain:,lumen-version:,ssh-setup" -o "h e: a: d: v:" -a -- "$@")
eval set -- "$options"
while true
do
case $1 in
-h|--help)
showHelp
;;
-e|--env)
ENV=$2
;;
-a|--appname)
APPNAME=$2
;;
-d|--domain)
DOMAIN=$2
;;
-v|--lumen-version)
LUMEN_VERSION="${2}"
;;
--ssh-setup)
set_connection_from_local_to_prod
exit 0
;;
--)
shift
break;;
esac
shift
done
shift "$(($OPTIND -1))"
if [ -f "$FILE_ENV_CONF" ] ; then
echo "[INFO] Env setup: env.cfg -> .env"
cp $FILE_ENV_CONF $FILE_ENV
exit 0
fi
if [ -z $ENV ] ; then
read -p 'Env, digit one of develop,qa,production: ' ENV
fi
[[ ${ENV_VALIDATOR[@]} =~ (^|[[:space:]])$ENV($|[[:space:]]) ]] || (echo "ENV not valid" && exit 1)
if [ -z $APPNAME ] ; then
read -p 'App name: ' APPNAME
fi
[[ "$APPNAME" != "" ]] || (echo 'APPNAME not valid' && exit 1)
if [ -z $DOMAIN ] ; then
DOMAIN="localhost"
fi
if [ -z $LUMEN_VERSION ] ; then
read -p 'Lumen version (default master): ' LUMEN_VERSION
if [[ "$LUMEN_VERSION" == "" ]] ; then
LUMEN_VERSION="master"
fi
fi
}
update_env() {
local _file=${1}
local _key=${2}
local _value=${3}
echo "${_key}=${_value}" >> ${_file}
}
set_connection_from_local_to_prod() {
while true; do
read -p "Set the configurations for accessing the prod machine? (y,n) " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
printf "\n#SSH CONNECTION\n" >> $FILE_ENV
read -p 'SSH key (enter to skip): ' SSHKEY
if [ -f ${SSHKEY} ] ; then
update_env $FILE_ENV "SSHKEY" $SSHKEY
else
echo "${SSHKEY} not exist"
exit 1
fi
read -p 'USER SERVER PROD (root or sudo user): ' SRV_USER
update_env $FILE_ENV "SRV_USER" $SRV_USER
read -p 'USER SERVER PROD WORKDIR: ' SRV_USER_WK
if [ "${SRV_USER_WK}" == "" ] ; then
SRV_USER_WK=$SRV_USER
fi
update_env $FILE_ENV "SRV_USER_WK" $SRV_USER_WK
read -p 'IP SERVER PROD: ' SRV_HOST
update_env $FILE_ENV "SRV_HOST" $SRV_HOST
}
download_wp(){
local _tag=${LUMEN_VERSION}
if [[ "${_tag}" != "master" ]] ; then
_tag="v${_tag}"
fi
curl -LkSs https://github.com/laravel/lumen/archive/${_tag}.zip -o lumen.zip
unzip lumen.zip
rm lumen.zip
mv lumen-${LUMEN_VERSION} ${SOURCEPATH}
}
main() {
parser $@
echo "[INFO] setup env"
touch $FILE_ENV_CONF
update_env $FILE_ENV_CONF "ENV" "$ENV"
update_env $FILE_ENV_CONF "APPNAME" "$APPNAME"
update_env $FILE_ENV_CONF "DOMAIN" "$DOMAIN"
update_env $FILE_ENV_CONF "DOCKERFILE_PATH" "."
update_env $FILE_ENV_CONF "WORKDIR_USER" "www-data"
update_env $FILE_ENV_CONF "WORKDIR_GROUP" "www-data"
update_env $FILE_ENV_CONF "WORKDIRPATH" "/var/www/${APPNAME}"
update_env $FILE_ENV_CONF "SOURCEPATH" ${SOURCEPATH}
update_env $FILE_ENV_CONF "CONTAINERPATH" "./container"
update_env $FILE_ENV_CONF "VOLUMESPATH" "./container/data"
update_env $FILE_ENV_CONF "IMAGENAME" "microservice/${APPNAME}"
cp $FILE_ENV_CONF $FILE_ENV
if [ "$ENV" == "develop" ] ; then
download_wp
set_connection_from_local_to_prod
fi
exit 0
}
main $@