openstack-ansible/playbooks/common-tasks/os-lxc-container-setup.yml
Markos Chandras 1938df4ab4 playbooks: os-lxc-container-setup: Only stop the container if it's running
Sometimes, despite the container being started by the
lxc_container_create role, we may find it stopped when we try to
restart it as we see in the following error

TASK [Lxc container restart] ***************************************************
Thursday 12 October 2017  16:56:03 +0000 (0:00:03.191)       2:11:34.445 ******
FAILED - RETRYING: Lxc container restart (3 retries left).
FAILED - RETRYING: Lxc container restart (2 retries left).
FAILED - RETRYING: Lxc container restart (1 retries left).
fatal: [controller00_horizon_container-a0c1ea76 -> 172.29.236.11]:
FAILED! => {"attempts": 3, "changed": true, "cmd": ["lxc-stop", "--name", "controller00_horizon_container-a0c1ea76", "--logfile",
"/var/log/lxc/lxc-controller00_horizon_container-a0c1ea76.log", "--logpriority", "INFO", "-t 10"], "delta": "0:00:00.019918", "end":
"2017-10-12 16:56:41.885367", "failed": true, "rc": 2, "start": "2017-10-12 16:56:41.865449", "stderr": "controller00_horizon_container-a0c1ea76 is not running",
"stderr_lines": ["controller00_horizon_container-a0c1ea76 is not running"], "stdout": "", "stdout_lines": []}

It's not exactly clear why the container stopped by itself so lets try
to workaround this by simply checking if the container is actually
running before we try to stop it. If the container is still in an unusable
state, then the lxc-start command that follows this task will fail anyway.

Change-Id: I179f9f5dd08c74eaa88666dc19698fcb348703eb
2017-10-13 15:10:06 +01:00

151 lines
5.3 KiB
YAML

---
# Copyright 2016, 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.
# Usage:
# This common task will update lxc containers to use the lxc-openstack
# app-armor profile by default however this profile can be changed as needed.
# This will also load in a list of bind mounts for a given container. To load
# in a list of bind mounts the variable, "list_of_bind_mounts" must be used
# containing at least one dictionary with the keys "bind_dir_path",
# "relative_bind_dir_path", and "mount_path".
# * bind_dir_path = Container path used in a bind mount
# * mount_path = Local path on the physical host used for a bind mount
# If extra container configurations are desirable set the
# "extra_container_config" list to strings containing the options needed.
- name: Set default bind mounts (bind var/log)
set_fact:
lxc_default_bind_mounts: '{{ lxc_default_bind_mounts | default([{"bind_dir_path": "/var/log", "mount_path": "/openstack/log/{{ inventory_hostname }}"}]) }}'
when:
- default_bind_mount_logs | bool
tags:
- common-lxc
- name: Ensure mount directories exists
file:
path: "{{ item['mount_path'] }}"
state: "directory"
with_items:
- "{{ lxc_default_bind_mounts | default([]) }}"
- "{{ list_of_bind_mounts | default([]) }}"
delegate_to: "{{ physical_host }}"
when:
- not is_metal | bool
tags:
- common-lxc
- name: Add bind mount configuration to container
lineinfile:
dest: "/var/lib/lxc/{{ inventory_hostname }}/config"
line: "lxc.mount.entry = {{ item['mount_path'] }} {{ item['bind_dir_path'].lstrip('/') }} none bind,create=dir 0 0"
backup: "true"
with_items:
- "{{ lxc_default_bind_mounts | default([]) }}"
- "{{ list_of_bind_mounts | default([]) }}"
delegate_to: "{{ physical_host }}"
when:
- not is_metal | bool
register: _mc
tags:
- common-lxc
- name: Extra lxc config
lineinfile:
dest: "/var/lib/lxc/{{ inventory_hostname }}/config"
line: "{{ item.split('=')[0] }} = {{ item.split('=', 1)[1] }}"
insertafter: "^{{ item.split('=')[0] }}"
backup: "true"
with_items: "{{ extra_container_config | default([]) }}"
delegate_to: "{{ physical_host }}"
register: _ec
when: not is_metal | bool
tags:
- common-lxc
- name: Extra lxc config no restart
lineinfile:
dest: "/var/lib/lxc/{{ inventory_hostname }}/config"
line: "{{ item.split('=')[0] }} = {{ item.split('=', 1)[1] }}"
insertafter: "^{{ item.split('=')[0] }}"
backup: "true"
with_items: "{{ extra_container_config_no_restart | default(['lxc.start.order=100']) }}"
delegate_to: "{{ physical_host }}"
when: not is_metal | bool
tags:
- common-lxc
- name: Check container state
command: "lxc-info -n {{ inventory_hostname }} --state"
changed_when: false
delegate_to: "{{ physical_host }}"
register: _lxc_container_state
until: _lxc_container_state | success
retries: 3
delay: 5
when:
- not is_metal | bool
- (_mc is defined and _mc | changed) or (_ec is defined and _ec | changed)
# Due to https://github.com/ansible/ansible-modules-extras/issues/2691
# this uses the LXC CLI tools to ensure that we get logging.
# TODO(odyssey4me): revisit this once the bug is fixed and released
- name: Lxc container restart
command: >
lxc-stop --name {{ inventory_hostname }}
--logfile {{ lxc_container_log_path }}/lxc-{{ inventory_hostname }}.log
--logpriority {{ (debug | bool) | ternary('DEBUG', 'INFO') }}
delegate_to: "{{ physical_host }}"
register: container_stop
until: container_stop | success
retries: 3
when:
- not is_metal | bool
- (_mc is defined and _mc | changed) or (_ec is defined and _ec | changed)
- _lxc_container_state.stdout.find('RUNNING') != -1
tags:
- common-lxc
# Due to https://github.com/ansible/ansible-modules-extras/issues/2691
# this uses the LXC CLI tools to ensure that we get logging.
# TODO(odyssey4me): revisit this once the bug is fixed and released
- name: Start Container
command: >
lxc-start --daemon --name {{ inventory_hostname }}
--logfile {{ lxc_container_log_path }}/lxc-{{ inventory_hostname }}.log
--logpriority {{ (debug | bool) | ternary('DEBUG', 'INFO') }}
delegate_to: "{{ physical_host }}"
register: container_start
until: container_start | success
retries: 3
when:
- not is_metal | bool
- (_mc is defined and _mc | changed) or (_ec is defined and _ec | changed)
tags:
- common-lxc
- name: Wait for container connectivity
wait_for_connection:
connect_timeout: "{{ lxc_container_wait_params.connect_timeout | default(omit) }}"
delay: "{{ lxc_container_wait_params.delay | default(omit) }}"
sleep: "{{ lxc_container_wait_params.sleep | default(omit) }}"
timeout: "{{ lxc_container_wait_params.timeout | default(omit) }}"
when:
- (_mc is defined and _mc | changed) or (_ec is defined and _ec | changed)
- not is_metal | bool
tags:
- common-lxc