|
import smtplib |
|
from email.mime.text import MIMEText |
|
from linode import api |
|
""" |
|
A small script to automate the creation, boot, and destruction of Linodes. |
|
|
|
Supports an interactive mode which fires off an email to users with their Linode |
|
details (useful for tutorials). |
|
|
|
Requires linode-python. |
|
""" |
|
|
|
# Linode parameters |
|
API_KEY = "linode_api_key" |
|
|
|
# Constants below determined from the results of the avail_foo() API commands |
|
DC_ID = 6 # Newark, NJ |
|
PLAN_ID = 1 # 1024 |
|
DISTRIBUTION_ID = 129 # CentOS 7 |
|
ROOT_PASS="please_changem3" |
|
|
|
# Disk parameters |
|
SWAP_LABEL="swap" |
|
SWAP_SIZE_MB=256 |
|
|
|
BOOT_LABEL='root' |
|
DISK_SIZE_GB=24 |
|
BOOT_SIZE=DISK_SIZE_GB*1024-SWAP_SIZE_MB |
|
|
|
# Linode Config parameters |
|
KERNEL_ID=138 # latest 64-bit |
|
|
|
# Mail parameters |
|
SMTP_HOST = '0.0.0.0' |
|
SMTP_PORT = 587 |
|
SMTP_USER = 'smtp_user' |
|
SMTP_PASS = 'smtp_pass' |
|
EMAIL_FROM = '[email protected]' |
|
EMAIL_SUBJECT = 'Your new Linode details' |
|
|
|
|
|
l = api.Api(key=API_KEY) |
|
|
|
def init(): |
|
linodeInfo = l.linode_create(DatacenterID=DC_ID, PlanID=PLAN_ID) |
|
linodeId = linodeInfo['LinodeID'] |
|
rootinfo = l.linode_disk_createfromdistribution(LinodeID=linodeId, DistributionID=DISTRIBUTION_ID, rootPass=ROOT_PASS, Label=BOOT_LABEL, Size=BOOT_SIZE) |
|
swapinfo = l.linode_disk_create(LinodeID=linodeId, Label=SWAP_LABEL, Type="swap", Size=SWAP_SIZE_MB) |
|
#l.linode_disk_list(LinodeID=linodeId) |
|
configinfo = l.linode_config_create( |
|
LinodeID=linodeId, |
|
KernelID=KERNEL_ID, |
|
Label='My CentOS 7 Profile', |
|
DiskList="%s,%s,,,,,,," % (rootinfo['DiskID'], swapinfo['DiskID']) |
|
) |
|
return linodeId |
|
|
|
def multi_init(total): |
|
linodeIds = [] |
|
for current in range(1, total+1): |
|
print "Creating Linode %d/%d..." % (current, total) |
|
linodeId = init(); |
|
linodeIds.append(linodeId) |
|
return linodeIds |
|
|
|
def boot(linodeId): |
|
l.linode_boot(LinodeID=linodeId) |
|
ipinfo = l.linode_ip_list(LinodeID=linodeId) |
|
return ipinfo[0]['IPADDRESS'] |
|
|
|
def multi_boot(linodeIds=[]): |
|
ips = [] |
|
for linodeId in linodeIds: |
|
print "Booting linode %d/%d..." % (linodeIds.index(linodeId)+1, len(linodeIds)) |
|
ipAddress = boot(linodeId) |
|
ips.append(ipAddress) |
|
return ips |
|
|
|
def reap(linodeId): |
|
l.linode_delete(LinodeID=linodeId, skipChecks=True) |
|
|
|
def multi_reap(linodeIds=[]): |
|
for linodeId in linodeIds: |
|
print "Reaping linode %d/%d..." % (linodeIds.index(linodeId)+1, len(linodeIds)) |
|
reap(linodeId) |
|
|
|
def interactive(): |
|
recipient = ''; |
|
while True: |
|
confirm = 'n' |
|
while confirm.lower() != 'y': |
|
recipient = raw_input("Email: ") |
|
confirm = raw_input("Confirm? [Y/n] ") |
|
|
|
if recipient == 'exit': |
|
break |
|
|
|
linodeId = init() |
|
boot(linodeId) |
|
print "Linode %d booted.\n" % linodeId |
|
|
|
# Open a plain text file for reading. For this example, assume that |
|
# the text file contains only ASCII characters. |
|
with open('linode-create.eml') as fp: |
|
fh = open('linodeIds.txt', 'a') |
|
fh.write("%s,%s\n" % (linodeId, recipient)) |
|
fh.close() |
|
|
|
ipinfo = l.linode_ip_list(LinodeID=linodeId) |
|
replacements = {'linodeIp': ipinfo[0]['IPADDRESS'], 'rootPw': ROOT_PASS} |
|
|
|
# Create a text/plain message |
|
msg = MIMEText(fp.read() % replacements) |
|
|
|
# me == the sender's email address |
|
# you == the recipient's email address |
|
msg['Subject'] = EMAIL_SUBJECT |
|
msg['From'] = EMAIL_FROM |
|
msg['To'] = recipient |
|
|
|
# Send the message via our own SMTP server. |
|
s = smtplib.SMTP(SMTP_HOST, SMTP_PORT) |
|
s.login(SMTP_USER, SMTP_PASS) |
|
s.sendmail(EMAIL_FROM, [recipient], msg.as_string()) |
|
s.quit() |
|
|
|
if __name__ == "__main__": |
|
# take your preferred action here |
|
pass |