Class: Fog::Bluebox::Compute::Server

Inherits:
Model
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/fog/compute/models/bluebox/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods included from Deprecation

deprecate, self_deprecate

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.



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

def initialize(attributes={})
  self.flavor_id ||= '94fd37a7-2606-47f7-84d5-9000deda52ae'
  super
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#private_keyObject



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

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

#private_key_pathObject



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

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

#public_keyObject



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

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

#public_key_pathObject



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

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

#usernameObject



118
119
120
# File 'lib/fog/compute/models/bluebox/server.rb', line 118

def username
  @username ||= 'deploy'
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



48
49
50
51
# File 'lib/fog/compute/models/bluebox/server.rb', line 48

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

#ready?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/fog/compute/models/bluebox/server.rb', line 71

def ready?
  status == 'running'
end

#reboot(type = 'SOFT') ⇒ Object



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

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

#saveObject

Raises:



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

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_id, :image_id
  options = if !password && !public_key
    raise(ArgumentError, "password or public_key is required for this operation")
  elsif public_key
    {'ssh_public_key' => public_key}
  elsif @password
    {'password' => password}
  end
  options['username'] = username
  data = connection.create_block(flavor_id, image_id, options)
  merge_attributes(data.body)
  true
end

#setup(credentials = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fog/compute/models/bluebox/server.rb', line 97

def setup(credentials = {})
  requires :identity, :ips, :public_key, :username
  Fog::SSH.new(ips.first['address'], 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



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

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

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