knife-xenserver 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ # 1.2 - Thu 25 Oct 2012
2
+
3
+ * Greatly improved 'vm list' command
4
+
5
+ - Table columns can now be disabled in output:
6
+
7
+ knife xenserver vm list --no-uuid \
8
+ --no-power \
9
+ --no-networks \
10
+ --no-tools
11
+
12
+ - Print CSV output with --csv instead of a regular ASCII table
13
+
14
+ knife xenserver vm list --csv
15
+
16
+ * Added --match option to 'vm list'
17
+
18
+ Print only VMs whose name matches the given regex:
19
+
20
+ knife xenserver vm list --match '^my-vm.*?devel.bvox.net$'
21
+
22
+ * FIX: Set exit status to 1 when XenServer auth fails
23
+ * FIX: Print error if XenServer host is not defined
24
+
1
25
  # 1.1 - 2012/10/21
2
26
 
3
27
  * Fixed --no-host-key-verify vm create flag
@@ -54,6 +54,11 @@ class Chef
54
54
  def connection
55
55
  if not @connection
56
56
  host = config[:xenserver_host] || Chef::Config[:knife][:xenserver_host]
57
+ if host.nil?
58
+ ui.error "XenServer host not defined."
59
+ ui.error "Use --xenserver-host or add it to the knife config file."
60
+ exit 1
61
+ end
57
62
  username = config[:xenserver_username] || Chef::Config[:knife][:xenserver_username]
58
63
  password = config[:xenserver_password] || Chef::Config[:knife][:xenserver_password]
59
64
  ui.info "Connecting to XenServer host #{host.yellow}..."
@@ -70,7 +75,7 @@ class Chef
70
75
  rescue Fog::XenServer::InvalidLogin => e
71
76
  ui.error "Error connecting to the hypervisor: #{host}"
72
77
  ui.error "Check the username and password."
73
- exit
78
+ exit 1
74
79
  rescue => e
75
80
  ui.error "Error connecting to the hypervisor"
76
81
  ui.error "#{e.class} #{e.message}"
@@ -25,36 +25,167 @@ class Chef
25
25
  include Knife::XenserverBase
26
26
 
27
27
  banner "knife xenserver vm list (options)"
28
+
29
+ option :csv,
30
+ :long => "--csv",
31
+ :description => "Comma separated list of VMs",
32
+ :boolean => true,
33
+ :default => false
34
+
35
+ option :uuid,
36
+ :long => "--[no-]uuid",
37
+ :description => "Print/Hide VM UUID",
38
+ :boolean => true,
39
+ :default => true
40
+
41
+ option :tools,
42
+ :long => "--[no-]tools",
43
+ :description => "Print/Hide XenTools Availability",
44
+ :boolean => true,
45
+ :default => true
46
+
47
+ option :networks,
48
+ :long => "--[no-]networks",
49
+ :description => "Print/Hide VM Networks",
50
+ :boolean => true,
51
+ :default => true
52
+
53
+ option :mem,
54
+ :long => "--[no-]mem",
55
+ :description => "Print/Hide VM Memory",
56
+ :boolean => true,
57
+ :default => true
28
58
 
29
- def run
30
- $stdout.sync = true
59
+ option :ips,
60
+ :long => "--[no-]ips",
61
+ :description => "Print/Hide VM IPs",
62
+ :boolean => true,
63
+ :default => true
64
+
65
+ option :power_state,
66
+ :long => "--[no-]power-state",
67
+ :description => "Print/Hide VM Power State",
68
+ :boolean => true,
69
+ :default => true
70
+
71
+ option :match,
72
+ :long => "--match REGEX",
73
+ :description => "Print only VMs whose name matches REGEX"
74
+
75
+ def gen_headings
76
+ headings = %w{NAME}
77
+ if config[:mem]
78
+ headings << 'MEM'
79
+ end
80
+ if config[:power_state]
81
+ headings << 'POWER'
82
+ end
83
+ if config[:tools]
84
+ headings << 'TOOLS'
85
+ end
86
+ if config[:networks]
87
+ headings << 'NETWORKS'
88
+ end
89
+ if config[:ips]
90
+ headings << 'IPs'
91
+ end
92
+ headings
93
+ end
94
+
95
+ def gen_table
96
+ # row format
97
+ # [uuid, name, [ips], [networks], mem, power, tools]
98
+ table = []
99
+ connection.servers.each do |vm|
100
+ if config[:match] and vm.name !~ /#{config[:match]}/
101
+ next
102
+ end
103
+ row = [vm.uuid, vm.name]
104
+ if vm.tools_installed?
105
+ ips = []
106
+ vm.guest_metrics.networks.each do |k,v|
107
+ ips << v
108
+ end
109
+ row << ips
110
+ else
111
+ row << []
112
+ end
113
+ networks = []
114
+ vm.vifs.each do |vif|
115
+ name = vif.network.name
116
+ if name.size > 20
117
+ name = name[0..16] + '...'
118
+ end
119
+ networks << name
120
+ end
121
+ row << networks
122
+ row << vm.memory_static_max.to_i.bytes.to.megabytes.round
123
+ row << vm.power_state
124
+ row << vm.tools_installed?
125
+ table << row
126
+ end
127
+ table
128
+ end
129
+
130
+ def print_table
31
131
  vm_table = table do |t|
32
- t.headings = %w{NAME MEM POWER TOOLS NETWORKS IPs}
33
- connection.servers.each do |vm|
34
- if vm.tools_installed?
35
- ips = []
36
- vm.guest_metrics.networks.each do |k,v|
37
- ips << v
38
- end
39
- ips = ips.join("\n")
132
+ t.headings = gen_headings
133
+ gen_table.each do |row|
134
+ # [uuid, name, [ips], [networks], mem, power, tools]
135
+ uuid, name, ips, networks, mem, power, tools = row
136
+ elements = []
137
+ if config[:uuid]
138
+ elements << "#{uuid}\n #{ui.color('name: ', :yellow)}#{name.ljust(32)}"
40
139
  else
41
- ips = "unknown"
42
- end
43
- networks = []
44
- vm.vifs.each do |vif|
45
- name = vif.network.name
46
- if name.size > 20
47
- name = name[0..16] + '...'
48
- end
49
- networks << name
140
+ elements << "#{ui.color('name: ', :yellow)}#{name.ljust(32)}"
50
141
  end
51
- networks = networks.join("\n")
52
- mem = vm.memory_static_max.to_i.bytes.to.megabytes.round
53
- t << ["#{vm.uuid}\n #{ui.color('name: ', :yellow)}#{vm.name.ljust(32)}", mem, vm.power_state, vm.tools_installed?, networks,ips]
142
+ elements << mem if config[:mem]
143
+ elements << power if config[:power_state]
144
+ elements << tools if config[:tools]
145
+ elements << networks.join("\n") if config[:networks]
146
+ elements << ips.join("\n") if config[:ips]
147
+ t << elements
54
148
  end
55
149
  end
56
150
  puts vm_table if connection.servers.size > 0
57
151
  end
152
+
153
+ def print_csv
154
+ lines = []
155
+ header = ""
156
+ gen_table.each do |row|
157
+ uuid, name, ips, networks, mem, power, tools = row
158
+ elements = []
159
+ elements << name
160
+ elements << mem if config[:mem]
161
+ elements << power if config[:power_state]
162
+ elements << tools if config[:tools]
163
+ elements << networks.join(";") if config[:networks]
164
+ elements << ips.join(";") if config[:ips]
165
+ if config[:uuid]
166
+ header = "UUID,#{gen_headings.join(',')}"
167
+ lines << "#{uuid},#{elements.join(',')}"
168
+ else
169
+ header = "#{gen_headings.join(',')}"
170
+ lines << "#{elements.join(',')}"
171
+ end
172
+ end
173
+
174
+ puts header
175
+ lines.each do |l|
176
+ puts l
177
+ end
178
+ end
179
+
180
+ def run
181
+ $stdout.sync = true
182
+ if config[:csv]
183
+ print_csv
184
+ else
185
+ print_table
186
+ end
187
+ end
188
+
58
189
  end
59
190
  end
60
191
  end
@@ -1,6 +1,6 @@
1
1
  module Knife
2
2
  module XenServer
3
- VERSION = "1.1"
3
+ VERSION = "1.2"
4
4
  MAJOR, MINOR, TINY = VERSION.split('.')
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-xenserver
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '1.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-21 00:00:00.000000000 Z
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: terminal-table