Add new workarounds mechanism to apply workarounds via Ansible.

The tripleo-upgrade has a workarounds logic which allows us to
apply patches or specific modifications prior and after some of
the upgrade steps. However, if these workarouds need to be applied
in the overcloud nodes, the only way to do it was via a bash script
which would iterate over the nodes and apply the patch or perform a
change on each of the nodes.

This patch adds a new workarounds field: ansible_hosts. When this
option will be present in the workaround and it will be different
than an empty string then the workaround will be aplied via Ansible in
the nodes specified in that ansible_hosts field. This ansible_hosts
option needs to be used in combination with the command one, as
the command will be transformed in a shell Ansible task which will
be executed in the nodes passed in the ansible_hosts option.

Example:
pre_overcloud_upgrade_prepare_workarounds:
- set_root_password:
    patch: false
    basedir: ''
    id: ''
    ansible_hosts: 'overcloud'
    command: |
        echo redhat | passwd root --stdin

will turn into a set_root_password.yaml Ansible playbook under
~/ansible_workarounds:

cat ~/ansible_workarounds/set_root_password.yaml
- hosts: overcloud
  tasks:
    - name: set_root_password workaround
      shell: |
        echo redhat | passwd root --stdin

When executing the workarounds, a new bash function ansible_patch
has been included which will take care of executing the generated
Ansible playbook.

Also, an optional input parameter could be passed to workarounds.sh,
when passed, it will be taken as input for the --limit option when
executing ansible-playbook. This way, we can execute a workaround
specifically in a server, instead of running it in all of them.

Change-Id: I421ebecfc5504ac2fd225de0c4fb0cbf735bbdaf
(cherry picked from commit e582d2f304)
This commit is contained in:
Jose Luis Franco Arza 2019-07-31 11:04:19 +02:00
parent 51d851cb7c
commit b6bac90695
1 changed files with 83 additions and 0 deletions

View File

@ -1,8 +1,22 @@
#!/bin/bash
#
# Apply upgrade workarounds
# These arguments are only used when the workaround contains
# the ansible_hosts option.
# Args:
# -l|--limit: Host to be passed in the --limit option when
# calling ansible-playbook. If not argument passed
# no --limit will be used = --limit all.
# -u|--ssh-ansible-user: User to be passed when creating the
# tripleo-inventory. Defaults to heat-admin.
set -euo pipefail
function usage {
echo "Usage: ${0} [ [-l|--limit <ansible_host_to_limit>] [-u|--ssh-ansible-user <ansible_ssh_user_for_inventory>]]"
}
function apply_patch {
local patch_dir=$1
local patch_id=$2
@ -29,6 +43,62 @@ function apply_patch {
}
function ansible_patch {
local ansible_filename=$1
local ansible_limit=""
local ansible_ssh_user="heat-admin"
local ansible_dir=$HOME/ansible_workarounds
if [ ! -z $LIMIT ]
then
ansible_limit="--limit ${LIMIT}"
fi
if [ ! -z $ANSIBLE_SSH_USER ]
then
ansible_ssh_user=$ANSIBLE_SSH_USER
fi
inventory_file=${ansible_dir}/tripleo-inventory.yaml
if [ ! -f "$inventory_file" ]; then
source {{ undercloud_rc }}
tripleo-ansible-inventory --ansible_ssh_user ${ansible_ssh_user} --static-yaml-inventory ${inventory_file}
fi
# Run ansible playbook
ansible-playbook -v -i ${inventory_file} --become --become-user root ${ansible_dir}/${ansible_filename}.yaml ${ansible_limit}
}
# Global vars
LIMIT=""
ANSIBLE_SSH_USER=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-l|--limit)
LIMIT="$2"
shift # past argument
shift # past value
;;
-u|--ssh-ansible-user)
ANSIBLE_SSH_USER="$2"
shift # past argument
shift # past value
;;
-h|--help)
usage
exit 1
;;
*)
usage
exit 1 # unknown option
;;
esac
done
{% macro render_workarounds(workaround_var, workaround_name) -%}
{% if workaround_name in item -%}
{% for bugs in workaround_var -%}
@ -36,6 +106,19 @@ function apply_patch {
echo {{ key }}
{% if value.patch -%}
apply_patch {{ value.basedir }} {{ value.id }}
{% elif value.ansible_hosts is defined and value.ansible_hosts != '' -%}
[ -d $HOME/ansible_workarounds ] || mkdir $HOME/ansible_workarounds
cat <<'EOF' > "$HOME/ansible_workarounds/{{ key }}.yaml"
- hosts: {{ value.ansible_hosts }}
tasks:
- name: {{key}} workaround
shell: |
{{ value.command | indent(width=8) }}
EOF
ansible_patch {{ key }}
{% else -%}
{{ value.command }}
{% endif -%}