-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·149 lines (124 loc) · 3.87 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
#!/bin/bash -e
if [ -z "$GIT_USER" ]; then
echo "Error: GIT_USER was not initialized."
exit 1
fi
if [ -z "$GIT_USER_EMAIL" ]; then
echo "Error: GIT_USER_EMAIL was not initialized."
exit 1
fi
if [ -z "$GIT_OWNER" ]; then
echo "Error: GIT_OWNER was not initialized."
exit 1
fi
if [ -z "$GIT_REPO" ]; then
echo "Error: GIT_REPO was not initialized."
exit 1
fi
if [ -z "$APP_NAME" ]; then
echo "Error: APP_NAME was not initialized."
exit 1
fi
if [ -z "$SCM_TYPE" ]; then
echo "Error: SCM_TYPE was not initialized."
exit 1
elif [ "$SCM_TYPE" = "github" ]; then
if [ -z "$GITHUB_USERNAME" ]; then
echo "Error: GITHUB_USERNAME was not initialized."
exit 1
fi
if [ -z "$GITHUB_TOKEN" ]; then
echo "Error: GITHUB_TOKEN was not initialized."
exit 1
fi
export GIT_USERNAME="${GIT_USERNAME:-$GITHUB_TOKEN}"
export GIT_PASSWORD="${GIT_PASSWORD:-}"
export GIT_REPO_URL="https://github.com/$GIT_OWNER/$GIT_REPO.git"
else
if [ -z "$GIT_REPO_URL" ]; then
echo "Error: GIT_REPO_URL was not initialized."
exit 1
fi
if [ -z "$GIT_USERNAME" ]; then
echo "Error: GIT_USERNAME was not initialized."
exit 1
fi
if [ -z "$GIT_PASSWORD" ]; then
echo "Error: GIT_PASSWORD was not initialized."
exit 1
fi
fi
APP_PORT="${APP_PORT:-8080}"
PYTHON_MAJOR_VERSION="${PYTHON_MAJOR_VERSION:-2}"
get_abs_filename() {
# $1 : relative filename
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}
function git_setup {
git config --global user.name "$GIT_USER"
git config --global user.email "$GIT_USER_EMAIL"
git config --global credential.helper cache
git config --global credential.$(dirname $(dirname $GIT_REPO_URL)).username "$GIT_USERNAME"
git config --global core.askpass "$(get_abs_filename gitpass.sh)"
}
function hook_setup {
if [ "${SCM_TYPE}" = "github" ]; then
if [ ! -z "${ORCHESTRATOR_HOOK_URL}" ]; then
curl -u "$GITHUB_USERNAME:$GITHUB_TOKEN" -XPOST -d \
"{\"name\": \"web\", \"active\": \"true\", \"events\": [\"push\", \"create\", \"delete\"], \
\"config\": {\"content_type\": \"json\", \"url\": \"$ORCHESTRATOR_HOOK_URL\",\"secret\": \"$HOOK_SECRET\"} }" \
https://api.github.com/repos/$GIT_OWNER/$GIT_REPO/hooks
fi
if [ ! -z "${IMAGE_FACTORY_HOOK_URL}" ]; then
curl -u "$GITHUB_USERNAME:$GITHUB_TOKEN" -XPOST -d \
"{\"name\": \"web\", \"active\": \"true\", \"events\": [\"push\"], \
\"config\": {\"content_type\": \"json\", \"url\": \"$IMAGE_FACTORY_HOOK_URL\",\"secret\": \"$HOOK_SECRET\"} }" \
https://api.github.com/repos/$GIT_OWNER/$GIT_REPO/hooks
fi
fi
}
function init_app {
cp -r ../skeleton/* .
if [ "$APP_NAME" != "myapp" ]; then
mv myapp $APP_NAME
grep -rl 'myapp' ./ | xargs sed -i "s/myapp/$APP_NAME/g"
fi
if [ "$APP_PORT" != "8080" ]; then
sed -i "s/EXPOSE 8080/EXPOSE $APP_PORT/g" Dockerfile
sed -i "s/port=8080/port=$APP_PORT/g" server.py
sed -i "s/8080/$APP_PORT/g" totem.yml
fi
if [ "$PYTHON_MAJOR_VERSION" != "2" ]; then
sed -i \
-e "s/2.7-trusty-.*/3.4-trusty-b4/g" \
-e "s/pip /pip3 /g" Dockerfile
fi
}
function git_init {
rm -rf $GIT_REPO
git clone "$GIT_REPO_URL"
cd $GIT_REPO
if git branch | grep 'master'; then
git checkout master
else
git checkout -b master
fi
}
function git_push {
echo "git push"
git add . -A
git commit . -m 'Totem Seed application for python'
git push origin master
if git branch | grep 'develop'; then
git checkout develop
git merge master
else
git checkout -b develop
fi
git push origin develop
}
git_setup
hook_setup
git_init
init_app
git_push