Create policy.json file in Ocata for non-admin user
[doctor.git] / tests / lib / installers / fuel
1 #!/bin/bash
2
3 if [[ "congress " == "$INSPECTOR_TYPE" ]]; then
4     die $LINENO "fuel does not support congress yet..."
5 fi
6
7 COMPUTE_USER=${COMPUTE_USER:-root}
8 ssh_opts_cpu="$ssh_opts -i instack_key"
9
10 function get_installer_ip {
11     is_set INSTALLER_IP && return
12     INSTALLER_IP=$(get_first_vnic_ip fuel-master)
13 }
14
15 function get_controller_ips {
16     is_set CONTROLLER_IPS && return
17     CONTROLLER_IPS=$(ssh $ssh_opts_cpu root@$INSTALLER_IP \
18                      "fuel node | grep controller | cut -d '|' -f 5|xargs")
19     die_if_not_set $LINENO CONTROLLER_IPS "No controller IPs"
20 }
21
22 function installer_get_ssh_keys {
23     if [[ -e instack_key ]]; then
24         echo "test existing instack_key..."
25         ssh $ssh_opts_cpu root@${INSTALLER_IP} "hostname" && return
26     fi
27     echo "getting instack_key from fuel node..."
28     sshpass -p r00tme scp $ssh_opts root@${INSTALLER_IP}:.ssh/id_rsa instack_key
29     sudo chown $(whoami):$(whoami) instack_key
30     chmod 400 instack_key
31 }
32
33 function installer_apply_patches {
34     # TODO(r-mibu): fix the followings in upstream (fuel)
35     for node in $CONTROLLER_IPS;do
36         echo "check controller configuration for doctor ($node)"
37         ssh $ssh_opts_cpu "root@$node" '
38             set -x
39             date
40             echo "### apply patches (installer=fuel)"
41
42             ep_conf=/etc/ceilometer/event_pipeline.yaml
43             entry="- notifier://?topic=alarm.all"
44             if ! grep -q -e "$entry" $ep_conf; then
45                 echo "modify the ceilometer config"
46                 echo "          $entry    # added by doctor script" >> $ep_conf
47                 service ceilometer-agent-notification restart
48             fi
49
50             # TODO(r-mibu): enable this section once congress 4.0.0 is available
51             if false; then
52             co_conf=/etc/congress/congress.conf
53             entry="congress.datasources.doctor_driver.DoctorDriver"
54             if ! grep -q -e "^drivers.*$entry" $co_conf; then
55                 echo "modify the congress config"
56                 sed -i -e "/^drivers/s/$/,$entry    # added by doctor script/" \
57                     $co_conf
58                 service congress-server restart
59             fi
60
61             rule="-m multiport -p tcp --dports 1789"
62             rule+=" -m comment --comment doctor-congress"
63             rule+=" -j ACCEPT"
64             if ! iptables -C INPUT $rule; then
65                 iptables -I INPUT $rule
66             fi
67
68             ha_conf=/etc/haproxy/conf.d/180-congress.cfg
69             if [[ ! -e $ha_conf ]]; then
70                 sed -e "1i# generated by doctor script" \
71                     -e "s/9696/1789/" \
72                     -e "s/neutron/congress/" \
73                     /etc/haproxy/conf.d/085-neutron.cfg > $ha_conf
74                 ip netns exec haproxy /usr/lib/ocf/resource.d/fuel/ns_haproxy restart
75             fi
76             fi
77
78             np_conf=/etc/nova/policy.json
79             if [ -e $np_conf ]; then
80                 entry="os_compute_api:servers:show:host_status"
81                 new="rule:admin_or_owner"
82                 np_backup="${np_conf}-doctor-saved"
83                 if grep -q "${entry}.*${new}" $np_conf; then
84                     echo "Not modifying nova policy"
85                 elif grep -q "${entry}" $np_conf; then
86                     echo "modify nova policy"
87                     cp $np_conf $np_backup
88                     oldline=$(grep "$entry" $np_conf)
89                     newline=$(echo "$oldline" | sed "s/rule.*\"/$new\"/")
90                     sed -i "s/$oldline/$newline/" $np_conf
91                     service nova-api restart
92                 else
93                     echo "add nova policy"
94                     cp $np_conf $np_backup
95                     sed -i "/{/a \    \"${entry}\": \"$new\"" $np_conf
96                     service nova-api restart
97                 fi
98             else
99                 # policy.json does not exist in Ocata.
100                 echo "$np_conf does not exist. Creating new one."
101                 echo -e "{\n    \"context_is_admin\":  \"role:admin\"," > $np_conf
102                 echo -e "    \"owner\" : \"user_id:%(user_id)s\"," >> $np_conf
103                 echo -e "    \"admin_or_owner\": \"rule:context_is_admin or rule:owner\"," >> $np_conf
104                 echo -e "    \"os_compute_api:servers:show:host_status\":  \"rule:admin_or_owner\" \n}" >> $np_conf
105                 np_rm="${np_conf}-doctor-rm"
106                 cp $np_conf $np_rm
107                 service nova-api restart
108             fi
109             ' > installer_apply_patches_$node.log 2>&1
110     done
111 }
112
113 function setup_installer {
114     get_installer_ip
115     installer_get_ssh_keys
116     get_controller_ips
117     installer_apply_patches
118     #Might take a moment for nova-api to restart
119     sleep 20
120     if ! openstack flavor show $VM_FLAVOR ; then
121         openstack flavor create --ram 512 --disk 1 $VM_FLAVOR \
122             && touch created_doctor_flavor
123     fi
124 }
125
126 function get_compute_ip_from_hostname {
127     local compute_host=$1
128
129     compute_host_in_undercloud=${compute_host%%.*}
130     node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
131     COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
132          "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
133     die_if_not_set $LINENO COMPUTE_IP "Could get IP address of $compute_host."
134 }
135
136 function installer_revert_patches {
137     # TODO(r-mibu): fix the followings in upstream (fuel)
138     get_controller_ips
139     for node in $CONTROLLER_IPS;do
140         echo "restore controller configuration if touched ($node)"
141         ssh $ssh_opts_cpu "root@$node" '
142             set -x
143             echo "### revert patches (installer=fuel)"
144             date
145
146             # TODO(r-mibu): enable this section once congress 4.0.0 is available
147             if false; then
148             ha_conf=/etc/haproxy/conf.d/180-congress.cfg
149             if grep -q "# generated by doctor script" $ha_conf; then
150                 rm -f $ha_conf
151                 ip netns exec haproxy /usr/lib/ocf/resource.d/fuel/ns_haproxy restart
152             fi
153
154             rule="-m multiport -p tcp --dports 1789"
155             rule+=" -m comment --comment doctor-congress"
156             rule+=" -j ACCEPT"
157             if iptables -C INPUT $rule; then
158                 iptables -D INPUT $rule
159             fi
160
161             co_conf=/etc/congress/congress.conf
162             entry="congress.datasources.doctor_driver.DoctorDriver"
163             if grep -q -e "^drivers.*$entry    # added by doctor script" $co_conf; then
164                 echo "modify the congress config"
165                 sed -i -e "/^drivers/s/^\(.*\),$entry/\1/" $co_conf
166                 service congress-server restart
167             fi
168             fi
169
170             ep_conf=/etc/ceilometer/event_pipeline.yaml
171             if grep -q "# added by doctor script" $ep_conf; then
172                 sed -ie "/# added by doctor script/d" $ep_conf
173                 service ceilometer-agent-notification restart
174             fi
175
176             np_conf=/etc/nova/policy.json
177             np_backup="${np_conf}-doctor-saved"
178             np_rm="${np_conf}-doctor-rm"
179             if [ -e $np_backup ]; then
180                 cp -f $np_backup $np_conf
181                 rm $np_backup
182                 service nova-api restart
183             elif [ -e $np_rm ]; then
184                 rm $np_conf
185                 rm $np_rm
186                 service nova-api restart
187             fi
188             ' >> installer_apply_patches_$node.log 2>&1
189     done
190 }
191
192 function cleanup_installer {
193     if [[ -e created_doctor_flavor ]] && openstack flavor show $VM_FLAVOR ; then
194         openstack flavor delete $VM_FLAVOR && rm -f created_doctor_flavor
195     fi
196     installer_revert_patches
197 }