esx 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,8 +6,8 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "shoulda", ">= 0"
9
+ gem "rspec", ">= 0"
10
10
  gem "bundler", "~> 1.0.0"
11
- gem "jeweler", "~> 1.5.2"
12
- gem "rcov", ">= 0"
11
+ gem "jeweler"
12
+ gem "simplecov", ">= 0"
13
13
  end
data/README.md CHANGED
@@ -100,6 +100,10 @@ __esx create-vm --user root --password foo --name esx-rubiojr --disk-file /path/
100
100
 
101
101
  end
102
102
 
103
+ # Testing
104
+
105
+ Run rspec from the base dir. By default, the tests try to connect to an ESX host named esx-test-host with user root and no password. Edit spec/spec_helper.rb to fit your needs.
106
+
103
107
  # Copyright
104
108
 
105
109
  Copyright (c) 2011 Sergio Rubio. See LICENSE.txt for
data/bin/esx CHANGED
@@ -75,6 +75,7 @@ class CreateVMCommand < BaseCommand
75
75
 
76
76
  parameter "ADDRESS", "ESX host address"
77
77
  option "--disk-file", "DISK_FILE", "VMDK file to import", :attribute_name => :disk_file
78
+ option "--disk-size", "DISK_SIZE", "VM Disk size", :attribute_name => :disk_size, :default => 8192
78
79
  option "--guest-id", "GUEST_ID", "GuestID value", :attribute_name => :guest_id, :default => 'otherGuest'
79
80
  option "--name", "VM NAME", "Virtual Machine name (required)"
80
81
  option "--memory", "MEMORY", "VM Memory size in MB", :default => 512
@@ -93,47 +94,58 @@ class CreateVMCommand < BaseCommand
93
94
  end
94
95
  exit 1
95
96
  end
97
+ if disk_size and disk_file
98
+ $stderr.puts "Both --disk-file and --disk-size specified. --disk-size will be ignored."
99
+ end
96
100
  downloaded_file = nil
97
- df = disk_file.dup
98
- if df.strip.chomp =~ /^http/
101
+ if disk_file.nil?
102
+ # if --disk-file nil? create the VM without disk
103
+ vm = host.create_vm :vm_name => name,
104
+ :datastore => datastore, :disk_type => :flat, :memory => memory,
105
+ :disk_size => disk_size,
106
+ :guest_id => guest_id, :mac_address => mac_address
107
+ else
108
+ df = disk_file.dup
109
+ if df.strip.chomp =~ /^http/
110
+ begin
111
+ downloaded_file = disk_file.dup
112
+ tmpfile = "#{tmpdir}/#{Time.now.to_i}.esx"
113
+ puts "Downloading file... (#{tmpfile})"
114
+ download! downloaded_file, tmpfile
115
+ puts
116
+ df = tmpfile
117
+ rescue Exception => e
118
+ FileUtils.rm_f(tmpfile)
119
+ $stderr.puts "Error downloading file from #{downloaded_file}."
120
+ $stderr.puts e.message if debug?
121
+ exit 1
122
+ end
123
+ end
124
+ raise Exception.new("Invalid disk file") if not File.exist?(df)
125
+ if not name
126
+ $stderr.puts "Invalid VM name."
127
+ $stderr.puts "Use --name option to specify the VM name"
128
+ exit 1
129
+ end
130
+ host.remote_command "mkdir /vmfs/volumes/#{datastore}/#{name}"
131
+
99
132
  begin
100
- downloaded_file = disk_file.dup
101
- tmpfile = "#{tmpdir}/#{Time.now.to_i}.esx"
102
- puts "Downloading file... (#{tmpfile})"
103
- download! downloaded_file, tmpfile
104
- puts
105
- df = tmpfile
133
+ host.import_disk df, "/vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk"
106
134
  rescue Exception => e
107
- FileUtils.rm_f(tmpfile)
108
- $stderr.puts "Error downloading file from #{downloaded_file}."
135
+ $stderr.puts "Error uploading file to /vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk"
109
136
  $stderr.puts e.message if debug?
110
137
  exit 1
111
138
  end
112
- end
113
- raise Exception.new("Invalid disk file") if not File.exist?(df)
114
- if not name
115
- $stderr.puts "Invalid VM name."
116
- $stderr.puts "Use --name option to specify the VM name"
117
- exit 1
118
- end
119
- host.remote_command "mkdir /vmfs/volumes/#{datastore}/#{name}"
120
139
 
121
- begin
122
- host.import_disk df, "/vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk"
123
- rescue Exception => e
124
- $stderr.puts "Error uploading file to /vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk"
125
- $stderr.puts e.message if debug?
126
- exit 1
127
- end
128
-
129
- if not downloaded_file.nil?
130
- puts "Deleting tmp file #{df}" if debug?
131
- FileUtils.rm_f(df)
140
+ if not downloaded_file.nil?
141
+ puts "Deleting tmp file #{df}" if debug?
142
+ FileUtils.rm_f(df)
143
+ end
144
+ vm = host.create_vm :vm_name => name,
145
+ :disk_file => "#{name}/#{name}.vmdk",
146
+ :datastore => datastore, :disk_type => :flat, :memory => memory,
147
+ :guest_id => guest_id, :mac_address => mac_address
132
148
  end
133
- vm = host.create_vm :vm_name => name,
134
- :disk_file => "#{name}/#{name}.vmdk",
135
- :datastore => datastore, :disk_type => :flat, :memory => memory,
136
- :guest_id => guest_id, :mac_address => mac_address
137
149
  if poweron?
138
150
  vm.power_on
139
151
  end
@@ -195,4 +207,12 @@ class DefaultCommand < Clamp::Command
195
207
  subcommand "create-vm", "Create a VM", CreateVMCommand
196
208
  end
197
209
 
198
- DefaultCommand.run
210
+ begin
211
+ DefaultCommand.run
212
+ rescue Exception => e
213
+ puts e.message
214
+ if $DEBUG
215
+ puts $!
216
+ puts $@
217
+ end
218
+ end
data/lib/esx.rb CHANGED
@@ -6,7 +6,7 @@ require 'net/ssh'
6
6
 
7
7
  module ESX
8
8
 
9
- VERSION = '0.2.1'
9
+ VERSION = '0.2.3'
10
10
 
11
11
  class Host
12
12
 
@@ -134,10 +134,13 @@ module ESX
134
134
  }
135
135
  ]
136
136
  }
137
+
138
+ # VMDK provided, replace the empty vmdk
137
139
  vm_cfg[:deviceChange].push(create_disk_spec(:disk_file => spec[:disk_file],
138
140
  :disk_type => spec[:disk_type],
139
141
  :disk_size => spec[:disk_size],
140
142
  :datastore => spec[:datastore]))
143
+
141
144
  VM.wrap(@_datacenter.vmFolder.CreateVM_Task(:config => vm_cfg, :pool => @_datacenter.hostFolder.children.first.resourcePool).wait_for_completion)
142
145
  end
143
146
 
@@ -337,6 +340,10 @@ module ESX
337
340
  vm_object.Destroy_Task.wait_for_completion
338
341
  end
339
342
 
343
+ def reset
344
+ vm_object.ResetVM_Task.wait_for_completion
345
+ end
346
+
340
347
  def guest_info
341
348
  GuestInfo.wrap(vm_object.guest)
342
349
  end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'rspec/autorun'
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start do
6
+ add_filter '/spec/'
7
+ end
8
+
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ require 'esx'
12
+
13
+ module ESXTestHelpers
14
+
15
+ def esx_host
16
+ "esx-test-host"
17
+ end
18
+
19
+ def esx_user
20
+ "root"
21
+ end
22
+
23
+ def esx_password
24
+ ""
25
+ end
26
+
27
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe "ESX host" do
4
+ include ESXTestHelpers
5
+
6
+ before do
7
+ @test_host = ESX::Host.connect(esx_host, esx_user, esx_password)
8
+ end
9
+
10
+ it "connects to and ESX with a valid user/pass" do
11
+ host = nil
12
+ lambda do
13
+ host = ESX::Host.connect(esx_host, esx_user, esx_password)
14
+ end.should_not raise_error
15
+
16
+ end
17
+
18
+ it "retrives the host name" do
19
+ @test_host.name.should_not be_nil
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-16 00:00:00.000000000 Z
12
+ date: 2011-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: shoulda
16
- requirement: &16159880 !ruby/object:Gem::Requirement
15
+ name: rspec
16
+ requirement: &15484180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *16159880
24
+ version_requirements: *15484180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &16158560 !ruby/object:Gem::Requirement
27
+ requirement: &15481960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,21 +32,21 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *16158560
35
+ version_requirements: *15481960
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &16157260 !ruby/object:Gem::Requirement
38
+ requirement: &15495640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
- - - ~>
41
+ - - ! '>='
42
42
  - !ruby/object:Gem::Version
43
- version: 1.5.2
43
+ version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *16157260
46
+ version_requirements: *15495640
47
47
  - !ruby/object:Gem::Dependency
48
- name: rcov
49
- requirement: &16155820 !ruby/object:Gem::Requirement
48
+ name: simplecov
49
+ requirement: &15492220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *16155820
57
+ version_requirements: *15492220
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: alchemist
60
- requirement: &16154620 !ruby/object:Gem::Requirement
60
+ requirement: &15503640 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *16154620
68
+ version_requirements: *15503640
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rbvmomi
71
- requirement: &16153360 !ruby/object:Gem::Requirement
71
+ requirement: &15498460 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *16153360
79
+ version_requirements: *15498460
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: terminal-table
82
- requirement: &16176540 !ruby/object:Gem::Requirement
82
+ requirement: &15511560 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *16176540
90
+ version_requirements: *15511560
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: net-ssh
93
- requirement: &16175200 !ruby/object:Gem::Requirement
93
+ requirement: &15510000 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *16175200
101
+ version_requirements: *15510000
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: net-scp
104
- requirement: &16173960 !ruby/object:Gem::Requirement
104
+ requirement: &15508740 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *16173960
112
+ version_requirements: *15508740
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: clamp
115
- requirement: &16172640 !ruby/object:Gem::Requirement
115
+ requirement: &15507180 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :runtime
122
122
  prerelease: false
123
- version_requirements: *16172640
123
+ version_requirements: *15507180
124
124
  description: Manage VMWare ESX hosts with ease
125
125
  email: rubiojr@frameos.org
126
126
  executables:
@@ -138,8 +138,8 @@ files:
138
138
  - bin/esx
139
139
  - examples/basics.rb
140
140
  - lib/esx.rb
141
- - test/helper.rb
142
- - test/test_esx.rb
141
+ - spec/spec_helper.rb
142
+ - spec/unit/host_spec.rb
143
143
  homepage: http://github.com/rubiojr/esx
144
144
  licenses:
145
145
  - MIT
@@ -1,18 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
10
- require 'test/unit'
11
- require 'shoulda'
12
-
13
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
- $LOAD_PATH.unshift(File.dirname(__FILE__))
15
- require 'esx'
16
-
17
- class Test::Unit::TestCase
18
- end
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestEsx < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end