Class: Fog::GoGrid::Compute::Server

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

Instance Attribute Summary

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#inspect, #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, #identity, #identity=, #merge_attributes, #new_record?, #requires

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



21
22
23
# File 'lib/fog/compute/models/go_grid/server.rb', line 21

def initialize(attributes={})
  super
end

Instance Method Details

#destroyObject



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

def destroy
  requires :id
  connection.grid_server_delete(id)
  true
end

#imageObject



31
32
33
34
# File 'lib/fog/compute/models/go_grid/server.rb', line 31

def image
  requires :image_id
  connection.grid_image_get(:image => image_id)
end

#ready?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/fog/compute/models/go_grid/server.rb', line 36

def ready?
  @state && @state["name"] == 'On'
end

#reloadObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/compute/models/go_grid/server.rb', line 40

def reload
  requires :name
  begin
    if data = collection.get(name)
      new_attributes = data.attributes
      merge_attributes(new_attributes)
      self
    end
  rescue Excon::Errors::BadRequest
    false
  end
end

#saveObject

Raises:



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

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :name, :image_id, :ip, :memory
  options = {
    'isSandbox'   => sandbox,
    'server.ram'  => memory,
    'image'       => image_id
  }
  options = options.reject {|key, value| value.nil?}
  data = connection.grid_server_add(name, image, ip, options)
  merge_attributes(data.body)
  true
end

#setup(credentials = {}) ⇒ Object



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

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

#ssh(commands) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/fog/compute/models/go_grid/server.rb', line 67

def ssh(commands)
  requires :ip, :identity, :username

  options = {}
  options[:key_data] = [private_key] if private_key
  Fog::SSH.new(ip['ip'], username, options).run(commands)
end

#usernameObject



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

def username
  @username ||= 'root'
end