class Magellan::Cli::Resources::Stage

Constants

VERSION_PARAMETER_NAME
VERSION_RESOURCE_KEY

Public Class Methods

deselect(selections) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 68
def self.deselect(selections)
  selections.delete(parameter_name)
  selections.delete(VERSION_PARAMETER_NAME)
  deselect_dependants(selections)
end

Public Instance Methods

build_name_query(name) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 45
def build_name_query(name)
  build_query("name" => name).update(default_query)
end
call_repair() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 107
def call_repair
  s = load_selection!(self.class)
  id = s["id"]
  post_json("/admin/stage~title/#{id}/simple_method_call.json", {method_name: "repair"})
end
create(name) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 22
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
current() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 80
def current
  switch_version(2)
end
logs() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 178
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
planning() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 75
def planning
  switch_version(1)
end
prepare() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 93
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_now() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 125
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
reload() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 198
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
repair() click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 101
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) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 51
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) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 190
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
switch_version(phase) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 85
def switch_version(phase)
  s = load_selection!(self.class)
  q = build_query("title" => s["id"], "phase" => phase) # 1: planning, 2: current, 3: used
  update_first_result(VERSION_PARAMETER_NAME, "phase=#{phase}", "/admin/stage~version.json", q, %w[id])
end
update(attrs) click to toggle source
# File lib/magellan/cli/resources/stage.rb, line 115
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