forked from personium/personium-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_cell.sh
executable file
·80 lines (68 loc) · 2.01 KB
/
create_cell.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
#!/bin/bash
CWD=$(dirname "$0")
ADMIN_FILE="${CWD}/unitadmin_account"
### Check jq
which jq >/dev/null
if [ "$?" -ne 0 ]; then
echo 'jq command is required. Please install jq.' >&2
exit 1
fi
### Check unitadmin_account
if [ ! -f "${ADMIN_FILE}" ]; then
echo 'unitadmin_account file does not exist. Please execute ./init.sh.' >&2
exit 1
fi
### Check arguments
if [ -z "$1" ]; then
echo 'Please specify cell_name. Usage: ./create_cell.sh <cell_name>' >&2
exit 1
fi
### Set variables
UNITADMIN_CELL_URL=$(sed -n 's|^unitadmin_cell_url=||p' ${ADMIN_FILE})
UNITADMIN_ACCOUNT=$(sed -n 's|^unitadmin_account=||p' ${ADMIN_FILE})
UNITADMIN_PASSWORD=$(sed -n 's|^unitadmin_password=||p' ${ADMIN_FILE})
ROOT_URL=http://3.137.157.204/
CELL_NAME="$1"
CELL_URL="${ROOT_URL}${CELL_NAME}/"
function check_error () {
RC="$1"
MSG="$2"
if [ "${RC}" -ne 0 ]; then
echo "${MSG}" >&2
exit 2
fi
}
### Get unit admin token
TOKEN=$(curl -f -s -X POST \
-H "Accept:application/json" \
-H "content-type:application/x-www-form-urlencoded" \
-d "grant_type=password" \
-d "username=${UNITADMIN_ACCOUNT}" \
-d "password=${UNITADMIN_PASSWORD}" \
-d "p_target=${ROOT_URL}" \
"${UNITADMIN_CELL_URL}__token" | jq -r '.access_token')
check_error $? 'Get unit admin token failed.'
### Create cell
curl -i -f -X POST \
-H "Authorization:Bearer ${TOKEN}" \
-H "Accept:application/json" \
-d '{"Name": "'${CELL_NAME}'"}' \
"${ROOT_URL}__ctl/Cell" >/dev/null
check_error $? 'Create cell failed.'
### Upload snapshot
curl -i -f -X PUT \
-H "Content-Type:application/x-zip-compressed" \
-H "Accept:application/json" \
-H "Authorization:Bearer ${TOKEN}" \
-T "${CWD}/template.zip" \
"${CELL_URL}__snapshot/template.zip"
check_error $? 'Upload snapshot failed.'
### 4. Import snapshot
curl -i -f -X POST \
-H "Accept:application/json" \
-H "Authorization:Bearer ${TOKEN}" \
-d '{"Name": "template"}' \
"${CELL_URL}__import"
check_error $? 'Import snapshot failed.'
echo "Creating cell succeeded!"
echo "Cell URL: ${CELL_URL}"