opennebula-cli 3.8.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/onevnet ADDED
@@ -0,0 +1,227 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # -------------------------------------------------------------------------- #
4
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
5
+ # #
6
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
7
+ # not use this file except in compliance with the License. You may obtain #
8
+ # a copy of the License at #
9
+ # #
10
+ # http://www.apache.org/licenses/LICENSE-2.0 #
11
+ # #
12
+ # Unless required by applicable law or agreed to in writing, software #
13
+ # distributed under the License is distributed on an "AS IS" BASIS, #
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15
+ # See the License for the specific language governing permissions and #
16
+ # limitations under the License. #
17
+ #--------------------------------------------------------------------------- #
18
+
19
+ ONE_LOCATION=ENV["ONE_LOCATION"]
20
+
21
+ if !ONE_LOCATION
22
+ RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
+ else
24
+ RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
+ end
26
+
27
+ $: << RUBY_LIB_LOCATION
28
+ $: << RUBY_LIB_LOCATION+"/cli"
29
+
30
+ require 'command_parser'
31
+ require 'one_helper/onevnet_helper'
32
+ require 'one_helper/onecluster_helper'
33
+
34
+ cmd=CommandParser::CmdParser.new(ARGV) do
35
+ usage "`onevnet` <command> [<args>] [<options>]"
36
+ version OpenNebulaHelper::ONE_VERSION
37
+
38
+ helper = OneVNetHelper.new
39
+
40
+ ########################################################################
41
+ # Global Options
42
+ ########################################################################
43
+ set :option, CommandParser::OPTIONS
44
+
45
+ CREATE_OPTIONS = [OneClusterHelper::CLUSTER]
46
+
47
+ ########################################################################
48
+ # Formatters for arguments
49
+ ########################################################################
50
+ set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg|
51
+ OpenNebulaHelper.rname_to_id(arg, "GROUP")
52
+ end
53
+
54
+ set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg|
55
+ OpenNebulaHelper.rname_to_id(arg, "USER")
56
+ end
57
+
58
+ set :format, :vnetid, OneVNetHelper.to_id_desc do |arg|
59
+ helper.to_id(arg)
60
+ end
61
+
62
+ set :format, :vnetid_list, OneVNetHelper.list_to_id_desc do |arg|
63
+ helper.list_to_id(arg)
64
+ end
65
+
66
+ set :format, :filterflag, OneVNetHelper.filterflag_to_i_desc do |arg|
67
+ helper.filterflag_to_i(arg)
68
+ end
69
+
70
+ ########################################################################
71
+ # Commands
72
+ ########################################################################
73
+
74
+ create_desc = <<-EOT.unindent
75
+ Creates a new Virtual Network from the given template file
76
+ EOT
77
+
78
+ command :create, create_desc, :file, :options=>CREATE_OPTIONS do
79
+ cid = options[:cluster] || ClusterPool::NONE_CLUSTER_ID
80
+
81
+ helper.create_resource(options) do |vn|
82
+ begin
83
+ template=File.read(args[0])
84
+ vn.allocate(template, cid)
85
+ rescue => e
86
+ STDERR.puts e.message
87
+ exit -1
88
+ end
89
+ end
90
+ end
91
+
92
+ delete_desc = <<-EOT.unindent
93
+ Deletes the given Virtual Network
94
+ EOT
95
+
96
+ command :delete, delete_desc, [:range, :vnetid_list] do
97
+ helper.perform_actions(args[0],options,"deleted") do |vn|
98
+ vn.delete
99
+ end
100
+ end
101
+
102
+ addleases_desc = <<-EOT.unindent
103
+ Adds a lease to the Virtual Network
104
+ EOT
105
+
106
+ command :addleases, addleases_desc, :vnetid, :ip, [:mac, nil] do
107
+ helper.perform_action(args[0],options,"lease added") do |vn|
108
+ vn.addleases(args[1], args[2])
109
+ end
110
+ end
111
+
112
+ rmleases_desc = <<-EOT.unindent
113
+ Removes a lease from the Virtual Network
114
+ EOT
115
+
116
+ command :rmleases, rmleases_desc, :vnetid, :ip do
117
+ helper.perform_action(args[0],options,"lease removed") do |vn|
118
+ vn.rmleases(args[1])
119
+ end
120
+ end
121
+
122
+ hold_desc = <<-EOT.unindent
123
+ Holds a Virtual Network lease, marking it as used
124
+ EOT
125
+
126
+ command :hold, hold_desc, :vnetid, :ip do
127
+ helper.perform_action(args[0],options,"lease on hold") do |vn|
128
+ vn.hold(args[1])
129
+ end
130
+ end
131
+
132
+ release_desc = <<-EOT.unindent
133
+ Releases a Virtual Network lease on hold
134
+ EOT
135
+
136
+ command :release, release_desc, :vnetid, :ip do
137
+ helper.perform_action(args[0],options,"lease released") do |vn|
138
+ vn.release(args[1])
139
+ end
140
+ end
141
+
142
+ publish_desc = <<-EOT.unindent
143
+ DEPRECATED, use chmod instead. Publishes the given Virtual Network.
144
+ A public Virtual Network can be seen and used by other users in the
145
+ Virtual Network's group.
146
+ EOT
147
+
148
+ command :publish, publish_desc, [:range,:vnetid_list] do
149
+ helper.perform_actions(args[0],options,"published") do |vn|
150
+ vn.publish
151
+ end
152
+ end
153
+
154
+ unpublish_desc = <<-EOT.unindent
155
+ DEPRECATED, use chmod instead. Unpublishes the given Virtual Network. A
156
+ private Virtual Network can't be used by any other user.
157
+ EOT
158
+
159
+ command :unpublish, unpublish_desc, [:range,:vnetid_list] do
160
+ helper.perform_actions(args[0],options,"unpublished") do |vn|
161
+ vn.unpublish
162
+ end
163
+ end
164
+
165
+ chgrp_desc = <<-EOT.unindent
166
+ Changes the Virtual Network group
167
+ EOT
168
+
169
+ command :chgrp, chgrp_desc,[:range, :vnetid_list], :groupid do
170
+ helper.perform_actions(args[0],options,"Group changed") do |vn|
171
+ vn.chown(-1, args[1].to_i)
172
+ end
173
+ end
174
+
175
+ chown_desc = <<-EOT.unindent
176
+ Changes the Virtual Network owner and group
177
+ EOT
178
+
179
+ command :chown, chown_desc, [:range, :vnetid_list], :userid,
180
+ [:groupid,nil] do
181
+ gid = args[2].nil? ? -1 : args[2].to_i
182
+ helper.perform_actions(args[0],options,"Owner/Group changed") do |vn|
183
+ vn.chown(args[1].to_i, gid)
184
+ end
185
+ end
186
+
187
+ chmod_desc = <<-EOT.unindent
188
+ Changes the Virtual Network permissions
189
+ EOT
190
+
191
+ command :chmod, chmod_desc, [:range, :vnetid_list], :octet do
192
+ helper.perform_actions(args[0],options, "Permissions changed") do |vn|
193
+ vn.chmod_octet(args[1])
194
+ end
195
+ end
196
+
197
+ list_desc = <<-EOT.unindent
198
+ Lists Virtual Networks in the pool
199
+ EOT
200
+
201
+ command :list, list_desc, [:filterflag, nil],
202
+ :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS+
203
+ [OpenNebulaHelper::DESCRIBE] do
204
+ helper.list_pool(options, false, args[0])
205
+ end
206
+
207
+ show_desc = <<-EOT.unindent
208
+ Shows information for the given Virtual Network
209
+ EOT
210
+
211
+ command :show, show_desc, :vnetid,
212
+ :options=>OpenNebulaHelper::XML do
213
+ helper.show_resource(args[0],options)
214
+ end
215
+
216
+ update_desc = <<-EOT.unindent
217
+ Update the template contents. If a path is not provided the editor will
218
+ be launched to modify the current content.
219
+ EOT
220
+
221
+ command :update, update_desc, :vnetid, [:file, nil] do
222
+ helper.perform_action(args[0],options,"modified") do |vnet|
223
+ str = OpenNebulaHelper.update_template(args[0], vnet, args[1])
224
+ vnet.update(str)
225
+ end
226
+ end
227
+ end
data/lib/cli_helper.rb ADDED
@@ -0,0 +1,294 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+ module CLIHelper
18
+ LIST = {
19
+ :name => "list",
20
+ :short => "-l x,y,z",
21
+ :large => "--list x,y,z",
22
+ :format => Array,
23
+ :description => "Selects columns to display with list command"
24
+ }
25
+
26
+ #ORDER = {
27
+ # :name => "order",
28
+ # :short => "-o x,y,z",
29
+ # :large => "--order x,y,z",
30
+ # :format => Array,
31
+ # :description => "Order by these columns, column starting with - means decreasing order"
32
+ #}
33
+ #
34
+ #FILTER = {
35
+ # :name => "filter",
36
+ # :short => "-f x,y,z",
37
+ # :large => "--filter x,y,z",
38
+ # :format => Array,
39
+ # :description => "Filter data. An array is specified with column=value pairs."
40
+ #}
41
+ #
42
+ #HEADER = {
43
+ # :name => "header",
44
+ # :short => "-H",
45
+ # :large => "--header",
46
+ # :description => "Shows the header of the table"
47
+ #}
48
+
49
+ DELAY = {
50
+ :name => "delay",
51
+ :short => "-d x",
52
+ :large => "--delay x",
53
+ :format => Integer,
54
+ :description => "Sets the delay in seconds for top command"
55
+ }
56
+
57
+ #OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY]
58
+ OPTIONS = [LIST, DELAY]
59
+
60
+ # Sets bold font
61
+ def CLIHelper.scr_bold
62
+ print "\33[1m"
63
+ end
64
+
65
+ # Sets underline
66
+ def CLIHelper.scr_underline
67
+ print "\33[4m"
68
+ end
69
+
70
+ # Restore normal font
71
+ def CLIHelper.scr_restore
72
+ print "\33[0m"
73
+ end
74
+
75
+ # Clears screen
76
+ def CLIHelper.scr_cls
77
+ print "\33[2J\33[H"
78
+ end
79
+
80
+ # Moves the cursor
81
+ def CLIHelper.scr_move(x,y)
82
+ print "\33[#{x};#{y}H"
83
+ end
84
+
85
+ def CLIHelper.scr_red
86
+ print "\33[31m"
87
+ end
88
+
89
+ def CLIHelper.scr_green
90
+ print "\33[32m"
91
+ end
92
+
93
+ ANSI_RED="\33[31m"
94
+ ANSI_GREEN="\33[32m"
95
+ ANSI_RESET="\33[0m"
96
+
97
+ OK_STATES=%w{runn rdy on}
98
+ BAD_STATES=%w{fail err err}
99
+
100
+ def CLIHelper.color_state(stat)
101
+ if $stdout.tty?
102
+ case stat.strip
103
+ when *OK_STATES
104
+ ANSI_GREEN+stat+ANSI_RESET
105
+ when *BAD_STATES
106
+ ANSI_RED+stat+ANSI_RESET
107
+ else
108
+ stat
109
+ end
110
+ else
111
+ stat
112
+ end
113
+ end
114
+
115
+ # Print header
116
+ def CLIHelper.print_header(str, underline=true)
117
+ if $stdout.tty?
118
+ scr_bold
119
+ scr_underline if underline
120
+ print str
121
+ scr_restore
122
+ else
123
+ print str
124
+ end
125
+ puts
126
+ end
127
+
128
+ class ShowTable
129
+ require 'yaml'
130
+
131
+ def initialize(conf=nil, ext=nil, &block)
132
+ @columns = Hash.new
133
+ @default_columns = Array.new
134
+
135
+ @ext = ext
136
+ @conf = conf
137
+
138
+ instance_eval(&block)
139
+ end
140
+
141
+ def helper
142
+ @ext
143
+ end
144
+
145
+ def column(name, desc, *conf, &block)
146
+ column = Hash.new
147
+ column[:desc] = desc
148
+ column[:size] = 5
149
+ conf.each{|c|
150
+ if c.instance_of?(Symbol)
151
+ column[c]=true
152
+ elsif c.instance_of?(Hash)
153
+ c.each{|key,value|
154
+ column[key]=value
155
+ }
156
+ end
157
+ }
158
+ column[:proc] = block
159
+ @columns[name.to_sym] = column
160
+ @default_columns<<name
161
+ end
162
+
163
+ def default(*args)
164
+ args.map!{|a| a.to_sym }
165
+ @default_columns=args
166
+ end
167
+
168
+ def show(data, options={})
169
+ update_columns(options)
170
+ print_table(data, options)
171
+ end
172
+
173
+ def top(options={}, &block)
174
+ delay=options[:delay] ? options[:delay] : 1
175
+
176
+ begin
177
+ while true
178
+ CLIHelper.scr_cls
179
+ CLIHelper.scr_move(0,0)
180
+
181
+ data = block.call
182
+
183
+ show(data, options)
184
+ sleep delay
185
+ end
186
+ rescue Exception => e
187
+ puts e.message
188
+ end
189
+ end
190
+
191
+ def describe_columns
192
+ str="%-20s: %-20s"
193
+
194
+ @columns.each do |column, d|
195
+ puts str % [column, d[:desc]]
196
+ end
197
+ end
198
+
199
+ private
200
+
201
+ def print_table(data, options)
202
+ CLIHelper.print_header(header_str)
203
+ data ? print_data(data, options) : puts
204
+ end
205
+
206
+ def print_data(data, options)
207
+ ncolumns=@default_columns.length
208
+ res_data=data_array(data, options)
209
+
210
+ if options[:stat_column]
211
+ stat_column=@default_columns.index(
212
+ options[:stat_column].upcase.to_sym)
213
+ else
214
+ stat_column=@default_columns.index(:STAT)
215
+ end
216
+
217
+ begin
218
+ print res_data.collect{|l|
219
+ (0..ncolumns-1).collect{ |i|
220
+ dat=l[i]
221
+ col=@default_columns[i]
222
+
223
+ str=format_str(col, dat)
224
+ str=CLIHelper.color_state(str) if i==stat_column
225
+
226
+ str
227
+ }.join(' ')
228
+ }.join("\n")
229
+ rescue Errno::EPIPE
230
+ end
231
+
232
+ puts
233
+ end
234
+
235
+ def data_array(data, options)
236
+ res_data=data.collect {|d|
237
+ @default_columns.collect {|c|
238
+ @columns[c][:proc].call(d).to_s if @columns[c]
239
+ }
240
+ }
241
+
242
+ if options
243
+ filter_data!(res_data, options[:filter]) if options[:filter]
244
+ sort_data!(res_data, options[:order]) if options[:order]
245
+ end
246
+
247
+ res_data
248
+ end
249
+
250
+ def format_str(field, data)
251
+ if @columns[field]
252
+ minus=( @columns[field][:left] ? "-" : "" )
253
+ size=@columns[field][:size]
254
+ return "%#{minus}#{size}.#{size}s" % [ data.to_s ]
255
+ else
256
+ exit -1, "Column #{field} not defined."
257
+ end
258
+ end
259
+
260
+ def update_columns(options)
261
+ begin
262
+ config = YAML.load_file(@conf)
263
+
264
+ default = config.delete(:default)
265
+ @default_columns = default unless default.empty?
266
+
267
+ # Filter show options with available columns
268
+ @default_columns &= @columns.keys
269
+
270
+ @columns.merge!(config) { |key, oldval, newval|
271
+ oldval.merge(newval)
272
+ }
273
+ rescue Exception => e
274
+ end
275
+
276
+ @default_columns = options[:list].collect{|o| o.to_sym} if options[:list]
277
+ end
278
+
279
+ def header_str
280
+ @default_columns.collect {|c|
281
+ if @columns[c]
282
+ format_str(c, c.to_s)
283
+ else
284
+ puts "Column #{c} not defined."
285
+ exit -1
286
+ end
287
+ }.compact.join(' ')
288
+ end
289
+
290
+ # TBD def filter_data!
291
+
292
+ # TBD def sort_data!
293
+ end
294
+ end