-
Notifications
You must be signed in to change notification settings - Fork 0
/
semcon
executable file
·237 lines (215 loc) · 6.49 KB
/
semcon
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
# resources
# - https://sap1ens.com/blog/2017/07/01/bash-scripting-best-practices/
set -o errexit
set -o pipefail
# helper functions ================
version_info()
{
echo "semcon, version 0.1"
echo ""
}
usage_info()
{
echo "Usage: $0"
echo -e "\t--help print this help message"
echo -e "\t--version version information"
echo -e "\t-v|--verbose print additional information"
echo ""
echo -e "\tstart <file.yaml> start a Semantic Container with configuration in"
echo -e "\t file.yaml"
echo -e "\tset <container> <config.yaml>"
echo -e "\t define a container as specified in <config.yaml>"
echo -e "\t<container> perm create <account> <scope>"
echo -e "\t create account with given scope"
echo -e "\t<container> perm show <account>"
echo -e "\t show account information"
echo -e "\t<container> perm setenv <account> <name>"
echo -e "\t set environment variable"
echo -e "\t<container> write [<account|token>]"
echo -e "\t read data from <stdin> and write as account"
echo -e "\t<container> read [<account|token>]"
echo -e "\t read data from <container> and write to <stdout>"
echo ""
}
extract_yaml()
{
eval "$3=$(grep "$2" $1 | head -n1 | cut -d ':' -f 2,3 | tr -d "[:space:]")"
}
invalid_syntax()
{
echo "invalid option or syntax: ${args}"
usage_info
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
return 1
else
exit 1
fi
}
# read from & write into container =================
get_port()
{
eval "$2=$(docker port hse | cut -d ":" -f 2)"
}
write_data()
{
if $VERBOSE ; then
echo "writing data into container"
fi
return_value='';
get_port $1 return_value
SC_PORT=$return_value
command="cat - | curl -X POST -d @- -H 'Content-Type: application/json' http://localhost:${SC_PORT}/api/data"
if $VERBOSE ; then
echo "${command}"
fi
eval $command
}
read_data()
{
if $VERBOSE ; then
echo "reading data from container"
fi
return_value='';
get_port $1 return_value
SC_PORT=$return_value
command="curl http://localhost:${SC_PORT}/api/data"
if $VERBOSE ; then
echo "${command}"
fi
eval $command
}
# start container =================
start_container()
{
if $VERBOSE ; then
echo "starting container"
fi
return_value='';
extract_yaml "./${SC_CONFIG_FILE}" "image" return_value
IMAGE=$return_value
return_value='';
extract_yaml "./${SC_CONFIG_FILE}" "port" return_value
PORT=$return_value
return_value='';
extract_yaml "./${SC_CONFIG_FILE}" "name" return_value
CONTAINER_NAME=$return_value
CONFIG_PATH=$(dirname "$PWD/${SC_CONFIG_FILE}")
CONFIG_FILE=$(basename "./${SC_CONFIG_FILE}")
command="docker rm -f ${CONTAINER_NAME};"
command="${command} docker run -d"
command="${command} --name ${CONTAINER_NAME}"
command="${command} -p ${PORT}:3000"
command="${command} -v ${CONFIG_PATH}:/config"
command="${command} -e CONFIG_FILE=${CONFIG_FILE}"
command="${command} -e IMAGE_SHA256=\"\$(docker image ls --no-trunc -q ${IMAGE} | cut -c8-)\""
command="${command} -e IMAGE_NAME=${IMAGE}"
command="${command} ${IMAGE}"
if $VERBOSE ; then
echo "${command}"
fi
eval $command
}
# option handling =================
args=("$@")
VERBOSE=false
if [[ $# -eq 0 ]]
then
usage_info
exit 0
fi
while [ $# -gt 0 ]; do
case "$1" in
--version)
version_info
exit 0
;;
--help)
version_info
usage_info
exit 0
;;
--verbose|-v)
VERBOSE=true
;;
start)
SC_MODE=start
shift
if [[ $# -eq 1 ]]
then
SC_CONFIG_FILE=$1
start_container
exit 0
else
invalid_syntax
fi
;;
*)
SC_CONTAINER_NAME=$1
if [ "$(docker ps -q -f name=${SC_CONTAINER_NAME})" ];
then
shift
case "$1" in
perm)
SC_MODE=perm
shift
case "$1" in
show)
shift
if [[ $# -eq 1 ]]
then
SC_ACCOUNT=$1
show_perm
exit 0
else
invalid_syntax
fi
;;
create)
shift
if [[ $# -eq 2 ]]
then
SC_ACCOUNT=$1
SC_ACCOUNT_SCOPE=$2
create_perm
exit 0
else
invalid_syntax
fi
;;
setenv)
shift
if [[ $# -eq 2 ]]
then
SC_ACCOUNT=$1
SC_ENV=$2
set_permenv
exit 0
else
invalid_syntax
fi
;;
*)
invalid_syntax
;;
esac
;;
write)
SC_MODE=write
write_data $SC_CONTAINER_NAME
;;
read)
SC_MODE=read
read_data $SC_CONTAINER_NAME
;;
*)
invalid_syntax
;;
esac
else
invalid_syntax
fi
;;
esac
shift
done