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
32
# 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)
  select(name)
end

#currentObject



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

def current
  switch_version(2)
end

#deselectObject



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

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

#logsObject



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

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



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

def planning
  switch_version(1)
end

#prepareObject



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

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



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

def release_now
  spacer = "\r" << (" " * 20)
  print "\rrelease starting"
  stage = load_selection("stage")
  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 "\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}"
          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



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

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



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

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



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

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



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

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



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

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