Create policy.json file in Ocata for non-admin user
[doctor.git] / tests / lib / installers / local
1 #!/bin/bash
2
3 COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
4 ssh_opts_cpu="$ssh_opts"
5
6 function installer_get_ssh_keys {
7     echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
8     return
9 }
10
11 function installer_apply_patches {
12     set -x
13     date
14     echo "### apply patches (installer=local)"
15     np_conf=/etc/nova/policy.json
16     if [ -e $np_conf ]; then
17         entry="os_compute_api:servers:show:host_status"
18         new="rule:admin_or_owner"
19         np_backup="${np_conf}-doctor-saved"
20         if grep -q "${entry}.*${new}" $np_conf; then
21             echo "Not modifying nova policy"
22         elif grep -q "${entry}" $np_conf; then
23             echo "modify nova policy"
24             cp $np_conf $np_backup
25             oldline=$(grep "$entry" $np_conf)
26             newline=$(echo "$oldline" | sed "s/rule.*\"/$new\"/")
27             sed -i "s/$oldline/$newline/" $np_conf
28             # TODO(umar): Update to systemd when screen is no more used for devstack
29             screen -S stack -p n-api -X stuff "^C^M^[[A^M" # restart n-api service
30         else
31             echo "add nova policy"
32             cp $np_conf $np_backup
33             sed -i "/{/a \    \"${entry}\": \"$new\"" $np_conf
34             screen -S stack -p n-api -X stuff "^C^M^[[A^M"
35         fi
36     else
37         # policy.json does not exist in Ocata.
38         echo "$np_conf does not exist. Creating a new one"
39         echo -e '{\n    "context_is_admin":  "role:admin",' > $np_conf
40         echo -e '    "owner" : "user_id:%(user_id)s",' >> $np_conf
41         echo -e '    "admin_or_owner": "rule:context_is_admin or rule:owner",' >> $np_conf
42         echo -e '    "os_compute_api:servers:show:host_status":  "rule:admin_or_owner"\n}' >> $np_conf
43         np_rm="${np_conf}-doctor-rm"
44         cp $np_conf $np_rm
45         screen -S stack -p n-api -X stuff "^C^M^[[A^M"
46     fi
47
48     return
49 }
50
51 function setup_installer {
52     installer_get_ssh_keys
53     installer_apply_patches
54 }
55
56 function get_compute_ip_from_hostname {
57     local compute_host=$1
58
59     if is_set COMPUTE_IP; then
60         echo "Using pre-configured COMPUTE_IP=$COMPUTE_IP ..."
61         return
62     fi
63     COMPUTE_IP=$(getent hosts "$compute_host" | awk '{ print $1 }')
64     die_if_not_set $LINENO COMPUTE_IP \
65         "Could not resolve $compute_host. Either manually set COMPUTE_IP or enable DNS resolution."
66 }
67
68 function cleanup_installer {
69     set -x
70     echo "### revert patches (installer=local)"
71     date
72
73     np_conf=/etc/nova/policy.json
74     np_backup="${np_conf}-doctor-saved"
75     np_rm="${np_conf}-doctor-rm"
76     if [ -e $np_backup ]; then
77         cp -f $np_backup $np_conf
78         rm $np_backup
79         screen -S stack -p n-api -X stuff "^C^M^[[A^M"
80     elif [ -e $np_rm ]; then
81         rm $np_conf
82         rm $np_rm
83         screen -S stack -p n-api -X stuff "^C^M^[[A^M"
84     fi
85
86     return
87 }