Class: Bosh::Cli::Command::Instances

Inherits:
Base show all
Defined in:
lib/cli/commands/instances.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_DIRECTOR_PORT

Instance Attribute Summary

Attributes inherited from Base

#args, #exit_code, #info, #options, #out, #runner, #work_dir

Instance Method Summary collapse

Methods inherited from Base

#add_option, #blob_manager, #blobstore, #cache_dir, #config, #confirmed?, #credentials, #deployment, #director, #initialize, #interactive?, #logged_in?, #non_interactive?, #progress_renderer, #redirect, #release, #remove_option, #run_nested_command, #show_current_state, #target, #target_name, #verbose?

Methods included from Bosh::Cli::CommandDiscovery

#desc, #method_added, #option, #register_command, #usage

Methods included from DeploymentHelper

#build_manifest, #cancel_deployment, #deployment_changed?, #inspect_deployment_changes, #job_exists_in_deployment?, #job_must_exist_in_deployment, #job_unique_in_deployment?, #jobs_and_indexes, #prepare_deployment_manifest, #prompt_for_errand_name, #prompt_for_job_and_index

Constructor Details

This class inherits a constructor from Bosh::Cli::Command::Base

Instance Method Details

#listObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cli/commands/instances.rb', line 12

def list()
  auth_required
  deployment_required
  manifest = Bosh::Cli::Manifest.new(deployment, director)
  manifest.load
  deployment_name = manifest.name

  deps = director.list_deployments
  selected = deps.select { |dep| dep['name'] == deployment_name }
  err("The deployment '#{deployment_name}' doesn't exist") if selected.size == 0

  show_current_state(deployment_name)
  no_track_unsupported

  unless deployment_name.nil?
    show_deployment(deployment_name, options)
  end
end

#show_deployment(name, options = {}) ⇒ Object



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
63
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cli/commands/instances.rb', line 31

def show_deployment(name, options={})
  instances = director.fetch_vm_state(name)
  instance_count = instances.size

  if instances.empty?
    nl
    say('No instances')
    nl
    return
  end

  sorted = instances.sort do |a, b|
    s = a['job_name'].to_s <=> b['job_name'].to_s
    s = a['index'].to_i <=> b['index'].to_i if s == 0
    s = a['resource_pool'].to_s <=> b['resource_pool'].to_s if s == 0
    s
  end

  row_count = 0
  has_disk_cid = instances[0].has_key?('disk_cid')
  has_uptime = instances[0]['processes'] && instances[0]['processes'].size > 0 && instances[0]['processes'][0].has_key?('uptime')
  has_cpu = instances[0]['processes'] && instances[0]['processes'].size > 0 && instances[0]['processes'][0].has_key?('cpu')

  instances_table = table do |t|
    headings = ['Instance', 'State', 'Resource Pool', 'IPs']
    if options[:details]
      if has_disk_cid
        headings += ['VM CID', 'Disk CID', 'Agent ID', 'Resurrection']
      else
        headings += ['VM CID', 'Agent ID', 'Resurrection']
      end
    end

    if options[:dns]
      headings += ['DNS A records']
    end

    if options[:vitals]
      headings += [{:value => "Uptime", :alignment => :center}] if options[:ps] && has_uptime
      headings += [{:value => "Load\n(avg01, avg05, avg15)", :alignment => :center}]
      headings += [{:value => "CPU %\n(User, Sys, Wait)", :alignment => :center}]
      headings += ["CPU %"] if options[:ps] && has_cpu
      headings += ['Memory Usage', 'Swap Usage']
      headings += ["System\nDisk Usage", "Ephemeral\nDisk Usage", "Persistent\nDisk Usage"]
    end

    last_job = ''
    sorted.each do |instance|
      if options[:failing]
        if options[:ps]
          instance['processes'].keep_if { |p| p['state'] != 'running' }
          if instance['processes'].size == 0 && instance['job_state'] == 'running'
            instance_count -= 1
            next
          end
        else
          if instance['job_state'] == 'running'
            instance_count -= 1
            next
          end
        end
      end

      row_count += 1

      job = "#{instance['job_name'] || 'unknown'}/#{instance['index'] || 'unknown'}"
      ips = Array(instance['ips']).join("\n")
      dns_records = Array(instance['dns']).join("\n")
      vitals = instance['vitals']

      row = [job, instance['job_state'], instance['resource_pool'], ips]
      t << :separator if row_count.between?(2, instance_count) && (options[:ps] || last_job != '' && instance['job_name'] != last_job)

      if options[:details]
        if has_disk_cid
          row += [instance['vm_cid'], instance['disk_cid'] || 'n/a', instance['agent_id'], instance['resurrection_paused'] ? 'paused' : 'active']
        else
          row += [instance['vm_cid'], instance['agent_id'], instance['resurrection_paused'] ? 'paused' : 'active']
        end
      end

      if options[:dns]
        row += [dns_records.empty? ? 'n/a' : dns_records]
      end

      if options[:vitals]
        if vitals
          cpu =  vitals['cpu']
          mem =  vitals['mem']
          swap = vitals['swap']
          disk = vitals['disk']

          row << '' if options[:ps] && has_uptime && instance['processes'].size > 0
          row << vitals['load'].join(', ')
          row << "#{cpu['user']}%, #{cpu['sys']}%, #{cpu['wait']}%"
          row << '' if options[:ps] && has_cpu && instance['processes'].size > 0
          row << "#{mem['percent']}% (#{pretty_size(mem['kb'].to_i * 1024)})"
          row << "#{swap['percent']}% (#{pretty_size(swap['kb'].to_i * 1024)})"
          row << "#{disk['system']['percent']}%"
          if disk['ephemeral'].nil?
            row << 'n/a'
          else
            row << "#{disk['ephemeral']['percent']}%"
          end
          if disk['persistent'].nil?
            row << 'n/a'
          else
            row << "#{disk['persistent']['percent']}%"
          end
        else
          9.times { row << 'n/a' }
        end
        t << row

        if options[:ps] && instance['processes']
          instance['processes'].each do |process|
            prow = ['  ' + process['name'], process['state'], '', '']
            if options[:details]
              prow += ['','','']
              prow << '' if has_disk_cid
            end
            if has_uptime
              if process['uptime'] && process['uptime']['secs']
                uptime = Integer(process['uptime']['secs'])
                days = uptime/60/60/24
                hours = uptime/60/60%24
                minutes = uptime/60%60
                seconds = uptime%60
                prow << "#{days}d #{hours}h #{minutes}m #{seconds}s"
              else
                prow << ''
              end
            end
            prow += ['','']
            prow << (process['cpu'] ? "#{process['cpu']['total']}%":'') if has_cpu
            prow << (process['mem'] ? "#{process['mem']['percent']}% (#{pretty_size(process['mem']['kb'].to_i * 1024)})":'')
            4.times { prow << '' }
            t << prow
          end
        end
      else
        t << row
        if options[:ps] && instance['processes']
          instance['processes'].each do |process|
            name = process['name']
            state = process['state']
            process_row = ["  #{name}", "#{state}"]
            (headings.size - 2).times { process_row << '' }
            t << process_row
          end
        end
      end

      last_job = instance['job_name'] || 'unknown'
      if instance['processes'].size == 0 && instance_count == 1
        headings.delete_at(4)
        headings.delete_at(6)
      end
    end
    t.headings = headings
  end

  if options[:failing] && row_count == 0
    nl
    say('No failing instances')
    nl
    return
  end
  nl
  say(instances_table)
  nl
  say('Instances total: %d' % row_count )
end