Class: CloudProviders::Vmware

Inherits:
CloudProvider show all
Includes:
SearchablePaths
Defined in:
lib/cloud_providers/vmware/vmware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchablePaths

included

Methods inherited from CloudProvider

inherited, #keypair, #keypair=

Methods included from Callbacks

included

Constructor Details

#initialize(o = {}, &block) ⇒ Vmware

Returns a new instance of Vmware.



53
54
55
56
# File 'lib/cloud_providers/vmware/vmware.rb', line 53

def initialize(o={}, &block)
  @cloud = o.delete(:cloud)
  super
end

Instance Attribute Details

#cloudObject (readonly)

Returns the value of attribute cloud.



51
52
53
# File 'lib/cloud_providers/vmware/vmware.rb', line 51

def cloud
  @cloud
end

Instance Method Details

#after_compile(args) ⇒ Object



127
# File 'lib/cloud_providers/vmware/vmware.rb', line 127

def after_compile(args); end

#before_compile(args) ⇒ Object



126
# File 'lib/cloud_providers/vmware/vmware.rb', line 126

def before_compile(args); end

#describe_instance(hash_of_criteria_to_select_instance_against) ⇒ Object

Will select the first instance matching the provided criteria hash



65
66
67
# File 'lib/cloud_providers/vmware/vmware.rb', line 65

def describe_instance(hash_of_criteria_to_select_instance_against)
  describe_instances.first
end

#describe_instances(o = {}) ⇒ Object

Describe instances



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cloud_providers/vmware/vmware.rb', line 70

def describe_instances(o={})
  output = vmrun("list")
  if output.first =~ /Total running VMs: 0/
    []
  else
    output.shift if output.first =~ /Total running/
    output.map do |line|
      vmware_instance(:vmx_file => line)
    end
  end
end

#nodes(hsh = {}) ⇒ Object

The nodes in the cloud_provider



90
91
92
# File 'lib/cloud_providers/vmware/vmware.rb', line 90

def nodes(hsh={})
  describe_instances
end

#path_to_binaryObject

Search for the vmrun binary



101
102
103
# File 'lib/cloud_providers/vmware/vmware.rb', line 101

def path_to_binary
  @path_to_binary ||= search_in_known_locations("vmrun")
end

#run_instance(o = {}) ⇒ Object

Start a new instance with the given options



59
60
61
62
# File 'lib/cloud_providers/vmware/vmware.rb', line 59

def run_instance(o={})
  vmrun("start #{vmx_file}")
  vmware_instance
end

#terminate_instance!(o = {}) ⇒ Object

Terminate an instance (or instances) by passing :instance_id and :instance_ids



83
84
85
86
87
# File 'lib/cloud_providers/vmware/vmware.rb', line 83

def terminate_instance!(o={})
  set_vars_from_options o
  vmrun("stop #{vmx_file}")
  describe_instances
end

#vmrun(cmd, o = {}) ⇒ Object

Run vmrun on the command-line



106
107
108
109
110
111
112
113
# File 'lib/cloud_providers/vmware/vmware.rb', line 106

def vmrun(cmd, o={})
  stdin, stdout, stderr = Open3.popen3("'#{path_to_binary}' #{cmd}")
  unless $?.success?
    $stderr.puts "FAILED: #{cmd}\n code = #{$?}"
    raise StandardError.new("ERROR: vmrun") if o.delete(:raise_on_error)
  end
  stdout.readlines
end

#vmware_instance(o = {}) ⇒ Object

Basic vmware_instance



95
96
97
98
# File 'lib/cloud_providers/vmware/vmware.rb', line 95

def vmware_instance(o={})
  VmwareInstance.new( :instance_id => (o[:vmx_file] || vmx_file), :public_ip => public_ip, :dns_name => public_ip,
                      :cloud_provider => self.dsl_options, :keypair_name => keypair.basename)
end

#vmx_fileObject

Search for the vmx_file



116
117
118
119
120
121
122
123
124
# File 'lib/cloud_providers/vmware/vmware.rb', line 116

def vmx_file
  return @vmx_file if @vmx_file
  o = if File.file?(f = File.expand_path(image_id))
    f
  elsif File.file?(f = File.expand_path(images_repo_path, image_id))
    f
  end
  @vmx_file = "'#{o}'"
end