2016-05-10 17:48:36 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2016, Hiroaki Nakamura <hnakamur@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: lxd_container
|
|
|
|
short_description: Manage LXD Containers
|
|
|
|
version_added: 2.2.0
|
|
|
|
description:
|
|
|
|
- Management of LXD containers
|
|
|
|
author: "Hiroaki Nakamura (@hnakamur)"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of a container.
|
|
|
|
required: true
|
2016-06-28 15:52:40 +02:00
|
|
|
architecture:
|
|
|
|
description:
|
|
|
|
- The archiecture for the container (e.g. "x86_64" or "i686").
|
|
|
|
See https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1
|
|
|
|
required: false
|
2016-05-10 17:48:36 +02:00
|
|
|
config:
|
|
|
|
description:
|
2016-06-28 16:02:08 +02:00
|
|
|
- >
|
|
|
|
The config for the container (e.g. {"limits.cpu": "2"}).
|
2016-05-10 17:48:36 +02:00
|
|
|
See https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1
|
2016-06-28 15:52:40 +02:00
|
|
|
- If the container already exists and its "config" value in metadata
|
|
|
|
obtained from
|
2016-06-04 09:43:21 +02:00
|
|
|
GET /1.0/containers/<name>
|
|
|
|
https://github.com/lxc/lxd/blob/master/doc/rest-api.md#10containersname
|
|
|
|
are different, they this module tries to apply the configurations.
|
2016-06-28 15:52:40 +02:00
|
|
|
- The key starts with 'volatile.' are ignored for this comparison.
|
2016-06-04 09:43:21 +02:00
|
|
|
- Not all config values are supported to apply the existing container.
|
|
|
|
Maybe you need to delete and recreate a container.
|
2016-05-10 17:48:36 +02:00
|
|
|
required: false
|
2016-06-28 15:52:40 +02:00
|
|
|
devices:
|
|
|
|
description:
|
2016-06-28 16:02:08 +02:00
|
|
|
- >
|
|
|
|
The devices for the container
|
|
|
|
(e.g. { "rootfs": { "path": "/dev/kvm", "type": "unix-char" }).
|
2016-06-28 15:52:40 +02:00
|
|
|
See https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1
|
|
|
|
required: false
|
|
|
|
ephemeral:
|
|
|
|
description:
|
|
|
|
- Whether or not the container is ephemeral (e.g. true or false).
|
|
|
|
See https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1
|
|
|
|
required: false
|
|
|
|
source:
|
|
|
|
description:
|
2016-06-28 16:02:08 +02:00
|
|
|
- >
|
|
|
|
The source for the container
|
|
|
|
(e.g. { "type": "image",
|
|
|
|
"mode": "pull",
|
|
|
|
"server": "https://images.linuxcontainers.org",
|
|
|
|
"protocol": "lxd",
|
|
|
|
"alias": "ubuntu/xenial/amd64" }).
|
2016-06-28 15:52:40 +02:00
|
|
|
See https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1
|
|
|
|
required: false
|
2016-05-10 17:48:36 +02:00
|
|
|
state:
|
|
|
|
choices:
|
|
|
|
- started
|
|
|
|
- stopped
|
|
|
|
- restarted
|
|
|
|
- absent
|
|
|
|
- frozen
|
|
|
|
description:
|
2016-06-30 17:42:50 +02:00
|
|
|
- Define the state of a container.
|
2016-05-10 17:48:36 +02:00
|
|
|
required: false
|
|
|
|
default: started
|
2016-06-04 07:11:15 +02:00
|
|
|
timeout:
|
2016-05-10 17:48:36 +02:00
|
|
|
description:
|
2016-06-30 14:02:16 +02:00
|
|
|
- A timeout for changing the state of the container.
|
2016-06-04 07:11:15 +02:00
|
|
|
- This is also used as a timeout for waiting until IPv4 addresses
|
|
|
|
are set to the all network interfaces in the container after
|
|
|
|
starting or restarting.
|
2016-05-10 17:48:36 +02:00
|
|
|
required: false
|
2016-06-04 07:11:15 +02:00
|
|
|
default: 30
|
|
|
|
wait_for_ipv4_addresses:
|
|
|
|
description:
|
|
|
|
- If this is true, the lxd_module waits until IPv4 addresses
|
|
|
|
are set to the all network interfaces in the container after
|
|
|
|
starting or restarting.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
force_stop:
|
|
|
|
description:
|
|
|
|
- If this is true, the lxd_module forces to stop the container
|
|
|
|
when it stops or restarts the container.
|
|
|
|
required: false
|
|
|
|
default: false
|
2016-06-29 18:01:07 +02:00
|
|
|
url:
|
|
|
|
description:
|
2016-06-30 15:36:58 +02:00
|
|
|
- The unix domain socket path or the https URL for the LXD server.
|
|
|
|
required: false
|
|
|
|
default: unix:/var/lib/lxd/unix.socket
|
2016-06-29 18:01:07 +02:00
|
|
|
key_file:
|
|
|
|
description:
|
|
|
|
- The client certificate key file path.
|
|
|
|
required: false
|
|
|
|
default: >
|
|
|
|
'{}/.config/lxc/client.key'.format(os.environ['HOME'])
|
|
|
|
cert_file:
|
|
|
|
description:
|
|
|
|
- The client certificate file path.
|
|
|
|
required: false
|
|
|
|
default: >
|
|
|
|
'{}/.config/lxc/client.crt'.format(os.environ['HOME'])
|
|
|
|
trust_password:
|
|
|
|
description:
|
|
|
|
- The client trusted password.
|
|
|
|
- You need to set this password on the LXD server before
|
|
|
|
running this module using the following command.
|
|
|
|
lxc config set core.trust_password <some random password>
|
|
|
|
See https://www.stgraber.org/2016/04/18/lxd-api-direct-interaction/
|
|
|
|
- If trust_password is set, this module send a request for
|
|
|
|
authentication before sending any requests.
|
|
|
|
required: false
|
2016-06-30 14:14:48 +02:00
|
|
|
debug:
|
|
|
|
description:
|
|
|
|
- If this flag is true, the logs key are added to the result object
|
|
|
|
which keeps all the requests and responses for calling APIs.
|
|
|
|
required: false
|
|
|
|
default: false
|
2016-05-10 17:48:36 +02:00
|
|
|
notes:
|
|
|
|
- Containers must have a unique name. If you attempt to create a container
|
|
|
|
with a name that already existed in the users namespace the module will
|
|
|
|
simply return as "unchanged".
|
2016-05-17 16:08:16 +02:00
|
|
|
- There are two ways to can run commands in containers, using the command
|
|
|
|
module or using the ansible lxd connection plugin bundled in Ansible >=
|
|
|
|
2.1, the later requires python to be installed in the container which can
|
|
|
|
be done with the command module.
|
|
|
|
- You can copy a file from the host to the container
|
2016-05-20 17:04:19 +02:00
|
|
|
with the Ansible `copy` and `template` module and the `lxd` connection plugin.
|
|
|
|
See the example below.
|
2016-05-16 21:12:58 +02:00
|
|
|
- You can copy a file in the creatd container to the localhost
|
2016-05-17 16:08:16 +02:00
|
|
|
with `command=lxc file pull container_name/dir/filename filename`.
|
2016-05-16 21:12:58 +02:00
|
|
|
See the first example below.
|
2016-05-10 17:48:36 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2016-06-30 17:42:50 +02:00
|
|
|
# An example for creating a Ubuntu container and install python
|
2016-05-16 21:12:58 +02:00
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Create a started container
|
|
|
|
lxd_container:
|
2016-05-20 17:04:19 +02:00
|
|
|
name: mycontainer
|
2016-05-16 21:12:58 +02:00
|
|
|
state: started
|
2016-06-28 15:52:40 +02:00
|
|
|
source:
|
|
|
|
type: image
|
|
|
|
mode: pull
|
|
|
|
server: https://images.linuxcontainers.org
|
|
|
|
protocol: lxd
|
2016-06-30 17:42:50 +02:00
|
|
|
alias: ubuntu/xenial/amd64
|
2016-06-28 15:52:40 +02:00
|
|
|
profiles: ["default"]
|
2016-06-30 17:42:50 +02:00
|
|
|
wait_for_ipv4_addresses: true
|
|
|
|
timeout: 600
|
|
|
|
|
|
|
|
- name: check python is installed in container
|
|
|
|
delegate_to: mycontainer
|
|
|
|
raw: dpkg-query -W -f='${Status}' python
|
|
|
|
register: python_install_check
|
|
|
|
failed_when: python_install_check.rc not in [0, 1]
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
- name: install python in container
|
|
|
|
delegate_to: mycontainer
|
|
|
|
raw: apt-get install -y python
|
|
|
|
when: python_install_check.rc == 1
|
|
|
|
|
|
|
|
# An example for deleting a container
|
2016-05-16 21:12:58 +02:00
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
2016-06-30 17:42:50 +02:00
|
|
|
- name: Restart a container
|
2016-05-16 21:12:58 +02:00
|
|
|
lxd_container:
|
2016-05-20 17:04:19 +02:00
|
|
|
name: mycontainer
|
2016-06-30 17:42:50 +02:00
|
|
|
state: restarted
|
2016-05-10 17:48:36 +02:00
|
|
|
|
2016-06-30 17:42:50 +02:00
|
|
|
# An example for restarting a container
|
2016-05-16 21:12:58 +02:00
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Restart a container
|
|
|
|
lxd_container:
|
2016-05-20 17:04:19 +02:00
|
|
|
name: mycontainer
|
2016-05-16 21:12:58 +02:00
|
|
|
state: restarted
|
2016-06-28 17:24:56 +02:00
|
|
|
|
2016-06-30 17:42:50 +02:00
|
|
|
# An example for restarting a container using https to connect to the LXD server
|
2016-06-28 17:24:56 +02:00
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
tasks:
|
2016-06-30 17:42:50 +02:00
|
|
|
- name: Restart a container
|
|
|
|
lxd_container:
|
|
|
|
url: https://127.0.0.1:8443
|
|
|
|
# These cert_file and key_file values are equal to the default values.
|
|
|
|
#cert_file: "{{ lookup('env', 'HOME') }}/.config/lxc/client.crt"
|
|
|
|
#key_file: "{{ lookup('env', 'HOME') }}/.config/lxc/client.key"
|
|
|
|
trust_password: mypassword
|
|
|
|
name: mycontainer
|
|
|
|
state: restarted
|
|
|
|
|
|
|
|
# Note your container must be in the inventory for the below example.
|
|
|
|
#
|
|
|
|
# [containers]
|
|
|
|
# mycontainer ansible_connection=lxd
|
|
|
|
#
|
|
|
|
- hosts:
|
|
|
|
- mycontainer
|
|
|
|
tasks:
|
|
|
|
- name: copy /etc/hosts in the created container to localhost with name "mycontainer-hosts"
|
|
|
|
fetch:
|
|
|
|
src: /etc/hosts
|
|
|
|
dest: /tmp/mycontainer-hosts
|
|
|
|
flat: true
|
2016-05-10 17:48:36 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN="""
|
|
|
|
lxd_container:
|
2016-05-11 00:37:34 +02:00
|
|
|
description: container information
|
|
|
|
returned: success
|
|
|
|
type: object
|
|
|
|
contains:
|
|
|
|
addresses:
|
2016-06-30 14:14:48 +02:00
|
|
|
description: Mapping from the network device name to a list of IPv4 addresses in the container
|
2016-05-11 00:37:34 +02:00
|
|
|
returned: when state is started or restarted
|
|
|
|
type: object
|
|
|
|
sample: {"eth0": ["10.155.92.191"]}
|
|
|
|
old_state:
|
2016-06-30 14:14:48 +02:00
|
|
|
description: The old state of the container
|
2016-05-11 00:37:34 +02:00
|
|
|
returned: when state is started or restarted
|
|
|
|
sample: "stopped"
|
2016-06-25 11:08:13 +02:00
|
|
|
logs:
|
2016-06-30 14:42:05 +02:00
|
|
|
descriptions: The logs of requests and responses.
|
|
|
|
returned: when the debug parameter is true and any requests were sent.
|
2016-06-04 07:11:15 +02:00
|
|
|
actions:
|
2016-06-30 14:14:48 +02:00
|
|
|
description: List of actions performed for the container.
|
2016-05-11 00:37:34 +02:00
|
|
|
returned: success
|
|
|
|
type: list
|
2016-06-04 07:11:15 +02:00
|
|
|
sample: ["create", "start"]
|
2016-05-10 17:48:36 +02:00
|
|
|
"""
|
|
|
|
|
2016-06-29 18:01:07 +02:00
|
|
|
import os
|
2016-06-30 17:03:46 +02:00
|
|
|
from ansible.modules.extras.cloud.lxd import LXDClient, LXDClientException
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
# LXD_ANSIBLE_STATES is a map of states that contain values of methods used
|
|
|
|
# when a particular state is evoked.
|
|
|
|
LXD_ANSIBLE_STATES = {
|
|
|
|
'started': '_started',
|
|
|
|
'stopped': '_stopped',
|
|
|
|
'restarted': '_restarted',
|
|
|
|
'absent': '_destroyed',
|
|
|
|
'frozen': '_frozen'
|
|
|
|
}
|
|
|
|
|
|
|
|
# ANSIBLE_LXD_STATES is a map of states of lxd containers to the Ansible
|
|
|
|
# lxc_container module state parameter value.
|
|
|
|
ANSIBLE_LXD_STATES = {
|
|
|
|
'Running': 'started',
|
|
|
|
'Stopped': 'stopped',
|
|
|
|
'Frozen': 'frozen',
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:42:50 +02:00
|
|
|
# CONFIG_PARAMS is a list of config attribute names.
|
|
|
|
CONFIG_PARAMS = [
|
|
|
|
'architecture', 'config', 'devices', 'ephemeral', 'profiles', 'source'
|
|
|
|
]
|
2016-06-28 17:24:56 +02:00
|
|
|
|
2016-05-10 17:48:36 +02:00
|
|
|
try:
|
|
|
|
callable(all)
|
|
|
|
except NameError:
|
|
|
|
# For python <2.5
|
|
|
|
# This definition is copied from https://docs.python.org/2/library/functions.html#all
|
|
|
|
def all(iterable):
|
|
|
|
for element in iterable:
|
|
|
|
if not element:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2016-06-30 17:42:50 +02:00
|
|
|
class LXDContainerManagement(object):
|
2016-05-10 17:48:36 +02:00
|
|
|
def __init__(self, module):
|
|
|
|
"""Management of LXC containers via Ansible.
|
|
|
|
|
|
|
|
:param module: Processed Ansible Module.
|
|
|
|
:type module: ``object``
|
|
|
|
"""
|
|
|
|
self.module = module
|
2016-06-28 17:24:56 +02:00
|
|
|
self.name = self.module.params['name']
|
|
|
|
self._build_config()
|
2016-06-29 18:59:37 +02:00
|
|
|
|
2016-05-10 17:48:36 +02:00
|
|
|
self.state = self.module.params['state']
|
2016-06-29 18:59:37 +02:00
|
|
|
|
2016-06-04 07:11:15 +02:00
|
|
|
self.timeout = self.module.params['timeout']
|
|
|
|
self.wait_for_ipv4_addresses = self.module.params['wait_for_ipv4_addresses']
|
|
|
|
self.force_stop = self.module.params['force_stop']
|
2016-05-10 17:48:36 +02:00
|
|
|
self.addresses = None
|
2016-06-30 17:03:46 +02:00
|
|
|
|
2016-06-30 15:36:58 +02:00
|
|
|
self.url = self.module.params['url']
|
2016-06-29 18:01:07 +02:00
|
|
|
self.key_file = self.module.params.get('key_file', None)
|
|
|
|
self.cert_file = self.module.params.get('cert_file', None)
|
2016-06-30 14:14:48 +02:00
|
|
|
self.debug = self.module.params['debug']
|
2016-06-30 17:03:46 +02:00
|
|
|
try:
|
|
|
|
self.client = LXDClient(
|
|
|
|
self.url, key_file=self.key_file, cert_file=self.cert_file,
|
|
|
|
debug=self.debug
|
|
|
|
)
|
|
|
|
except LXDClientException as e:
|
|
|
|
self.module.fail_json(msg=e.msg)
|
|
|
|
self.trust_password = self.module.params.get('trust_password', None)
|
2016-06-04 07:11:15 +02:00
|
|
|
self.actions = []
|
|
|
|
|
2016-06-28 17:24:56 +02:00
|
|
|
def _build_config(self):
|
|
|
|
self.config = {}
|
2016-06-30 17:42:50 +02:00
|
|
|
for attr in CONFIG_PARAMS:
|
2016-06-28 17:24:56 +02:00
|
|
|
param_val = self.module.params.get(attr, None)
|
|
|
|
if param_val is not None:
|
|
|
|
self.config[attr] = param_val
|
|
|
|
|
2016-06-04 09:43:21 +02:00
|
|
|
def _get_container_json(self):
|
2016-06-30 17:03:46 +02:00
|
|
|
return self.client.do(
|
2016-06-28 17:24:56 +02:00
|
|
|
'GET', '/1.0/containers/{0}'.format(self.name),
|
2016-06-04 09:43:21 +02:00
|
|
|
ok_error_codes=[404]
|
|
|
|
)
|
|
|
|
|
2016-06-04 07:11:15 +02:00
|
|
|
def _get_container_state_json(self):
|
2016-06-30 17:03:46 +02:00
|
|
|
return self.client.do(
|
2016-06-28 17:24:56 +02:00
|
|
|
'GET', '/1.0/containers/{0}/state'.format(self.name),
|
2016-06-04 07:11:15 +02:00
|
|
|
ok_error_codes=[404]
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2016-06-04 09:43:21 +02:00
|
|
|
def _container_json_to_module_state(resp_json):
|
2016-06-04 07:11:15 +02:00
|
|
|
if resp_json['type'] == 'error':
|
|
|
|
return 'absent'
|
|
|
|
return ANSIBLE_LXD_STATES[resp_json['metadata']['status']]
|
|
|
|
|
|
|
|
def _change_state(self, action, force_stop=False):
|
2016-06-26 01:30:38 +02:00
|
|
|
body_json={'action': action, 'timeout': self.timeout}
|
2016-06-04 07:11:15 +02:00
|
|
|
if force_stop:
|
2016-06-26 01:30:38 +02:00
|
|
|
body_json['force'] = True
|
2016-06-30 17:03:46 +02:00
|
|
|
return self.client.do('PUT', '/1.0/containers/{0}/state'.format(self.name), body_json=body_json)
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _create_container(self):
|
2016-06-28 17:24:56 +02:00
|
|
|
config = self.config.copy()
|
|
|
|
config['name'] = self.name
|
2016-06-30 17:03:46 +02:00
|
|
|
self.client.do('POST', '/1.0/containers', config)
|
2016-06-28 15:52:40 +02:00
|
|
|
self.actions.append('create')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _start_container(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
self._change_state('start')
|
|
|
|
self.actions.append('start')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _stop_container(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
self._change_state('stop', self.force_stop)
|
|
|
|
self.actions.append('stop')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _restart_container(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
self._change_state('restart', self.force_stop)
|
|
|
|
self.actions.append('restart')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _delete_container(self):
|
2016-06-28 17:24:56 +02:00
|
|
|
return self._operate_and_wait('DELETE', '/1.0/containers/{0}'.format(self.name))
|
2016-06-04 07:11:15 +02:00
|
|
|
self.actions.append('delete')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _freeze_container(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
self._change_state('freeze')
|
|
|
|
self.actions.append('freeze')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _unfreeze_container(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
self._change_state('unfreeze')
|
|
|
|
self.actions.append('unfreez')
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _container_ipv4_addresses(self, ignore_devices=['lo']):
|
2016-06-04 07:11:15 +02:00
|
|
|
resp_json = self._get_container_state_json()
|
|
|
|
network = resp_json['metadata']['network'] or {}
|
2016-06-04 09:43:21 +02:00
|
|
|
network = dict((k, v) for k, v in network.items() if k not in ignore_devices) or {}
|
|
|
|
addresses = dict((k, [a['address'] for a in v['addresses'] if a['family'] == 'inet']) for k, v in network.items()) or {}
|
2016-05-10 17:48:36 +02:00
|
|
|
return addresses
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _has_all_ipv4_addresses(addresses):
|
|
|
|
return len(addresses) > 0 and all([len(v) > 0 for v in addresses.itervalues()])
|
|
|
|
|
|
|
|
def _get_addresses(self):
|
2016-06-30 17:03:46 +02:00
|
|
|
try:
|
|
|
|
due = datetime.datetime.now() + datetime.timedelta(seconds=self.timeout)
|
|
|
|
while datetime.datetime.now() < due:
|
|
|
|
time.sleep(1)
|
|
|
|
addresses = self._container_ipv4_addresses()
|
|
|
|
if self._has_all_ipv4_addresses(addresses):
|
|
|
|
self.addresses = addresses
|
|
|
|
return
|
|
|
|
except LXDClientException as e:
|
|
|
|
e.msg = 'timeout for getting IPv4 addresses'
|
|
|
|
raise
|
2016-05-10 17:48:36 +02:00
|
|
|
|
2016-06-04 07:11:15 +02:00
|
|
|
def _started(self):
|
|
|
|
if self.old_state == 'absent':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._create_container()
|
|
|
|
self._start_container()
|
|
|
|
else:
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state == 'frozen':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._unfreeze_container()
|
2016-06-04 09:43:21 +02:00
|
|
|
elif self.old_state == 'stopped':
|
|
|
|
self._start_container()
|
2016-06-28 17:24:56 +02:00
|
|
|
if self._needs_to_apply_container_configs():
|
|
|
|
self._apply_container_configs()
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.wait_for_ipv4_addresses:
|
|
|
|
self._get_addresses()
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _stopped(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state == 'absent':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._create_container()
|
|
|
|
else:
|
2016-06-04 09:43:21 +02:00
|
|
|
if self.old_state == 'stopped':
|
2016-06-28 17:24:56 +02:00
|
|
|
if self._needs_to_apply_container_configs():
|
2016-06-04 09:43:21 +02:00
|
|
|
self._start_container()
|
2016-06-28 17:24:56 +02:00
|
|
|
self._apply_container_configs()
|
2016-06-04 09:43:21 +02:00
|
|
|
self._stop_container()
|
|
|
|
else:
|
|
|
|
if self.old_state == 'frozen':
|
|
|
|
self._unfreeze_container()
|
2016-06-28 17:24:56 +02:00
|
|
|
if self._needs_to_apply_container_configs():
|
|
|
|
self._apply_container_configs()
|
2016-06-04 09:43:21 +02:00
|
|
|
self._stop_container()
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _restarted(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state == 'absent':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._create_container()
|
|
|
|
self._start_container()
|
|
|
|
else:
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state == 'frozen':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._unfreeze_container()
|
2016-06-28 17:24:56 +02:00
|
|
|
if self._needs_to_apply_container_configs():
|
|
|
|
self._apply_container_configs()
|
2016-06-04 07:11:15 +02:00
|
|
|
self._restart_container()
|
|
|
|
if self.wait_for_ipv4_addresses:
|
|
|
|
self._get_addresses()
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def _destroyed(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state != 'absent':
|
|
|
|
if self.old_state == 'frozen':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._unfreeze_container()
|
2016-06-04 07:11:15 +02:00
|
|
|
self._stop_container()
|
2016-05-10 17:48:36 +02:00
|
|
|
self._delete_container()
|
|
|
|
|
|
|
|
def _frozen(self):
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state == 'absent':
|
2016-05-10 17:48:36 +02:00
|
|
|
self._create_container()
|
|
|
|
self._start_container()
|
|
|
|
self._freeze_container()
|
|
|
|
else:
|
2016-06-04 07:11:15 +02:00
|
|
|
if self.old_state == 'stopped':
|
|
|
|
self._start_container()
|
2016-06-28 17:24:56 +02:00
|
|
|
if self._needs_to_apply_container_configs():
|
|
|
|
self._apply_container_configs()
|
2016-06-04 07:11:15 +02:00
|
|
|
self._freeze_container()
|
2016-05-10 17:48:36 +02:00
|
|
|
|
2016-06-28 17:24:56 +02:00
|
|
|
def _needs_to_change_container_config(self, key):
|
|
|
|
if key not in self.config:
|
2016-06-04 09:43:21 +02:00
|
|
|
return False
|
|
|
|
if key == 'config':
|
2016-06-25 11:04:29 +02:00
|
|
|
old_configs = dict((k, v) for k, v in self.old_container_json['metadata'][key].items() if not k.startswith('volatile.'))
|
2016-06-04 09:43:21 +02:00
|
|
|
else:
|
|
|
|
old_configs = self.old_container_json['metadata'][key]
|
2016-06-28 17:24:56 +02:00
|
|
|
return self.config[key] != old_configs
|
2016-06-04 09:43:21 +02:00
|
|
|
|
2016-06-28 17:24:56 +02:00
|
|
|
def _needs_to_apply_container_configs(self):
|
2016-06-04 09:43:21 +02:00
|
|
|
return (
|
2016-06-28 17:24:56 +02:00
|
|
|
self._needs_to_change_container_config('architecture') or
|
|
|
|
self._needs_to_change_container_config('config') or
|
|
|
|
self._needs_to_change_container_config('ephemeral') or
|
|
|
|
self._needs_to_change_container_config('devices') or
|
|
|
|
self._needs_to_change_container_config('profiles')
|
2016-06-04 09:43:21 +02:00
|
|
|
)
|
|
|
|
|
2016-06-28 17:24:56 +02:00
|
|
|
def _apply_container_configs(self):
|
2016-06-04 09:43:21 +02:00
|
|
|
old_metadata = self.old_container_json['metadata']
|
2016-06-26 01:30:38 +02:00
|
|
|
body_json = {
|
2016-06-04 09:43:21 +02:00
|
|
|
'architecture': old_metadata['architecture'],
|
|
|
|
'config': old_metadata['config'],
|
|
|
|
'devices': old_metadata['devices'],
|
|
|
|
'profiles': old_metadata['profiles']
|
|
|
|
}
|
2016-06-29 15:00:51 +02:00
|
|
|
if self._needs_to_change_container_config('architecture'):
|
2016-06-28 17:24:56 +02:00
|
|
|
body_json['architecture'] = self.config['architecture']
|
2016-06-29 15:00:51 +02:00
|
|
|
if self._needs_to_change_container_config('config'):
|
2016-06-28 17:24:56 +02:00
|
|
|
for k, v in self.config['config'].items():
|
2016-06-26 01:30:38 +02:00
|
|
|
body_json['config'][k] = v
|
2016-06-29 15:00:51 +02:00
|
|
|
if self._needs_to_change_container_config('ephemeral'):
|
2016-06-28 17:24:56 +02:00
|
|
|
body_json['ephemeral'] = self.config['ephemeral']
|
2016-06-29 15:00:51 +02:00
|
|
|
if self._needs_to_change_container_config('devices'):
|
2016-06-28 17:24:56 +02:00
|
|
|
body_json['devices'] = self.config['devices']
|
2016-06-29 15:00:51 +02:00
|
|
|
if self._needs_to_change_container_config('profiles'):
|
2016-06-28 17:24:56 +02:00
|
|
|
body_json['profiles'] = self.config['profiles']
|
|
|
|
self._operate_and_wait('PUT', '/1.0/containers/{0}'.format(self.name), body_json=body_json)
|
|
|
|
self.actions.append('apply_container_configs')
|
|
|
|
|
2016-05-10 17:48:36 +02:00
|
|
|
def run(self):
|
|
|
|
"""Run the main method."""
|
|
|
|
|
2016-06-30 17:03:46 +02:00
|
|
|
try:
|
|
|
|
if self.trust_password is not None:
|
|
|
|
self.client.authenticate(self.trust_password)
|
|
|
|
|
2016-06-30 17:42:50 +02:00
|
|
|
self.old_container_json = self._get_container_json()
|
|
|
|
self.old_state = self._container_json_to_module_state(self.old_container_json)
|
|
|
|
action = getattr(self, LXD_ANSIBLE_STATES[self.state])
|
|
|
|
action()
|
2016-06-30 17:03:46 +02:00
|
|
|
|
|
|
|
state_changed = len(self.actions) > 0
|
|
|
|
result_json = {
|
|
|
|
'changed': state_changed,
|
|
|
|
'old_state': self.old_state,
|
|
|
|
'actions': self.actions
|
|
|
|
}
|
|
|
|
if self.client.debug:
|
|
|
|
result_json['logs'] = self.client.logs
|
|
|
|
if self.addresses is not None:
|
|
|
|
result_json['addresses'] = self.addresses
|
|
|
|
self.module.exit_json(**result_json)
|
|
|
|
except LXDClientException as e:
|
|
|
|
state_changed = len(self.actions) > 0
|
|
|
|
fail_params = {
|
|
|
|
'msg': e.msg,
|
|
|
|
'changed': state_changed,
|
|
|
|
'actions': self.actions
|
|
|
|
}
|
|
|
|
if self.client.debug:
|
|
|
|
fail_params['logs'] = e.kwargs['logs']
|
|
|
|
self.module.fail_json(**fail_params)
|
2016-05-10 17:48:36 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Ansible Main module."""
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(
|
|
|
|
type='str',
|
|
|
|
required=True
|
|
|
|
),
|
2016-06-28 15:52:40 +02:00
|
|
|
architecture=dict(
|
|
|
|
type='str',
|
|
|
|
),
|
2016-05-10 17:48:36 +02:00
|
|
|
config=dict(
|
|
|
|
type='dict',
|
|
|
|
),
|
2016-06-28 17:24:56 +02:00
|
|
|
description=dict(
|
|
|
|
type='str',
|
|
|
|
),
|
2016-06-28 15:52:40 +02:00
|
|
|
devices=dict(
|
|
|
|
type='dict',
|
|
|
|
),
|
|
|
|
ephemeral=dict(
|
|
|
|
type='bool',
|
|
|
|
),
|
|
|
|
profiles=dict(
|
|
|
|
type='list',
|
|
|
|
),
|
|
|
|
source=dict(
|
|
|
|
type='dict',
|
|
|
|
),
|
2016-05-10 17:48:36 +02:00
|
|
|
state=dict(
|
2016-06-30 17:42:50 +02:00
|
|
|
choices=LXD_ANSIBLE_STATES.keys(),
|
2016-05-10 17:48:36 +02:00
|
|
|
default='started'
|
|
|
|
),
|
2016-06-04 07:11:15 +02:00
|
|
|
timeout=dict(
|
2016-05-10 17:48:36 +02:00
|
|
|
type='int',
|
2016-06-04 07:11:15 +02:00
|
|
|
default=30
|
|
|
|
),
|
|
|
|
wait_for_ipv4_addresses=dict(
|
|
|
|
type='bool',
|
2016-06-04 09:43:21 +02:00
|
|
|
default=False
|
2016-06-04 07:11:15 +02:00
|
|
|
),
|
|
|
|
force_stop=dict(
|
|
|
|
type='bool',
|
|
|
|
default=False
|
|
|
|
),
|
2016-06-29 18:01:07 +02:00
|
|
|
url=dict(
|
|
|
|
type='str',
|
2016-06-30 15:36:58 +02:00
|
|
|
default='unix:/var/lib/lxd/unix.socket'
|
2016-06-29 18:01:07 +02:00
|
|
|
),
|
|
|
|
key_file=dict(
|
|
|
|
type='str',
|
|
|
|
default='{}/.config/lxc/client.key'.format(os.environ['HOME'])
|
|
|
|
),
|
|
|
|
cert_file=dict(
|
|
|
|
type='str',
|
|
|
|
default='{}/.config/lxc/client.crt'.format(os.environ['HOME'])
|
|
|
|
),
|
|
|
|
trust_password=dict(
|
|
|
|
type='str',
|
2016-06-30 14:14:48 +02:00
|
|
|
),
|
|
|
|
debug=dict(
|
|
|
|
type='bool',
|
|
|
|
default=False
|
2016-05-10 17:48:36 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
supports_check_mode=False,
|
|
|
|
)
|
|
|
|
|
2016-06-30 17:42:50 +02:00
|
|
|
lxd_manage = LXDContainerManagement(module=module)
|
2016-05-10 17:48:36 +02:00
|
|
|
lxd_manage.run()
|
|
|
|
|
|
|
|
# import module bits
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|