Class: Bosh::Cli::Command::Vms

Inherits:
Base show all
Defined in:
lib/cli/commands/vms.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_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

#list(deployment_name = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cli/commands/vms.rb', line 10

def list(deployment_name = nil)
  auth_required
  show_current_state(deployment_name)
  no_track_unsupported

  if deployment_name.nil?
    deps = director.list_deployments
    err('No deployments') if deps.empty?
    deps.each do |dep|
      say("Deployment '#{dep['name'].make_green}'")
      show_deployment(dep['name'], options)
    end
  else
    show_deployment deployment_name, options
  end
end

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



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
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
# File 'lib/cli/commands/vms.rb', line 27

def show_deployment(name, options={})
  vms = director.fetch_vm_state(name)

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

  sorted = sort(vms)

  has_az = vms.any? {|vm| vm.has_key? 'az' }

  vms_table = table do |t|
    headings = ['VM', 'State']

    if has_az
      headings << 'AZ'
    end
    headings += ['VM Type', 'IPs']

    if options[:details]
      headings += ['CID', 'Agent ID', 'Resurrection', 'Ignore']
    end
    if options[:dns]
      headings += ['DNS A records']
    end
    if options[:vitals]
      headings += [{:value => "Load\n(avg01, avg05, avg15)", :alignment => :center}]
      headings += ["CPU\nUser", "CPU\nSys", "CPU\nWait"]
      headings += ['Memory Usage', 'Swap Usage']
      headings += ["System\nDisk Usage", "Ephemeral\nDisk Usage", "Persistent\nDisk Usage"]
    end
    t.headings = headings

    sorted.each do |vm|
      job_name = vm['job_name'] || 'unknown'
      job_index = vm['index'] || 'unknown'
      job = vm.has_key?('id') ? "#{job_name}/#{job_index} (#{vm['id']})" : "#{job_name}/#{job_index}"
      ips = Array(vm['ips']).join("\n")
      dns_records = Array(vm['dns']).join("\n")
      vitals = vm['vitals']
      az = vm['az'].nil? ? 'n/a' : vm['az']

      row = [job, vm['job_state']]
      if has_az
        row << az
      end

      if vm['resource_pool']
        row << vm['resource_pool']
      else
        row << vm['vm_type']
      end

      row << ips

      if options[:details]
        row += [vm['vm_cid'], vm['agent_id'], vm['resurrection_paused'] ? 'paused' : 'active', vm['ignore'].nil? ? 'n/a' : vm['ignore']]
      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 << vitals['load'].join(', ')
          row << "#{cpu['user']}%"
          row << "#{cpu['sys']}%"
          row << "#{cpu['wait']}%"
          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
      end

      t << row
    end
  end

  nl
  say(vms_table)
  nl
  say('VMs total: %d' % vms.size)
end

#sort(vms) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/cli/commands/vms.rb', line 132

def sort(vms)
  sorted = vms.sort do |a, b|
    comparison = a['job_name'].to_s <=> b['job_name'].to_s
    comparison = a['az'].to_s <=> b['az'].to_s if comparison == 0
    comparison = a['index'].to_i <=> b['index'].to_i if comparison == 0
    comparison
  end
  sorted
end