Skip to content

Instantly share code, notes, and snippets.

@greinacker
Created February 9, 2016 21:50
Show Gist options
  • Save greinacker/3888373ee4a5cb54732c to your computer and use it in GitHub Desktop.
Save greinacker/3888373ee4a5cb54732c to your computer and use it in GitHub Desktop.

Revisions

  1. greinacker created this gist Feb 9, 2016.
    46 changes: 46 additions & 0 deletions ec2_start.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    # coding: utf-8
    import sys; sys.path.append('../boto-module')
    import boto.ec2
    import time
    import keychain
    import clipboard
    import console
    import webbrowser

    instance_id = "i-123456ab"
    key = "ABCDEFGHIJKLMNOPQRST"

    return_url = None
    if len(sys.argv) >= 2:
    return_url = sys.argv[1]

    secret = keychain.get_password("aws", key)
    if secret == None:
    secret = console.password_alert("Enter secret key")
    keychain.set_password("aws",key,secret)

    print "Connecting"
    ec2_conn = boto.connect_ec2(aws_access_key_id=key,aws_secret_access_key=secret)

    print "Starting instance"
    instances = ec2_conn.start_instances(instance_ids=[instance_id])
    instance = instances[0]

    print "Waiting for IP"
    ip_address = None
    waiting = True
    while waiting:
    status = instance.update()
    print status
    ip_address = instance.ip_address
    if ip_address != None:
    waiting = False
    time.sleep(2)

    print "Public IP: {}".format(ip_address)

    clipboard.set(ip_address)
    print "IP address {} copied to clipboard".format(ip_address)

    if return_url != None:
    webbrowser.open(return_url)
    33 changes: 33 additions & 0 deletions ec2_stop.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # coding: utf-8
    import sys; sys.path.append('../boto-module')
    import boto.ec2
    import time
    import keychain
    import clipboard
    import console
    import webbrowser

    instance_id = "i-123456ab"
    key = "ABCDEFGHIJKLMNOPQRST"

    return_url = None
    if len(sys.argv) >= 2:
    return_url = sys.argv[1]

    secret = keychain.get_password("aws", key)
    if secret == None:
    secret = console.password_alert("Enter secret key")
    keychain.set_password("aws",key,secret)

    print "Connecting"
    ec2_conn = boto.connect_ec2(aws_access_key_id=key,aws_secret_access_key=secret)

    print "Stopping instance"
    ec2_conn.stop_instances(instance_ids=[instance_id])

    msg = "Shutdown in progress"
    clipboard.set(msg)
    print msg

    if return_url != None:
    webbrowser.open(return_url)