@ -179,14 +179,14 @@ def randompass():
'''
'''
Generate a long random password that comply to Linode requirements
Generate a long random password that comply to Linode requirements
'''
'''
# Linode API currently requires the following:
# Linode API currently requires the following:
# It must contain at least two of these four character classes:
# It must contain at least two of these four character classes:
# lower case letters - upper case letters - numbers - punctuation
# lower case letters - upper case letters - numbers - punctuation
# we play it safe :)
# we play it safe :)
import random
import random
import string
import string
# as of python 2.4, this reseeds the PRNG from urandom
# as of python 2.4, this reseeds the PRNG from urandom
random . seed ( )
random . seed ( )
lower = ' ' . join ( random . choice ( string . ascii_lowercase ) for x in range ( 6 ) )
lower = ' ' . join ( random . choice ( string . ascii_lowercase ) for x in range ( 6 ) )
upper = ' ' . join ( random . choice ( string . ascii_uppercase ) for x in range ( 6 ) )
upper = ' ' . join ( random . choice ( string . ascii_uppercase ) for x in range ( 6 ) )
number = ' ' . join ( random . choice ( string . digits ) for x in range ( 6 ) )
number = ' ' . join ( random . choice ( string . digits ) for x in range ( 6 ) )
@ -218,11 +218,11 @@ def getInstanceDetails(api, server):
' ip_id ' : ip [ ' IPADDRESSID ' ] } )
' ip_id ' : ip [ ' IPADDRESSID ' ] } )
return instance
return instance
def linodeServers ( module , api , state , name , plan , distribution , datacenter , 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
new_server = False
new_server = False
servers = [ ]
servers = [ ]
disks = [ ]
disks = [ ]
configs = [ ]
configs = [ ]
@ -233,7 +233,7 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
# 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 )
# Attempt to fetch details about disks and configs only if servers are
# Attempt to fetch details about disks and configs only if servers are
# found with linode_id
# found with linode_id
if servers :
if servers :
disks = api . linode_disk_list ( LinodeId = linode_id )
disks = api . linode_disk_list ( LinodeId = linode_id )
@ -256,7 +256,7 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
# Create linode entity
# Create linode entity
new_server = True
new_server = True
try :
try :
res = api . linode_create ( DatacenterID = datacenter , PlanID = plan ,
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
@ -282,17 +282,17 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
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 = distribution ,
LinodeId = linode_id , DistributionID = distribution ,
rootPass = password , rootSSHKey = ssh_pub_key ,
rootPass = password , rootSSHKey = ssh_pub_key ,
Label = ' %s data disk (lid: %s ) ' % ( name , linode_id ) , 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 = distribution , rootPass = password ,
LinodeId = linode_id , DistributionID = distribution , rootPass = password ,
Label = ' %s data disk (lid: %s ) ' % ( name , linode_id ) , Size = size )
Label = ' %s data disk (lid: %s ) ' % ( name , linode_id ) , 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 (lid: %s ) ' % ( name , linode_id ) ,
Label = ' %s swap disk (lid: %s ) ' % ( name , linode_id ) ,
Size = swap )
Size = swap )
jobs . append ( res [ ' JobID ' ] )
jobs . append ( res [ ' JobID ' ] )
except Exception as e :
except Exception as e :
@ -364,12 +364,12 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
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 (lid: %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 (lid: %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
@ -380,7 +380,7 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
else :
else :
instance [ ' status ' ] = ' Starting '
instance [ ' status ' ] = ' Starting '
# Return the root password if this is a new box and no SSH key
# Return the root password if this is a new box and no SSH key
# has been provided
# has been provided
if new_server and not ssh_pub_key :
if new_server and not ssh_pub_key :
instance [ ' password ' ] = password
instance [ ' password ' ] = password
@ -495,7 +495,7 @@ def main():
except Exception as e :
except Exception as e :
module . fail_json ( msg = ' %s ' % e . value [ 0 ] [ ' ERRORMESSAGE ' ] )
module . fail_json ( msg = ' %s ' % e . value [ 0 ] [ ' ERRORMESSAGE ' ] )
linodeServers ( module , api , state , name , plan , distribution , datacenter , 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 )
# import module snippets
# import module snippets