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

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

Instance Method Summary collapse

Methods included from FileAccess

#load_selection, #load_selections, #update_selections

Instance Method Details

#create(name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/magellan/cli/resources/stage.rb', line 17

def create(name)
  type = options["t"]
  unless %w{ development staging production other }.include?(type)
    raise "Unknown Stage Type #{type}"
  end
  proj = load_selection("project")
  params = {
    parameter_name => {
      "project_id" => proj["id"],
      "name" => name,
      "stage_type" => type,
    }
  }
  post_json("/admin/#{resource_name}/new.json", params)
end

#currentObject



61
62
63
# File 'lib/magellan/cli/resources/stage.rb', line 61

def current
  switch_version(2)
end

#deselectObject



48
49
50
51
52
53
# File 'lib/magellan/cli/resources/stage.rb', line 48

def deselect
  update_selections do |s|
    s.delete("stage")
    s.delete("stage-version")
  end
end

#logsObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/magellan/cli/resources/stage.rb', line 158

def logs
  s = load_selection("stage")
  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_i).strftime("%Y-%m-%d %H:%M:%S")}:#{log["version"]}:#{log["container"]}: #{log["message"]}"
    end
  end
end

#planningObject



56
57
58
# File 'lib/magellan/cli/resources/stage.rb', line 56

def planning
  switch_version(1)
end

#prepareObject



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

def prepare
  s = load_selection("stage")
  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



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
155
# File 'lib/magellan/cli/resources/stage.rb', line 106

def release_now
  spacer = "\r" << (" " * 20)
  print "\rrelease starting"
  s = load_selection("stage")
  id = s["id"]
  res0 = post_json("/admin/stage~title/#{id}/simple_method_call.json", {method_name: "release_now"})
  res1 = res0["result"]
  if res1
    print spacer
    print "\rnow #{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}"
          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

#repairObject



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

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



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

def select(name)
  q = build_query("name" => name).update(default_query)
  r = update_first_result("stage", "/admin/stage~title.json", q)

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

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

#set_container_num(num) ⇒ Object



170
171
172
173
174
175
# File 'lib/magellan/cli/resources/stage.rb', line 170

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

#update(attrs) ⇒ Object



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

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