---
- import_tasks: get_cell_settings.yml
  when:
    - inventory_hostname == groups[nova_conductor.group][0] | default(None)

- name: Create cell
  vars:
    nova_conductor: "{{ nova_cell_services['nova-conductor'] }}"
  become: true
  kolla_docker:
    action: "start_container"
    command: bash -c 'sudo -E kolla_set_configs && nova-manage cell_v2 create_cell{% if nova_cell_name %} --name {{ nova_cell_name }}{% endif %}'
    common_options: "{{ docker_common_options }}"
    detach: False
    image: "{{ nova_conductor.image }}"
    labels:
      BOOTSTRAP:
    name: "create_cell_nova"
    restart_policy: no
    volumes: "{{ nova_cell_bootstrap_default_volumes + nova_cell_bootstrap_extra_volumes }}"
  register: nova_cell_create
  changed_when:
    - nova_cell_create is success
  failed_when:
    - nova_cell_create.rc != 0
    - '"already exists" not in nova_cell_create.stdout'
  when:
    - inventory_hostname == groups[nova_conductor.group][0] | default(None)
    - nova_cell_settings | length == 0

- name: Update cell
  vars:
    nova_cell_database_url: "mysql+pymysql://{{ nova_cell_database_user }}:{{ nova_cell_database_password }}@{{ nova_cell_database_address | put_address_in_context('url') }}:{{ nova_cell_database_port }}/{{ nova_cell_database_name }}"  # noqa 204
    nova_conductor: "{{ nova_cell_services['nova-conductor'] }}"
  become: true
  kolla_docker:
    action: "start_container"
    command: "bash -c 'sudo -E kolla_set_configs && nova-manage cell_v2 update_cell --cell_uuid {{ nova_cell_settings.cell_uuid }}'"
    common_options: "{{ docker_common_options }}"
    detach: False
    image: "{{ nova_conductor.image }}"
    labels:
      BOOTSTRAP:
    name: "create_cell_nova"
    restart_policy: no
    volumes: "{{ nova_cell_bootstrap_default_volumes + nova_cell_bootstrap_extra_volumes }}"
  register: nova_cell_updated
  changed_when:
    - nova_cell_updated is success
  failed_when:
    - nova_cell_updated.rc != 0
  when:
    - inventory_hostname == groups[nova_conductor.group][0] | default(None)
    - nova_cell_settings | length > 0
    - nova_cell_settings.cell_message_queue != nova_cell_rpc_transport_url or nova_cell_settings.cell_database != nova_cell_database_url