-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.sh
executable file
·200 lines (174 loc) · 5.24 KB
/
config.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
193
194
195
196
197
198
199
200
#!/bin/bash
#------------------------------------------------------------
# Script: config.sh
# Author: Swift@IBM
# -----------------------------------------------------------
VERSION="1.0"
BUILD_DIR=".build-linux"
BRIDGE_APP_NAME="containerbridge"
DATABASE_NAME="TodoListCloudantDatabase"
REGISTRY_URL="registry.ng.bluemix.net"
DATABASE_TYPE="cloudantNoSQLDB"
DATABASE_LEVEL="Lite"
function help {
cat <<-!!EOF
Usage: $CMD [ build | run | push-docker ] [arguments...]
Where:
install-tools Installs necessary tools for config, like Cloud Foundry CLI
login Logs into IBM Cloud and Container APIs
build <imageName> Builds Docker container from Dockerfile
run <imageName> Runs Docker container, ensuring it was built properly
stop <imageName> Stops Docker container, if running
push-docker <imageName> Tags and pushes Docker container to IBM Cloud
create-bridge Creates empty bridge application
create-db Creates database service and binds to bridge
deploy <imageName> Binds everything together (app, db, container) through container group
populate-db <imageName> Populates database with initial data
delete <imageName> Delete the group container and deletes created service if possible
all <imageName> Combines all necessary commands to deploy an app to IBM Cloud in a Docker container.
!!EOF
}
install-tools () {
brew tap cloudfoundry/tap
brew install cf-cli
cf install-plugin https://static-ice.ng.bluemix.net/ibm-containers-mac
}
login () {
echo "Setting api and login tools."
cf api https://api.ng.bluemix.net
cf login
cf ic login
}
buildDocker () {
if [ -z "$1" ]
then
echo "Error: build failed, docker name not provided."
return
fi
docker build -t $1 --force-rm .
}
runDocker () {
if [ -z "$1" ]
then
echo "Error: run failed, docker name not provided."
return
fi
docker run --name $1 -d -p 8080:8080 $1
}
stopDocker () {
if [ -z "$1" ]
then
echo "Error: clean failed, docker name not provided."
return
fi
docker rm -fv $1 || true
}
pushDocker () {
if [ -z "$1" ] || [ -z $REGISTRY_URL ]
then
echo "Error: Pushing Docker container to IBM Cloud failed, missing variables."
return
fi
echo "Tagging and pushing docker container..."
namespace=$(cf ic namespace get)
docker tag $1 $REGISTRY_URL/$namespace/$1
docker push $REGISTRY_URL/$namespace/$1
}
createBridge () {
if [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Creating bridge application failed, missing BRIDGE_APP_NAME."
return
fi
mkdir $BRIDGE_APP_NAME
cd $BRIDGE_APP_NAME
touch empty.txt
cf push $BRIDGE_APP_NAME -p . -i 1 -d mybluemix.net -k 1M -m 64M --no-hostname --no-manifest --no-route --no-start
rm empty.txt
cd ..
rm -rf $BRIDGE_APP_NAME
}
createDatabase () {
if [ -z $DATABASE_TYPE ] || [ -z $DATABASE_LEVEL ] || [ -z $DATABASE_NAME ] || [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Creating bridge application failed, missing variables."
return
fi
cf create-service $DATABASE_TYPE $DATABASE_LEVEL $DATABASE_NAME
cf bind-service $BRIDGE_APP_NAME $DATABASE_NAME
cf restage $BRIDGE_APP_NAME
}
deployContainer () {
if [ -z "$1" ] || [ -z $REGISTRY_URL ] || [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Could not deploy container to IBM Cloud, missing variables."
return
fi
namespace=$(cf ic namespace get)
hostname=$1"-app"
cf ic group create \
--anti \
--auto \
-m 128 \
--name $1 \
-p 8080 \
-n $hostname \
-e "CCS_BIND_APP="$BRIDGE_APP_NAME \
-d mybluemix.net $REGISTRY_URL/$namespace/$1
}
populateDB () {
if [ -z "$1" ]
then
echo "Error: Could not populate db with sample data, missing imageName."
return
fi
appURL="https://"$1"-app.mybluemix.net"
eval $(curl -X POST -H "Content-Type: application/json" -d '{ "title": "Wash the car", "order": 0, "completed": false }' $appURL)
eval $(curl -X POST -H "Content-Type: application/json" -d '{ "title": "Walk the dog", "order": 2, "completed": true }' $appURL)
eval $(curl -X POST -H "Content-Type: application/json" -d '{ "title": "Clean the gutters", "order": 1, "completed": false }' $appURL)
}
delete () {
if [ -z "$1" ] || [ -z $DATABASE_NAME ] || [ -z $BRIDGE_APP_NAME ]
then
echo "Error: Could not delete container group and service, missing variables."
return
fi
cf ic group rm $1
cf unbind-service $BRIDGE_APP_NAME $DATABASE_NAME
cf delete-service $DATABASE_NAME
}
all () {
if [ -z "$1" ]
then
echo "Error: Could not complete entire deployment process, missing variables."
return
fi
login
buildDocker $1
pushDocker $1
createBridge
createDatabase
deployContainer $1
}
#----------------------------------------------------------
# MAIN
# ---------------------------------------------------------
ACTION="$1"
[[ -z $ACTION ]] && help && exit 0
# Initialize the SwiftEnv project environment
eval "$(swiftenv init -)"
case $ACTION in
"install-tools") install-tools;;
"login") login;;
"build") buildDocker "$2";;
"run") runDocker "$2";;
"stop") stopDocker "$2";;
"push-docker") pushDocker "$2";;
"create-bridge") createBridge;;
"create-db") createDatabase;;
"deploy") deployContainer "$2";;
"populate-db") populateDB "$2";;
"delete") delete "$2";;
"all") all "$2";;
*) help;;
esac