Class: Fog::Slicehost::Compute::Server

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

Instance Attribute Summary collapse

Attributes inherited from Model

#connection

Instance Method Summary collapse

Methods inherited from Model

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

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



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

def initialize(attributes={})
  @flavor_id ||= 1
  super
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#private_keyObject



50
51
52
# File 'lib/fog/slicehost/models/compute/server.rb', line 50

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

#private_key_pathObject



46
47
48
# File 'lib/fog/slicehost/models/compute/server.rb', line 46

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

#public_keyObject



58
59
60
# File 'lib/fog/slicehost/models/compute/server.rb', line 58

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

#public_key_pathObject



54
55
56
# File 'lib/fog/slicehost/models/compute/server.rb', line 54

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

#usernameObject



100
101
102
# File 'lib/fog/slicehost/models/compute/server.rb', line 100

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



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

def destroy
  requires :id
  connection.delete_slice(@id)
  true
end

#flavorObject



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

def flavor
  requires :flavor_id
  connection.flavors.get(@flavor_id)
end

#imageObject



41
42
43
44
# File 'lib/fog/slicehost/models/compute/server.rb', line 41

def image
  requires :image_id
  connection.images.get(@image_id)
end

#ready?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/fog/slicehost/models/compute/server.rb', line 62

def ready?
  @status == 'active'
end

#reboot(type = 'SOFT') ⇒ Object



66
67
68
69
70
# File 'lib/fog/slicehost/models/compute/server.rb', line 66

def reboot(type = 'SOFT')
  requires :id
  connection.reboot_slice(@id, type)
  true
end

#saveObject

Raises:



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

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_id, :image_id, :name

  data = connection.create_slice(@flavor_id, @image_id, @name)
  merge_attributes(data.body)
  true
end

#setup(credentials = {}) ⇒ Object



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

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

#ssh(commands) ⇒ Object



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

def ssh(commands)
  requires :addresses, :identity, :private_key, :username
  @ssh ||= Fog::SSH.new(addresses.first, username, :key_data => [private_key])
  @ssh.run(commands)
end