Merge "Adds pacemaker update_tasks for Pike minor update workflow" into stable/pike
[apex-tripleo-heat-templates.git] / docker / services / pacemaker / database / mysql.yaml
1 heat_template_version: pike
2
3 description: >
4   MySQL service deployment with pacemaker bundle
5
6 parameters:
7   DockerMysqlImage:
8     description: image
9     type: string
10   DockerMysqlConfigImage:
11     description: The container image to use for the mysql 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   MysqlRootPassword:
32     type: string
33     hidden: true
34     default: ''
35   MysqlClustercheckPassword:
36     type: string
37     hidden: true
38   RoleName:
39     default: ''
40     description: Role name on which the service is applied
41     type: string
42   RoleParameters:
43     default: {}
44     description: Parameters specific to the role
45     type: json
46   EnableInternalTLS:
47     type: boolean
48     default: false
49   InternalTLSCAFile:
50     default: '/etc/ipa/ca.crt'
51     type: string
52     description: Specifies the default CA cert to use if TLS is used for
53                  services in the internal network.
54
55 resources:
56
57   ContainersCommon:
58     type: ../../containers-common.yaml
59
60   MysqlPuppetBase:
61     type: ../../../../puppet/services/pacemaker/database/mysql.yaml
62     properties:
63       EndpointMap: {get_param: EndpointMap}
64       ServiceData: {get_param: ServiceData}
65       ServiceNetMap: {get_param: ServiceNetMap}
66       DefaultPasswords: {get_param: DefaultPasswords}
67       RoleName: {get_param: RoleName}
68       RoleParameters: {get_param: RoleParameters}
69
70 conditions:
71
72   internal_tls_enabled: {equals: [{get_param: EnableInternalTLS}, true]}
73
74 outputs:
75   role_data:
76     description: Containerized service MySQL using composable services.
77     value:
78       service_name: {get_attr: [MysqlPuppetBase, role_data, service_name]}
79       config_settings:
80         map_merge:
81           - {get_attr: [MysqlPuppetBase, role_data, config_settings]}
82           - tripleo::profile::pacemaker::database::mysql_bundle::mysql_docker_image: &mysql_image_pcmklatest
83               list_join:
84                 - ':'
85                 - - yaql:
86                       data: {get_param: DockerMysqlImage}
87                       expression: $.data.rightSplit(separator => ":", maxSplits => 1)[0]
88                   - 'pcmklatest'
89             tripleo::profile::pacemaker::database::mysql_bundle::control_port: 3123
90             tripleo.mysql.firewall_rules:
91               '104 mysql galera-bundle':
92                 dport:
93                   - 873
94                   - 3123
95                   - 3306
96                   - 4444
97                   - 4567
98                   - 4568
99                   - 9200
100             tripleo::profile::pacemaker::database::mysql_bundle::bind_address:
101               str_replace:
102                 template:
103                   "%{hiera('fqdn_$NETWORK')}"
104                 params:
105                   $NETWORK: {get_param: [ServiceNetMap, MysqlNetwork]}
106           -
107             if:
108             - internal_tls_enabled
109             -
110               tripleo::profile::pacemaker::database::mysql_bundle::ca_file:
111                 get_param: InternalTLSCAFile
112             - {}
113       logging_source: {get_attr: [MysqlPuppetBase, role_data, logging_source]}
114       logging_groups: {get_attr: [MysqlPuppetBase, role_data, logging_groups]}
115       step_config: ""
116       # BEGIN DOCKER SETTINGS #
117       puppet_config:
118         config_volume: mysql
119         puppet_tags: file # set this even though file is the default
120         step_config:
121           list_join:
122             - "\n"
123             - - "['Mysql_datadir', 'Mysql_user', 'Mysql_database', 'Mysql_grant', 'Mysql_plugin'].each |String $val| { noop_resource($val) }"
124               - "exec {'wait-for-settle': command => '/bin/true' }"
125               - "include ::tripleo::profile::pacemaker::database::mysql_bundle"
126         config_image: {get_param: DockerMysqlConfigImage}
127       kolla_config:
128         /var/lib/kolla/config_files/mysql.json:
129           command: /usr/sbin/pacemaker_remoted
130           config_files:
131             - dest: /etc/libqb/force-filesystem-sockets
132               source: /dev/null
133               owner: root
134               perm: '0644'
135             - source: "/var/lib/kolla/config_files/src/*"
136               dest: "/"
137               merge: true
138               preserve_properties: true
139             - source: "/var/lib/kolla/config_files/src-tls/*"
140               dest: "/"
141               merge: true
142               optional: true
143               preserve_properties: true
144           permissions:
145             - path: /etc/pki/tls/certs/mysql.crt
146               owner: mysql:mysql
147               perm: '0600'
148               optional: true
149             - path: /etc/pki/tls/private/mysql.key
150               owner: mysql:mysql
151               perm: '0600'
152               optional: true
153       docker_config:
154         step_1:
155           mysql_data_ownership:
156             start_order: 0
157             detach: false
158             image: {get_param: DockerMysqlImage}
159             net: host
160             user: root
161             # Kolla does only non-recursive chown
162             command: ['chown', '-R', 'mysql:', '/var/lib/mysql']
163             volumes:
164               - /var/lib/mysql:/var/lib/mysql
165           mysql_bootstrap:
166             start_order: 1
167             detach: false
168             image: {get_param: DockerMysqlImage}
169             net: host
170             user: root
171             # Kolla bootstraps aren't idempotent, explicitly checking if bootstrap was done
172             command:
173               - 'bash'
174               - '-ecx'
175               -
176                 list_join:
177                   - "\n"
178                   - - 'if [ -e /var/lib/mysql/mysql ]; then exit 0; fi'
179                     - 'echo -e "\n[mysqld]\nwsrep_provider=none" >> /etc/my.cnf'
180                     - 'sudo -u mysql -E kolla_start'
181                     - 'mysqld_safe --skip-networking --wsrep-on=OFF &'
182                     - 'timeout ${DB_MAX_TIMEOUT} /bin/bash -c ''until mysqladmin -uroot -p"${DB_ROOT_PASSWORD}" ping 2>/dev/null; do sleep 1; done'''
183                     - 'mysql -uroot -p"${DB_ROOT_PASSWORD}" -e "CREATE USER ''clustercheck''@''localhost'' IDENTIFIED BY ''${DB_CLUSTERCHECK_PASSWORD}'';"'
184                     - 'mysql -uroot -p"${DB_ROOT_PASSWORD}" -e "GRANT PROCESS ON *.* TO ''clustercheck''@''localhost'' WITH GRANT OPTION;"'
185                     - 'timeout ${DB_MAX_TIMEOUT} mysqladmin -uroot -p"${DB_ROOT_PASSWORD}" shutdown'
186             volumes: &mysql_volumes
187               list_concat:
188                 - {get_attr: [ContainersCommon, volumes]}
189                 -
190                   - /var/lib/kolla/config_files/mysql.json:/var/lib/kolla/config_files/config.json
191                   - /var/lib/config-data/puppet-generated/mysql/:/var/lib/kolla/config_files/src:ro
192                   - /var/lib/mysql:/var/lib/mysql
193             environment:
194               - KOLLA_CONFIG_STRATEGY=COPY_ALWAYS
195               - KOLLA_BOOTSTRAP=True
196               # NOTE(mandre) skip wsrep cluster status check
197               - KOLLA_KUBERNETES=True
198               - DB_MAX_TIMEOUT=60
199               -
200                 list_join:
201                   - '='
202                   - - 'DB_CLUSTERCHECK_PASSWORD'
203                     - {get_param: MysqlClustercheckPassword}
204               -
205                 list_join:
206                   - '='
207                   - - 'DB_ROOT_PASSWORD'
208                     -
209                       yaql:
210                         expression: $.data.passwords.where($ != '').first()
211                         data:
212                           passwords:
213                             - {get_param: MysqlRootPassword}
214                             - {get_param: [DefaultPasswords, mysql_root_password]}
215           mysql_image_tag:
216             start_order: 2
217             detach: false
218             net: host
219             user: root
220             command:
221               - '/bin/bash'
222               - '-c'
223               - str_replace:
224                   template:
225                     "/usr/bin/docker tag 'MYSQL_IMAGE' 'MYSQL_IMAGE_PCMKLATEST'"
226                   params:
227                     MYSQL_IMAGE: {get_param: DockerMysqlImage}
228                     MYSQL_IMAGE_PCMKLATEST: *mysql_image_pcmklatest
229             image: {get_param: DockerMysqlImage}
230             volumes:
231               - /etc/hosts:/etc/hosts:ro
232               - /etc/localtime:/etc/localtime:ro
233               - /dev/shm:/dev/shm:rw
234               - /etc/sysconfig/docker:/etc/sysconfig/docker:ro
235               - /usr/bin:/usr/bin:ro
236               - /var/run/docker.sock:/var/run/docker.sock:rw
237         step_2:
238           mysql_init_bundle:
239             start_order: 1
240             detach: false
241             net: host
242             user: root
243             command:
244               - '/bin/bash'
245               - '-c'
246               - str_replace:
247                   template:
248                     list_join:
249                       - '; '
250                       - - "cp -a /tmp/puppet-etc/* /etc/puppet; echo '{\"step\": 2}' > /etc/puppet/hieradata/docker.json"
251                         - "FACTER_uuid=docker puppet apply --tags file,file_line,concat,augeas,TAGS -v -e 'CONFIG'"
252                   params:
253                     TAGS: 'pacemaker::resource::bundle,pacemaker::property,pacemaker::resource::ocf,pacemaker::constraint::order,pacemaker::constraint::colocation,galera_ready,mysql_database,mysql_grant,mysql_user'
254                     CONFIG: 'include ::tripleo::profile::base::pacemaker;include ::tripleo::profile::pacemaker::database::mysql_bundle'
255             image: {get_param: DockerMysqlImage}
256             volumes:
257               - /etc/hosts:/etc/hosts:ro
258               - /etc/localtime:/etc/localtime:ro
259               - /etc/puppet:/tmp/puppet-etc:ro
260               - /usr/share/openstack-puppet/modules:/usr/share/openstack-puppet/modules:ro
261               - /etc/corosync/corosync.conf:/etc/corosync/corosync.conf:ro
262               - /dev/shm:/dev/shm:rw
263               - /var/lib/mysql:/var/lib/mysql:rw
264       host_prep_tasks:
265         - name: create /var/lib/mysql
266           file:
267             path: /var/lib/mysql
268             state: directory
269       metadata_settings:
270         get_attr: [MysqlPuppetBase, role_data, metadata_settings]
271       upgrade_tasks:
272         - name: get bootstrap nodeid
273           tags: common
274           command: hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid
275           register: bootstrap_node
276         - name: set is_bootstrap_node fact
277           tags: common
278           set_fact: is_bootstrap_node={{bootstrap_node.stdout|lower == ansible_hostname|lower}}
279         - name: Check cluster resource status
280           tags: step2
281           pacemaker_resource:
282             resource: galera
283             state: master
284             check_mode: true
285           ignore_errors: true
286           register: galera_res
287         - name: Disable the galera cluster resource
288           tags: step2
289           pacemaker_resource:
290             resource: galera
291             state: disable
292             wait_for_resource: true
293           register: output
294           retries: 5
295           until: output.rc == 0
296           when: is_bootstrap_node and galera_res|succeeded
297         - name: Delete the stopped galera cluster resource.
298           tags: step2
299           pacemaker_resource:
300             resource: galera
301             state: delete
302             wait_for_resource: true
303           register: output
304           retries: 5
305           until: output.rc == 0
306           when: is_bootstrap_node and galera_res|succeeded
307         - name: Disable mysql service
308           tags: step2
309           service: name=mariadb enabled=no
310         - name: Remove clustercheck service from xinetd
311           tags: step2
312           file: state=absent path=/etc/xinetd.d/galera-monitor
313         - name: Restart xinetd service after clustercheck removal
314           tags: step2
315           service: name=xinetd state=restarted
316       update_tasks:
317         - name: Get docker Mariadb image
318           set_fact:
319             docker_image: {get_param: DockerMysqlImage}
320             docker_image_latest: *mysql_image_pcmklatest
321           when: step == '2'
322         - name: Get previous Mariadb image id
323           shell: "docker images | awk '/mariadb.* pcmklatest/{print $3}'"
324           register: mariadb_image_id
325         - block:
326             - name: Get a list of container using Mariadb image
327               shell: "docker ps -q -f 'ancestor={{mariadb_image_id.stdout}}'"
328               register: mariadb_containers_to_destroy
329             # It will be recreated with the delpoy step.
330             - name: Remove any container using the same Mariadb image
331               shell: "docker rm -fv {{item}}"
332               with_items: "{{ mariadb_containers_to_destroy.stdout_lines }}"
333             - name: Remove previous Mariadb images
334               shell: "docker rmi -f {{mariadb_image_id.stdout}}"
335           when:
336             - step == '2'
337             - mariadb_image_id.stdout != ''
338         - name: Pull latest Mariadb images
339           command: "docker pull {{docker_image}}"
340           when: step == "2"
341         - name: Retag pcmklatest to latest Mariadb image
342           shell: "docker tag {{docker_image}} {{docker_image_latest}}"
343           when: step == "2"
344         # Got to check that pacemaker_is_active is working fine with bundle.
345         # TODO: pacemaker_is_active resource doesn't support bundle.