Class: Magellan::Cli::Resources::Stage

Inherits:
Base
  • Object
show all
Includes:
Deletable
Defined in:
lib/magellan/cli/resources/stage.rb

Constant Summary collapse

VERSION_RESOURCE_KEY =
"stage~version".freeze
VERSION_PARAMETER_NAME =
"stage_version".freeze

Constants included from FileAccess

FileAccess::DEFAULT_SELECTION_FILENAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deletable

included

Methods included from FileAccess

ensure_config_dir, load_selection, load_selections, remove_selection_file, selection_filename, update_selections

Methods inherited from Base

command_help, help, log_error, log_info, log_success, log_verbose, log_warning, puts_with_color, sorted_commands, sorted_printable_commands, update_common_help_message

Class Method Details

.deselect(selections) ⇒ Object



69
70
71
72
73
# File 'lib/magellan/cli/resources/stage.rb', line 69

def self.deselect(selections)
  selections.delete(parameter_name)
  selections.delete(VERSION_PARAMETER_NAME)
  deselect_dependants(selections)
end

Instance Method Details

#create(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/magellan/cli/resources/stage.rb', line 23

def create(name)
  type = options["t"]
  unless %w{ development staging production }.include?(type)
    raise "Unknown Stage Type #{type}"
  end
  size = options["s"]
  unless %w{ micro standard }.include?(size)
    raise "Unknown Stage Size #{size}"
  end
  proj = load_selection!(Project)
  params = {
    parameter_name => {
      "project_id" => proj["id"],
      "name" => name,
      "stage_type" => type,
      "stage_size" => size,
    }
  }
  post_json("/admin/#{resource_key}/new.js", params)
  select(name)
end

#currentObject



81
82
83
# File 'lib/magellan/cli/resources/stage.rb', line 81

def current
  switch_version(2)
end

#logsObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/magellan/cli/resources/stage.rb', line 179

def logs
  s = load_selection!(self.class)
  id = s["id"]
  obj = get_json("/admin/stage~title/#{id}/logs.json")
  if obj["value"]
    obj["value"].each do |log|
      puts "#{Time.at(log["time"].to_f).strftime("%Y-%m-%d %H:%M:%S")}:#{log["version"]}:#{log["container"]}: #{log["message"]}"
    end
  end
end

#planningObject



76
77
78
# File 'lib/magellan/cli/resources/stage.rb', line 76

def planning
  switch_version(1)
end

#prepareObject



94
95
96
97
98
99
# File 'lib/magellan/cli/resources/stage.rb', line 94

def prepare
  s = load_selection!(self.class)
  id = s["id"]
  r = post_json("/admin/stage~title/#{id}/simple_method_call.json", {method_name: "prepare_containers"})
  Container.new.show_list(r["result"])
end

#release_nowObject



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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/magellan/cli/resources/stage.rb', line 126

def release_now
  spacer = "\r" << (" " * 20)
  stage = load_selection!(self.class)
  print "\rrelease starting"
  id = stage["id"]
  res0 = post_json("/admin/stage~title/#{id}/simple_method_call.json", {method_name: "release_now"})
  res1 = res0["result"]
  if res1
    print spacer
    print "\r#{res1['status']}"
  else
    print spacer
    puts "\e[31m#{res0.inspect}\e[0m"
    raise res0["message"]
  end

  return res1 if options["A"]
  res2 = get_json("/admin/release~operation.json", build_query("release_job" => res1["id"]))
  ope = res2.first
  ope_id = ope["id"]
  i = options["i"]
  Timeout.timeout(options["t"]) do
    loop do
      sleep(i)
      res3 = get_json("/admin/release~operation/#{ope_id}.json", default_query)
      st = res3["status"]
      unless res1["status"] == st
        case st
        when "executing" then
          res4 = get_json("/admin/release~transaction.json", build_query("release_operation" => ope_id))
          total = res4.length
          complete = res4.select{ |r| r["status"] == "completed" }.length
          print spacer
          print "\rProgress: %2d /%2d" % [complete, total]
        when "completed"
          print spacer
          puts "\r#{st}"
          reload
          return
        when "aborted" then
          print spacer
          puts "\rrelease #{st}"
          puts "now repairing stage automatically..."
          r = call_repair
          puts r["success"] ? "succeeded to repair stage. try `stage release_now` again after fix" : "\e[31mfailed to repair stage\e[0m"
          raise Magellan::Cli::Error, "release #{st}"
        end
      end
    end
  end
end

#reloadObject



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/magellan/cli/resources/stage.rb', line 199

def reload
  s = load_selection!(self.class)
  select(s["name"])
  [Worker, Image, Container].each do |klass|
    s = (load_selections || {})[klass.parameter_name]
    next unless s
    name = s["name"]
    next unless name
    klass.new.select(name)
  end
end

#repairObject



102
103
104
105
# File 'lib/magellan/cli/resources/stage.rb', line 102

def repair
  r = call_repair
  puts r["success"] ? "\e[32msucceeded to repair stage\e[0m" : "\e[31mfailed to repair stage\e[0m"
end

#select(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/magellan/cli/resources/stage.rb', line 52

def select(name)
  if selected = load_selections[parameter_name]
    deselect unless selected["name"] == name
  end

  q = build_name_query(name)
  r = update_first_result(parameter_name, name, "/admin/stage~title.json", q)

  # # current
  # q = build_query("title" => r["id"], "phase" => 2) # 2: current
  # update_first_result(VERSION_PARAMETER_NAME, "phase=2", "/admin/stage~version.json", q, %w[id])

  # # planning
  q = build_query("title" => r["id"], "phase" => 1) # 1: planning
  update_first_result(VERSION_PARAMETER_NAME, "phase=1", "/admin/stage~version.json", q, %w[id])
end

#set_container_num(num) ⇒ Object



191
192
193
194
195
196
# File 'lib/magellan/cli/resources/stage.rb', line 191

def set_container_num(num)
  s = load_selection!(self.class)
  v = load_selection!(VERSION_PARAMETER_NAME)
  i = load_selection!(Image)
  post_json("/admin/stage~version/#{v["id"]}/set_container_num.json", { container_num: num, container_image_id: i["id"] })
end

#update(attrs) ⇒ Object



116
117
118
119
120
# File 'lib/magellan/cli/resources/stage.rb', line 116

def update(attrs)
  s = load_selection!(self.class)
  attrs = JSON.parse(File.readable?(attrs) ? File.read(attrs) : attrs)
  put_json("/admin/stage~title/#{s['id']}/edit.js", {"stage_title" => attrs})
end