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

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, puts_with_color, sorted_commands, sorted_printable_commands, update_common_help_message

Instance Method Details

#create(name) ⇒ Object



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

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_key}/new.json", params)
  select(name)
end

#current ⇒ Object



72
73
74
# File 'lib/magellan/cli/resources/stage.rb', line 72

def current
  switch_version(2)
end

#deselect ⇒ Object



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

def deselect
  update_selections! do |s|
    s.delete(parameter_name)
    s.delete(VERSION_PARAMETER_NAME)
  end
  deselect_dependants
end

#logs ⇒ Object



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

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

#planning ⇒ Object



67
68
69
# File 'lib/magellan/cli/resources/stage.rb', line 67

def planning
  switch_version(1)
end

#prepare ⇒ Object



85
86
87
88
89
90
# File 'lib/magellan/cli/resources/stage.rb', line 85

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 ⇒ Object



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
158
159
160
161
162
163
164
165
166
167
# File 'lib/magellan/cli/resources/stage.rb', line 117

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 ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/magellan/cli/resources/stage.rb', line 190

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 ⇒ Object



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

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/magellan/cli/resources/stage.rb', line 40

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

  q = build_query("name" => name).update(default_query)
  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])

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

#set_container_num(num) ⇒ Object



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

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



107
108
109
110
111
# File 'lib/magellan/cli/resources/stage.rb', line 107

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", {"stage_title" => attrs})
end