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

Inherits:
Fog::Compute::Server show all
Defined in:
lib/fog/slicehost/models/compute/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Fog::Compute::Server

#scp, #scp_download, #ssh

Methods inherited from Model

#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

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



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

def initialize(attributes={})
  self.flavor_id  ||= 1  # 256 server
  self.image_id   ||= 49 # Ubuntu 10.04 LTS 64bit
  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



56
57
58
# File 'lib/fog/slicehost/models/compute/server.rb', line 56

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

#private_key_pathObject



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

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

#public_keyObject



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

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

#public_key_pathObject



64
65
66
67
# File 'lib/fog/slicehost/models/compute/server.rb', line 64

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

#usernameObject



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

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



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

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

#private_ip_addressObject



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

def private_ip_address
  nil
end

#public_ip_addressObject



60
61
62
# File 'lib/fog/slicehost/models/compute/server.rb', line 60

def public_ip_address
  addresses.first
end

#ready?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/fog/slicehost/models/compute/server.rb', line 73

def ready?
  self.state == 'active'
end

#reboot(type = 'SOFT') ⇒ Object



77
78
79
80
81
# File 'lib/fog/slicehost/models/compute/server.rb', line 77

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

#saveObject

Raises:



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

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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fog/slicehost/models/compute/server.rb', line 92

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 #{username}},
    %{echo "#{MultiJson.encode(attributes)}" >> ~/attributes.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end