Add neutron_migrations_facts module

This change does the following:

1. Adds neutron_migrations_facts module, which creates a fact
   with the state of each neutron alembic migration branch
2. Removes var neutron_db_revision since this is no longer used
3. Removes the migration check task since this is now handled by the
   neutron_migrations_facts module
4. Updates roles/os_neutron/tasks/main.yml so the init scripts are
   dropped before we handle db syncing, which means we can stop the
   neutron-server on a greenfield deploy

DocImpact
Closes-Bug: #1486593
Change-Id: Ie0c5cb5abff61d11b7a9278de6771fa008b69218
This commit is contained in:
Matt Thompson 2015-09-02 16:55:20 +01:00 committed by Jesse Pretorius
parent 88c948c455
commit 1c22ffa986
4 changed files with 111 additions and 22 deletions

View File

@ -32,7 +32,6 @@ neutron_system_home_folder: "/var/lib/{{ neutron_system_user_name }}"
neutron_galera_user: neutron
neutron_galera_password: "{{ neutron_container_mysql_password }}"
neutron_galera_database: neutron
neutron_db_revision: heads
neutron_db_config: /etc/neutron/neutron.conf
neutron_db_plugin: /etc/neutron/plugins/ml2/ml2_conf.ini
neutron_db_max_overflow: 20

View File

@ -0,0 +1,101 @@
#!/usr/bin/env python
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import subprocess
from ansible.module_utils.basic import *
DOCUMENTATION = """
---
module: neutron_migrations_facts
short_description:
- A module for gathering neutron migrations facts.
description:
- This module creates a fact called 'neutron_migrations', which is a dict
containing keys that represent each alembic migration branch. The value
for each key is another dict, containing a key value for the current
neutron revision for that branch (revision), and whether or not the
branch is currently at the latest revision (head). The
'neutron_migrations' fact can then be used to determine if migrations
for either branch are required, and allows us to only apply migrations
if necessary.
options:
release:
description:
- This is the OpenStack release you're running, used when
searching for migration revisions in the neutron code.
default: liberty
author: Rcbops
"""
EXAMPLES = """
- name: Gather neutron migration facts
neutron_migrations_facts:
release: mitaka
"""
MIGRATIONS = {'expand': {'revision': None, 'head': None},
'contract': {'revision': None, 'head': None}}
def get_branch(release, revision):
migrations_dir = '/usr/local/lib/python2.7/dist-packages/neutron/db/' \
'migration/alembic_migrations/versions/%s/' % release
for branch in MIGRATIONS.keys():
for file in os.listdir('%s/%s' % (migrations_dir, branch)):
if file.endswith('.py') and file.split('_')[0] == revision:
return branch
def main():
module = AnsibleModule(
argument_spec=dict(
release=dict(
type='str',
default='liberty'
)
),
supports_check_mode=False
)
state_change = False
try:
current = subprocess.check_output(['neutron-db-manage', 'current'])
except subprocess.CalledProcessError as e:
message = 'neutron fact collection failed: "%s".' % e
module.fail_json(msg=message)
for line in current.splitlines():
head = False
match = re.search("^([0-9a-z]{4,12})(\s\(head\))?$", line)
if match:
revision = match.group(1)
if match.group(2):
head = True
branch = get_branch(module.params['release'], revision)
if branch is None:
message = 'neutron fact collection failed: unable to find ' \
'migration with revision %s' % revision
module.fail_json(msg=message)
MIGRATIONS[branch]['revision'] = revision
MIGRATIONS[branch]['head'] = head
module.exit_json(changed=state_change,
ansible_facts={'neutron_migrations': MIGRATIONS})
if __name__ == '__main__':
main()

View File

@ -16,6 +16,7 @@
- include: neutron_pre_install.yml
- include: neutron_install.yml
- include: neutron_post_install.yml
- include: neutron_upstart_init.yml
- include: neutron_db_setup.yml
when: >
@ -29,7 +30,5 @@
when: >
inventory_hostname in groups['neutron_agent']
- include: neutron_upstart_init.yml
- name: Flush handlers
meta: flush_handlers

View File

@ -39,25 +39,15 @@
tags:
- neutron-db-setup
- name: Check for existing migrations
shell: |
neutron-db-manage --config-file {{ neutron_db_config }} --config-file {{ neutron_db_plugin }} current | egrep "^[0-9a-z]{12}"
sudo: yes
sudo_user: "{{ neutron_system_user_name }}"
failed_when: false
register: neutron_migrations_previously_run
- name: Get neutron migrations facts
neutron_migrations_facts:
release: liberty
tags:
- neutron-db-setup
- neutron-upgrade
- name: Perform an initial Neutron DB sync
command: |
neutron-db-manage --config-file {{ neutron_db_config }}
--config-file {{ neutron_db_plugin }}
upgrade {{ neutron_db_revision }}
sudo: yes
sudo_user: "{{ neutron_system_user_name }}"
when: neutron_migrations_previously_run.rc == 1
- name: Print neutron migrations facts
debug: var=neutron_migrations
tags:
- neutron-db-setup
- neutron-upgrade
@ -69,7 +59,7 @@
upgrade --expand
sudo: yes
sudo_user: "{{ neutron_system_user_name }}"
when: neutron_migrations_previously_run.rc == 0
when: not neutron_migrations['expand']['head']|bool
tags:
- neutron-db-setup
- neutron-upgrade
@ -81,7 +71,7 @@
pattern: "neutron-server"
delegate_to: "{{ item }}"
with_items: groups['neutron_server']
when: neutron_migrations_previously_run.rc == 0
when: not neutron_migrations['contract']['head']|bool
tags:
- neutron-db-setup
- neutron-upgrade
@ -93,7 +83,7 @@
upgrade --contract
sudo: yes
sudo_user: "{{ neutron_system_user_name }}"
when: neutron_migrations_previously_run.rc == 0
when: not neutron_migrations['contract']['head']|bool
tags:
- neutron-db-setup
- neutron-upgrade
@ -105,7 +95,7 @@
pattern: "neutron-server"
delegate_to: "{{ item }}"
with_items: groups['neutron_server']
when: neutron_migrations_previously_run.rc == 0
when: not neutron_migrations['contract']['head']|bool
tags:
- neutron-db-setup
- neutron-upgrade