-
Notifications
You must be signed in to change notification settings - Fork 8
/
init.sh
executable file
·116 lines (96 loc) · 4.48 KB
/
init.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
#!/bin/bash -x
#
# personium
# Copyright 2014-2018 FUJITSU LIMITED
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Initial setup script of Unitadmin cell
# Environmental setting
DOMAIN=localhost
MASTER_TOKEN=personiumio
CELL_NAME=unitadmin
ROLE_UA_NAME=UnitAdmin
ROLE_CCA_NAME=CellContentsAdmin
UNITADMIN_ACCOUNT_FILE=unitadmin_account
PATH_BASED_CELL_URL=true
if [ "false" == "${PATH_BASED_CELL_URL}" ]; then
grep ${CELL_NAME}.${DOMAIN} /etc/hosts > /dev/null
if [ $? -eq 1 ]; then
echo 127.0.0.1 ${CELL_NAME}.${DOMAIN} >> /etc/hosts
fi
CELL_URL=http://${CELL_NAME}.${DOMAIN}/
else
CELL_URL=http://${DOMAIN}/${CELL_NAME}/
fi
CELL_OWNER=${CELL_URL}#unitadmin
# Account and password to create
UU_NAME=(unitadmin)
UU_PASS=()
function check_response() {
STATUS=${1}
OPERATION=${2}
RESPONSE_CODE=`echo "${CURL_RESULT}" | grep 'status:'`
if [ "${RESPONSE_CODE}" != "status:${STATUS}" ]; then
echo "${OPERATION} failed."
exit 2
fi
}
#Create Cell
echo "###### Create UNIT ADMIN Cell ######"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" http://$DOMAIN/__ctl/Cell -X POST -H "Authorization: Bearer $MASTER_TOKEN" -H "X-Personium-Unit-User: $CELL_OWNER" -d "{\"Name\":\"$CELL_NAME\"}" -k -i -s`
echo "$CURL_RESULT"
echo "-- Cell check"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" "http://$DOMAIN/__ctl/Cell(%27$CELL_NAME%27)" -X GET -H "Authorization: Bearer $MASTER_TOKEN" -k -i -s`
echo "$CURL_RESULT"
check_response 200 "UNIT ADMIN Cell created"
# Create account
echo "###### Create account ######"
UU_PASS+=(`perl -le 'print map { ("a".."z","A".."Z", 0..9)[rand 36] } 1..16'`)
echo " creating account ["${UU_NAME}"]"
echo " passwd ["${UU_PASS}"]"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" ${CELL_URL}__ctl/Account -X POST -H "Authorization: Bearer $MASTER_TOKEN" -H "X-Personium-Credential: ${UU_PASS}" -d "{\"Name\":\"${UU_NAME}\"}" -k -i -s`
echo "$CURL_RESULT"
check_response 201 "Account created"
echo " -- Account check"
echo "checking account["${UU_NAME}"]"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" ${CELL_URL}__token -X POST -d "grant_type=password&username=${UU_NAME}&password=${UU_PASS}" -k -i -s`
echo "$CURL_RESULT"
check_response 200 "Check Account Created"
# Create UnitAdmin role
echo "###### Create UnitAdmin role ######"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" ${CELL_URL}__ctl/Role -X POST -H "Authorization: Bearer $MASTER_TOKEN" -d "{\"Name\":\"$ROLE_UA_NAME\"}" -k -i -s`
echo "$CURL_RESULT"
check_response 201 "UnitAdmin Role created"
echo "-- UnitAdmin Role check"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" "${CELL_URL}__ctl/Role(%27$ROLE_UA_NAME%27)" -X GET -H "Authorization: Bearer $MASTER_TOKEN" -k -i -s`
echo "$CURL_RESULT"
check_response 200 "Check UnitAdmin Role Created"
# Create CellContentsAdmin role
echo "###### Create CellContentsAdmin role ######"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" ${CELL_URL}__ctl/Role -X POST -H "Authorization: Bearer $MASTER_TOKEN" -d "{\"Name\":\"$ROLE_CCA_NAME\"}" -k -i -s`
echo "$CURL_RESULT"
check_response 201 "CellContentsAdmin Role created"
echo "-- CellContentsAdmin Role check"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" "${CELL_URL}__ctl/Role(%27$ROLE_CCA_NAME%27)" -X GET -H "Authorization: Bearer $MASTER_TOKEN" -k -i -s`
echo "$CURL_RESULT"
check_response 200 "Check CellContentsAdmin Role Created"
# Link unitadmin - CellContentsAdmin
echo "###### Link unitadmin - CellContentsAdmin ######"
CURL_RESULT=`curl -w "\nstatus:%{http_code}\n" "${CELL_URL}__ctl/Role(%27$ROLE_CCA_NAME%27)/\\$links/_Account" -X POST -H "Authorization: Bearer $MASTER_TOKEN" -d "{\"uri\":\"${CELL_URL}__ctl/Account('${UU_NAME}')\"}" -k -i -s`
echo "$CURL_RESULT"
check_response 204 "Link unitadmin - CellContentsAdmin"
# Upon confirming the password without trouble, drop the created information into a file
echo "unitadmin_cell_url=${CELL_URL}" > ${UNITADMIN_ACCOUNT_FILE}
echo "unitadmin_account=${UU_NAME}" >> ${UNITADMIN_ACCOUNT_FILE}
echo "unitadmin_password=${UU_PASS}" >> ${UNITADMIN_ACCOUNT_FILE}