Skip to content

Instantly share code, notes, and snippets.

@stewartadam
Last active September 13, 2018 13:25
Show Gist options
  • Save stewartadam/c1b5541ff05ca5510dc2 to your computer and use it in GitHub Desktop.
Save stewartadam/c1b5541ff05ca5510dc2 to your computer and use it in GitHub Desktop.
A quick Python script that uses the Linode API to create, boot and delete Linodes. Supports an interactive mode that sends emails to users with their Linode credentials, great for providing temporary Linodes for a tutorial.

This is a little automation script I wrote to configure, boot and/or destroy Linodes using the Linode API. This script requires the Requires the linode-python module to wrap the API.

I had written this with the intent of providing users with a temporary Linode that would be destroyed after an hour or two, but the functions are pretty generic and could be repurposed. The interactive mode lets users enter their e-mail address, and upon confirmation sends them a message with their Linode's IP & password.

If you'd like to use this script as-is, you'll need to substitute in your Linode API key, SMTP server details and you will also likely have use the avail_foo() commands to grab the current IDs (e.g. for kernel release, distrubution, etc) and change the variables at the start of the script.

Your Linode IP currently being provisioned and will be ready momentarily.
Your Linode's IP is: %(linodeIp)s
Username: root
Password: %(rootPw)s
If your machine has SSH installed, you can use the following command to remote in:
# ssh root@%(linodeIp)s
Happy hacking!
Note: please do not reply to this message, this alias is not monitored.
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 = 'noreply@domain.com'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment