From 1c22ffa986a5270b444ea7af55231b97cfff1ba3 Mon Sep 17 00:00:00 2001 From: Matt Thompson Date: Wed, 2 Sep 2015 16:55:20 +0100 Subject: [PATCH] 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 --- playbooks/roles/os_neutron/defaults/main.yml | 1 - .../library/neutron_migrations_facts | 101 ++++++++++++++++++ playbooks/roles/os_neutron/tasks/main.yml | 3 +- .../os_neutron/tasks/neutron_db_setup.yml | 28 ++--- 4 files changed, 111 insertions(+), 22 deletions(-) create mode 100644 playbooks/roles/os_neutron/library/neutron_migrations_facts diff --git a/playbooks/roles/os_neutron/defaults/main.yml b/playbooks/roles/os_neutron/defaults/main.yml index 2a60d51240..42161f0d0d 100644 --- a/playbooks/roles/os_neutron/defaults/main.yml +++ b/playbooks/roles/os_neutron/defaults/main.yml @@ -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 diff --git a/playbooks/roles/os_neutron/library/neutron_migrations_facts b/playbooks/roles/os_neutron/library/neutron_migrations_facts new file mode 100644 index 0000000000..1fbb8e8659 --- /dev/null +++ b/playbooks/roles/os_neutron/library/neutron_migrations_facts @@ -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() diff --git a/playbooks/roles/os_neutron/tasks/main.yml b/playbooks/roles/os_neutron/tasks/main.yml index 5862a9982c..779f9f1f0a 100644 --- a/playbooks/roles/os_neutron/tasks/main.yml +++ b/playbooks/roles/os_neutron/tasks/main.yml @@ -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 diff --git a/playbooks/roles/os_neutron/tasks/neutron_db_setup.yml b/playbooks/roles/os_neutron/tasks/neutron_db_setup.yml index 8a968c71eb..a5328722e4 100644 --- a/playbooks/roles/os_neutron/tasks/neutron_db_setup.yml +++ b/playbooks/roles/os_neutron/tasks/neutron_db_setup.yml @@ -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