esxi 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 948e7741c10ee20d459d83a8ccaa24706785ee8b
4
+ data.tar.gz: 412e1c03df08f0279ca1492d8e76c806c8291cc3
5
+ SHA512:
6
+ metadata.gz: 07ae76a2cadffcce64475e45fe5fc7923e03c5c4ef27f436fe317e0e2be3b8794c440d333fc53ff71ee208f67c4f7452795e1039f9358653e7c17a37b54e9213
7
+ data.tar.gz: adea87269e39aa9fa82366097b1861f970061b65379d257737d8755cea2b41694d619bb32024c25eb2c51140cd2378f948c2e7012004f4fc657e906bb0f8411a
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in esxi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Prashanth Rajagopal
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Esxi
2
+
3
+ A simple gem written to solve only once purpose - Interact with ESXI
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'esxi'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install esxi
18
+
19
+ ## Usage
20
+
21
+ `require 'esxi'`
22
+ `esxi = VM.new({"host"=>"#{IP}", "user"=>"#{USER}", "password"=>"#{PASSWORD}", "port"=>"22"})`
23
+ `esxi.get_snapshots 1`
24
+ `esxi.create 1 name description`
25
+
26
+ Not tested extensively yet.
27
+
28
+ ## TODO
29
+
30
+ Write test Cases.
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/prashanthrajagopal/esxi/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/esxi.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'esxi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "esxi"
8
+ spec.version = Esxi::VERSION
9
+ spec.authors = ["Prashanth Rajagopal"]
10
+ spec.email = ["mail@prashanthr.net"]
11
+ spec.summary = %q{Essential interaction with ESXI}
12
+ spec.description = %q{A simple gem written to solve only once purpose - Interact with ESXI}
13
+ spec.homepage = "http://prashanthr.net/blog/2014/09/08/esxi_ruby/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency 'rake', '~> 10.3'
23
+ spec.add_runtime_dependency 'net-ssh', '~> 2.7'
24
+ end
@@ -0,0 +1,3 @@
1
+ module Esxi
2
+ VERSION = "0.0.1"
3
+ end
data/lib/esxi.rb ADDED
@@ -0,0 +1,101 @@
1
+ require "esxi/version"
2
+ require "util"
3
+
4
+ class VM
5
+ def initialize config
6
+ unless config['host'] then raise ArgumentError, "Must provide a hostname" end
7
+ unless config['user'] then raise ArgumentError, "Must provide a username" end
8
+
9
+ @host = config["host"]
10
+ @user = config["user"]
11
+ @password = config["password"] if config["password"]
12
+ @port = config["port"] ? config["port"] : 22
13
+
14
+ @session = Util.start_ssh_session({:host => @host, :port => @port, :user => @user, :password => @password, :retries => 5, :timeout => 75})
15
+ end
16
+
17
+ def start vmid
18
+ Util.run(@session, "vim-cmd vmsvc/power.on #{vmid}")
19
+ end
20
+
21
+ def stop vmid
22
+ Util.run(@session, "vim-cmd vmsvc/power.off #{vmid}")
23
+ end
24
+
25
+ def suspend vmid
26
+ Util.run(@session, "vim-cmd vmsvc/power.suspend #{vmid}")
27
+ end
28
+
29
+ def pause vmid
30
+ Util.run(@session, "vim-cmd vmsvc/power.suspend #{vmid}")
31
+ end
32
+
33
+ def resume vmid
34
+ Util.run(@session, "vim-cmd vmsvc/power.suspendResume #{vmid}")
35
+ end
36
+
37
+ def reset vmid
38
+ Util.run(@session, "vim-cmd vmsvc/power.reset #{vmid}")
39
+ end
40
+
41
+ def create_snapshot vmid, name, description
42
+ description ||= "Snapshot created by https://github.com/prashanthrajagopal/esxi"
43
+ Util.run(@session, "vim-cmd vmsvc/snapshot.create #{vmid} #{name} #{description} 1 0")
44
+ end
45
+
46
+ def revert_snapshot vmid, snapshot_name
47
+ snapshots = get_snapshots(vmid)
48
+ snapshots.each do |snap|
49
+ #puts "DEBUG: checking #{snapshot}"
50
+ if snap["name"].downcase == snapshot_name.strip.downcase
51
+ snap_id = snap["id"]
52
+ #puts "DEBUG: I would revert to #{snapshot}"
53
+ Util.run(@session, "vim-cmd vmsvc/snapshot.revert #{vmid} #{snap_id} 0")
54
+ return true
55
+ end
56
+ end
57
+ raise "Snapshot not found"
58
+ end
59
+
60
+ def delete_snapshot vmid, snapshot
61
+ snapshots = get_snapshots(vmid)
62
+ snapshots.each do |snap|
63
+ #puts "DEBUG: checking #{snapshot}"
64
+ if snap["name"].downcase == snapshot_name.strip.downcase
65
+ snap_id = snap["id"]
66
+ #puts "DEBUG: I would remove #{snapshot}"
67
+ Util.run(@session, "vim-cmd vmsvc/snapshot.remove #{vmid} #{snap_id}")
68
+ return true
69
+ end
70
+ end
71
+ raise "Invalid Snapshot Name"
72
+ end
73
+
74
+ def get_snapshots vmid
75
+ snapshots = Util.run(@session, "vim-cmd vmsvc/snapshot.get #{vmid}").split('|')
76
+ snapshots.shift
77
+ snapshot_list = []
78
+ snapshots.each do |snapshot|
79
+ info = {}
80
+ snap = snapshot.gsub('--','').split("\n")
81
+ snap.shift
82
+ snap.each do |x|
83
+ info[x.split(':')[0].strip.downcase.gsub("snapshot ", '')] = x.split(':')[1].strip
84
+ end
85
+ snapshot_list << info unless info == {}
86
+ end
87
+ snapshot_list
88
+ end
89
+
90
+ def delete_all_snapshots vmid
91
+ Util.run(@session, "vim-cmd vmsvc/snapshot.removeall #{vmid}")
92
+ end
93
+
94
+ def running? vmid
95
+ if Util.run(@session, "vim-cmd vmsvc/power.getstate #{vmid}").match /Powered on/
96
+ return true
97
+ else
98
+ return false
99
+ end
100
+ end
101
+ end
data/lib/util.rb ADDED
@@ -0,0 +1,51 @@
1
+ require 'net/ssh'
2
+
3
+ class Util
4
+
5
+ def self.start_ssh_session config
6
+ session = nil
7
+ host = config[:host]
8
+ user = config[:user]
9
+ params = {
10
+ :password => config[:password],
11
+ :port => config[:port]
12
+ }
13
+ retryable(:tries => config[:retries] || 10) do
14
+ Timeout::timeout(config[:timeout] || 10 ) do
15
+ session = Net::SSH.start(host, user, params)
16
+ end
17
+ end
18
+ session
19
+ end
20
+
21
+ def self.close_ssh_session session
22
+ session.close
23
+ end
24
+
25
+ def self.run session, cmd
26
+ result = "NA"
27
+ begin
28
+ Timeout::timeout(30) do
29
+ result = session.exec!(cmd)
30
+ end
31
+ rescue Exception => e
32
+ raise e.message
33
+ end
34
+ result
35
+ end
36
+
37
+ def self.retryable options = {}
38
+ opts = { :tries => 1, :on => Exception }.merge(options)
39
+ retry_exception, retries = opts[:on], opts[:tries]
40
+ begin
41
+ return yield
42
+ rescue retry_exception
43
+ if (retries -= 1) > 0
44
+ sleep 2
45
+ retry
46
+ else
47
+ raise
48
+ end
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esxi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Prashanth Rajagopal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: net-ssh
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ description: A simple gem written to solve only once purpose - Interact with ESXI
56
+ email:
57
+ - mail@prashanthr.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - esxi.gemspec
68
+ - lib/esxi.rb
69
+ - lib/esxi/version.rb
70
+ - lib/util.rb
71
+ homepage: http://prashanthr.net/blog/2014/09/08/esxi_ruby/
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Essential interaction with ESXI
95
+ test_files: []