Class: AppRb::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/app-rb/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Command

Returns a new instance of Command.



3
4
5
# File 'lib/app-rb/command.rb', line 3

def initialize(config)
  @config = config
end

Instance Method Details

#cleanObject



97
98
99
100
101
# File 'lib/app-rb/command.rb', line 97

def clean
  base = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "base")
  ips = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "nodes").split(",")
  stop_services(ips, base)
end

#deploy(target) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/app-rb/command.rb', line 7

def deploy(target)
  @base = "#{@config.app}-#{Time.now.to_i}"

  # init
  current_hash = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "hash")
  build_nodes = @config.nodes(@config.image["constraint"])
  pre_payloads = @config.pre_deploy.map do |pre|
    {
      "nodes" => @config.nodes(pre["constraint"]),
      "cmd" => pre["cmd"],
      "opts" => pre["opts"] || [],
    }
  end
  deploy_payloads = @config.deploy.map { |key, section|
    nodes = @config.nodes(section["constraint"])
    {
      "key" => key,
      "nodes" => nodes,
      "amount" => (section["per"] ? section["per"]*nodes.count : section["amount"]),
      "cmd" => section["cmd"],
      "port" => section["port"],
      "check_url" => section["check_url"],
      "opts" => section["opts"] || [],
    }
  }
  old_ips = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "nodes").split(",")
  new_ips = (
    build_nodes.map(&:ip) +
    pre_payloads.flat_map { |p| p["nodes"].map(&:ip) } +
    deploy_payloads.flat_map { |p| p["nodes"].map(&:ip) }
  ).uniq
  ips = (old_ips + new_ips).uniq
  AppRb::Util::Consul.kv_set(@config.consul, @config.app, "nodes", ips.join(","))

  # pre
  new_hash = prepare_image(build_nodes, target)
  pre_deploy(pre_payloads, new_hash)
  stop_bg_jobs(ips)

  # deploy
  do_deploy(deploy_payloads, new_hash)

  # switch
  blue_green(deploy_payloads, new_hash)

  # clean
  stop_services(ips)
  clean_registry(current_hash, [current_hash, new_hash].uniq)

  # finish
  AppRb::Util::Consul.kv_set(@config.consul, @config.app, "nodes", new_ips.join(","))
  puts AppRb::Util.green("Done.")
  if current_hash != "" && !target && current_hash != target
    puts "to rollback fire: app-rb #{ARGV[0]} deploy #{current_hash}"
  end
end

#restartObject



103
104
105
106
107
108
# File 'lib/app-rb/command.rb', line 103

def restart
  hash = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "hash")
  raise "FATAL: app is not started?" if hash == ""
  puts "hash=#{hash}"
  deploy(hash)
end

#statusObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/app-rb/command.rb', line 64

def status
  ips = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "nodes").split(",")
  nodes = @config.nodes.select { |n| ips.index(n.ip) }
  max_name_len = nodes.map { |n| n.name.length }.max
  max_ip_len = nodes.map { |n| n.ip.length }.max
  max_roles_len = nodes.map { |n| n.roles.inspect.length }.max
  current_base = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "base")
  current_hash = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "hash")
  current_dockers = nodes.map { |n| 
    AppRb::Util::Docker.ids(@config.user, n.ip, {app: @config.app, build: current_base}).count
  }
  dockers = nodes.map { |n| 
    AppRb::Util::Docker.ids(@config.user, n.ip, {app: @config.app}).count
  }
  puts ""
  puts AppRb::Util.green("App:     ") + @config.app
  puts AppRb::Util.green("Base:    ") + current_base
  puts AppRb::Util.green("Hash:    ") + current_hash
  nodes.each_with_index do |n, i|
    puts(
      " "*5 + n.name.rjust(max_name_len) + 
      " "*2 + n.ip.ljust(max_ip_len) + 
      " "*2 + n.roles.inspect.ljust(max_roles_len) +
      " "*2 + AppRb::Util.green(current_dockers[i]) + " / " + (dockers[i] - current_dockers[i] == 0 ? "0" : AppRb::Util.red(dockers[i] - current_dockers[i]))
    )
    end
end

#stopObject



92
93
94
95
# File 'lib/app-rb/command.rb', line 92

def stop
  ips = AppRb::Util::Consul.kv_get(@config.consul, @config.app, "nodes").split(",")
  stop_all(ips)
end