Class: Fog::Compute::Clodo::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/rackspace-fog/clodo/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.



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

def initialize(attributes={})
  self.image_id   ||= attributes[:vps_os] ? attributes[:vps_os] : 666
  super attributes
end

Instance Attribute Details

#private_keyObject



70
71
72
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 70

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

#private_key_pathObject



65
66
67
68
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 65

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

#public_keyObject



96
97
98
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 96

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

#public_key_pathObject



91
92
93
94
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 91

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

#usernameObject



148
149
150
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 148

def username
  @username ||= 'root'
end

Instance Method Details

#add_ip_addressObject



79
80
81
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 79

def add_ip_address
  connection.add_ip_address(id)
end

#delete_ip_address(ip_address) ⇒ Object



87
88
89
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 87

def delete_ip_address(ip_address)
  connection.delete_ip_address(id, ip_address)
end

#destroyObject



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

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

#imageObject



56
57
58
59
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 56

def image
  requires :image_id
  image_id # API does not support image details request. :-(
end

#move_ip_address(ip_address) ⇒ Object



83
84
85
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 83

def move_ip_address(ip_address)
  connection.move_ip_address(id, ip_address)
end

#passwordObject



152
153
154
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 152

def password
	 vps_root_pass
end

#private_ip_addressObject



61
62
63
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 61

def private_ip_address
  nil
end

#public_ip_addressObject



74
75
76
77
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 74

def public_ip_address
  pubaddrs = addresses && addresses['public'] ? addresses['public'].select {|ip| ip['primary_ip']} : nil
  pubaddrs && !pubaddrs.empty? ? pubaddrs.first['ip'] : nil
end

#ready?Boolean

Returns:

  • (Boolean)


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

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

#reboot(type = 'SOFT') ⇒ Object



104
105
106
107
108
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 104

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

#saveObject

Raises:



110
111
112
113
114
115
116
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 110

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :image_id
  data = connection.create_server(image_id, attributes)
  merge_attributes(data.body['server'])
  true
end

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



140
141
142
143
144
145
146
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 140

def scp(local_path, remote_path, upload_options = {})
  requires :public_ip_address, :username

  scp_options = {}
  scp_options[:key_data] = [private_key] if private_key
  Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end

#setup(credentials = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 118

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

#ssh(commands) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/rackspace-fog/clodo/models/compute/server.rb', line 131

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

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