Class: Fog::Compute::VirtualBox::Server

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

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#inspect, #reload, #symbolize_keys, #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

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



74
75
76
77
78
79
# File 'lib/fog/virtual_box/models/compute/server.rb', line 74

def initialize(attributes={})
  self.memory_size = 256
  self.rtc_use_utc = true
  self.vram_size   = 8
  super
end

Instance Attribute Details

#private_keyObject



110
111
112
# File 'lib/fog/virtual_box/models/compute/server.rb', line 110

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

#private_key_pathObject



105
106
107
108
# File 'lib/fog/virtual_box/models/compute/server.rb', line 105

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

#public_keyObject



123
124
125
# File 'lib/fog/virtual_box/models/compute/server.rb', line 123

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

#public_key_pathObject



118
119
120
121
# File 'lib/fog/virtual_box/models/compute/server.rb', line 118

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

#usernameObject



207
208
209
# File 'lib/fog/virtual_box/models/compute/server.rb', line 207

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fog/virtual_box/models/compute/server.rb', line 81

def destroy
  requires :name, :raw
  unless raw.state == :powered_off
    stop
    wait_for { raw.session_state == :closed }
  end
  raw.unregister(:full)
  config_file = connection.compose_machine_filename(name)
  config_directory = config_file.split(File::SEPARATOR)[0...-1].join(File::SEPARATOR)
  FileUtils.rm_rf(config_directory)
  true
end

#network_adaptersObject



94
95
96
97
98
99
# File 'lib/fog/virtual_box/models/compute/server.rb', line 94

def network_adapters
  Fog::Compute::VirtualBox::NetworkAdapters.new(
    :connection => connection,
    :machine => self
  )
end

#private_ip_addressObject



101
102
103
# File 'lib/fog/virtual_box/models/compute/server.rb', line 101

def private_ip_address
  nil
end

#public_ip_addressObject



114
115
116
# File 'lib/fog/virtual_box/models/compute/server.rb', line 114

def public_ip_address
  nil
end

#ready?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/fog/virtual_box/models/compute/server.rb', line 127

def ready?
  status == :running
end

#rebootObject



131
132
133
134
135
# File 'lib/fog/virtual_box/models/compute/server.rb', line 131

def reboot
  requires :raw
  session.console.reset
  true
end

#saveObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fog/virtual_box/models/compute/server.rb', line 137

def save
  unless identity
    requires :name, :os
    self.raw = connection.create_machine(nil, name, os)
    connection.register_machine(raw)
    with_session do |session|
      for attribute in [:description, :memory_size, :rtc_use_utc, :vram_size]
        session.machine.send(:"#{attribute}=", attributes[attribute])
      end
      session.machine.save_settings
    end
    true
  else
    raise Fog::Errors::Error.new('Updating an existing server is not yet implemented. Contributions welcome!')
  end
end

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



154
155
156
157
158
159
160
161
# File 'lib/fog/virtual_box/models/compute/server.rb', line 154

def scp(local_path, remote_path, upload_options = {})
  raise 'Not Implemented'
  # requires :addresses, :username
  #
  # options = {}
  # options[:key_data] = [private_key] if private_key
  # Fog::SCP.new(addresses['public'].first, username, options).upload(local_path, remote_path, scp_options)
end

#setup(credentials = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fog/virtual_box/models/compute/server.rb', line 163

def setup(credentials = {})
  raise 'Not Implemented'
#   requires :addresses, :identity, :public_key, :username
#   Fog::SSH.new(addresses['public'].first, username, credentials).run([
#     %{mkdir .ssh},
#     %{echo "#{public_key}" >> ~/.ssh/authorized_keys},
#     %{passwd -l #{username}},
#     %{echo "#{MultiJson.encode(attributes)}" >> ~/attributes.json},
#     %{echo "#{MultiJson.encode(metadata)}" >> ~/metadata.json}
#   ])
# rescue Errno::ECONNREFUSED
#   sleep(1)
#   retry
end

#ssh(commands) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/fog/virtual_box/models/compute/server.rb', line 178

def ssh(commands)
  raise 'Not Implemented'
  # requires :addresses, :identity, :username
  #
  # options = {}
  # options[:key_data] = [private_key] if private_key
  # Fog::SSH.new(addresses['public'].first, username, options).run(commands)
end

#start(type = 'headless') ⇒ Object



187
188
189
190
191
192
# File 'lib/fog/virtual_box/models/compute/server.rb', line 187

def start(type = 'headless')
  requires :raw
  # session, type in ['gui', 'headless'], key[=value]\n env variables
  raw.launch_vm_process(session, type, '').wait
  true
end

#stopObject



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

def stop
  requires :raw
  session.console.power_down.wait
  true
end

#storage_controllersObject



200
201
202
203
204
205
# File 'lib/fog/virtual_box/models/compute/server.rb', line 200

def storage_controllers
  Fog::Compute::VirtualBox::StorageControllers.new(
    :connection => connection,
    :machine    => self
  )
end