regluit/bitnami_launch.ipynb

921 lines
29 KiB
Plaintext
Raw Normal View History

{
"metadata": {
"name": "bitnami_launch"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from regluit.sysadmin import aws"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reminding myself the relationship among various resources/entities in AWS\n",
"\n",
"* instances\n",
"* images\n",
"* volumes\n",
"* security groups\n",
"* keypairs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pull up all instances"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"instances = aws.all_instances()\n",
"len(instances)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 2,
"text": [
"13"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pull up instance by name"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest0_i = aws.instance('bitnamitest0')\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest0_i.state, bitnamitest0_i.image_id \n",
"\n",
"# ami-7afa9c13 -> bitnami-djangostack-1.4.5-0-linux-ubuntu-12.04.1-x86_64-ebs-mp-e490fac7-7e6f-4c5b-9f7d-51b555bf7bae-ami-b4d143dd.1 (ami-7afa9c13)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"(u'running', u'ami-7afa9c13')"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if bitnamitest0_i.state == 'stopped':\n",
" bitnamitest0_i.start()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 134
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest0_i.update()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 135,
"text": [
"u'running'"
]
}
],
"prompt_number": 135
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest0_i.ip_address"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 136,
"text": [
"u'23.20.67.193'"
]
}
],
"prompt_number": 136
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"how to get at Route53?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# boto.route53.connection\n",
"# http://boto.readthedocs.org/en/latest/ref/route53.html\n",
"\n",
"route53 = aws.boto.connect_route53()\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"route53.get_all_hosted_zones()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 27,
"text": [
"{u'ListHostedZonesResponse': {u'HostedZones': [{u'CallerReference': u'79E047AE-F7E6-0920-AB6A-DC6BA50AEB82',\n",
" u'Config': {u'Comment': u'primary domain for unglue.it service'},\n",
" u'Id': u'/hostedzone/ZNLXEIJIA7XKC',\n",
" u'Name': u'unglue.it.',\n",
" u'ResourceRecordSetCount': u'13'},\n",
" {u'CallerReference': u'56701A07-A460-7A4B-9A9B-AE72BE8C282F',\n",
" u'Config': {u'Comment': u'mispelling of unglu.it'},\n",
" u'Id': u'/hostedzone/Z240ILWPUJWS3F',\n",
" u'Name': u'unglu.it.',\n",
" u'ResourceRecordSetCount': u'4'},\n",
" {u'CallerReference': u'C5C90688-2FB3-BC15-8B7E-46FE623530A1',\n",
" u'Config': {u'Comment': u'.com domain for unglue.it'},\n",
" u'Id': u'/hostedzone/Z1Z8IXB17ZL7JL',\n",
" u'Name': u'unglueit.com.',\n",
" u'ResourceRecordSetCount': u'6'}],\n",
" u'IsTruncated': u'false',\n",
" u'MaxItems': u'100'}}"
]
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"unglue_it_zone =route53.get_hosted_zone_by_name('unglue.it')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 76
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"unglue_it_zone"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 77,
"text": [
"{u'GetHostedZoneResponse': {u'DelegationSet': {u'NameServers': [u'ns-1147.awsdns-15.org',\n",
" u'ns-299.awsdns-37.com',\n",
" u'ns-920.awsdns-51.net',\n",
" u'ns-1728.awsdns-24.co.uk']},\n",
" u'HostedZone': {u'CallerReference': u'79E047AE-F7E6-0920-AB6A-DC6BA50AEB82',\n",
" u'Config': {u'Comment': u'primary domain for unglue.it service'},\n",
" u'Id': u'/hostedzone/ZNLXEIJIA7XKC',\n",
" u'Name': u'unglue.it.',\n",
" u'ResourceRecordSetCount': u'13'}}}"
]
}
],
"prompt_number": 77
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest53.to_xml()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 82,
"text": [
"u'<ResourceRecordSet>\\n <Name>bitnamitest.unglue.it.</Name>\\n <Type>A</Type>\\n \\n \\n <TTL>60</TTL>\\n <ResourceRecords>\\n <ResourceRecord>\\n <Value>23.21.30.4</Value>\\n </ResourceRecord>\\n </ResourceRecords>\\n </ResourceRecordSet>'"
]
}
],
"prompt_number": 82
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets.commit()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "DNSServerError",
"evalue": "DNSServerError: 400 Bad Request\n<?xml version=\"1.0\"?>\n<ErrorResponse xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Error><Type>Sender</Type><Code>InvalidChangeBatch</Code><Message>Tried to delete resource record set [name='bitnami2.unglue.it.', type=A', version=0'] but the values provided do not match the current values</Message></Error><RequestId>48c071ad-bf4b-11e2-87a7-0bfb4d71c2e0</RequestId></ErrorResponse>",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mDNSServerError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-121-4355e992fc8a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrrsets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcommit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/raymondyee/.virtualenvs/regluitepd1/lib/python2.7/site-packages/boto/route53/record.pyc\u001b[0m in \u001b[0;36mcommit\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 147\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mboto\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnection\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mboto\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnect_route53\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 149\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchange_rrsets\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhosted_zone_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_xml\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 150\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mendElement\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconnection\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/raymondyee/.virtualenvs/regluitepd1/lib/python2.7/site-packages/boto/route53/connection.pyc\u001b[0m in \u001b[0;36mchange_rrsets\u001b[0;34m(self, hosted_zone_id, xml_body)\u001b[0m\n\u001b[1;32m 313\u001b[0m raise exception.DNSServerError(response.status,\n\u001b[1;32m 314\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreason\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 315\u001b[0;31m body)\n\u001b[0m\u001b[1;32m 316\u001b[0m \u001b[0me\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mboto\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjsonresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mElement\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 317\u001b[0m \u001b[0mh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mboto\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjsonresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mXmlHandler\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDNSServerError\u001b[0m: DNSServerError: 400 Bad Request\n<?xml version=\"1.0\"?>\n<ErrorResponse xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Error><Type>Sender</Type><Code>InvalidChangeBatch</Code><Message>Tried to delete resource record set [name='bitnami2.unglue.it.', type=A', version=0'] but the values provided do not match the current values</Message></Error><RequestId>48c071ad-bf4b-11e2-87a7-0bfb4d71c2e0</RequestId></ErrorResponse>"
]
}
],
"prompt_number": 121
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets = route53.get_all_rrsets('ZNLXEIJIA7XKC')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 85
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# http://stackoverflow.com/questions/9706883/using-route53-to-update-dns-with-boto-should-i-use-a-name-or-cname-and-what-ttl\n",
"# add_change(action, name, type, ttl=600, alias_hosted_zone_id=None, alias_dns_name=None, identifier=None, weight=None, region=None)\n",
"change = rrsets.add_change(\"CREATE\",\"bitnami2.unglue.it\", \"A\", ttl=60)\n",
"change.add_value(bitnamitest0_i.ip_address)\n",
"rrsets.commit()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 102,
"text": [
"{u'ChangeResourceRecordSetsResponse': {u'ChangeInfo': {u'Comment': u'None',\n",
" u'Id': u'/change/C25UE0Z5GIOBNT',\n",
" u'Status': u'PENDING',\n",
" u'SubmittedAt': u'2013-05-17T23:34:08.013Z'}}}"
]
}
],
"prompt_number": 102
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets.changes"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 129,
"text": [
"[['DELETE', <Record:bitnami2.unglue.it:A:23.20.67.193>]]"
]
}
],
"prompt_number": 129
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#http://hackinghabits.com/2011/08/20/boto-for-amazon-route53/\n",
"# even for deletion, value and TTL have to match record to delete. \n",
"\n",
"change = rrsets.add_change(\"DELETE\",\"bitnami2.unglue.it\", \"A\", ttl=60)\n",
"change.add_value(\"23.20.67.193\")\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 125
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets.to_xml()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 127,
"text": [
"'<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n <ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\">\\n <ChangeBatch>\\n <Comment>None</Comment>\\n <Changes><Change>\\n <Action>DELETE</Action>\\n <ResourceRecordSet>\\n <Name>bitnami2.unglue.it</Name>\\n <Type>A</Type>\\n \\n \\n <TTL>60</TTL>\\n <ResourceRecords>\\n <ResourceRecord>\\n <Value>23.20.67.193</Value>\\n </ResourceRecord>\\n </ResourceRecords>\\n </ResourceRecordSet>\\n </Change></Changes>\\n </ChangeBatch>\\n </ChangeResourceRecordSetsRequest>'"
]
}
],
"prompt_number": 127
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets.commit()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 128,
"text": [
"{u'ChangeResourceRecordSetsResponse': {u'ChangeInfo': {u'Comment': u'None',\n",
" u'Id': u'/change/C19PCC04HCHY7G',\n",
" u'Status': u'PENDING',\n",
" u'SubmittedAt': u'2013-05-17T23:43:36.190Z'}}}"
]
}
],
"prompt_number": 128
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# unglue.it\n",
"[rset for rset in route53.get_all_rrsets('ZNLXEIJIA7XKC')]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 83,
"text": [
"[<Record:unglue.it.:A:50.17.216.246>,\n",
" <Record:unglue.it.:MX:0 mx1.sub4.homie.mail.dreamhost.com.>,\n",
" <Record:unglue.it.:NS:ns-1147.awsdns-15.org.,ns-299.awsdns-37.com.,ns-920.awsdns-51.net.,ns-1728.awsdns-24.co.uk.>,\n",
" <Record:unglue.it.:SOA:ns-1147.awsdns-15.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400>,\n",
" <Record:\\052.unglue.it.:A:50.17.216.246>,\n",
" <Record:bitnamitest.unglue.it.:A:23.21.30.4>,\n",
" <Record:blog.unglue.it.:CNAME:unglueit.wordpress.com>,\n",
" <Record:just.unglue.it.:A:23.23.184.9>,\n",
" <Record:mail.unglue.it.:A:208.97.132.231>,\n",
" <Record:mail.unglue.it.:MX:0 mx1.sub4.homie.mail.dreamhost.com.>,\n",
" <Record:ry-dev.unglue.it.:A:54.224.124.254>,\n",
" <Record:webmail.unglue.it.:A:208.97.187.139>,\n",
" <Record:www.webmail.unglue.it.:A:208.97.187.139>]"
]
}
],
"prompt_number": 83
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest53 = route53.get_all_rrsets('ZNLXEIJIA7XKC')[5]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest53.name, bitnamitest53.type, bitnamitest53.ttl, bitnamitest53.resource_records"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 56,
"text": [
"(u'bitnamitest.unglue.it.', u'A', u'60', [u'23.21.30.4'])"
]
}
],
"prompt_number": 56
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# now try to change IP attached to bitnamitest53\n",
"\n",
"bitnamitest53.\n",
"\n",
"#bitnamitest0_i.ip_address"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 74,
"text": [
"'<ResourceRecordSet>\\n <Name>%(name)s</Name>\\n <Type>%(type)s</Type>\\n %(weight)s\\n %(body)s\\n </ResourceRecordSet>'"
]
}
],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"route53."
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"i0 = instances[0]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[aname for aname in dir(i0) if not aname.startswith(\"__\")]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": [
"['_in_monitoring_element',\n",
" '_placement',\n",
" '_previous_state',\n",
" '_state',\n",
" '_update',\n",
" 'add_tag',\n",
" 'ami_launch_index',\n",
" 'architecture',\n",
" 'block_device_mapping',\n",
" 'client_token',\n",
" 'confirm_product',\n",
" 'connection',\n",
" 'create_image',\n",
" 'dns_name',\n",
" 'ebs_optimized',\n",
" 'endElement',\n",
" 'eventsSet',\n",
" 'get_attribute',\n",
" 'get_console_output',\n",
" 'group_name',\n",
" 'groups',\n",
" 'hypervisor',\n",
" 'id',\n",
" 'image_id',\n",
" 'instance_profile',\n",
" 'instance_type',\n",
" 'interfaces',\n",
" 'ip_address',\n",
" 'item',\n",
" 'kernel',\n",
" 'key_name',\n",
" 'launch_time',\n",
" 'modify_attribute',\n",
" 'monitor',\n",
" 'monitored',\n",
" 'monitoring',\n",
" 'persistent',\n",
" 'placement',\n",
" 'placement_group',\n",
" 'placement_tenancy',\n",
" 'platform',\n",
" 'previous_state',\n",
" 'previous_state_code',\n",
" 'private_dns_name',\n",
" 'private_ip_address',\n",
" 'product_codes',\n",
" 'public_dns_name',\n",
" 'ramdisk',\n",
" 'reason',\n",
" 'reboot',\n",
" 'region',\n",
" 'remove_tag',\n",
" 'requester_id',\n",
" 'reset_attribute',\n",
" 'root_device_name',\n",
" 'root_device_type',\n",
" 'spot_instance_request_id',\n",
" 'start',\n",
" 'startElement',\n",
" 'state',\n",
" 'state_code',\n",
" 'state_reason',\n",
" 'stop',\n",
" 'subnet_id',\n",
" 'tags',\n",
" 'terminate',\n",
" 'unmonitor',\n",
" 'update',\n",
" 'use_ip',\n",
" 'virtualization_type',\n",
" 'vpc_id']"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[aws.instance_info(i) for i in instances]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 6,
"text": [
"[{'id': u'i-4f7a8327',\n",
" 'ip_address': u'23.21.30.4',\n",
" 'name_tag': u'bitnamitest0',\n",
" 'tags': {u'Name': u'bitnamitest0'}},\n",
" {'id': u'i-512dd634',\n",
" 'ip_address': None,\n",
" 'name_tag': u'selenium-dev',\n",
" 'tags': {u'Name': u'selenium-dev'}},\n",
" {'id': u'i-f17cc397',\n",
" 'ip_address': u'54.224.124.254',\n",
" 'name_tag': u'ry-dev',\n",
" 'tags': {u'Name': u'ry-dev'}},\n",
" {'id': u'i-41f44a27',\n",
" 'ip_address': u'23.23.184.9',\n",
" 'name_tag': u'just',\n",
" 'tags': {u'Name': u'just'}},\n",
" {'id': u'i-1749f371',\n",
" 'ip_address': None,\n",
" 'name_tag': u'please',\n",
" 'tags': {u'Name': u'please'}},\n",
" {'id': u'i-233b5845',\n",
" 'ip_address': None,\n",
" 'name_tag': u'web1_large',\n",
" 'tags': {u'Name': u'web1_large'}},\n",
" {'id': u'i-bd5264db',\n",
" 'ip_address': None,\n",
" 'name_tag': u'web1_medium',\n",
" 'tags': {u'Name': u'web1_medium'}},\n",
" {'id': u'i-e606249f',\n",
" 'ip_address': None,\n",
" 'name_tag': u'web1_micro',\n",
" 'tags': {u'Name': u'web1_micro'}},\n",
" {'id': u'i-2b43a450',\n",
" 'ip_address': None,\n",
" 'name_tag': u'web1_highcpumed1',\n",
" 'tags': {u'Name': u'web1_highcpumed1'}},\n",
" {'id': u'i-5197452c',\n",
" 'ip_address': u'23.20.143.15',\n",
" 'name_tag': u'jenkins_small',\n",
" 'tags': {u'Name': u'jenkins_small'}},\n",
" {'id': u'i-2d379250',\n",
" 'ip_address': None,\n",
" 'name_tag': u'web2dj14',\n",
" 'tags': {u'Name': u'web2dj14'}},\n",
" {'id': u'i-37b70c4a',\n",
" 'ip_address': None,\n",
" 'name_tag': u'web2dj14_med',\n",
" 'tags': {u'Name': u'web2dj14_med'}},\n",
" {'id': u'i-4ec6fd33',\n",
" 'ip_address': u'50.17.216.246',\n",
" 'name_tag': u'web2_relauch_c_med',\n",
" 'tags': {u'Name': u'web2_relauch_c_med'}}]"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"aws.extraneous_snapshot_ids()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 7,
"text": [
"set([u'snap-09159e7e',\n",
" u'snap-7d760501',\n",
" u'snap-d957d0ae',\n",
" u'snap-9b3f7bf9',\n",
" u'snap-e313b99d',\n",
" u'snap-ea5da29e',\n",
" u'snap-13f59c6f',\n",
" u'snap-23f09e5f',\n",
" u'snap-f957ee8d',\n",
" u'snap-88c4a8ec',\n",
" u'snap-ba5e0ec7',\n",
" u'snap-9d113ee1',\n",
" u'snap-1ada476f',\n",
" u'snap-35f43345',\n",
" u'snap-b5d341f4'])"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"aws.stats_for_instances(aws.all_instances())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 8,
"text": [
"[(u'i-4f7a8327',\n",
" u'bitnamitest0',\n",
" u'rdhyee_public_key',\n",
" u'running',\n",
" u'23.21.30.4',\n",
" u'ec2-23-21-30-4.compute-1.amazonaws.com'),\n",
" (u'i-512dd634', u'selenium-dev', u'selenium-dev', u'stopped', None, ''),\n",
" (u'i-f17cc397',\n",
" u'ry-dev',\n",
" None,\n",
" u'running',\n",
" u'54.224.124.254',\n",
" u'ec2-54-224-124-254.compute-1.amazonaws.com'),\n",
" (u'i-41f44a27',\n",
" u'just',\n",
" None,\n",
" u'running',\n",
" u'23.23.184.9',\n",
" u'ec2-23-23-184-9.compute-1.amazonaws.com'),\n",
" (u'i-1749f371', u'please', None, u'stopped', None, ''),\n",
" (u'i-233b5845', u'web1_large', None, u'stopped', None, ''),\n",
" (u'i-bd5264db', u'web1_medium', None, u'stopped', None, ''),\n",
" (u'i-e606249f', u'web1_micro', None, u'stopped', None, ''),\n",
" (u'i-2b43a450', u'web1_highcpumed1', None, u'stopped', None, ''),\n",
" (u'i-5197452c',\n",
" u'jenkins_small',\n",
" None,\n",
" u'running',\n",
" u'23.20.143.15',\n",
" u'ec2-23-20-143-15.compute-1.amazonaws.com'),\n",
" (u'i-2d379250', u'web2dj14', None, u'stopped', None, ''),\n",
" (u'i-37b70c4a', u'web2dj14_med', None, u'stopped', None, ''),\n",
" (u'i-4ec6fd33',\n",
" u'web2_relauch_c_med',\n",
" None,\n",
" u'running',\n",
" u'50.17.216.246',\n",
" u'ec2-50-17-216-246.compute-1.amazonaws.com')]"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(aws.all_images())"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[s.description for s in aws.all_snapshots()]"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[i.id for i in aws.all_images()]"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import re\n",
"\n",
"rexp = re.compile(r'(ami-\\S+)')\n",
"d = u'Created by CreateImage(i-4ba65a2e) for ami-2d865744 from vol-d58f99b8'\n",
"\n",
"images = set([i.id for i in aws.all_images()])\n",
"\n",
"s = re.search(rexp, d)\n",
"s.group(1)\n",
"\n",
"snapshot_amis = set([re.search(rexp,s.description).group(1) for s in aws.all_snapshots()])\n",
"\n",
"# overlap\n",
"\n",
"print \"overlap\", images & snapshot_amis\n",
"\n",
"print \"in snapshot_amis, but not images\", snapshot_amis - images\n",
"print \"in images, but not snapshot_amis\", images - snapshot_amis\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Consolidate code to pull up route53 record \n",
"\n",
"def route53_records(domain_name, name, record_type):\n",
" \n",
" zone = route53.get_hosted_zone_by_name(domain_name)\n",
" \n",
" if zone is None:\n",
" return ([], None)\n",
" \n",
" zone_id = zone.GetHostedZoneResponse.Id.replace('/hostedzone/', '')\n",
" rrsets = route53.get_all_rrsets(zone_id)\n",
" \n",
" full_name = \"{0}.{1}.\".format(name, domain_name)\n",
" \n",
" return ([rset for rset in route53.get_all_rrsets(zone_id) if rset.name == full_name and rset.type == record_type ],\n",
" rrsets)\n",
" \n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 177
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#http://hackinghabits.com/2011/08/20/boto-for-amazon-route53/\n",
"# even for deletion, value and TTL have to match record to delete. \n",
"\n",
"(rrsets0, rrsets) = route53_records('unglue.it', 'bitnamitest', 'A')\n",
"\n",
"# if an old record exists, the get rid of it\n",
"\n",
"if len(rrsets0) == 1:\n",
" change = rrsets.add_change(\"DELETE\", rrsets0[0].name, rrsets0[0].type, rrsets0[0].ttl)\n",
" change.add_value(rrsets0[0].resource_records[0])\n",
" rrsets.commit()\n",
" rrsets.changes[:] = []\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 190
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bitnamitest0_i = aws.instance('bitnamitest0')\n",
"\n",
"change = rrsets.add_change(\"CREATE\", 'bitnamitest.unglue.it.', 'A', 60)\n",
"change.add_value(bitnamitest0_i.ip_address)\n",
"rrsets.commit()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 197,
"text": [
"{u'ChangeResourceRecordSetsResponse': {u'ChangeInfo': {u'Comment': u'None',\n",
" u'Id': u'/change/C1SXJFL1W6P1OX',\n",
" u'Status': u'PENDING',\n",
" u'SubmittedAt': u'2013-05-18T21:05:39.338Z'}}}"
]
}
],
"prompt_number": 197
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets.changes[:] = []"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 195
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rrsets.changes"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 196,
"text": [
"[]"
]
}
],
"prompt_number": 196
}
],
"metadata": {}
}
]
}