mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
A few more organizational cleanups from the repo merge
This commit is contained in:
committed by
Matt Clay
parent
011ea55a8f
commit
14833f1c7a
191
lib/ansible/modules/cloud/misc/serverless.py
Normal file
191
lib/ansible/modules/cloud/misc/serverless.py
Normal file
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# (c) 2016, Ryan Scott Brown <ryansb@redhat.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/>.
|
||||
#
|
||||
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: serverless
|
||||
short_description: Manages a Serverless Framework project
|
||||
description:
|
||||
- Provides support for managing Serverless Framework (https://serverless.com/) project deployments and stacks.
|
||||
version_added: "2.3"
|
||||
options:
|
||||
state:
|
||||
choices: ['present', 'absent']
|
||||
description:
|
||||
- Goal state of given stage/project
|
||||
required: false
|
||||
default: present
|
||||
service_path:
|
||||
description:
|
||||
- The path to the root of the Serverless Service to be operated on.
|
||||
required: true
|
||||
functions:
|
||||
description:
|
||||
- A list of specific functions to deploy. If this is not provided, all functions in the service will be deployed.
|
||||
required: false
|
||||
default: []
|
||||
region:
|
||||
description:
|
||||
- AWS region to deploy the service to
|
||||
required: false
|
||||
default: us-east-1
|
||||
deploy:
|
||||
description:
|
||||
- Whether or not to deploy artifacts after building them. When this option is `false` all the functions will be built, but no stack update will be run to send them out. This is mostly useful for generating artifacts to be stored/deployed elsewhere.
|
||||
required: false
|
||||
default: true
|
||||
notes:
|
||||
- Currently, the `serverless` command must be in the path of the node executing the task. In the future this may be a flag.
|
||||
requirements: [ "serverless" ]
|
||||
author: "Ryan Scott Brown @ryansb"
|
||||
'''
|
||||
|
||||
EXAMPLES = """
|
||||
# Basic deploy of a service
|
||||
- serverless:
|
||||
service_path: '{{ project_dir }}'
|
||||
state: present
|
||||
|
||||
# Deploy specific functions
|
||||
- serverless:
|
||||
service_path: '{{ project_dir }}'
|
||||
functions:
|
||||
- my_func_one
|
||||
- my_func_two
|
||||
|
||||
# deploy a project, then pull its resource list back into Ansible
|
||||
- serverless:
|
||||
stage: dev
|
||||
region: us-east-1
|
||||
service_path: '{{ project_dir }}'
|
||||
register: sls
|
||||
# The cloudformation stack is always named the same as the full service, so the
|
||||
# cloudformation_facts module can get a full list of the stack resources, as
|
||||
# well as stack events and outputs
|
||||
- cloudformation_facts:
|
||||
region: us-east-1
|
||||
stack_name: '{{ sls.service_name }}'
|
||||
stack_resources: true
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
service_name:
|
||||
type: string
|
||||
description: Most
|
||||
returned: always
|
||||
sample: my-fancy-service-dev
|
||||
state:
|
||||
type: string
|
||||
description: Whether the stack for the serverless project is present/absent.
|
||||
returned: always
|
||||
command:
|
||||
type: string
|
||||
description: Full `serverless` command run by this module, in case you want to re-run the command outside the module.
|
||||
returned: always
|
||||
sample: serverless deploy --stage production
|
||||
"""
|
||||
|
||||
|
||||
import os
|
||||
import traceback
|
||||
import yaml
|
||||
|
||||
|
||||
def read_serverless_config(module):
|
||||
path = os.path.expanduser(module.params.get('service_path'))
|
||||
|
||||
try:
|
||||
with open(os.path.join(path, 'serverless.yml')) as sls_config:
|
||||
config = yaml.safe_load(sls_config.read())
|
||||
return config
|
||||
except IOError as e:
|
||||
module.fail_json(msg="Could not open serverless.yml in {}. err: {}".format(path, str(e)), exception=traceback.format_exc())
|
||||
|
||||
module.fail_json(msg="Failed to open serverless config at {}".format(
|
||||
os.path.join(path, 'serverless.yml')))
|
||||
|
||||
|
||||
def get_service_name(module, stage):
|
||||
config = read_serverless_config(module)
|
||||
if config.get('service') is None:
|
||||
module.fail_json(msg="Could not read `service` key from serverless.yml file")
|
||||
|
||||
if stage:
|
||||
return "{}-{}".format(config['service'], stage)
|
||||
|
||||
return "{}-{}".format(config['service'], config.get('stage', 'dev'))
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
service_path = dict(required=True),
|
||||
state = dict(default='present', choices=['present', 'absent'], required=False),
|
||||
functions = dict(type='list', required=False),
|
||||
region = dict(default='', required=False),
|
||||
stage = dict(default='', required=False),
|
||||
deploy = dict(default=True, type='bool', required=False),
|
||||
),
|
||||
)
|
||||
|
||||
service_path = os.path.expanduser(module.params.get('service_path'))
|
||||
state = module.params.get('state')
|
||||
functions = module.params.get('functions')
|
||||
region = module.params.get('region')
|
||||
stage = module.params.get('stage')
|
||||
deploy = module.params.get('deploy', True)
|
||||
|
||||
command = "serverless "
|
||||
if state == 'present':
|
||||
command += 'deploy '
|
||||
elif state == 'absent':
|
||||
command += 'remove '
|
||||
else:
|
||||
module.fail_json(msg="State must either be 'present' or 'absent'. Received: {}".format(state))
|
||||
|
||||
if not deploy and state == 'present':
|
||||
command += '--noDeploy '
|
||||
if region:
|
||||
command += '--region {} '.format(region)
|
||||
if stage:
|
||||
command += '--stage {} '.format(stage)
|
||||
|
||||
rc, out, err = module.run_command(command, cwd=service_path)
|
||||
if rc != 0:
|
||||
if state == 'absent' and "-{}' does not exist".format(stage) in out:
|
||||
module.exit_json(changed=False, state='absent', command=command,
|
||||
out=out, service_name=get_service_name(module, stage))
|
||||
|
||||
module.fail_json(msg="Failure when executing Serverless command. Exited {}.\nstdout: {}\nstderr: {}".format(rc, out, err))
|
||||
|
||||
# gather some facts about the deployment
|
||||
module.exit_json(changed=True, state='present', out=out, command=command,
|
||||
service_name=get_service_name(module, stage))
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
209
lib/ansible/modules/cloud/misc/xenserver_facts.py
Normal file
209
lib/ansible/modules/cloud/misc/xenserver_facts.py
Normal file
@@ -0,0 +1,209 @@
|
||||
#!/usr/bin/python -tt
|
||||
# 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/>.
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: xenserver_facts
|
||||
version_added: "2.0"
|
||||
short_description: get facts reported on xenserver
|
||||
description:
|
||||
- Reads data out of XenAPI, can be used instead of multiple xe commands.
|
||||
author:
|
||||
- Andy Hill (@andyhky)
|
||||
- Tim Rupp
|
||||
'''
|
||||
|
||||
import platform
|
||||
|
||||
HAVE_XENAPI = False
|
||||
try:
|
||||
import XenAPI
|
||||
HAVE_XENAPI = True
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Gather facts from xenserver
|
||||
xenserver:
|
||||
|
||||
- name: Print running VMs
|
||||
debug: msg="{{ item }}"
|
||||
with_items: "{{ xs_vms.keys() }}"
|
||||
when: xs_vms[item]['power_state'] == "Running"
|
||||
|
||||
TASK: [Print running VMs] ***********************************************************
|
||||
skipping: [10.13.0.22] => (item=CentOS 4.7 (32-bit))
|
||||
ok: [10.13.0.22] => (item=Control domain on host: 10.0.13.22) => {
|
||||
"item": "Control domain on host: 10.0.13.22",
|
||||
"msg": "Control domain on host: 10.0.13.22"
|
||||
}
|
||||
'''
|
||||
|
||||
class XenServerFacts:
|
||||
def __init__(self):
|
||||
self.codes = {
|
||||
'5.5.0': 'george',
|
||||
'5.6.100': 'oxford',
|
||||
'6.0.0': 'boston',
|
||||
'6.1.0': 'tampa',
|
||||
'6.2.0': 'clearwater'
|
||||
}
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
# Be aware! Deprecated in Python 2.6!
|
||||
result = platform.dist()[1]
|
||||
return result
|
||||
|
||||
@property
|
||||
def codename(self):
|
||||
if self.version in self.codes:
|
||||
result = self.codes[self.version]
|
||||
else:
|
||||
result = None
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_xenapi_session():
|
||||
session = XenAPI.xapi_local()
|
||||
session.xenapi.login_with_password('', '')
|
||||
return session
|
||||
|
||||
|
||||
def get_networks(session):
|
||||
recs = session.xenapi.network.get_all_records()
|
||||
xs_networks = {}
|
||||
networks = change_keys(recs, key='uuid')
|
||||
for network in networks.itervalues():
|
||||
xs_networks[network['name_label']] = network
|
||||
return xs_networks
|
||||
|
||||
|
||||
def get_pifs(session):
|
||||
recs = session.xenapi.PIF.get_all_records()
|
||||
pifs = change_keys(recs, key='uuid')
|
||||
xs_pifs = {}
|
||||
devicenums = range(0, 7)
|
||||
for pif in pifs.itervalues():
|
||||
for eth in devicenums:
|
||||
interface_name = "eth%s" % (eth)
|
||||
bond_name = interface_name.replace('eth', 'bond')
|
||||
if pif['device'] == interface_name:
|
||||
xs_pifs[interface_name] = pif
|
||||
elif pif['device'] == bond_name:
|
||||
xs_pifs[bond_name] = pif
|
||||
return xs_pifs
|
||||
|
||||
|
||||
def get_vlans(session):
|
||||
recs = session.xenapi.VLAN.get_all_records()
|
||||
return change_keys(recs, key='tag')
|
||||
|
||||
|
||||
def change_keys(recs, key='uuid', filter_func=None):
|
||||
"""
|
||||
Take a xapi dict, and make the keys the value of recs[ref][key].
|
||||
|
||||
Preserves the ref in rec['ref']
|
||||
|
||||
"""
|
||||
new_recs = {}
|
||||
|
||||
for ref, rec in recs.iteritems():
|
||||
if filter_func is not None and not filter_func(rec):
|
||||
continue
|
||||
|
||||
new_recs[rec[key]] = rec
|
||||
new_recs[rec[key]]['ref'] = ref
|
||||
|
||||
return new_recs
|
||||
|
||||
def get_host(session):
|
||||
"""Get the host"""
|
||||
host_recs = session.xenapi.host.get_all()
|
||||
# We only have one host, so just return its entry
|
||||
return session.xenapi.host.get_record(host_recs[0])
|
||||
|
||||
def get_vms(session):
|
||||
xs_vms = {}
|
||||
recs = session.xenapi.VM.get_all()
|
||||
if not recs:
|
||||
return None
|
||||
|
||||
vms = change_keys(recs, key='uuid')
|
||||
for vm in vms.itervalues():
|
||||
xs_vms[vm['name_label']] = vm
|
||||
return xs_vms
|
||||
|
||||
|
||||
def get_srs(session):
|
||||
xs_srs = {}
|
||||
recs = session.xenapi.SR.get_all()
|
||||
if not recs:
|
||||
return None
|
||||
srs = change_keys(recs, key='uuid')
|
||||
for sr in srs.itervalues():
|
||||
xs_srs[sr['name_label']] = sr
|
||||
return xs_srs
|
||||
|
||||
def main():
|
||||
module = AnsibleModule({})
|
||||
|
||||
if not HAVE_XENAPI:
|
||||
module.fail_json(changed=False, msg="python xen api required for this module")
|
||||
|
||||
obj = XenServerFacts()
|
||||
try:
|
||||
session = get_xenapi_session()
|
||||
except XenAPI.Failure as e:
|
||||
module.fail_json(msg='%s' % e)
|
||||
|
||||
data = {
|
||||
'xenserver_version': obj.version,
|
||||
'xenserver_codename': obj.codename
|
||||
}
|
||||
|
||||
xs_networks = get_networks(session)
|
||||
xs_pifs = get_pifs(session)
|
||||
xs_vlans = get_vlans(session)
|
||||
xs_vms = get_vms(session)
|
||||
xs_srs = get_srs(session)
|
||||
|
||||
if xs_vlans:
|
||||
data['xs_vlans'] = xs_vlans
|
||||
if xs_pifs:
|
||||
data['xs_pifs'] = xs_pifs
|
||||
if xs_networks:
|
||||
data['xs_networks'] = xs_networks
|
||||
|
||||
if xs_vms:
|
||||
data['xs_vms'] = xs_vms
|
||||
|
||||
if xs_srs:
|
||||
data['xs_srs'] = xs_srs
|
||||
|
||||
module.exit_json(ansible=data)
|
||||
|
||||
from ansible.module_utils.basic import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user