Class: Apmate::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/apmate/cli.rb

Defined Under Namespace

Modules: Command

Constant Summary collapse

APMATE_BASE_URL =
ENV['APMATE_BASE_URL'] || 'http://localhost'
APMATE_PORT =
ENV['APMATE_PORT'] || '3000'
APMATE_URL =
"#{APMATE_BASE_URL}:#{APMATE_PORT}"
ENDPOINTS =
{
  get_artifact_environment: '/api/artifacts/environment',
  get_artifact: '/api/artifacts',
  get_artifact_name: '/api/artifacts/name',
  mass_promote_environment: '/api/applications/mass_promote',
  promote_environment: '/api/environments/promote',
  create_execution: '/api/executions',
  publish_artifact: '/api/artifacts/publish'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apmate_apply(yaml_hash, opts) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/apmate/cli.rb', line 85

def self.apmate_apply(yaml_hash, opts)
  Apmate::Logger.logger.debug "Beginning post request."
  
  endpoint_object = yaml_hash['kind'].downcase.pluralize
  Apmate::Logger.logger.debug "endpoint object: #{endpoint_object}"
  
  uri = URI("#{Apmate::CLI::APMATE_BASE_URL}:#{Apmate::CLI::APMATE_PORT}/api/#{endpoint_object}/apply")
  Apmate::Logger.logger.debug "uri: #{uri}"
  
  payload = {'name' => yaml_hash['metadata']['name']}.merge yaml_hash['spec']
  Apmate::Logger.logger.debug "payload: #{payload}"
  
  apmate_post uri, payload, opts
end

.apmate_create_execution(app_name, env_name, comp_name, action, opts) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/apmate/cli.rb', line 62

def self.apmate_create_execution(app_name, env_name, comp_name, action, opts)
  uri = URI("#{Apmate::CLI::APMATE_BASE_URL}:#{Apmate::CLI::APMATE_PORT}#{ENDPOINTS[:create_execution]}")
  params = {
    execution: {
      application_name: app_name,
      environment_name: env_name,
      component_name: comp_name,
      action: action
    }
  }
  apmate_post uri, params, opts
end

.apmate_get(endpoint, opts, params = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/apmate/cli.rb', line 75

def self.apmate_get(endpoint, opts, params=nil)
  Apmate::Logger.logger.debug "Beginning get request."
  uri = URI("#{Apmate::CLI::APMATE_URL}#{endpoint}")
  Apmate::Logger.logger.debug "uri: #{uri}"
  uri.query = URI.encode_www_form(params)
  response = Net::HTTP.get_response(uri)
  output = JSON.pretty_generate(JSON.parse(response.body))
  log_output(output, opts)
end

.apmate_get_artifact(artifact_name, opts) ⇒ Object



57
58
59
60
# File 'lib/apmate/cli.rb', line 57

def self.apmate_get_artifact(artifact_name, opts)
  params = {name: artifact_name}
  apmate_get(ENDPOINTS[:get_artifact], opts, params)
end

.apmate_get_artifact_environment(artifact_name, environment_name, opts) ⇒ Object



47
48
49
50
# File 'lib/apmate/cli.rb', line 47

def self.apmate_get_artifact_environment(artifact_name, environment_name, opts)
  params = { :name => artifact_name, :environment_name => environment_name }
  apmate_get(ENDPOINTS[:get_artifact_environment], opts, params)
end

.apmate_get_artifact_name(artifact_id, group_id, filename_extension, opts) ⇒ Object



52
53
54
55
# File 'lib/apmate/cli.rb', line 52

def self.apmate_get_artifact_name(artifact_id, group_id, filename_extension, opts)
  params = { artifact_id: artifact_id, group_id: group_id, filename_extension: filename_extension }
  apmate_get(ENDPOINTS[:get_artifact_name], opts, params)
end

.apmate_mass_promote(env_from, env_to, opts) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/apmate/cli.rb', line 111

def self.apmate_mass_promote(env_from, env_to, opts)
  Apmate::Logger.logger.debug "Beginning post request for mass promote."

  uri = URI("#{Apmate::CLI::APMATE_URL}#{ENDPOINTS[:mass_promote_environment]}")
  Apmate::Logger.logger.debug "uri: #{uri}"

  payload = { env_from: env_from, env_to: env_to }
  Apmate::Logger.logger.debug "payload: #{payload}"

  apmate_post uri, payload, opts
end

.apmate_post(uri, payload, opts) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/apmate/cli.rb', line 100

def self.apmate_post(uri, payload, opts)
  request = Net::HTTP::Post.new(uri)
  request.content_type = 'application/json'
  request.body = payload.to_json
  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(request)
  end
  output = JSON.pretty_generate(JSON.parse(response.body))
  log_output(output, opts)
end

.apmate_promote(env_from, env_to, artifact, opts) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/apmate/cli.rb', line 123

def self.apmate_promote(env_from, env_to, artifact, opts)
  Apmate::Logger.logger.debug "Beginning post request."

  uri = URI("#{Apmate::CLI::APMATE_URL}#{ENDPOINTS[:promote_environment]}")
  Apmate::Logger.logger.debug "uri: #{uri}"

  payload = { environment_from: env_from, environment_to: env_to, artifact: artifact }
  Apmate::Logger.logger.debug "payload: #{payload}"

  apmate_post uri, payload, opts
end

.apply_dir(dir, opts) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/apmate/cli.rb', line 34

def self.apply_dir(dir, opts)
  yaml_files = []
  if Pathname.new(dir).relative?
    Dir["#{File.expand_path(dir, Dir.pwd)}/**/*.yaml"].each {|f| yaml_files << File.expand_path(f) }
  else
    Dir["#{dir}/**/*.yaml"].each {|f| yaml_files << File.expand_path(f) }
  end

  yaml_files.each do |file|
    apply_file file, opts
  end
end

.apply_file(file, opts) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/apmate/cli.rb', line 26

def self.apply_file(file, opts)
  Apmate::Logger.logger.info "applying file #{file}"
  YAML.load_stream(File.read file).each do |yaml_hash|
    Apmate::Logger.logger.debug "yaml_hash: #{yaml_hash}"
    apmate_apply yaml_hash, opts
  end
end

.log_output(output, opts) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/apmate/cli.rb', line 150

def self.log_output(output, opts)
  Apmate::Logger.logger.info output
  if opts[:output]
    open(opts[:output], 'w') do |f|
      f.puts output
    end
  elsif !opts[:quiet]
    puts output
  end
end

.publish_artifact(name, environment_name, opts) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/apmate/cli.rb', line 135

def self.publish_artifact(name, environment_name, opts)
  Apmate::Logger.logger.debug "Beginning to publish artifact."

  uri = URI("#{Apmate::CLI::APMATE_URL}#{ENDPOINTS[:publish_artifact]}")
  Apmate::Logger.logger.debug "uri: #{uri}"

  payload = { artifact: { name: name, environment_name: environment_name } }
  opts.each do |opt, value|
    payload[:artifact][opt] = value
  end
  Apmate::Logger.logger.debug "payload: #{payload}"

  apmate_post uri, payload, opts
end

Instance Method Details

#runObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/apmate/cli.rb', line 161

def run
  get_command = Command::GET
  
  [
    Command::Get::ARTIFACT,
    Command::Get::ARTIFACT_ENVIRONMENT,
    Command::Get::ARTIFACT_NAME
  ].each do |cmd|
    get_command.add_command cmd
  end

  command = Command::BASE
  [
    Command::HELP,
    Command::APPLY,
    get_command,
    Command::MASS_PROMOTE,
    Command::PROMOTE,
    Command::CREATE_EXECUTION,
    Command::PUBLISH_ARTIFACT
  ].each do |cmd|
    command.add_command cmd
  end

  command.run ARGV
  Apmate::Logger.logger.close if Apmate::Logger.logger
end