ccbuilder 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/bin/befog +52 -25
  3. data/lib/ONEClient.rb +39 -6
  4. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
data/bin/befog CHANGED
@@ -4,33 +4,49 @@ require "rubygems"
4
4
  require "getoptlong"
5
5
  require "ONEClient"
6
6
 
7
- help = <<EOF
8
- Usage: befog [OPTIONS] COMMAND
7
+ help = <<EOHELP
8
+ Authors: Dennis Klein <d.klein@gsi.de>, Victor Penso <v.penso@gsi.de>
9
+ USAGE:
10
+ befog [OPTIONS] COMMAND
9
11
 
10
- -h, --help: Display this help
12
+ OPTIONS:
13
+ -h, --help Display this help
14
+ -v, --verbose Print out progress and debug logs
11
15
 
12
- COMMANDS:
16
+ COMMANDS:
13
17
 
14
- create FILE | create n virtual machines of type specified in FILE
15
- -n, --repeat: Number of virtual machines, default n=1
16
- FILE: Open Nebula virtual machine descriptor
18
+ * create (create n virtual machines of type specified in FILE)
19
+ befog create FILE
20
+
21
+ -n, --repeat Number of virtual machines, default n=1
22
+ FILE Open Nebula virtual machine descriptor
17
23
 
18
- list | show a list of your virtual machines
24
+ * list (show a list of your virtual machines)
25
+ befog list
19
26
 
20
- delete ID[..ID] | delete specified virtual machines
21
- ID: single virtual machine ID
22
- ID..ID: range of virtual machine IDs
23
- EOF
27
+ -t, --table Display table header
28
+
29
+ * delete (delete specified virtual machines)
30
+ befog delete ID[..ID]
31
+
32
+ ID single virtual machine ID
33
+ ID..ID range of virtual machine IDs
34
+
35
+ EOHELP
24
36
 
25
37
  begin
26
38
 
27
39
  opts = GetoptLong.new(
28
40
  [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
29
- [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ]
41
+ [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
42
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
43
+ [ '--table', '-t', GetoptLong::NO_ARGUMENT ]
30
44
  )
31
-
45
+
32
46
  # parse OPTIONS
33
47
  repeat = 1
48
+ verbose = false
49
+ table = false
34
50
  opts.each do |opt, arg|
35
51
  case opt
36
52
  when '--help'
@@ -38,35 +54,46 @@ begin
38
54
  exit
39
55
  when '--repeat'
40
56
  repeat = arg.to_i
57
+ when '--verbose'
58
+ verbose = true
59
+ when '--table'
60
+ table = true
41
61
  end
42
62
  end
43
63
 
44
64
  # parse COMMAND
45
- raise "Missing command (try --help)" if ARGV.length != 1
65
+ raise "Missing command (try --help)" if ARGV.length < 1
46
66
  command = ARGV[0]
47
67
 
48
68
  case command
49
69
  when "create"
50
70
  file = ARGV[1]
51
- raise "Missing Open Nebula virtual machine decriptor FILE (try --help)" if !File.exists?(file)
52
- one_client = CCBuilder::ONEClient.new
71
+ raise "Missing Open Nebula virtual machine decriptor FILE (try --help)" if !file
72
+ raise "File does not exist: #{file}" if !File.exists?(file)
73
+ one_client = CCBuilder::ONEClient.new(verbose)
53
74
  one_client.create(file, repeat)
75
+
54
76
  when "list"
55
- one_client = CCBuilder::ONEClient.new
77
+ one_client = CCBuilder::ONEClient.new(verbose)
78
+ format = "%7s %16s %30s %15s"
79
+ puts format % ['ID', 'IP', 'HOST', 'STATE'] if table
56
80
  one_client.list.each do |vm|
57
- print vm['id'] + "\t" + vm['ip'] + "\t" + vm['host']
81
+ puts format % [vm['id'], vm['ip'], vm['host'], vm['state']]
58
82
  end
83
+
59
84
  when "delete"
60
85
  begin
61
- id_low = ARGV[1].match(/^([12345679][:digit:]*)/)[0].to_i
62
- id_high = ARGV[1].match(/\.\.([12345679][:digit:]*)?/)[0].to_i
63
- rescue
64
- raise "Illegal ID input (try --help)"
86
+ id_low, id_high = ARGV[1].split('..')
87
+ id_low = id_low.to_i
88
+ id_high = id_high.to_i
89
+ raise "Invalid or missing low ID" if !id_low
90
+ rescue Exception => e
91
+ raise "Illegal ID input. #{e.message} (try --help)"
65
92
  end
66
- raise "Missing ID (try --help)" if id_low == 0
67
93
  id_high = id_low if id_high < id_low
68
- one_client = CCBuilder::ONEClient.new
94
+ one_client = CCBuilder::ONEClient.new(verbose)
69
95
  one_client.delete(id_low, id_high)
96
+
70
97
  else
71
98
  raise "Unrecognized command: #{command} (try --help)"
72
99
  end
@@ -7,11 +7,13 @@ RUBY_LIB_LOCATION = !ONE_LOCATION ? "/usr/lib/one/ruby" : ONE_LOCATION+"/lib/rub
7
7
  $: << RUBY_LIB_LOCATION
8
8
 
9
9
  require "OpenNebula"
10
+ require "resolv"
10
11
 
11
12
  module CCBuilder
12
13
  class ONEClient
13
14
 
14
- def initialize()
15
+ def initialize(verbose = false)
16
+ @verbose = verbose
15
17
  @one_client = OpenNebula::Client.new(nil)
16
18
  rescue OpenNebula::Error => e
17
19
  raise Error.new("OpenNebula: #{e.message}")
@@ -21,20 +23,51 @@ module CCBuilder
21
23
  def create(one_vm_descriptor, n = 1)
22
24
  n.times do |i|
23
25
  create_single(one_vm_descriptor)
26
+ puts "created VM #i" if @verbose
24
27
  end
25
28
  end
26
29
 
27
30
  def create_single(one_vm_descriptor)
28
31
  vm = OpenNebula::VirtualMachine.new(OpenNebula::VirtualMachine.build_xml, @one_client)
29
32
  template=File.read(one_vm_descriptor)
30
- vm.allocate(template)
33
+ vm.allocate(template)
31
34
  rescue
32
35
  raise Error.new("OpenNebula: Can not read template: #{one_vm_descriptor}")
33
36
  end
34
37
 
35
38
  # API-method list
36
39
  def list
37
- raise Error.new("not yet implemented")
40
+ n = 3
41
+ vmpool = OpenNebula::VirtualMachinePool.new(@one_client, -1)
42
+ result = vmpool.info
43
+ vms = Array.new
44
+ queue = Queue.new # queue to hold ids which need to be looked up
45
+ threads = Array.new
46
+ n.times do |i| # Launch n threads which do the lookups#
47
+ threads[i] = Thread.new do
48
+ id = queue.pop
49
+ until id == 0
50
+ vm = OpenNebula::VirtualMachine.new_with_id(id, @one_client)
51
+ vm.info
52
+ ip = vm.template_str.scan(/IP=(\d+\.\d+\.\d+\.\d+),/)[0][0]
53
+ state = vm.lcm_state_str
54
+ host = Resolv::getname(ip)
55
+ Thread.pass
56
+ puts "VM ##{id} with ip #{ip} resolved to name #{host}" if @verbose
57
+ vms << { 'id' => id, 'ip' => ip, 'host' => host, 'state' => state }
58
+ STDOUT.flush
59
+ id = queue.pop
60
+ end
61
+ end
62
+ end
63
+ vmpool.map do |vm|
64
+ queue << vm.id
65
+ end
66
+ n.times { queue << 0 } # send 0 to terminate threads
67
+ n.times { |i| threads[i].join } # wait until threads finished
68
+ return vms
69
+ ensure
70
+ n.times { |i| threads[i].terminate }
38
71
  end
39
72
 
40
73
  # API-method delete
@@ -42,7 +75,8 @@ module CCBuilder
42
75
  id_high = id_low if id_high < id_low
43
76
  Range.new(id_low,id_high).each do |id|
44
77
  delete_single(id)
45
- end
78
+ puts "deleted VM #{id}" if @verbose
79
+ end
46
80
  end
47
81
 
48
82
  def delete_single(id)
@@ -52,8 +86,7 @@ module CCBuilder
52
86
  raise Error.new("OpenNebula: #{e.message}")
53
87
  end
54
88
 
89
+ class Error < Exception
55
90
  end
56
-
57
- class Error < Exception end
58
91
  end
59
92
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccbuilder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dennis Klein