Class: Fog::Compute::Vmfusion::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/vmfusion/models/compute/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #reload, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/fog/vmfusion/models/compute/server.rb', line 14

def password
  @password
end

#private_keyObject



187
188
189
# File 'lib/fog/vmfusion/models/compute/server.rb', line 187

def private_key
  @private_key ||= private_key_path && File.read(private_key_path)
end

#private_key_pathObject



182
183
184
185
# File 'lib/fog/vmfusion/models/compute/server.rb', line 182

def private_key_path
  @private_key_path ||= Fog.credentials[:private_key_path]
  @private_key_path &&= File.expand_path(@private_key_path)
end

#public_keyObject



196
197
198
# File 'lib/fog/vmfusion/models/compute/server.rb', line 196

def public_key
  @public_key ||= public_key_path && File.read(public_key_path)
end

#public_key_pathObject



191
192
193
194
# File 'lib/fog/vmfusion/models/compute/server.rb', line 191

def public_key_path
  @public_key_path ||= Fog.credentials[:public_key_path]
  @public_key_path &&= File.expand_path(@public_key_path)
end

#usernameObject



126
127
128
# File 'lib/fog/vmfusion/models/compute/server.rb', line 126

def username
  @username ||= 'root'
end

Instance Method Details

#clone(name) ⇒ Object



24
25
26
27
28
29
# File 'lib/fog/vmfusion/models/compute/server.rb', line 24

def clone(name)
  requires :raw

  ::Fission::VM.clone(@raw.name,name)
  return connection.servers.get(name)
end

#destroy(options = { :force => false}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/vmfusion/models/compute/server.rb', line 31

def destroy(options={ :force => false})
  requires :raw

  if state=="running"
    if options[:force]
      @raw.stop
    end
  end

  ::Fission::VM.delete @raw.name
end

#haltObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/fog/vmfusion/models/compute/server.rb', line 77

def halt
  requires :raw
  if state=="running"
    @raw.halt
    return true
  else
    return false
  end

end

#initalize(attributes = {}) ⇒ Object



17
18
# File 'lib/fog/vmfusion/models/compute/server.rb', line 17

def initalize(attributes={})
end

#poweroffObject



88
89
90
91
# File 'lib/fog/vmfusion/models/compute/server.rb', line 88

def poweroff
  requires :raw
  halt
end

#private_ip_addressObject



117
118
119
# File 'lib/fog/vmfusion/models/compute/server.rb', line 117

def private_ip_address
  ip_address(:private)
end

#public_ip_addressObject



121
122
123
# File 'lib/fog/vmfusion/models/compute/server.rb', line 121

def public_ip_address
  ip_address(:public)
end

#ready?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/fog/vmfusion/models/compute/server.rb', line 113

def ready?
  state == "running"
end

#rebootObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/vmfusion/models/compute/server.rb', line 65

def reboot
  requires :raw
  if state=="running"
    @raw.stop
    wait_for { state!="running"}
    @raw.start
    return true
  else
    return false
  end
end

#resumeObject



98
99
100
101
# File 'lib/fog/vmfusion/models/compute/server.rb', line 98

def resume
  requires :raw
  @raw.resume
end

#saveObject

Raises:



20
21
22
# File 'lib/fog/vmfusion/models/compute/server.rb', line 20

def save
  raise Fog::Errors::Error.new('Creating a new vm is not yet supported')
end

#scp(local_path, remote_path, upload_options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/fog/vmfusion/models/compute/server.rb', line 142

def scp(local_path, remote_path, upload_options = {})
  requires :public_ip_address, :username

  scp_options = {}
  scp_options[:password] = password unless self.password.nil?
  scp_options[:key_data] = [private_key] if self.private_key

  Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end

#setup(credentials = {}) ⇒ Object

Sets up a new key



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/fog/vmfusion/models/compute/server.rb', line 153

def setup(credentials = {})
  requires :public_key, :public_ip_address, :username

  credentials[:password] = password unless self.password.nil?
  credentails[:key_data] = [private_key] if self.private_key

  commands = [
    %{mkdir .ssh},
  ]
  if public_key
    commands << %{echo "#{public_key}" >> ~/.ssh/authorized_keys}
  end

  # wait for domain to be ready
  Timeout::timeout(360) do
    begin
      Timeout::timeout(8) do
        Fog::SSH.new(public_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
      end
    rescue Errno::ECONNREFUSED
      sleep(2)
      retry
    rescue Net::SSH::AuthenticationFailed, Timeout::Error
      retry
    end
  end
  Fog::SSH.new(public_ip_address, username, credentials).run(commands)
end

#shutdownObject



93
94
95
96
# File 'lib/fog/vmfusion/models/compute/server.rb', line 93

def shutdown
  requires :raw
  stop
end

#ssh(commands) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fog/vmfusion/models/compute/server.rb', line 130

def ssh(commands)
  requires :public_ip_address, :username

  #requires :password, :private_key
  ssh_options={}
  ssh_options[:password] = password unless password.nil?
  ssh_options[:key_data] = [private_key] if private_key

  Fog::SSH.new(public_ip_address, @username, ssh_options).run(commands)

end

#startObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/vmfusion/models/compute/server.rb', line 43

def start
  requires :raw

  unless state=="running"
    @raw.start
    return true
  else
    return false
  end
end

#stateObject



108
109
110
111
# File 'lib/fog/vmfusion/models/compute/server.rb', line 108

def state
  requires :raw
  @raw.state
end

#stopObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/fog/vmfusion/models/compute/server.rb', line 54

def stop
  requires :raw

  if state=="running"
    @raw.stop
    return true
  else
    return false
  end
end

#suspendObject



103
104
105
106
# File 'lib/fog/vmfusion/models/compute/server.rb', line 103

def suspend
  requires :raw
  @raw.suspend
end