Update stor4nfv install scripts according to opensds aruba release
[stor4nfv.git] / ci / ansible / script / keystone.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2018 Huawei Technologies Co., Ltd. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # 'stack' user is just for install keystone through devstack
18
19 create_user(){
20     if id "${STACK_USER_NAME}" &> /dev/null; then
21         return
22     fi
23     sudo useradd -s /bin/bash -d "${STACK_HOME}" -m "${STACK_USER_NAME}"
24     echo "stack ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/stack
25 }
26
27
28 remove_user(){
29     userdel "${STACK_USER_NAME}" -f -r
30     rm /etc/sudoers.d/stack
31 }
32
33 devstack_local_conf(){
34 DEV_STACK_LOCAL_CONF=${DEV_STACK_DIR}/local.conf
35 cat > "$DEV_STACK_LOCAL_CONF" << DEV_STACK_LOCAL_CONF_DOCK
36 [[local|localrc]]
37 # use TryStack git mirror
38 GIT_BASE=$STACK_GIT_BASE
39
40 # If the "*_PASSWORD" variables are not set here you will be prompted to enter
41 # values for them by "stack.sh" and they will be added to "local.conf".
42 ADMIN_PASSWORD=$STACK_PASSWORD
43 DATABASE_PASSWORD=$STACK_PASSWORD
44 RABBIT_PASSWORD=$STACK_PASSWORD
45 SERVICE_PASSWORD=$STACK_PASSWORD
46
47 # Neither is set by default.
48 HOST_IP=$HOST_IP
49
50 # path of the destination log file.  A timestamp will be appended to the given name.
51 LOGFILE=\$DEST/logs/stack.sh.log
52
53 # Old log files are automatically removed after 7 days to keep things neat.  Change
54 # the number of days by setting "LOGDAYS".
55 LOGDAYS=2
56
57 ENABLED_SERVICES=mysql,key
58 # Using stable/queens branches
59 # ---------------------------------
60 KEYSTONE_BRANCH=$STACK_BRANCH
61 KEYSTONECLIENT_BRANCH=$STACK_BRANCH
62 DEV_STACK_LOCAL_CONF_DOCK
63 chown stack:stack "$DEV_STACK_LOCAL_CONF"
64 }
65
66 opensds_conf() {
67 cat >> "$OPENSDS_CONFIG_DIR/opensds.conf" << OPENSDS_GLOBAL_CONFIG_DOC
68
69
70 [keystone_authtoken]
71 memcached_servers = $HOST_IP:11211
72 signing_dir = /var/cache/opensds
73 cafile = /opt/stack/data/ca-bundle.pem
74 auth_uri = http://$HOST_IP/identity
75 project_domain_name = Default
76 project_name = service
77 user_domain_name = Default
78 password = $STACK_PASSWORD
79 username = $OPENSDS_SERVER_NAME
80 auth_url = http://$HOST_IP/identity
81 auth_type = password
82
83 OPENSDS_GLOBAL_CONFIG_DOC
84
85 cp "$OPENSDS_DIR/examples/policy.json" "$OPENSDS_CONFIG_DIR"
86 }
87
88 create_user_and_endpoint(){
89     . "$DEV_STACK_DIR/openrc" admin admin
90     openstack user create --domain default --password "$STACK_PASSWORD" "$OPENSDS_SERVER_NAME"
91     openstack role add --project service --user opensds admin
92     openstack group create service
93     openstack group add user service opensds
94     openstack role add service --project service --group service
95     openstack group add user admins admin
96     openstack service create --name "opensds$OPENSDS_VERSION" --description "OpenSDS Block Storage" "opensds$OPENSDS_VERSION"
97     openstack endpoint create --region RegionOne "opensds$OPENSDS_VERSION" public "http://$HOST_IP:50040/$OPENSDS_VERSION/%\(tenant_id\)s"
98     openstack endpoint create --region RegionOne "opensds$OPENSDS_VERSION" internal "http://$HOST_IP:50040/$OPENSDS_VERSION/%\(tenant_id\)s"
99     openstack endpoint create --region RegionOne "opensds$OPENSDS_VERSION" admin "http://$HOST_IP:50040/$OPENSDS_VERSION/%\(tenant_id\)s"
100 }
101
102 delete_redundancy_data() {
103     . "$DEV_STACK_DIR/openrc" admin admin
104     openstack project delete demo
105     openstack project delete alt_demo
106     openstack project delete invisible_to_admin
107     openstack user delete demo
108     openstack user delete alt_demo
109 }
110
111 download_code(){
112     if [ ! -d "${DEV_STACK_DIR}" ];then
113         git clone "${STACK_GIT_BASE}/openstack-dev/devstack.git" -b "${STACK_BRANCH}" "${DEV_STACK_DIR}"
114         chown stack:stack -R "${DEV_STACK_DIR}"
115     fi
116 }
117
118 install(){
119     create_user
120     download_code
121     opensds_conf
122
123     # If keystone is ready to start, there is no need continue next step.
124     if wait_for_url "http://$HOST_IP/identity" "keystone" 0.25 4; then
125         return
126     fi
127     devstack_local_conf
128     cd "${DEV_STACK_DIR}"
129     su "$STACK_USER_NAME" -c "${DEV_STACK_DIR}/stack.sh" >/dev/null
130     create_user_and_endpoint
131     delete_redundancy_data
132 }
133
134 cleanup() {
135     su "$STACK_USER_NAME" -c "${DEV_STACK_DIR}/clean.sh" >/dev/null
136 }
137
138 uninstall(){
139     su "$STACK_USER_NAME" -c "${DEV_STACK_DIR}/unstack.sh" >/dev/null
140 }
141
142 uninstall_purge(){
143     rm "${STACK_HOME:?'STACK_HOME must be defined and cannot be empty'}/*" -rf
144     remove_user
145 }
146
147 # ***************************
148 TOP_DIR=$(cd $(dirname "$0") && pwd)
149
150 # OpenSDS configuration directory
151 OPENSDS_CONFIG_DIR=${OPENSDS_CONFIG_DIR:-/etc/opensds}
152
153 source "$TOP_DIR/util.sh"
154 source "$TOP_DIR/sdsrc"
155
156 case "$# $1" in
157     "1 install")
158     echo "Starting install keystone..."
159     install
160     ;;
161     "1 uninstall")
162     echo "Starting uninstall keystone..."
163     uninstall
164     ;;
165     "1 cleanup")
166     echo "Starting cleanup keystone..."
167     cleanup
168     ;;
169     "1 uninstall_purge")
170     echo "Starting uninstall purge keystone..."
171     uninstall_purge
172     ;;
173      *)
174     echo "The value of the parameter can only be one of the following: install/uninstall/cleanup/uninstall_purge"
175     exit 1
176     ;;
177 esac
178