~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to lib/parse_args.rb

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby
 
2
# Programmer: Chris Bunch
 
3
 
 
4
FILE_FLAG_NOT_VALID_MSG = "File must be either a directory or end with .tar.gz"
 
5
IPS_FLAG_NOT_A_YAML_MSG = "YAML file must end with .yaml or .yml"
 
6
MIN_FLAG_NOT_A_NUM_MSG = "Min images must be a positive integer"
 
7
MAX_FLAG_NOT_A_NUM_MSG = "Max images must be a positive integer"
 
8
TABLE_FLAG_NOT_IN_SET_MSG = "Invalid table type. Table must be set to one of the following: #{VALID_TABLE_TYPES.join(', ')}"
 
9
MIN_FLAG_NOT_POSITIVE_MSG = "Minimum image number must be larger than zero"
 
10
MAX_FLAG_NOT_POSITIVE_MSG = "Maximum image number must be larger than zero"
 
11
 
 
12
MAX_SMALLER_THAN_MIN_MSG = "Maximum image number must be larger than the minimum image number"
 
13
 
 
14
YAML_CONTROL_MSG = "The provided IP yaml file did not have one IP address for the controller node"
 
15
 
 
16
EC2_USAGE_MSG = "You did not provide an ips.yaml file, and you did not provide a machine id."
 
17
EC2_IPS_MISSING_MSG = "You did not provide ips.yaml or its empty."
 
18
INSTANCE_FLAG_NOT_IN_SET_MSG = "The instance type you provided was not one of the allowed values. Currently we allow m1.large, m1.xlarge, and c1.xlarge as the instance types."
 
19
 
 
20
DJINN_SERVER_DIED_MSG = "\nEither the head node was unable to get IP addresses for any slave nodes or a bug in the head node's code caused it to crash. We have left your virtual machines running in case you wish to submit a bug report or investigate further via the Troubleshooting page."
 
21
 
 
22
RW_REQUIRES_VOLDEMORT_MSG = "The r and w flags can only be used in conjunction with the Voldemort database. Please either specify the voldemort database to be used or remove the r and w flags."
 
23
 
 
24
BACKUP_TAR_EXISTS_MSG = "The tar file you specified to back up to already exists. Please specify a new file name and try again."
 
25
RESTORE_TAR_NOT_EXISTS_MSG = "The tar file you specified to back up from does not exist. Please specify a new file name and try again."
 
26
 
 
27
FILE_REGEX = /\.tar\.gz$/
 
28
POS_NUM_REGEX = /^[1-9]\d*$/
 
29
TAR_REGEX = /\.tar\.gz$/
 
30
YAML_REGEX = /\.ya?ml$/
 
31
 
 
32
def parse_args(command_line)
 
33
  raise if command_line.class != Array
 
34
  
 
35
  arg_hash = {}
 
36
  
 
37
  arg_found = nil
 
38
  command_line.each { |arg|
 
39
    if arg[0].chr == "-"
 
40
      arg = arg[1, arg.length]
 
41
      arg = arg[1, arg.length] if arg[0].chr == "-" # to handle --arg
 
42
      arg_found = arg
 
43
      if !ALL_FLAGS.include?(arg)
 
44
        abort "The flag #{arg} cannot be used here.\n\n#{USAGE}"
 
45
      end
 
46
      
 
47
      arg_hash[arg] = "NO ARG"
 
48
    elsif !arg_found.nil?
 
49
      arg_hash[arg_found] = arg
 
50
      arg_found = nil
 
51
    else
 
52
      abort "The parameter #{arg} was specified without a corresponding flag."
 
53
    end
 
54
  }
 
55
  
 
56
  return arg_hash
 
57
end
 
58
 
 
59
arg_hash = parse_args(ARGV)
 
60
 
 
61
flags = arg_hash.keys
 
62
flags.each { |flag|
 
63
  unless ALL_FLAGS.include?(flag)
 
64
    message = "Flag #{flag} not recognized.\n\n" + USAGE
 
65
    abort(message)
 
66
  end
 
67
}
 
68
 
 
69
if arg_hash['help'] || arg_hash['h'] || arg_hash['usage']
 
70
  abort(USAGE)
 
71
end
 
72
 
 
73
if arg_hash['version']
 
74
  abort(AS_VERSION)
 
75
end
 
76
 
 
77
if arg_hash['min']
 
78
  abort(MIN_FLAG_NOT_A_NUM_MSG) if arg_hash['min'] !~ POS_NUM_REGEX
 
79
  min_images = Integer(arg_hash['min'])
 
80
  if min_images < 1
 
81
    abort("--min needs to be at least one")
 
82
  end
 
83
else
 
84
  #min_images = 4
 
85
end
 
86
 
 
87
if arg_hash['max']
 
88
  abort(MAX_FLAG_NOT_A_NUM_MSG) if arg_hash['max'] !~ POS_NUM_REGEX
 
89
  max_images = Integer(arg_hash['max'])
 
90
  
 
91
  unless arg_hash['min']
 
92
    min_images = max_images
 
93
  end
 
94
else
 
95
  #max_images = min_images
 
96
end
 
97
 
 
98
#if min_images > max_images
 
99
#  abort(MAX_SMALLER_THAN_MIN_MSG)
 
100
#end
 
101
 
 
102
if arg_hash['file']
 
103
  true_location = File.expand_path(arg_hash['file'])
 
104
  file_doesnt_exist = "The specified AppEngine program, #{arg_hash['file']}," + 
 
105
    " didn't exist. Please specify one that exists and try again"
 
106
  abort(file_doesnt_exist) unless File.exists?(true_location)
 
107
 
 
108
  if not ((File.directory?(true_location)) or (true_location =~ TAR_REGEX))
 
109
    abort(FILE_FLAG_NOT_VALID_MSG) 
 
110
  end
 
111
  FILE_LOCATION = arg_hash['file']
 
112
else
 
113
  FILE_LOCATION = nil
 
114
end
 
115
 
 
116
if arg_hash['table']
 
117
  abort(TABLE_FLAG_NOT_IN_SET_MSG) unless VALID_TABLE_TYPES.include?(arg_hash['table'])
 
118
  TABLE = arg_hash['table']
 
119
else
 
120
  TABLE = "voldemort"
 
121
end
 
122
 
 
123
if arg_hash['infrastructure']
 
124
  abort(INFRASTRUCTURE_FLAG_NOT_IN_SET_MSG) unless VALID_CLOUD_TYPES.include?(arg_hash['infrastructure'])
 
125
  INFRASTRUCTURE = arg_hash['infrastructure']
 
126
else
 
127
  INFRASTRUCTURE = nil
 
128
end
 
129
 
 
130
if arg_hash['ips']
 
131
  begin
 
132
    IPS = YAML.load_file(arg_hash['ips'])
 
133
  rescue Errno::ENOENT
 
134
    ips_doesnt_exist = "The configuration file you specified, " + 
 
135
      "#{arg_hash['ips']}, did not exist. Please specify one that" + 
 
136
      " exists and try again."
 
137
    abort(ips_doesnt_exist)
 
138
  end
 
139
else
 
140
  IPS = nil
 
141
end
 
142
 
 
143
if arg_hash['n']
 
144
  abort("n must be a positive integer") if arg_hash['n'] !~ POS_NUM_REGEX
 
145
  
 
146
  REPLICATION = Integer(arg_hash['n'])
 
147
else
 
148
  REPLICATION = nil
 
149
end
 
150
 
 
151
if arg_hash['r']
 
152
  abort(RW_REQUIRES_VOLDEMORT_MSG) if TABLE != "voldemort"
 
153
  
 
154
  VOLDEMORT_R = Integer(arg_hash['r'])
 
155
else
 
156
  VOLDEMORT_R = nil
 
157
end
 
158
 
 
159
if arg_hash['w']
 
160
  abort(RW_REQUIRES_VOLDEMORT_MSG) if TABLE != "voldemort"
 
161
  
 
162
  VOLDEMORT_W = Integer(arg_hash['w'])
 
163
else
 
164
  VOLDEMORT_W = nil
 
165
end
 
166
 
 
167
MIN_IMAGES = min_images
 
168
MAX_IMAGES = max_images
 
169
 
 
170
if arg_hash['machine']
 
171
  MACHINE = arg_hash['machine']
 
172
  if MACHINE == "NO ARG"
 
173
    abort("You failed to provide an argument for the #{flag} flag. Please do so and try again.")
 
174
  end
 
175
else
 
176
  MACHINE = ENV['APPSCALE_MACHINE']
 
177
end 
 
178
 
 
179
possible_instance_types = ["m1.large", "m1.xlarge", "c1.xlarge"]
 
180
if arg_hash['instance_type']
 
181
  abort(INSTANCE_FLAG_NOT_IN_SET_MSG) unless possible_instance_types.include?(arg_hash['instance_type'])
 
182
  INSTANCE_TYPE = arg_hash['instance_type']
 
183
else
 
184
  INSTANCE_TYPE = "m1.large"
 
185
end
 
186
 
 
187
if arg_hash['v'] or arg_hash['verbose']
 
188
  @@verbose = true
 
189
else
 
190
  @@verbose = false
 
191
end
 
192
 
 
193
if arg_hash['scp']
 
194
  SCP = true
 
195
else
 
196
  SCP = false
 
197
end
 
198
 
 
199
if arg_hash['test']
 
200
  TEST = true
 
201
else
 
202
  TEST = false
 
203
end
 
204
 
 
205
if arg_hash['keyname']
 
206
  KEYNAME = arg_hash['keyname']
 
207
else
 
208
  KEYNAME = "appscale"
 
209
end
 
210
 
 
211
if arg_hash['appname']
 
212
  APPNAME = arg_hash['appname'].gsub(/[^\w\d-]/, "")
 
213
else
 
214
  APPNAME = nil
 
215
end
 
216
 
 
217
if arg_hash['appengine']
 
218
  APPENGINE = Integer(arg_hash['appengine'])
 
219
else
 
220
  APPENGINE = 3
 
221
end
 
222
 
 
223
if arg_hash['force']
 
224
  FORCE = true
 
225
else
 
226
  FORCE = false
 
227
end
 
228
 
 
229
if arg_hash['separate']
 
230
  SEPARATE = true
 
231
else
 
232
  SEPARATE = false
 
233
end
 
234
 
 
235
if arg_hash['email']
 
236
  EMAIL = arg_hash['email']
 
237
else
 
238
  EMAIL = false
 
239
end
 
240
 
 
241
CONFIRM = !arg_hash['confirm'].nil?
 
242
 
 
243
if arg_hash['backup_to_tar']
 
244
  if File.exists?(arg_hash['backup_to_tar'])
 
245
    abort(BACKUP_TAR_EXISTS_MSG)
 
246
  end
 
247
 
 
248
  BACKUP_TAR_LOCATION = arg_hash['backup_to_tar']
 
249
else
 
250
  BACKUP_TAR_LOCATION = nil
 
251
end
 
252
 
 
253
if arg_hash['backup_to_ebs']
 
254
  BACKUP_EBS_LOCATION = arg_hash['backup_to_ebs']
 
255
else
 
256
  BACKUP_EBS_LOCATION = nil
 
257
end
 
258
 
 
259
if arg_hash['restore_from_tar']
 
260
  unless File.exists?(arg_hash['restore_from_tar'])
 
261
    abort(RESTORE_TAR_NOT_EXISTS_MSG)
 
262
  end
 
263
 
 
264
  RESTORE_FROM_TAR = arg_hash['restore_from_tar']
 
265
else
 
266
  RESTORE_FROM_TAR = nil
 
267
end
 
268
 
 
269
if arg_hash['restore_from_ebs']
 
270
  RESTORE_FROM_EBS = arg_hash['restore_from_ebs']
 
271
else
 
272
  RESTORE_FROM_EBS = nil
 
273
end
 
274
 
 
275
if FILE_LOCATION && (RESTORE_FROM_TAR || RESTORE_FROM_EBS)
 
276
  bad_restore_params = "You cannot restore an AppScale instance " + 
 
277
    "and upload a new application. Please remove one and try again."
 
278
  abort(bad_restore_params)
 
279
end