Class: Fog::Rackspace::Compute::Server

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

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

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

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



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

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

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#private_keyObject



59
60
61
# File 'lib/fog/compute/models/rackspace/server.rb', line 59

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

#private_key_pathObject



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

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

#public_keyObject



72
73
74
# File 'lib/fog/compute/models/rackspace/server.rb', line 72

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

#public_key_pathObject



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

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

#usernameObject



130
131
132
# File 'lib/fog/compute/models/rackspace/server.rb', line 130

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



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

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

#imagesObject



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

def images
  requires :id
  connection.images(:server => self)
end

#private_ip_addressObject



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

def private_ip_address
  nil
end

#public_ip_addressObject



63
64
65
# File 'lib/fog/compute/models/rackspace/server.rb', line 63

def public_ip_address
  addresses.first
end

#ready?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/fog/compute/models/rackspace/server.rb', line 76

def ready?
  status == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



80
81
82
83
84
# File 'lib/fog/compute/models/rackspace/server.rb', line 80

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

#saveObject

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fog/compute/models/rackspace/server.rb', line 86

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_id, :image_id
  options = {
    'metadata'    => ,
    'name'        => name,
    'personality' => personality
  }
  options = options.reject {|key, value| value.nil?}
  data = connection.create_server(flavor_id, image_id, options)
  merge_attributes(data.body['server'])
  true
end

#scp(local_path, remote_path) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/fog/compute/models/rackspace/server.rb', line 122

def scp(local_path, remote_path)
  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)
end

#setup(credentials = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fog/compute/models/rackspace/server.rb', line 100

def setup(credentials = {})
  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 "#{attributes.to_json}" >> ~/attributes.json},
    %{echo "#{.to_json}" >> ~/metadata.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#ssh(commands) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/fog/compute/models/rackspace/server.rb', line 114

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

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