collection 교체

This commit is contained in:
정훈 변
2024-02-23 16:37:40 +09:00
parent b494779b5b
commit 3fd554eee9
38862 changed files with 220204 additions and 6600073 deletions

View File

@@ -1,8 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2017, Jasper Lievisse Adriaanse <j@jasper.la>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Copyright (c) 2017, Jasper Lievisse Adriaanse <j@jasper.la>
# 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
from __future__ import absolute_import, division, print_function
__metaclass__ = type
@@ -11,10 +12,17 @@ __metaclass__ = type
DOCUMENTATION = '''
---
module: vmadm
short_description: Manage SmartOS virtual machines and zones.
short_description: Manage SmartOS virtual machines and zones
description:
- Manage SmartOS virtual machines through vmadm(1M).
author: Jasper Lievisse Adriaanse (@jasperla)
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
options:
archive_on_delete:
required: false
@@ -31,7 +39,7 @@ options:
choices: [ joyent, joyent-minimal, lx, kvm, bhyve ]
default: joyent
description:
- Type of virtual machine. The C(bhyve) option was added in community.general 0.2.0.
- Type of virtual machine. The V(bhyve) option was added in community.general 0.2.0.
type: str
boot:
required: false
@@ -42,7 +50,7 @@ options:
required: false
description:
- Sets a limit on the amount of CPU time that can be used by a VM.
Use C(0) for no cap.
Use V(0) for no cap.
type: int
cpu_shares:
required: false
@@ -87,7 +95,7 @@ options:
docker:
required: false
description:
- Docker images need this flag enabled along with the I(brand) set to C(lx).
- Docker images need this flag enabled along with the O(brand) set to C(lx).
type: bool
filesystems:
required: false
@@ -139,8 +147,8 @@ options:
internal_metadata_namespace:
required: false
description:
- List of namespaces to be set as I(internal_metadata-only); these namespaces
will come from I(internal_metadata) rather than I(customer_metadata).
- List of namespaces to be set as C(internal_metadata-only); these namespaces
will come from O(internal_metadata) rather than O(customer_metadata).
type: str
kernel_version:
required: false
@@ -156,7 +164,7 @@ options:
required: false
description:
- Resolvers in C(/etc/resolv.conf) will be updated when updating
the I(resolvers) property.
the O(resolvers) property.
type: bool
max_locked_memory:
required: false
@@ -255,11 +263,11 @@ options:
choices: [ present, running, absent, deleted, stopped, created, restarted, rebooted ]
default: running
description:
- States for the VM to be in. Please note that C(present), C(stopped) and C(restarted)
operate on a VM that is currently provisioned. C(present) means that the VM will be
created if it was absent, and that it will be in a running state. C(absent) will
- States for the VM to be in. Please note that V(present), V(stopped) and V(restarted)
operate on a VM that is currently provisioned. V(present) means that the VM will be
created if it was absent, and that it will be in a running state. V(absent) will
shutdown the zone before removing it.
C(stopped) means the zone will be created if it doesn't exist already, before shutting
V(stopped) means the zone will be created if it does not exist already, before shutting
it down.
type: str
tmpfs:
@@ -270,7 +278,7 @@ options:
uuid:
required: false
description:
- UUID of the VM. Can either be a full UUID or C(*) for all VMs.
- UUID of the VM. Can either be a full UUID or V(*) for all VMs.
type: str
vcpus:
required: false
@@ -301,8 +309,8 @@ options:
vnc_port:
required: false
description:
- TCP port to listen of the VNC server. Or set C(0) for random,
or C(-1) to disable.
- TCP port to listen of the VNC server. Or set V(0) for random,
or V(-1) to disable.
type: int
zfs_data_compression:
required: false
@@ -346,8 +354,6 @@ options:
description:
- ZFS pool the VM's zone dataset will be created in.
type: str
requirements:
- python >= 2.6
'''
EXAMPLES = '''
@@ -357,8 +363,8 @@ EXAMPLES = '''
state: present
alias: fw_zone
image_uuid: 95f265b8-96b2-11e6-9597-972f3af4b6d5
firewall_enabled: yes
indestructible_zoneroot: yes
firewall_enabled: true
indestructible_zoneroot: true
nics:
- nic_tag: admin
ip: dhcp
@@ -415,7 +421,7 @@ from ansible.module_utils.common.text.converters import to_native
def get_vm_prop(module, uuid, prop):
# Lookup a property for the given VM.
# Returns the property, or None if not found.
cmd = '{0} lookup -j -o {1} uuid={2}'.format(module.vmadm, prop, uuid)
cmd = [module.vmadm, 'lookup', '-j', '-o', prop, 'uuid={0}'.format(uuid)]
(rc, stdout, stderr) = module.run_command(cmd)
@@ -430,16 +436,14 @@ def get_vm_prop(module, uuid, prop):
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(prop),
details=to_native(e), exception=traceback.format_exc())
if len(stdout_json) > 0 and prop in stdout_json[0]:
return stdout_json[0][prop]
else:
return None
if stdout_json:
return stdout_json[0].get(prop)
def get_vm_uuid(module, alias):
# Lookup the uuid that goes with the given alias.
# Returns the uuid or '' if not found.
cmd = '{0} lookup -j -o uuid alias={1}'.format(module.vmadm, alias)
cmd = [module.vmadm, 'lookup', '-j', '-o', 'uuid', 'alias={0}'.format(alias)]
(rc, stdout, stderr) = module.run_command(cmd)
@@ -450,23 +454,20 @@ def get_vm_uuid(module, alias):
# If no VM was found matching the given alias, we get back an empty array.
# That is not an error condition as we might be explicitly checking it's
# absence.
if stdout.strip() == '[]':
return None
else:
try:
stdout_json = json.loads(stdout)
except Exception as e:
module.fail_json(
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(alias),
details=to_native(e), exception=traceback.format_exc())
try:
stdout_json = json.loads(stdout)
except Exception as e:
module.fail_json(
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(alias),
details=to_native(e), exception=traceback.format_exc())
if len(stdout_json) > 0 and 'uuid' in stdout_json[0]:
return stdout_json[0]['uuid']
if stdout_json:
return stdout_json[0].get('uuid')
def get_all_vm_uuids(module):
# Retrieve the UUIDs for all VMs.
cmd = '{0} lookup -j -o uuid'.format(module.vmadm)
cmd = [module.vmadm, 'lookup', '-j', '-o', 'uuid']
(rc, stdout, stderr) = module.run_command(cmd)
@@ -484,7 +485,7 @@ def get_all_vm_uuids(module):
def new_vm(module, uuid, vm_state):
payload_file = create_payload(module, uuid)
(rc, stdout, stderr) = vmadm_create_vm(module, payload_file)
(rc, dummy, stderr) = vmadm_create_vm(module, payload_file)
if rc != 0:
changed = False
@@ -519,7 +520,7 @@ def new_vm(module, uuid, vm_state):
def vmadm_create_vm(module, payload_file):
# Create a new VM using the provided payload.
cmd = '{0} create -f {1}'.format(module.vmadm, payload_file)
cmd = [module.vmadm, 'create', '-f', payload_file]
return module.run_command(cmd)
@@ -541,27 +542,22 @@ def set_vm_state(module, vm_uuid, vm_state):
'rebooted': ['reboot', False]
}
if p['force'] and cmds[vm_state][1]:
force = '-F'
else:
force = ''
command, forceable = cmds[vm_state]
force = ['-F'] if p['force'] and forceable else []
cmd = 'vmadm {0} {1} {2}'.format(cmds[vm_state][0], force, vm_uuid)
cmd = [module.vmadm, command] + force + [vm_uuid]
(rc, stdout, stderr) = module.run_command(cmd)
(dummy, dummy, stderr) = module.run_command(cmd)
match = re.match('^Successfully.*', stderr)
if match:
return True
else:
return False
return match is not None
def create_payload(module, uuid):
# Create the JSON payload (vmdef) and return the filename.
# Filter out the few options that are not valid VM properties.
module_options = ['debug', 'force', 'state']
module_options = ['force', 'state']
# @TODO make this a simple {} comprehension as soon as py2 is ditched
# @TODO {k: v for k, v in p.items() if k not in module_options}
vmdef = dict([(k, v) for k, v in module.params.items() if k not in module_options and v])
@@ -601,23 +597,17 @@ def vm_state_transition(module, uuid, vm_state):
def is_valid_uuid(uuid):
if re.match('^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$', uuid, re.IGNORECASE):
return True
else:
return False
return re.match('^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$', uuid, re.IGNORECASE) is not None
def validate_uuids(module):
# Perform basic UUID validation.
failed = []
failed = [
name
for name, pvalue in [(x, module.params[x]) for x in ['uuid', 'image_uuid']]
if pvalue and pvalue != '*' and not is_valid_uuid(pvalue)
]
for u in [['uuid', module.params['uuid']],
['image_uuid', module.params['image_uuid']]]:
if u[1] and u[1] != '*':
if not is_valid_uuid(u[1]):
failed.append(u[0])
if len(failed) > 0:
if failed:
module.fail_json(msg='No valid UUID(s) found for: {0}'.format(", ".join(failed)))
@@ -642,7 +632,7 @@ def manage_all_vms(module, vm_state):
if (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state):
any_changed = True
else:
any_changed = (vm_state_transition(module, uuid, vm_state) | any_changed)
any_changed = vm_state_transition(module, uuid, vm_state) or any_changed
return any_changed
@@ -661,7 +651,7 @@ def main():
'zfs_root_compression', 'zpool'
],
'bool': [
'archive_on_delete', 'autoboot', 'debug', 'delegate_dataset',
'archive_on_delete', 'autoboot', 'delegate_dataset',
'docker', 'firewall_enabled', 'force', 'indestructible_delegated',
'indestructible_zoneroot', 'maintain_resolvers', 'nowait'
],
@@ -783,14 +773,9 @@ def main():
elif module.check_mode:
# Shortcut for check mode, if there is no VM yet, it will need to be created.
# Or, if the VM is not in the desired state yet, it needs to transition.
if (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state):
result['changed'] = True
else:
result['changed'] = False
module.exit_json(**result)
# No VM was found that matched the given ID (alias or uuid), so we create it.
result['changed'] = (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state)
elif not current_vm_state:
# No VM was found that matched the given ID (alias or uuid), so we create it.
result['changed'], result['uuid'] = new_vm(module, uuid, vm_state)
else:
# VM was found, operate on its state directly.