Update default cell_v2 cell when it already exists
[apex-tripleo-heat-templates.git] / docker / services / nova-api.yaml
1 heat_template_version: pike
2
3 description: >
4   OpenStack containerized Nova API service
5
6 parameters:
7   DockerNovaApiImage:
8     description: image
9     type: string
10   DockerNovaConfigImage:
11     description: The container image to use for the nova config_volume
12     type: string
13   EndpointMap:
14     default: {}
15     description: Mapping of service endpoint -> protocol. Typically set
16                  via parameter_defaults in the resource registry.
17     type: json
18   ServiceData:
19     default: {}
20     description: Dictionary packing service data
21     type: json
22   ServiceNetMap:
23     default: {}
24     description: Mapping of service_name -> network name. Typically set
25                  via parameter_defaults in the resource registry.  This
26                  mapping overrides those in ServiceNetMapDefaults.
27     type: json
28   DefaultPasswords:
29     default: {}
30     type: json
31   RoleName:
32     default: ''
33     description: Role name on which the service is applied
34     type: string
35   RoleParameters:
36     default: {}
37     description: Parameters specific to the role
38     type: json
39   EnableInternalTLS:
40     type: boolean
41     default: false
42   UpgradeRemoveUnusedPackages:
43     default: false
44     description: Remove package if the service is being disabled during upgrade
45     type: boolean
46
47 conditions:
48
49   internal_tls_enabled: {equals: [{get_param: EnableInternalTLS}, true]}
50
51 resources:
52
53   ContainersCommon:
54     type: ./containers-common.yaml
55
56   MySQLClient:
57     type: ../../puppet/services/database/mysql-client.yaml
58
59   NovaApiBase:
60     type: ../../puppet/services/nova-api.yaml
61     properties:
62       EndpointMap: {get_param: EndpointMap}
63       ServiceData: {get_param: ServiceData}
64       ServiceNetMap: {get_param: ServiceNetMap}
65       DefaultPasswords: {get_param: DefaultPasswords}
66       RoleName: {get_param: RoleName}
67       RoleParameters: {get_param: RoleParameters}
68
69 outputs:
70   role_data:
71     description: Role data for the Nova API role.
72     value:
73       service_name: {get_attr: [NovaApiBase, role_data, service_name]}
74       config_settings:
75         map_merge:
76           - get_attr: [NovaApiBase, role_data, config_settings]
77           - apache::default_vhost: false
78       logging_source: {get_attr: [NovaApiBase, role_data, logging_source]}
79       logging_groups: {get_attr: [NovaApiBase, role_data, logging_groups]}
80       step_config: &step_config
81         list_join:
82           - "\n"
83           - - "['Nova_cell_v2'].each |String $val| { noop_resource($val) }"
84             - {get_attr: [NovaApiBase, role_data, step_config]}
85             - {get_attr: [MySQLClient, role_data, step_config]}
86       service_config_settings: {get_attr: [NovaApiBase, role_data, service_config_settings]}
87       # BEGIN DOCKER SETTINGS
88       puppet_config:
89         config_volume: nova
90         puppet_tags: nova_config
91         step_config: *step_config
92         config_image: {get_param: DockerNovaConfigImage}
93       kolla_config:
94         /var/lib/kolla/config_files/nova_api.json:
95           command: /usr/sbin/httpd -DFOREGROUND
96           config_files:
97             - source: "/var/lib/kolla/config_files/src/*"
98               dest: "/"
99               merge: true
100               preserve_properties: true
101           permissions:
102             - path: /var/log/nova
103               owner: nova:nova
104               recurse: true
105         /var/lib/kolla/config_files/nova_api_cron.json:
106           command: /usr/sbin/crond -n
107           config_files:
108             - source: "/var/lib/kolla/config_files/src/*"
109               dest: "/"
110               merge: true
111               preserve_properties: true
112           permissions:
113             - path: /var/log/nova
114               owner: nova:nova
115               recurse: true
116       docker_config_scripts:
117         nova_api_discover_hosts.sh:
118           mode: "0700"
119           content: |
120             #!/bin/bash
121             export OS_PROJECT_DOMAIN_NAME=$(crudini --get /etc/nova/nova.conf keystone_authtoken project_domain_name)
122             export OS_USER_DOMAIN_NAME=$(crudini --get /etc/nova/nova.conf keystone_authtoken user_domain_name)
123             export OS_PROJECT_NAME=$(crudini --get /etc/nova/nova.conf keystone_authtoken project_name)
124             export OS_USERNAME=$(crudini --get /etc/nova/nova.conf keystone_authtoken username)
125             export OS_PASSWORD=$(crudini --get /etc/nova/nova.conf keystone_authtoken password)
126             export OS_AUTH_URL=$(crudini --get /etc/nova/nova.conf keystone_authtoken auth_url)
127             export OS_AUTH_TYPE=password
128             export OS_IDENTITY_API_VERSION=3
129
130             echo "(cellv2) Running cell_v2 host discovery"
131             timeout=600
132             loop_wait=30
133             declare -A discoverable_hosts
134             for host in $(hiera -c /etc/puppet/hiera.yaml cellv2_discovery_hosts | sed -e '/^nil$/d' |  tr "," " "); do discoverable_hosts[$host]=1; done
135             timeout_at=$(( $(date +"%s") + ${timeout} ))
136             echo "(cellv2) Waiting ${timeout} seconds for hosts to register"
137             finished=0
138             while : ; do
139               for host in $(openstack -q compute service list -c 'Host' -c 'Zone' -f value | awk '$2 != "internal" { print $1 }'); do
140                 if (( discoverable_hosts[$host] == 1 )); then
141                   echo "(cellv2) compute node $host has registered"
142                   unset discoverable_hosts[$host]
143                 fi
144               done
145               finished=1
146               for host in "${!discoverable_hosts[@]}"; do
147                 if (( ${discoverable_hosts[$host]} == 1 )); then
148                   echo "(cellv2) compute node $host has not registered"
149                   finished=0
150                 fi
151               done
152               remaining=$(( $timeout_at - $(date +"%s") ))
153               if (( $finished == 1 )); then
154                 echo "(cellv2) All nodes registered"
155                 break
156               elif (( $remaining <= 0 )); then
157                 echo "(cellv2) WARNING: timeout waiting for nodes to register, running host discovery regardless"
158                 echo "(cellv2) Expected host list:" $(hiera -c /etc/puppet/hiera.yaml cellv2_discovery_hosts | sed -e '/^nil$/d' | sort -u |  tr ',' ' ')
159                 echo "(cellv2) Detected host list:" $(openstack -q compute service list -c 'Host' -c 'Zone' -f value | awk '$2 != "internal" { print $1 }' | sort -u | tr '\n', ' ')
160                 break
161               else
162                 echo "(cellv2) Waiting ${remaining} seconds for hosts to register"
163                 sleep $loop_wait
164               fi
165             done
166             echo "(cellv2) Running host discovery..."
167             su nova -s /bin/bash -c "/usr/bin/nova-manage cell_v2 discover_hosts --verbose"
168         nova_api_ensure_default_cell.sh:
169           mode: "0700"
170           content: |
171             #!/bin/bash
172             DEFID=$(nova-manage cell_v2 list_cells | sed -e '1,3d' -e '$d' | awk -F ' *| *' '$2 == "default" {print $4}')
173             if [ "$DEFID" ]; then
174               echo "(cellv2) Updating default cell_v2 cell $DEFID"
175               su nova -s /bin/bash -c "/usr/bin/nova-manage cell_v2 update_cell --cell_uuid $DEFID --name=default"
176             else
177               echo "(cellv2) Creating default cell_v2 cell"
178               su nova -s /bin/bash -c "/usr/bin/nova-manage cell_v2 create_cell --name=default"
179             fi
180       docker_config:
181         # db sync runs before permissions set by kolla_config
182         step_2:
183           nova_init_logs:
184             image: &nova_api_image {get_param: DockerNovaApiImage}
185             privileged: false
186             user: root
187             volumes:
188               - /var/log/containers/nova:/var/log/nova
189               - /var/log/containers/httpd/nova-api:/var/log/httpd
190             command: ['/bin/bash', '-c', 'chown -R nova:nova /var/log/nova']
191         step_3:
192           nova_api_db_sync:
193             start_order: 0
194             image: *nova_api_image
195             net: host
196             detach: false
197             user: root
198             volumes: &nova_api_bootstrap_volumes
199               list_concat:
200                 - {get_attr: [ContainersCommon, volumes]}
201                 -
202                   - /var/lib/config-data/nova/etc/my.cnf.d/tripleo.cnf:/etc/my.cnf.d/tripleo.cnf:ro
203                   - /var/lib/config-data/nova/etc/nova/:/etc/nova/:ro
204                   - /var/log/containers/nova:/var/log/nova
205                   - /var/log/containers/httpd/nova-api:/var/log/httpd
206             command: "/usr/bin/bootstrap_host_exec nova_api su nova -s /bin/bash -c '/usr/bin/nova-manage api_db sync'"
207           nova_api_map_cell0:
208             start_order: 1
209             image: *nova_api_image
210             net: host
211             detach: false
212             user: root
213             volumes: *nova_api_bootstrap_volumes
214             command: "/usr/bin/bootstrap_host_exec nova_api su nova -s /bin/bash -c '/usr/bin/nova-manage cell_v2 map_cell0'"
215           nova_api_ensure_default_cell:
216             start_order: 2
217             image: *nova_api_image
218             net: host
219             detach: false
220             volumes:
221               list_concat:
222                 - *nova_api_bootstrap_volumes
223                 -
224                   - /var/lib/config-data/nova/etc/my.cnf.d/tripleo.cnf:/etc/my.cnf.d/tripleo.cnf:ro
225                   - /var/lib/config-data/nova/etc/nova/:/etc/nova/:ro
226                   - /var/log/containers/nova:/var/log/nova
227                   - /var/lib/docker-config-scripts/nova_api_ensure_default_cell.sh:/nova_api_ensure_default_cell.sh:ro
228             user: root
229             command: "/usr/bin/bootstrap_host_exec nova_api /nova_api_ensure_default_cell.sh"
230           nova_db_sync:
231             start_order: 3
232             image: *nova_api_image
233             net: host
234             detach: false
235             volumes: *nova_api_bootstrap_volumes
236             user: root
237             command: "/usr/bin/bootstrap_host_exec nova_api su nova -s /bin/bash -c '/usr/bin/nova-manage db sync'"
238         step_4:
239           nova_api:
240             start_order: 2
241             image: *nova_api_image
242             net: host
243             user: root
244             privileged: true
245             restart: always
246             volumes:
247               list_concat:
248                 - {get_attr: [ContainersCommon, volumes]}
249                 -
250                   - /var/lib/kolla/config_files/nova_api.json:/var/lib/kolla/config_files/config.json:ro
251                   - /var/lib/config-data/puppet-generated/nova/:/var/lib/kolla/config_files/src:ro
252                   - /var/log/containers/nova:/var/log/nova
253                   - /var/log/containers/httpd/nova-api:/var/log/httpd
254                   -
255                     if:
256                       - internal_tls_enabled
257                       - /etc/pki/tls/certs/httpd:/etc/pki/tls/certs/httpd:ro
258                       - ''
259                   -
260                     if:
261                       - internal_tls_enabled
262                       - /etc/pki/tls/private/httpd:/etc/pki/tls/private/httpd:ro
263                       - ''
264             environment:
265               - KOLLA_CONFIG_STRATEGY=COPY_ALWAYS
266           nova_api_cron:
267             image: *nova_api_image
268             net: host
269             user: root
270             privileged: false
271             restart: always
272             healthcheck:
273               test: /bin/true
274             volumes:
275               list_concat:
276                 - {get_attr: [ContainersCommon, volumes]}
277                 -
278                   - /var/lib/kolla/config_files/nova_api_cron.json:/var/lib/kolla/config_files/config.json:ro
279                   - /var/lib/config-data/puppet-generated/nova/:/var/lib/kolla/config_files/src:ro
280                   - /var/log/containers/nova:/var/log/nova
281                   - /var/log/containers/httpd/nova-api:/var/log/httpd
282             environment:
283               - KOLLA_CONFIG_STRATEGY=COPY_ALWAYS
284         step_5:
285           nova_api_discover_hosts:
286             start_order: 1
287             image: *nova_api_image
288             net: host
289             detach: false
290             volumes:
291               list_concat:
292                 - *nova_api_bootstrap_volumes
293                 -
294                   - /var/lib/config-data/nova/etc/my.cnf.d/tripleo.cnf:/etc/my.cnf.d/tripleo.cnf:ro
295                   - /var/lib/config-data/nova/etc/nova/:/etc/nova/:ro
296                   - /var/log/containers/nova:/var/log/nova
297                   - /var/lib/docker-config-scripts/nova_api_discover_hosts.sh:/nova_api_discover_hosts.sh:ro
298             user: root
299             command: "/usr/bin/bootstrap_host_exec nova_api /nova_api_discover_hosts.sh"
300       metadata_settings:
301         get_attr: [NovaApiBase, role_data, metadata_settings]
302       host_prep_tasks:
303         - name: create persistent logs directory
304           file:
305             path: "{{ item }}"
306             state: directory
307           with_items:
308             - /var/log/containers/nova
309             - /var/log/containers/httpd/nova-api
310       upgrade_tasks:
311         - name: Stop and disable nova_api service
312           tags: step2
313           service: name=openstack-nova-api state=stopped enabled=no
314         - name: Remove openstack-nova-api package if operator requests it
315           yum: name=openstack-nova-api state=removed
316           tags: step2
317           ignore_errors: True
318           when: {get_param: UpgradeRemoveUnusedPackages}
319         - name: remove old nova cron jobs
320           tags: step2
321           file:
322             path: /var/spool/cron/nova
323             state: absent