Adding more functionality for Linode cloud module
This commit is contained in:
parent
48b02336ab
commit
320c791cfb
1 changed files with 201 additions and 6 deletions
|
@ -41,12 +41,90 @@ options:
|
|||
- To keep sanity on the Linode Web Console, name is prepended with LinodeID_
|
||||
default: null
|
||||
type: string
|
||||
displaygroup:
|
||||
description:
|
||||
- Add the instance to a Display Group in Linode Manager
|
||||
default: null
|
||||
type: string
|
||||
version_added: "2.3"
|
||||
linode_id:
|
||||
description:
|
||||
- Unique ID of a linode server
|
||||
aliases: [ 'lid' ]
|
||||
default: null
|
||||
type: integer
|
||||
additional_disks:
|
||||
description: >
|
||||
List of dictionaries for creating additional disks that are added to the Linode configuration settings.
|
||||
Dictionary takes Size, Label, Type. Size is in MB.
|
||||
default: null
|
||||
version_added: "2.3"
|
||||
alert_bwin_enabled:
|
||||
description:
|
||||
- Set status of bandwidth in alerts.
|
||||
default: "True"
|
||||
choices: [ "True", "False" ]
|
||||
version_added: "2.3"
|
||||
alert_bwin_threshold:
|
||||
description:
|
||||
- Set threshold in MB of bandwidth in alerts.
|
||||
default: null
|
||||
type: integer
|
||||
version_added: "2.3"
|
||||
alert_bwout_enabled:
|
||||
description:
|
||||
- Set status of bandwidth out alerts.
|
||||
default: "True"
|
||||
choices: [ "True", "False" ]
|
||||
version_added: "2.3"
|
||||
alert_bwout_threshold:
|
||||
description:
|
||||
- Set threshold in MB of bandwidth out alerts.
|
||||
default: null
|
||||
type: integer
|
||||
version_added: "2.3"
|
||||
alert_bwquota_enabled:
|
||||
description:
|
||||
- Set status of bandwidth quota alerts as percentage of network tranfer quota.
|
||||
default: "True"
|
||||
choices: [ "True", "False" ]
|
||||
version_added: "2.3"
|
||||
alert_bwquota_threshold:
|
||||
description:
|
||||
- Set threshold in MB of bandwidth quota alerts.
|
||||
default: null
|
||||
type: integer
|
||||
version_added: "2.3"
|
||||
alert_cpu_enabled:
|
||||
description:
|
||||
- Set status of receiving CPU usage alerts.
|
||||
default: "True"
|
||||
choices: [ "True", "False" ]
|
||||
version_added: "2.3"
|
||||
alert_cpu_threshold:
|
||||
description:
|
||||
- Set percentage threshold for receiving CPU usage alerts. Each CPU core adds 100% to total.
|
||||
default: null
|
||||
type: integer
|
||||
version_added: "2.3"
|
||||
alert_diskio_enabled:
|
||||
description:
|
||||
- Set status of receiving disk IO alerts.
|
||||
default: "True"
|
||||
choices: [ "True", "False" ]
|
||||
version_added: "2.3"
|
||||
alert_diskio_threshold:
|
||||
description:
|
||||
- Set threshold for average IO ops/sec over 2 hour period.
|
||||
default: null
|
||||
type: integer
|
||||
version_added: "2.3"
|
||||
backupweeklyday:
|
||||
description:
|
||||
- Integer value for what day of the week to store weekly backups.
|
||||
default: null
|
||||
type: integer
|
||||
version_added: "2.3"
|
||||
plan:
|
||||
description:
|
||||
- plan to use for the instance (Linode plan)
|
||||
|
@ -63,6 +141,12 @@ options:
|
|||
- root password to apply to a new server (auto generated if missing)
|
||||
default: null
|
||||
type: string
|
||||
private_ip:
|
||||
description:
|
||||
- Add private IPv4 address when Linode is created.
|
||||
default: "no"
|
||||
choices: [ "yes", "no" ]
|
||||
version_added: "2.3"
|
||||
ssh_pub_key:
|
||||
description:
|
||||
- SSH public key applied to root user
|
||||
|
@ -92,6 +176,12 @@ options:
|
|||
description:
|
||||
- how long before wait gives up, in seconds
|
||||
default: 300
|
||||
watchdog:
|
||||
description:
|
||||
- Set status of Lassie watchdog.
|
||||
default: "True"
|
||||
choices: [ "True", "False" ]
|
||||
version_added: "2.2"
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
- "linode-python"
|
||||
|
@ -102,7 +192,7 @@ notes:
|
|||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Create a server
|
||||
# Create a server with a private IP Address
|
||||
- local_action:
|
||||
module: linode
|
||||
api_key: 'longStringFromLinodeApi'
|
||||
|
@ -111,12 +201,47 @@ EXAMPLES = '''
|
|||
datacenter: 2
|
||||
distribution: 99
|
||||
password: 'superSecureRootPassword'
|
||||
private_ip: yes
|
||||
ssh_pub_key: 'ssh-rsa qwerty'
|
||||
swap: 768
|
||||
wait: yes
|
||||
wait_timeout: 600
|
||||
state: present
|
||||
|
||||
# Fully configure new server
|
||||
- local_action:
|
||||
module: linode
|
||||
api_key: 'longStringFromLinodeApi'
|
||||
name: linode-test1
|
||||
plan: 4
|
||||
datacenter: 2
|
||||
distribution: 99
|
||||
password: 'superSecureRootPassword'
|
||||
private_ip: yes
|
||||
ssh_pub_key: 'ssh-rsa qwerty'
|
||||
swap: 768
|
||||
wait: yes
|
||||
wait_timeout: 600
|
||||
state: present
|
||||
alert_bwquota_enabled: True
|
||||
alert_bwquota_threshold: 80
|
||||
alert_bwin_enabled: True
|
||||
alert_bwin_threshold: 10
|
||||
alert_cpu_enabled: True
|
||||
alert_cpu_threshold: 210
|
||||
alert_diskio_enabled: True
|
||||
alert_bwout_enabled: True
|
||||
alert_bwout_threshold: 10
|
||||
alert_diskio_enabled: True
|
||||
alert_diskio_threshold: 10000
|
||||
backupweeklyday: 1
|
||||
backupwindow: 2
|
||||
displaygroup: 'test'
|
||||
additional_disks:
|
||||
- {Label: 'disk1', Size: 2500, Type: 'raw'}
|
||||
- {Label: 'newdisk', Size: 2000}
|
||||
watchdog: True
|
||||
|
||||
# Ensure a running server (create if missing)
|
||||
- local_action:
|
||||
module: linode
|
||||
|
@ -218,8 +343,10 @@ def getInstanceDetails(api, server):
|
|||
'ip_id': ip['IPADDRESSID']})
|
||||
return instance
|
||||
|
||||
def linodeServers(module, api, state, name, plan, distribution, datacenter, linode_id,
|
||||
payment_term, password, ssh_pub_key, swap, wait, wait_timeout):
|
||||
def linodeServers(module, api, state, name, alert_bwin_enabled, alert_bwin_threshold, alert_bwout_enabled, alert_bwout_threshold,
|
||||
alert_bwquota_enabled, alert_bwquota_threshold, alert_cpu_enabled, alert_cpu_threshold, alert_diskio_enabled,
|
||||
alert_diskio_threshold,backupweeklyday, backupwindow, displaygroup, plan, additional_disks, distribution,
|
||||
datacenter, linode_id, payment_term, password, private_ip, ssh_pub_key, swap, wait, wait_timeout, watchdog):
|
||||
instances = []
|
||||
changed = False
|
||||
new_server = False
|
||||
|
@ -227,6 +354,7 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
|
|||
disks = []
|
||||
configs = []
|
||||
jobs = []
|
||||
disk_size = 0
|
||||
|
||||
# See if we can match an existing server details with the provided linode_id
|
||||
if linode_id:
|
||||
|
@ -255,17 +383,36 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
|
|||
module.fail_json(msg='%s is required for active state' % arg)
|
||||
# Create linode entity
|
||||
new_server = True
|
||||
|
||||
# Get size of all individually listed disks to subtract from Distribution disk
|
||||
used_disk_space = 0 if additional_disks is None else sum(disk['Size'] for disk in additional_disks)
|
||||
|
||||
try:
|
||||
res = api.linode_create(DatacenterID=datacenter, PlanID=plan,
|
||||
PaymentTerm=payment_term)
|
||||
linode_id = res['LinodeID']
|
||||
# Update linode Label to match name
|
||||
api.linode_update(LinodeId=linode_id, Label='%s_%s' % (linode_id, name))
|
||||
# Update Linode with Ansible configuration options
|
||||
api.linode_update(LinodeId=linode_id, ALERT_BWIN_ENABLED=alert_bwin_enabled,
|
||||
ALERT_BWIN_THRESHOLD=alert_bwin_threshold, ALERT_BWOUT_ENABLED=alert_bwout_enabled,
|
||||
ALERT_BWOUT_THRESHOLD=alert_bwout_threshold, ALERT_BWQUOTA_ENABLED=alert_bwquota_enabled,
|
||||
ALERT_BWQUOTA_THRESHOLD=alert_bwquota_threshold, ALERT_CPU_ENABLED=alert_cpu_enabled,
|
||||
ALERT_CPU_THRESHOLD=alert_cpu_threshold, ALERT_DISKIO_ENABLED=alert_diskio_enabled,
|
||||
ALERT_DISKIO_THRESHOLD=alert_diskio_threshold, BACKUPWEEKLYDAY=backupweeklyday,
|
||||
BACKUPWINDOW=backupwindow, LPM_DISPLAYGROUP=displaygroup, WATCHDOG=watchdog)
|
||||
# Save server
|
||||
servers = api.linode_list(LinodeId=linode_id)
|
||||
except Exception as e:
|
||||
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
||||
|
||||
#Add private IP to Linode
|
||||
if private_ip:
|
||||
try:
|
||||
res = api.linode_ip_addprivate(LinodeID=linode_id)
|
||||
except Exception as e:
|
||||
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
||||
|
||||
if not disks:
|
||||
for arg in (name, linode_id, distribution):
|
||||
if not arg:
|
||||
|
@ -279,7 +426,8 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
|
|||
if not swap:
|
||||
swap = 512
|
||||
# Create data disk
|
||||
size = servers[0]['TOTALHD'] - swap
|
||||
size = servers[0]['TOTALHD'] - used_disk_space - swap
|
||||
|
||||
if ssh_pub_key:
|
||||
res = api.linode_disk_createfromdistribution(
|
||||
LinodeId=linode_id, DistributionID=distribution,
|
||||
|
@ -294,6 +442,14 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
|
|||
res = api.linode_disk_create(LinodeId=linode_id, Type='swap',
|
||||
Label='%s swap disk (lid: %s)' % (name, linode_id),
|
||||
Size=swap)
|
||||
# Create individually listed disks at specified size
|
||||
if additional_disks:
|
||||
for disk in additional_disks:
|
||||
# If a disk Type is not passed in, default to ext4
|
||||
if disk.get('Type') is None:
|
||||
disk['Type'] = 'ext4'
|
||||
res = api.linode_disk_create(LinodeID=linode_id, Label=disk['Label'], Size=disk['Size'], Type=disk['Type'])
|
||||
|
||||
jobs.append(res['JobID'])
|
||||
except Exception as e:
|
||||
# TODO: destroy linode ?
|
||||
|
@ -449,16 +605,32 @@ def main():
|
|||
'restarted']),
|
||||
api_key = dict(no_log=True),
|
||||
name = dict(type='str'),
|
||||
alert_bwin_enabled = dict(type='bool', default=True),
|
||||
alert_bwin_threshold = dict(type='int'),
|
||||
alert_bwout_enabled = dict(type='bool', default=True),
|
||||
alert_bwout_threshold = dict(type='int'),
|
||||
alert_bwquota_enabled = dict(type='bool', default=True),
|
||||
alert_bwquota_threshold = dict(type='int'),
|
||||
alert_cpu_enabled = dict(type='bool', default=True),
|
||||
alert_cpu_threshold = dict(type='int'),
|
||||
alert_diskio_enabled = dict(type='bool', default=True),
|
||||
alert_diskio_threshold = dict(type='int'),
|
||||
backupweeklyday = dict(type='int'),
|
||||
backupwindow = dict(type='int'),
|
||||
displaygroup = dict(type='str', default=''),
|
||||
plan = dict(type='int'),
|
||||
additional_disks= dict(type='list'),
|
||||
distribution = dict(type='int'),
|
||||
datacenter = dict(type='int'),
|
||||
linode_id = dict(type='int', aliases=['lid']),
|
||||
payment_term = dict(type='int', default=1, choices=[1, 12, 24]),
|
||||
password = dict(type='str', no_log=True),
|
||||
private_ip = dict(type='bool'),
|
||||
ssh_pub_key = dict(type='str'),
|
||||
swap = dict(type='int', default=512),
|
||||
wait = dict(type='bool', default=True),
|
||||
wait_timeout = dict(default=300),
|
||||
watchdog = dict(type='bool', default=True),
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -470,16 +642,33 @@ def main():
|
|||
state = module.params.get('state')
|
||||
api_key = module.params.get('api_key')
|
||||
name = module.params.get('name')
|
||||
alert_bwin_enabled = int(module.params.get('alert_bwin_enabled'))
|
||||
alert_bwin_threshold = module.params.get('alert_bwin_threshold')
|
||||
alert_bwout_enabled = int(module.params.get('alert_bwout_enabled'))
|
||||
alert_bwout_threshold = module.params.get('alert_bwout_threshold')
|
||||
alert_bwquota_enabled = int(module.params.get('alert_bwquota_enabled'))
|
||||
alert_bwquota_threshold = module.params.get('alert_bwquota_threshold')
|
||||
alert_cpu_enabled = int(module.params.get('alert_cpu_enabled'))
|
||||
alert_cpu_threshold = module.params.get('alert_cpu_threshold')
|
||||
alert_diskio_enabled = int(module.params.get('alert_diskio_enabled'))
|
||||
alert_diskio_threshold = module.params.get('alert_diskio_threshold')
|
||||
backupsenabled = module.params.get('backupsenabled')
|
||||
backupweeklyday = module.params.get('backupweeklyday')
|
||||
backupwindow = module.params.get('backupwindow')
|
||||
displaygroup = module.params.get('displaygroup')
|
||||
plan = module.params.get('plan')
|
||||
additional_disks = module.params.get('additional_disks')
|
||||
distribution = module.params.get('distribution')
|
||||
datacenter = module.params.get('datacenter')
|
||||
linode_id = module.params.get('linode_id')
|
||||
payment_term = module.params.get('payment_term')
|
||||
password = module.params.get('password')
|
||||
private_ip = module.params.get('private_ip')
|
||||
ssh_pub_key = module.params.get('ssh_pub_key')
|
||||
swap = module.params.get('swap')
|
||||
wait = module.params.get('wait')
|
||||
wait_timeout = int(module.params.get('wait_timeout'))
|
||||
watchdog = int(module.params.get('watchdog'))
|
||||
|
||||
# Setup the api_key
|
||||
if not api_key:
|
||||
|
@ -495,8 +684,14 @@ def main():
|
|||
except Exception as e:
|
||||
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
|
||||
|
||||
linodeServers(module, api, state, name, plan, distribution, datacenter, linode_id,
|
||||
payment_term, password, ssh_pub_key, swap, wait, wait_timeout)
|
||||
linodeServers(module, api, state, name, alert_bwin_enabled,
|
||||
alert_bwin_threshold, alert_bwout_enabled, alert_bwout_threshold,
|
||||
alert_bwquota_enabled, alert_bwquota_threshold, alert_cpu_enabled,
|
||||
alert_cpu_threshold, alert_diskio_enabled, alert_diskio_threshold,
|
||||
backupweeklyday, backupwindow, displaygroup, plan,
|
||||
additional_disks, distribution, datacenter, linode_id,
|
||||
payment_term, password, private_ip, ssh_pub_key, swap, wait,
|
||||
wait_timeout, watchdog)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
|
|
Loading…
Reference in a new issue