2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2016 Matt Clay <matt@mystile.com>
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
2020-09-28 21:21:51 +02:00
|
|
|
author: Matt Clay (@mattclay) <matt@mystile.com>
|
2021-01-12 07:12:03 +01:00
|
|
|
name: lxd
|
2024-01-24 13:35:17 +01:00
|
|
|
short_description: Run tasks in LXD instances via C(lxc) CLI
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2024-01-24 13:35:17 +01:00
|
|
|
- Run commands or put/fetch files to an existing instance using C(lxc) CLI.
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
remote_addr:
|
|
|
|
description:
|
2023-10-25 08:47:27 +02:00
|
|
|
- Instance (container/VM) identifier.
|
|
|
|
- Since community.general 8.0.0, a FQDN can be provided; in that case, the first component (the part before C(.))
|
|
|
|
is used as the instance identifier.
|
2024-07-21 21:04:53 +02:00
|
|
|
type: string
|
2020-03-09 10:11:07 +01:00
|
|
|
default: inventory_hostname
|
|
|
|
vars:
|
2022-07-07 21:49:10 +02:00
|
|
|
- name: inventory_hostname
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: ansible_host
|
|
|
|
- name: ansible_lxd_host
|
|
|
|
executable:
|
|
|
|
description:
|
2024-01-24 13:35:17 +01:00
|
|
|
- Shell to use for execution inside instance.
|
2024-07-21 21:04:53 +02:00
|
|
|
type: string
|
2020-03-09 10:11:07 +01:00
|
|
|
default: /bin/sh
|
|
|
|
vars:
|
|
|
|
- name: ansible_executable
|
|
|
|
- name: ansible_lxd_executable
|
2021-01-04 06:30:47 +01:00
|
|
|
remote:
|
|
|
|
description:
|
|
|
|
- Name of the LXD remote to use.
|
2024-07-21 21:04:53 +02:00
|
|
|
type: string
|
2021-01-04 06:30:47 +01:00
|
|
|
default: local
|
|
|
|
vars:
|
|
|
|
- name: ansible_lxd_remote
|
|
|
|
version_added: 2.0.0
|
|
|
|
project:
|
|
|
|
description:
|
|
|
|
- Name of the LXD project to use.
|
2024-07-21 21:04:53 +02:00
|
|
|
type: string
|
2021-01-04 06:30:47 +01:00
|
|
|
vars:
|
|
|
|
- name: ansible_lxd_project
|
|
|
|
version_added: 2.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound
|
2022-01-04 06:56:28 +01:00
|
|
|
from ansible.module_utils.common.process import get_bin_path
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.plugins.connection import ConnectionBase
|
|
|
|
|
|
|
|
|
|
|
|
class Connection(ConnectionBase):
|
|
|
|
""" lxd based connections """
|
|
|
|
|
|
|
|
transport = 'community.general.lxd'
|
|
|
|
has_pipelining = True
|
|
|
|
default_user = 'root'
|
|
|
|
|
|
|
|
def __init__(self, play_context, new_stdin, *args, **kwargs):
|
|
|
|
super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
|
|
|
|
|
2022-01-04 06:56:28 +01:00
|
|
|
try:
|
|
|
|
self._lxc_cmd = get_bin_path("lxc")
|
|
|
|
except ValueError:
|
2020-03-09 10:11:07 +01:00
|
|
|
raise AnsibleError("lxc command not found in PATH")
|
|
|
|
|
|
|
|
if self._play_context.remote_user is not None and self._play_context.remote_user != 'root':
|
2024-01-24 13:35:17 +01:00
|
|
|
self._display.warning('lxd does not support remote_user, using default: root')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-10-25 08:47:27 +02:00
|
|
|
def _host(self):
|
|
|
|
""" translate remote_addr to lxd (short) hostname """
|
|
|
|
return self.get_option("remote_addr").split(".", 1)[0]
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def _connect(self):
|
|
|
|
"""connect to lxd (nothing to do here) """
|
|
|
|
super(Connection, self)._connect()
|
|
|
|
|
|
|
|
if not self._connected:
|
2023-10-25 08:47:27 +02:00
|
|
|
self._display.vvv(u"ESTABLISH LXD CONNECTION FOR USER: root", host=self._host())
|
2020-03-09 10:11:07 +01:00
|
|
|
self._connected = True
|
|
|
|
|
|
|
|
def exec_command(self, cmd, in_data=None, sudoable=True):
|
|
|
|
""" execute a command on the lxd host """
|
|
|
|
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
|
|
|
|
2023-10-25 08:47:27 +02:00
|
|
|
self._display.vvv(u"EXEC {0}".format(cmd), host=self._host())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-01-04 06:30:47 +01:00
|
|
|
local_cmd = [self._lxc_cmd]
|
|
|
|
if self.get_option("project"):
|
|
|
|
local_cmd.extend(["--project", self.get_option("project")])
|
|
|
|
local_cmd.extend([
|
|
|
|
"exec",
|
2023-10-25 08:47:27 +02:00
|
|
|
"%s:%s" % (self.get_option("remote"), self._host()),
|
2021-01-04 06:30:47 +01:00
|
|
|
"--",
|
2021-12-09 21:18:39 +01:00
|
|
|
self.get_option("executable"), "-c", cmd
|
2021-01-04 06:30:47 +01:00
|
|
|
])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-12-03 09:41:04 +01:00
|
|
|
self._display.vvvvv(u"EXEC {0}".format(local_cmd), host=self._host())
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
|
|
|
in_data = to_bytes(in_data, errors='surrogate_or_strict', nonstring='passthru')
|
|
|
|
|
|
|
|
process = Popen(local_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
|
|
|
stdout, stderr = process.communicate(in_data)
|
|
|
|
|
|
|
|
stdout = to_text(stdout)
|
|
|
|
stderr = to_text(stderr)
|
|
|
|
|
2023-12-03 09:41:04 +01:00
|
|
|
self._display.vvvvv(u"EXEC lxc output: {0} {1}".format(stdout, stderr), host=self._host())
|
|
|
|
|
2023-10-25 08:47:27 +02:00
|
|
|
if "is not running" in stderr:
|
|
|
|
raise AnsibleConnectionFailure("instance not running: %s" % self._host())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-12-03 09:41:04 +01:00
|
|
|
if stderr.strip() == "Error: Instance not found" or stderr.strip() == "error: not found":
|
2023-10-25 08:47:27 +02:00
|
|
|
raise AnsibleConnectionFailure("instance not found: %s" % self._host())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
return process.returncode, stdout, stderr
|
|
|
|
|
|
|
|
def put_file(self, in_path, out_path):
|
|
|
|
""" put a file from local to lxd """
|
|
|
|
super(Connection, self).put_file(in_path, out_path)
|
|
|
|
|
2023-10-25 08:47:27 +02:00
|
|
|
self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._host())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not os.path.isfile(to_bytes(in_path, errors='surrogate_or_strict')):
|
|
|
|
raise AnsibleFileNotFound("input path is not a file: %s" % in_path)
|
|
|
|
|
2021-01-04 06:30:47 +01:00
|
|
|
local_cmd = [self._lxc_cmd]
|
|
|
|
if self.get_option("project"):
|
|
|
|
local_cmd.extend(["--project", self.get_option("project")])
|
|
|
|
local_cmd.extend([
|
|
|
|
"file", "push",
|
|
|
|
in_path,
|
2023-10-25 08:47:27 +02:00
|
|
|
"%s:%s/%s" % (self.get_option("remote"), self._host(), out_path)
|
2021-01-04 06:30:47 +01:00
|
|
|
])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
|
|
|
|
|
|
|
process = Popen(local_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
|
|
|
process.communicate()
|
|
|
|
|
|
|
|
def fetch_file(self, in_path, out_path):
|
|
|
|
""" fetch a file from lxd to local """
|
|
|
|
super(Connection, self).fetch_file(in_path, out_path)
|
|
|
|
|
2023-10-25 08:47:27 +02:00
|
|
|
self._display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self._host())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-01-04 06:30:47 +01:00
|
|
|
local_cmd = [self._lxc_cmd]
|
|
|
|
if self.get_option("project"):
|
|
|
|
local_cmd.extend(["--project", self.get_option("project")])
|
|
|
|
local_cmd.extend([
|
|
|
|
"file", "pull",
|
2023-10-25 08:47:27 +02:00
|
|
|
"%s:%s/%s" % (self.get_option("remote"), self._host(), in_path),
|
2021-01-04 06:30:47 +01:00
|
|
|
out_path
|
|
|
|
])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
|
|
|
|
|
|
|
process = Popen(local_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
|
|
|
process.communicate()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
""" close the connection (nothing to do here) """
|
|
|
|
super(Connection, self).close()
|
|
|
|
|
|
|
|
self._connected = False
|