~kirkland/+junk/myFirstGo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
#
#    ubuntu-run-instances: ec2-run-instances that support human readable aliases for AMI's
#
#    Copyright (C) 2011 Dustin Kirkland <kirkland@ubuntu.com>
#
#    Authors: Dustin Kirkland <kirkland@ubuntu.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


import os, string, sys, urllib2

def parse_alias(word):
	# BUG: cloud-images.ubuntu.com should maintain a current->oneiric symlink
	# BUG: shall we debate these defaults?
	alias = {"release":"oneiric", "flavor":"server", "age":"daily", "rootstore":"ebs"}
	parts = string.split(word, ",")
	# Autodetct the alias parts; allows for arbitrary ordering of options
	for p in parts:
		if p in ["server", "desktop"]:
			alias["flavor"] = p
		elif p in ["daily", "released"]:
			alias["age"] = p
		elif p in ["ebs", "instance-store"]:
			alias["rootstore"] = p
		else:
			alias["release"] = p
	return alias

def is_alias(aliases, word):
	# An alias word is of the form:
	#     release[,flavor[,age[,rootstore]]]
	#   where:
	#     - release is one of [hardy|karmic|lucid|maverick|natty|oneiric], defaults to 'oneiric'
	#     - flavor is one of [server|desktop], defaults to 'server'
	#     - age is one of [daily|released], defaults to 'daily'
	#     - rootstore is one of [ebs|instance-store], defaults to 'ebs'
	# Note that they can be in ANY comma-separated order!
	alias = parse_alias(word)
	for a in aliases:
		parts = string.split(a)
		if len(parts) > 3 and parts[0] == alias["release"] and parts[1] == alias["flavor"] and parts[2] == alias["age"]:
			return True
	return False

def get_region(args):
	# Default to us-east-1 (least expensive option)
	region = "us-east-1"
	i = 0
	# Look for a region option
	while i < len(args):
		if args[i] == "--region":
			region = args[i+1]
		i += 1
	return region

def get_instance_type(args):
	# Default to t1.micro (least expensive option)
	instance_type = "t1.micro"
	i = 0
	# Look for an instance type option
	while i < len(args):
		if args[i] == "--instance-type" or args[i] == "-t":
			instance_type = args[i+1]
		i += 1
	return instance_type

def get_arch(instance_type):
	# Default to amd64 wherever possible
	if instance_type in ["c1.medium", "m1.small"]:
		return "i386"
	else:
		return "amd64"

def get_virt(instance_type):
	# Default to paravirtual
	if instance_type in ["cc1.4xlarge", "cg1.4xlarge"]:
		return "hvm"
	else:
		return "paravirtual"

def get_ami(region, instance_type, alias):
	alias = parse_alias(alias)
	request = urllib2.Request("https://cloud-images.ubuntu.com/query/%s/%s/%s.current.txt" % (alias["release"], alias["flavor"], alias["age"]))
	response = urllib2.urlopen(request)
	arch = get_arch(instance_type)
	virt = get_virt(instance_type)
	for line in response.readlines():
		parts = string.split(line)
		if len(parts) > 9 and parts[0] == alias["release"] and parts[1] == alias["flavor"] and parts[2] == alias["age"] and parts[5] == arch and parts[4] == alias["rootstore"] and parts[6] == region and parts[9] == virt:
			return parts[7]
	return False

def get_aliases():
	request = urllib2.Request("https://cloud-images.ubuntu.com/query/daily.latest.txt")
	response = urllib2.urlopen(request)
	image_aliases = []
	for line in response.readlines():
		image_aliases.append(line)
	request = urllib2.Request("https://cloud-images.ubuntu.com/query/released.latest.txt")
	response = urllib2.urlopen(request)
	for line in response.readlines():
		image_aliases.append(line)
	return image_aliases


################################################################################
# 1) Obtain the list of valid aliases
image_aliases = get_aliases()

# 2) Search and replace valid aliases in argv with ami-* values
i = 0
while i < len(sys.argv):
	if is_alias(image_aliases, sys.argv[i]):
		r = get_region(sys.argv)
		t = get_instance_type(sys.argv)
		ami = get_ami(r, t, sys.argv[i])
		if ami:
			sys.argv[i] = ami
		else:
			print("Invalid AMI Alias [%s]" % sys.argv[i])
			exit(1)
	i += 1

# 3) Execute
#os.execv("/bin/echo", sys.argv)
os.execv("/usr/bin/ec2-run-instances", sys.argv)
################################################################################