ec2launcher 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,166 @@
1
+ #
2
+ # Copyright (c) 2012 Sean Laurent
3
+ #
4
+ require 'rubygems'
5
+ require 'optparse'
6
+ require 'ostruct'
7
+
8
+ require 'ec2launcher/defaults'
9
+
10
+ class InitOptions
11
+ attr_reader :command
12
+ attr_reader :options
13
+ attr_reader :location
14
+
15
+ SUB_COMMANDS = %w{init launch}
16
+
17
+ def initialize
18
+ @opts = OptionParser.new do |opts|
19
+ opts.banner = "Usage: ec2launcher [COMMAND]
20
+
21
+ where [COMMAND] is one of:
22
+
23
+ init [LOCATION] Initialize a repository in the specified directory.
24
+ launch [OPTIONS] Launch a new instance.
25
+
26
+ and [OPTIONS] include:
27
+
28
+ "
29
+ opts.separator "Query options:"
30
+
31
+ opts.on("-l", "--list", "Show environments and applications.") do
32
+ @options.list = true
33
+ end
34
+
35
+ opts.on("-s", "--show-defaults", "Show settings, but do not launch any instances.") do
36
+ @options.show_defaults = true
37
+ end
38
+
39
+ opts.separator ""
40
+ opts.separator "Required Launch options:"
41
+
42
+ opts.on("-e", "--environment ENV", "The environment for the server.") do |env|
43
+ @options.environ = env
44
+ end
45
+
46
+ opts.on("-a", "--application NAME", "The name of the application class for the new server.") do |app_name|
47
+ @options.application = app_name
48
+ end
49
+
50
+ opts.separator ""
51
+ opts.separator "Additional launch options:"
52
+
53
+ opts.on("--command [CMD]", "Additional command to run during launch sequence.") do |command|
54
+ @options.commands << command
55
+ end
56
+
57
+ opts.on("-c", "--clone HOST", "Clone the latest snapshots from a specific host.") do |clone_host|
58
+ @options.clone_host = clone_host
59
+ end
60
+
61
+ opts.separator ""
62
+ opts.separator "Overrides:"
63
+
64
+ opts.on("-d", "--directory DIRECTORY", "Location of configuration directory. Defaults to current directory.") do |directory|
65
+ @options.directory = directory
66
+ end
67
+
68
+ opts.on("-h", "--hostname NAME", "The name for the new server.") do |hostname|
69
+ @options.hostname = hostname
70
+ end
71
+
72
+ opts.on("-u", "--chef-validation-url", "URL for the Chef validation pem file.") do |chef_validation_url|
73
+ @options.chef_validation_url = chef_validation_url
74
+ end
75
+
76
+ opts.on("--ami AMI_ID", "AMI id") do |ami_id|
77
+ @options.ami_id = ami_id
78
+ end
79
+
80
+ opts.on("-z", "--availability-zone ZONE", EC2Launcher::AVAILABILITY_ZONES, "AWS availability zone (#{EC2Launcher::AVAILABILITY_ZONES.join(', ')}).") do |zone|
81
+ @options.zone = zone
82
+ end
83
+
84
+ opts.on("-i", "--instance-type TYPE", EC2Launcher::INSTANCE_TYPES, "EC2 instance type (#{EC2Launcher::INSTANCE_TYPES.join(', ')}).") do |instance_type|
85
+ @options.instance_type = instance_type
86
+ end
87
+
88
+ opts.on("-v", "--volume-size SIZE", Integer, "EBS volume size in GB. Defaults to #{EC2Launcher::DEFAULT_VOLUME_SIZE} GB") do |volume_size|
89
+ @options.volume_size = volume_size
90
+ end
91
+
92
+ opts.separator ""
93
+ opts.separator "AWS Security Options:"
94
+
95
+ opts.on("--access-key KEY", "Amazon access key. Overrides AWS_ACCESS_KEY environment variable.") do |access_key|
96
+ @options.access_key = access_key
97
+ end
98
+
99
+ opts.on("--secret SECRET", "Amazon secret access key. Overrides AWS_SECRET_ACCESS_KEY environment variable.") do |secret|
100
+ @options.secret = secret
101
+ end
102
+
103
+ opts.separator ""
104
+ opts.separator "Common options:"
105
+
106
+ # No argument, shows at tail. This will print an options summary.
107
+ # Try it and see!
108
+ opts.on_tail("-?", "--help", "Show this message") do
109
+ puts opts
110
+ exit
111
+ end
112
+ end
113
+ end
114
+
115
+ def parse(args)
116
+ @command = args.shift
117
+ unless SUB_COMMANDS.include?(@command)
118
+ puts "Missing command! " if @command.nil?
119
+ puts "Invalid command: #{@command}" unless @command.nil? || @command == "-?" || @command == "--help"
120
+ puts @opts
121
+ exit 1
122
+ end
123
+
124
+ @options = OpenStruct.new
125
+ @options.list = false
126
+ @options.show_defaults = false
127
+
128
+ @options.environ = nil
129
+ @options.application = nil
130
+ @options.commands = []
131
+ @options.clone_host = nil
132
+
133
+ @options.ami_id = nil
134
+ @options.hostname = nil
135
+ @options.zone = nil
136
+ @options.instance_type = nil
137
+ @options.volume_size = nil
138
+
139
+ @options.directory = "./"
140
+
141
+ if @command == "init"
142
+ unless args.length >= 1
143
+ puts "Missing location!"
144
+ puts
145
+ help
146
+ exit 1
147
+ end
148
+ @location = args[0]
149
+ else
150
+ @opts.parse!(args)
151
+
152
+ if (@options.environ.nil? || @options.application.nil?) && ! @options.list
153
+ puts "Missing a required parameter: #{@options.environ.nil? ? '--environment' : '--application'}"
154
+ puts
155
+ help
156
+ exit 1
157
+ end
158
+ end
159
+
160
+ @options
161
+ end
162
+
163
+ def help
164
+ puts @opts
165
+ end
166
+ end
@@ -0,0 +1,6 @@
1
+ #
2
+ # Copyright (c) 2012 Sean Laurent
3
+ #
4
+ module Ec2launcher
5
+ VERSION = "1.0.0"
6
+ end