Class: Bosh::Cli::Command::ConsulCommands

Inherits:
Base
  • Object
show all
Includes:
Validation
Defined in:
lib/bosh/cli/commands/consul_commands.rb

Constant Summary collapse

DEFAULT_CONFIG_PATH =
File.expand_path('~/.bosh_consul_config')

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ ConsulCommands

Returns a new instance of ConsulCommands.



9
10
11
12
# File 'lib/bosh/cli/commands/consul_commands.rb', line 9

def initialize(runner)
  super(runner)
  # options[:config] ||= DEFAULT_CONFIG_PATH # hijack Cli::Config
end

Instance Method Details

#consul_helpObject



16
17
18
19
20
21
22
23
# File 'lib/bosh/cli/commands/consul_commands.rb', line 16

def consul_help
  say("bosh consul commands:")
  nl
  cmds = Bosh::Cli::Config.commands.values.find_all {|c|
    c.usage =~ /consul/
  }
  Bosh::Cli::Command::Help.list_commands(cmds)
end

#display_servicesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bosh/cli/commands/consul_commands.rb', line 67

def display_services
  require "bosh/consul"
  show_dns = options[:dns]

  cluster = load_cluster
  domain = cluster.domain

  services = Bosh::Consul::Models::Services.new
  require "pp"
  items = services.load_from_ip(cluster.leader)

  previous_name = nil
  view = table(items) do |t|
    t.headings = ["name", "service id", "ip", "port", "tags"]
    t.headings << "dns" if show_dns
    items.each do |item|
      same_as_previous = (previous_name == item["ServiceName"])
      if previous_name && !same_as_previous
        t.add_separator
      end

      tags = item["ServiceTags"] || []
      if show_dns
        dns = ["#{item["ServiceName"]}.service.#{domain}"]
        tags.each do |tag|
          dns << "#{tag}.#{item["ServiceName"]}.service.#{domain}"
        end
      end

      row = [
        same_as_previous ? "" : item["ServiceName"],
        same_as_previous ? "" : item["ServiceID"],
        item["Address"],
        item["ServicePort"],
        tags.join(", ")
      ]
      row << dns.join(", ") if show_dns
      t << row

      previous_name = item["ServiceName"]
    end
  end
  say(view)

end

#display_statusObject



56
57
58
59
60
61
62
# File 'lib/bosh/cli/commands/consul_commands.rb', line 56

def display_status
  require "bosh/consul"

  verify_target_consul_config
  cluster = load_cluster
  show_cluster(cluster)
end

#target_consul(deployment_name = nil) ⇒ Object



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
# File 'lib/bosh/cli/commands/consul_commands.rb', line 28

def target_consul(deployment_name=nil)
  require "bosh/consul"

  unless deployment_name
    deployment_name = prompt_for_deployment
  end

  say "Fetching consul cluster info from deployment '#{deployment_name}'..."
  cluster = Bosh::Consul::Models::Cluster.new(director)
  cluster.load_from_deployment_name(deployment_name)
  step("Current consul cluster from deployment '#{deployment_name}'", "Cannot find consul cluster '#{deployment_name}'", :fatal) do
    cluster.valid?
  end
  show_cluster(cluster)

  consul_config = config.read("consul") || {}
  consul_config[target] = { "leader" => cluster.leader, "peers" => cluster.peers, "domain" => cluster.domain }
  config.write_global("consul", consul_config)
  config.save

rescue Bosh::Cli::ValidationHalted
  err errors.first
# rescue => e
#   puts e.message
end