Class: EPC::Runner

Inherits:
Object show all
Includes:
Client, Error, Help, PersistentAttributes
Defined in:
lib/epc/runner.rb

Constant Summary

Constants included from Help

Help::COMMAND_USAGES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PersistentAttributes

#auth_token, #caller_id, #target_url

Methods included from Help

#display_command_usage, #display_usage, #display_verbose_usage

Constructor Details

#initialize(args = []) ⇒ Runner



13
14
15
16
17
# File 'lib/epc/runner.rb', line 13

def initialize(args = [])
  @args = args
  @options = Hash.new
  @parent_commands = %w(list define undefine refresh approve deny bind unbind attach detach submit copy set request)
end

Class Method Details

.run(args) ⇒ Object



9
10
11
# File 'lib/epc/runner.rb', line 9

def self.run(args)
  new(args).run
end

Instance Method Details

#parse_commandObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/epc/runner.rb', line 156

def parse_command
  begin
    command_name = @args.shift
    if command_name == "help" || command_name.nil?
      display_command_usage(*@args)
      exit 0
    else
      if command_name == "approve" || command_name == "deny"
        @args.shift
        klass_name = "EPC::Command::VoteDeploymentCommand"
        @options[:approval] = command_name
      elsif @parent_commands.include?(command_name)
        subcommand_name = @args.shift.gsub("-", "")
        klass_name = "EPC::Command::#{command_name.to_s.capitalize}#{subcommand_name.to_s.capitalize}Command"
      else
        klass_name = "EPC::Command::#{command_name.to_s.capitalize}Command"
      end
      command_klass = eval(klass_name).new(client, @options)
    end
  rescue NameError => ex
    say("No such command: #{command_name} #{subcommand_name}")
    display_verbose_usage
    exit 1
  end
end

#parse_options!Object



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/epc/runner.rb', line 71

def parse_options!
  opts_parser = OptionParser.new do |opts|
    opts.banner = "\nAvailable options:\n\n"

    opts.on('--email EMAIL')             { |email| @options[:email] = email }
    opts.on('--passwd PASS')             { |pass|  @options[:password] = pass }
    opts.on('--up-script FILE')          { |file| @options[:up_script] = file}
    opts.on('--down-script FILE')        { |file| @options[:down_script] = file}
    opts.on('--required')                { |required| @options[:required] = true }
    opts.on('--f FILE')                  { |file| @options[:file] = file }
    opts.on('--file FILE')                  { |file| @options[:file] = file }
    opts.on('--force')                   { |force| @options[:force] = true }
    opts.on('-s NAME')                   { |name| @options[:solution_name] = name}
    opts.on('-S NAME')                   { |name| @options[:stage_name] = name}
    opts.on('-u NAME')                   { |name| @options[:user_name] = name}
    opts.on('--stage NAME')              { |name| @options[:stage_name] = name}
    opts.on('-t SERVICE_TYPE')           { |service_type| @options[:service_type] = service_type}
    opts.on('--type TYPE')               { |type| @options[:type] = type}
    opts.on('--no-required')             { |required| @options[:required] = false }
    opts.on('--nodir')                   { |nodir| @options[:nodir] = true}
    opts.on('--value-type TYPE')         { |type| @options[:value_type] = type}
    opts.on('--dep-config NAME')         { |name| @options[:dep_config] = name}
    opts.on('-h', '--help')              { display_verbose_usage; exit }
    opts.on('-d DEPLOYMENT')             { |deployment| @options[:deployment_id] = deployment}
    opts.on('--compile')                 { |value| @options[:dependency_type] = 1}
    opts.on('--runtime')                 { |value| @options[:dependency_type] = 2}
    opts.on('--depends DEPENDENCY')      { |dependency| @options[:dependency] = dependency}
    opts.on('--group')                   { |group| @options[:group] = group}
    opts.on('--uri-name URI_NAME')       { |uri_name| @options[:uri_name] = uri_name}
    opts.on('--no-override')             { |val| @options[:no_override] = true }
    opts.on('--uselocal')                { |local| @options[:use_local] = true}
    opts.on('--replaces DEPLOYMENT')     { |deployment| @options[:replaces] = deployment}
    opts.on('--instances INSTANCES')     { |instances| @options[:instances] = instances}
    opts.on('--uris URIS')               { |uris| @options[:uris] = uris}
    opts.on('--scope SCOPE')             { |scope| @options[:scope] = scope}
    opts.on('--note NOTE')               { |note| @options[:note] = note}
    opts.on('--json')                    { |json| @options[:json] = true}
    opts.on('-y')                        { |skip| @options[:skip_prompts] = true}
    opts.on('--pom POM')                 { |pom| @options[:pom] = pom}
    opts.on('--add-user USER_ID')        { |user_id| @options[:add_user] = user_id}
    opts.on('--remove-user USER_ID')     { |user_id| @options[:remove_user] = user_id}
    opts.on('--language LANGUAGE')       { |language| @options[:language] = language}
    opts.on('--add-library LIB')         { |lib| @options[:add_library] = lib}
    opts.on('--remove-library LIB')      { |lib| @options[:remove_library] = lib}
    opts.on('--token TOKEN')             { |token| @options[:token] = token}
  opts.on('--name NAME')              { |name| @options[:name] = name}
    opts.on('--include-inactive')        { |bool| @options[:include_inactive] = true}
    opts.on('--target TARGET')          { |target| @options[:target] = target}
    opts.on('--metrics METRICS')         { |metrics| @options[:metrics] = metrics}
    opts.on('--start START')            { |start| @options[:start] = start}
    opts.on('--stop STOP')              { |stop| @options[:stop] = stop}
    opts.on('--step STEP')              { |step| @options[:step] = step}
    opts.on('--graph')                  { |bool| @options[:graph] = true}
    opts.on('--add-grant grant')         { |grant| @options[:add_grant] = grant}
    opts.on('--remove-grant grant')      { |grant| @options[:remove_grant] = grant}
    opts.on('--add-group group')         { |group| @options[:add_group] = group}
    opts.on('--remove-group group')      { |group| @options[:remove_group] = group}
    opts.on('--nopoll')                  { |poll| @options[:no_poll] = true}
    opts.on('--timeout TIMEOUT')         { |timeout| @options[:timeout] = timeout}
    opts.on('--extensions')              { |ext| @options[:extensions] = true}
    opts.on('--inherited')               { |inherited| @options[:inherited] = true}
    opts.on('--configuration CONFIGURATION')  { |configuration| @options[:configuration] = configuration}
    opts.on('--executions executions')  { |executions| @options[:executions] = executions}

    opts.on('-p NAME') do |name|
      if @options[:project_name].nil?
        @options[:project_name] = name
      else
        @options[:project_name] += "|#{name}"
      end
    end

    EPC::Command::CreateConfigCommand::CONFIG_LEVELS.keys.each do |opt|
      opts.on("--#{opt.to_s.gsub(/[_\ ]/, "-")} opt") {|param| @options[opt.to_sym] = param}
    end
  end

  begin
    @args = opts_parser.parse!(@args)
  rescue
    say("Invalid input.")
    exit
  end
end

#runObject



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
# File 'lib/epc/runner.rb', line 19

def run
  begin
    trap('TERM') { print "\nInterrupted\n"; exit(false)}
    trap('INT')  { print "\nInterrupted\n"; exit(false)}

    parse_options!
    command_klass = parse_command

    if command_klass
      begin
        code = command_klass.go(*@args)
        exit translate_exit_code(code)
      rescue InternalError => ex
        say("An internal error occured. Please contact the AgileMethods support team.")
        exit 1
      rescue InputError => ex
        say(ex.to_s)
        exit 1
      rescue FatalError => ex
        command_name = EPC::Config.underscore(command_klass.class.to_s.split("::").last)
        # say("USAGE: " + EPC::Help::COMMAND_USAGES[command_name.split("_")[0...-1].join("_").to_sym])
        say(ex.to_s)
        exit 1
      # rescue ArgumentError => ex
      #   command_name = EPC::Config.underscore(command_klass.class.to_s.split("::").last)
      #   say("Wrong number of attributes for #{command_name}")
      #   # say("USAGE: " + EPC::Help::COMMAND_USAGES[command_name.split("_")[0...-1].join("_").to_sym])
        exit 1
      rescue BaseClient::HTTPException => ex
        say(ex.to_s)
        exit 1
      rescue BaseClient::BadResponse => ex
        say(ex.to_s)
        exit 1
      rescue SocketError => ex
        say(ex.to_s)
        exit 1
      end
    end

  rescue OptionParser::InvalidOption => ex
  rescue OptionParser::AmbiguousOption => ex
    display_usage
  end
  exit 1
end

#translate_exit_code(code) ⇒ Object



66
67
68
69
# File 'lib/epc/runner.rb', line 66

def translate_exit_code(code)
  return code if code == 1 || code == 0
  return (code.to_s =~ /2\d\d/) || 1
end