mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fix last bugs, complete all tests.
This commit is contained in:
parent
17a55d6adf
commit
2eaf5ee206
1 changed files with 96 additions and 66 deletions
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python -tt
|
#!/usr/bin/python
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
@ -33,11 +33,19 @@ options:
|
||||||
default: null
|
default: null
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
- Name to give the instance
|
- Name to give the instance (alphanumeric, dashes, underscore)
|
||||||
|
- To keep sanity on the Linode Web Console, name is prepended with LinodeID_
|
||||||
default: null
|
default: null
|
||||||
flavor:
|
type: string
|
||||||
|
linode_id:
|
||||||
description:
|
description:
|
||||||
- flavor to use for the instance (Linode plan)
|
- Unique ID of a linode server
|
||||||
|
aliases: lid
|
||||||
|
default: null
|
||||||
|
type: integer
|
||||||
|
plan:
|
||||||
|
description:
|
||||||
|
- plan to use for the instance (Linode plan)
|
||||||
default: null
|
default: null
|
||||||
type: integer
|
type: integer
|
||||||
payment_term:
|
payment_term:
|
||||||
|
@ -61,14 +69,14 @@ options:
|
||||||
- swap size in MB
|
- swap size in MB
|
||||||
default: 512
|
default: 512
|
||||||
type: integer
|
type: integer
|
||||||
image:
|
distribution:
|
||||||
description:
|
description:
|
||||||
- image to use for the instance (Linode Distribution)
|
- distribution to use for the instance (Linode Distribution)
|
||||||
default: null
|
default: null
|
||||||
type: integer
|
type: integer
|
||||||
region:
|
datacenter:
|
||||||
description:
|
description:
|
||||||
- Region to create an instance in (Linode Datacenter)
|
- datacenter to create an instance in (Linode Datacenter)
|
||||||
default: null
|
default: null
|
||||||
type: integer
|
type: integer
|
||||||
wait:
|
wait:
|
||||||
|
@ -92,9 +100,9 @@ EXAMPLES = '''
|
||||||
module: linode
|
module: linode
|
||||||
api_key: 'longStringFromLinodeApi'
|
api_key: 'longStringFromLinodeApi'
|
||||||
name: linode-test1
|
name: linode-test1
|
||||||
flavor: 1
|
plan: 1
|
||||||
region: 2
|
datacenter: 2
|
||||||
image: 99
|
distribution: 99
|
||||||
password: 'superSecureRootPassword'
|
password: 'superSecureRootPassword'
|
||||||
ssh_pub_key: 'ssh-rsa qwerty'
|
ssh_pub_key: 'ssh-rsa qwerty'
|
||||||
swap: 768
|
swap: 768
|
||||||
|
@ -108,9 +116,9 @@ EXAMPLES = '''
|
||||||
api_key: 'longStringFromLinodeApi'
|
api_key: 'longStringFromLinodeApi'
|
||||||
name: linode-test1
|
name: linode-test1
|
||||||
linode_id: 12345678
|
linode_id: 12345678
|
||||||
flavor: 1
|
plan: 1
|
||||||
region: 2
|
datacenter: 2
|
||||||
image: 99
|
distribution: 99
|
||||||
password: 'superSecureRootPassword'
|
password: 'superSecureRootPassword'
|
||||||
ssh_pub_key: 'ssh-rsa qwerty'
|
ssh_pub_key: 'ssh-rsa qwerty'
|
||||||
swap: 768
|
swap: 768
|
||||||
|
@ -148,6 +156,9 @@ import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# linode module raise warning due to ssl - silently ignore them ...
|
||||||
|
import warnings
|
||||||
|
warnings.simplefilter("ignore")
|
||||||
from linode import api as linode_api
|
from linode import api as linode_api
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("failed=True msg='linode-python required for this module'")
|
print("failed=True msg='linode-python required for this module'")
|
||||||
|
@ -181,7 +192,7 @@ def getInstanceDetails(api, server):
|
||||||
|
|
||||||
# Populate with ips
|
# Populate with ips
|
||||||
for ip in api.linode_ip_list(LinodeId=server['LINODEID']):
|
for ip in api.linode_ip_list(LinodeId=server['LINODEID']):
|
||||||
if ip['ISPUBLIC'] and not instance['ipv4']:
|
if ip['ISPUBLIC'] and 'ipv4' not in instance:
|
||||||
instance['ipv4'] = ip['IPADDRESS']
|
instance['ipv4'] = ip['IPADDRESS']
|
||||||
instance['fqdn'] = ip['RDNS_NAME']
|
instance['fqdn'] = ip['RDNS_NAME']
|
||||||
if ip['ISPUBLIC']:
|
if ip['ISPUBLIC']:
|
||||||
|
@ -194,7 +205,7 @@ def getInstanceDetails(api, server):
|
||||||
'ip_id': ip['IPADDRESSID']})
|
'ip_id': ip['IPADDRESSID']})
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
def linodeServers(module, api, state, name, plan, distribution, datacenter, linode_id,
|
||||||
payment_term, password, ssh_pub_key, swap, wait, wait_timeout):
|
payment_term, password, ssh_pub_key, swap, wait, wait_timeout):
|
||||||
instances = []
|
instances = []
|
||||||
changed = False
|
changed = False
|
||||||
|
@ -209,12 +220,15 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
# For the moment we only consider linode_id as criteria for match
|
# For the moment we only consider linode_id as criteria for match
|
||||||
# Later we can use more (size, name, etc.) and update existing
|
# Later we can use more (size, name, etc.) and update existing
|
||||||
servers = api.linode_list(LinodeId=linode_id)
|
servers = api.linode_list(LinodeId=linode_id)
|
||||||
disks = api.linode_disk_list(LinodeId=linode_id)
|
# Attempt to fetch details about disks and configs only if servers are
|
||||||
configs = api.linode_config_list(LinodeId=linode_id)
|
# found with linode_id
|
||||||
|
if servers:
|
||||||
|
disks = api.linode_disk_list(LinodeId=linode_id)
|
||||||
|
configs = api.linode_config_list(LinodeId=linode_id)
|
||||||
|
|
||||||
# Act on the state
|
# Act on the state
|
||||||
if state in ('active', 'present', 'started'):
|
if state in ('active', 'present', 'started'):
|
||||||
# TODO: validate all the flavor / image / region are valid
|
# TODO: validate all the plan / distribution / datacenter are valid
|
||||||
|
|
||||||
# Multi step process/validation:
|
# Multi step process/validation:
|
||||||
# - need linode_id (entity)
|
# - need linode_id (entity)
|
||||||
|
@ -225,16 +239,22 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
if not servers:
|
if not servers:
|
||||||
new_server = True
|
new_server = True
|
||||||
# TODO - improve
|
# TODO - improve
|
||||||
for arg in (name, flavor, image, region):
|
if not name:
|
||||||
if not arg:
|
module.fail_json(msg='%s is required for active state' % 'name')
|
||||||
module.fail_json(msg='%s is required for active state' % arg)
|
if not plan:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'plan')
|
||||||
|
if not distribution:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'distribution')
|
||||||
|
if not datacenter:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'datacenter')
|
||||||
|
|
||||||
# Create linode entity
|
# Create linode entity
|
||||||
try:
|
try:
|
||||||
res = api.linode_create(DatacenterID=region, PlanID=flavor,
|
res = api.linode_create(DatacenterID=datacenter, PlanID=plan,
|
||||||
PaymentTerm=payment_term)
|
PaymentTerm=payment_term)
|
||||||
linode_id = res['LinodeID']
|
linode_id = res['LinodeID']
|
||||||
# Update linode Label to match name
|
# Update linode Label to match name
|
||||||
api.linode_update(LinodeId=linode_id, Label=name)
|
api.linode_update(LinodeId=linode_id, Label='%s_%s' % (linode_id, name))
|
||||||
# Save server
|
# Save server
|
||||||
servers = api.linode_list(LinodeId=linode_id);
|
servers = api.linode_list(LinodeId=linode_id);
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
@ -243,9 +263,13 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
if not disks:
|
if not disks:
|
||||||
new_server = True
|
new_server = True
|
||||||
# TODO - improve
|
# TODO - improve
|
||||||
for arg in (linode_id, name, image):
|
if not name:
|
||||||
if not arg:
|
module.fail_json(msg='%s is required for active state' % 'name')
|
||||||
module.fail_json(msg='%s is required for active state' % arg)
|
if not linode_id:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'linode_id')
|
||||||
|
if not distribution:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'distribution')
|
||||||
|
|
||||||
# Create disks (1 from distrib, 1 for SWAP)
|
# Create disks (1 from distrib, 1 for SWAP)
|
||||||
try:
|
try:
|
||||||
if not password:
|
if not password:
|
||||||
|
@ -257,18 +281,18 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
size = servers[0]['TOTALHD'] - swap
|
size = servers[0]['TOTALHD'] - swap
|
||||||
if ssh_pub_key:
|
if ssh_pub_key:
|
||||||
res = api.linode_disk_createfromdistribution(
|
res = api.linode_disk_createfromdistribution(
|
||||||
LinodeId=linode_id, DistributionID=image,
|
LinodeId=linode_id, DistributionID=distribution,
|
||||||
rootPass=password, rootSSHKey=ssh_pub_key,
|
rootPass=password, rootSSHKey=ssh_pub_key,
|
||||||
Label='%s data disk' % name, Size=size)
|
Label='%s data disk (lid: %s)' % (name, linode_id), Size=size)
|
||||||
else:
|
else:
|
||||||
res = api.linode_disk_createfromdistribution(
|
res = api.linode_disk_createfromdistribution(
|
||||||
LinodeId=linode_id, DistributionID=image,
|
LinodeId=linode_id, DistributionID=distribution, rootPass=password,
|
||||||
rootPass=password, Label='%s data disk' % name,
|
Label='%s data disk (lid: %s)' % (name, linode_id), Size=size)
|
||||||
Size=size)
|
|
||||||
jobs.append(res['JobID'])
|
jobs.append(res['JobID'])
|
||||||
# Create SWAP disk
|
# Create SWAP disk
|
||||||
res = api.linode_disk_create(LinodeId=linode_id, Type='swap',
|
res = api.linode_disk_create(LinodeId=linode_id, Type='swap',
|
||||||
Label='%s swap disk' % name, Size=swap)
|
Label='%s swap disk (lid: %s)' % (name, linode_id),
|
||||||
|
Size=swap)
|
||||||
jobs.append(res['JobID'])
|
jobs.append(res['JobID'])
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
# TODO: destroy linode ?
|
# TODO: destroy linode ?
|
||||||
|
@ -277,12 +301,16 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
if not configs:
|
if not configs:
|
||||||
new_server = True
|
new_server = True
|
||||||
# TODO - improve
|
# TODO - improve
|
||||||
for arg in (linode_id, name, image):
|
if not name:
|
||||||
if not arg:
|
module.fail_json(msg='%s is required for active state' % 'name')
|
||||||
module.fail_json(msg='%s is required for active state' % arg)
|
if not linode_id:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'linode_id')
|
||||||
|
if not distribution:
|
||||||
|
module.fail_json(msg='%s is required for active state' % 'distribution')
|
||||||
|
|
||||||
# Check architecture
|
# Check architecture
|
||||||
for distrib in api.avail_distributions():
|
for distrib in api.avail_distributions():
|
||||||
if distrib['DISTRIBUTIONID'] != image:
|
if distrib['DISTRIBUTIONID'] != distribution:
|
||||||
continue
|
continue
|
||||||
arch = '32'
|
arch = '32'
|
||||||
if distrib['IS64BIT']:
|
if distrib['IS64BIT']:
|
||||||
|
@ -298,11 +326,11 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
|
|
||||||
# Get disk list
|
# Get disk list
|
||||||
disks_id = []
|
disks_id = []
|
||||||
for disk in api.linode_disk_list():
|
for disk in api.linode_disk_list(LinodeId=linode_id):
|
||||||
if disk['TYPE'] == 'ext3':
|
if disk['TYPE'] == 'ext3':
|
||||||
disks_id.insert(0, disk['DISKID'])
|
disks_id.insert(0, str(disk['DISKID']))
|
||||||
continue
|
continue
|
||||||
disks_id.append(disk['DISKID'])
|
disks_id.append(str(disk['DISKID']))
|
||||||
# Trick to get the 9 items in the list
|
# Trick to get the 9 items in the list
|
||||||
while len(disks_id) < 9:
|
while len(disks_id) < 9:
|
||||||
disks_id.append('')
|
disks_id.append('')
|
||||||
|
@ -310,9 +338,8 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
|
|
||||||
# Create config
|
# Create config
|
||||||
try:
|
try:
|
||||||
res = api.linode_config_create(LinodeId=linode_id, KernelId=kernel_id,
|
api.linode_config_create(LinodeId=linode_id, KernelId=kernel_id,
|
||||||
Disklist=disks_list, Label='%s config' % name)
|
Disklist=disks_list, Label='%s config' % name)
|
||||||
config_id = res['ConfigId']
|
|
||||||
configs = api.linode_config_list(LinodeId=linode_id)
|
configs = api.linode_config_list(LinodeId=linode_id)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
||||||
|
@ -340,12 +367,12 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
if wait and wait_timeout <= time.time():
|
if wait and wait_timeout <= time.time():
|
||||||
# waiting took too long
|
# waiting took too long
|
||||||
module.fail_json(msg = 'Timeout waiting on %s - %s' %
|
module.fail_json(msg = 'Timeout waiting on %s (lid: %s)' %
|
||||||
(server['LABEL'], server['LINODEID']))
|
(server['LABEL'], server['LINODEID']))
|
||||||
# Get a fresh copy of the server details
|
# Get a fresh copy of the server details
|
||||||
server = api.linode_list(LinodeId=server['LINODEID'])[0]
|
server = api.linode_list(LinodeId=server['LINODEID'])[0]
|
||||||
if server['STATUS'] == -2:
|
if server['STATUS'] == -2:
|
||||||
module.fail_json(msg = '%s - %s failed to boot' %
|
module.fail_json(msg = '%s (lid: %s) failed to boot' %
|
||||||
(server['LABEL'], server['LINODEID']))
|
(server['LABEL'], server['LINODEID']))
|
||||||
# From now on we know the task is a success
|
# From now on we know the task is a success
|
||||||
# Build instance report
|
# Build instance report
|
||||||
|
@ -362,12 +389,13 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
instance['password'] = password
|
instance['password'] = password
|
||||||
instances.append(instance)
|
instances.append(instance)
|
||||||
elif state in ('stopped'):
|
elif state in ('stopped'):
|
||||||
for arg in (linode_id, name):
|
if not name:
|
||||||
if not arg:
|
module.fail_json(msg='%s is required for stopped state' % 'name')
|
||||||
module.fail_json(msg='%s is required for stopped state' % arg)
|
if not linode_id:
|
||||||
|
module.fail_json(msg='%s is required for stopped state' % 'linode_id')
|
||||||
|
|
||||||
if not servers:
|
if not servers:
|
||||||
module.fail_json(msg = 'Server %s (%s) not found' %
|
module.fail_json(msg = 'Server %s (lid: %s) not found' % (name, linode_id))
|
||||||
(name, linode_id))
|
|
||||||
for server in servers:
|
for server in servers:
|
||||||
instance = getInstanceDetails(api, server)
|
instance = getInstanceDetails(api, server)
|
||||||
if server['STATUS'] != 2:
|
if server['STATUS'] != 2:
|
||||||
|
@ -381,12 +409,13 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
instance['status'] = 'Stopped'
|
instance['status'] = 'Stopped'
|
||||||
instances.append(instance)
|
instances.append(instance)
|
||||||
elif state in ('restarted'):
|
elif state in ('restarted'):
|
||||||
for arg in ('linode_id', 'name'):
|
if not name:
|
||||||
if not arg:
|
module.fail_json(msg='%s is required for restarted state' % 'name')
|
||||||
module.fail_json(msg='%s is required for restarted state' % arg)
|
if not linode_id:
|
||||||
|
module.fail_json(msg='%s is required for restarted state' % 'linode_id')
|
||||||
|
|
||||||
if not servers:
|
if not servers:
|
||||||
module.fail_json(msg = 'Server %s (%s) not found' %
|
module.fail_json(msg = 'Server %s (lid: %s) not found' % (name, linode_id))
|
||||||
(name, linode_id))
|
|
||||||
for server in servers:
|
for server in servers:
|
||||||
instance = getInstanceDetails(api, server)
|
instance = getInstanceDetails(api, server)
|
||||||
try:
|
try:
|
||||||
|
@ -415,18 +444,19 @@ def linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
||||||
def main():
|
def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
state = dict(default='present', choices=['active', 'present', 'started'
|
state = dict(default='present', choices=['active', 'present', 'started',
|
||||||
'deleted', 'absent', 'stopped']),
|
'deleted', 'absent', 'stopped',
|
||||||
|
'restarted']),
|
||||||
api_key = dict(),
|
api_key = dict(),
|
||||||
name = dict(type='str'),
|
name = dict(type='str'),
|
||||||
flavor = dict(type='int'),
|
plan = dict(type='int'),
|
||||||
image = dict(type='int'),
|
distribution = dict(type='int'),
|
||||||
region = dict(type='int'),
|
datacenter = dict(type='int'),
|
||||||
linode_id = dict(type='int'),
|
linode_id = dict(type='int', aliases=['lid']),
|
||||||
payment_term = dict(default=1, choices=[1, 12, 24]),
|
payment_term = dict(default=1, choices=[1, 12, 24]),
|
||||||
password = dict(type='str'),
|
password = dict(type='str'),
|
||||||
ssh_pub_key = dict(type='str'),
|
ssh_pub_key = dict(type='str'),
|
||||||
swap = dict(type='int', default=512)
|
swap = dict(type='int', default=512),
|
||||||
wait = dict(type='bool', choices=BOOLEANS, default=True),
|
wait = dict(type='bool', choices=BOOLEANS, default=True),
|
||||||
wait_timeout = dict(default=500),
|
wait_timeout = dict(default=500),
|
||||||
)
|
)
|
||||||
|
@ -435,9 +465,9 @@ def main():
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
api_key = module.params.get('api_key')
|
api_key = module.params.get('api_key')
|
||||||
name = module.params.get('name')
|
name = module.params.get('name')
|
||||||
flavor = module.params.get('flavor')
|
plan = module.params.get('plan')
|
||||||
image = module.params.get('image')
|
distribution = module.params.get('distribution')
|
||||||
region = module.params.get('region')
|
datacenter = module.params.get('datacenter')
|
||||||
linode_id = module.params.get('linode_id')
|
linode_id = module.params.get('linode_id')
|
||||||
payment_term = module.params.get('payment_term')
|
payment_term = module.params.get('payment_term')
|
||||||
password = module.params.get('password')
|
password = module.params.get('password')
|
||||||
|
@ -460,7 +490,7 @@ def main():
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
||||||
|
|
||||||
linodeServers(module, api, state, name, flavor, image, region, linode_id,
|
linodeServers(module, api, state, name, plan, distribution, datacenter, linode_id,
|
||||||
payment_term, password, ssh_pub_key, swap, wait, wait_timeout)
|
payment_term, password, ssh_pub_key, swap, wait, wait_timeout)
|
||||||
|
|
||||||
# this is magic, see lib/ansible/module_common.py
|
# this is magic, see lib/ansible/module_common.py
|
||||||
|
|
Loading…
Reference in a new issue