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

Inherits:
Server show all
Defined in:
lib/fog/compute/models/bluebox/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Server

#scp, #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.



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

def initialize(attributes={})
  self.flavor_id  ||= '94fd37a7-2606-47f7-84d5-9000deda52ae' # Block 1GB Virtual Server
  self.image_id   ||= '03807e08-a13d-44e4-b011-ebec7ef2c928' # Ubuntu LTS 10.04 64bit
  super
end

Instance Attribute Details

#lb_applicationsObject

Returns the value of attribute lb_applications.



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

def lb_applications
  @lb_applications
end

#lb_backendsObject

Returns the value of attribute lb_backends.



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

def lb_backends
  @lb_backends
end

#lb_servicesObject

Returns the value of attribute lb_services.



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

def lb_services
  @lb_services
end

#passwordObject

Returns the value of attribute password.



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

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



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

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

#public_key_pathObject



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

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

#usernameObject



123
124
125
# File 'lib/fog/compute/models/bluebox/server.rb', line 123

def username
  @username ||= 'deploy'
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



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

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

#private_ip_addressObject



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

def private_ip_address
  nil
end

#public_ip_addressObject



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

def public_ip_address
  ips.first
end

#ready?Boolean

Returns:

  • (Boolean)


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

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

#reboot(type = 'SOFT') ⇒ Object



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

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

#saveObject

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fog/compute/models/bluebox/server.rb', line 85

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

  if identity.nil?  # new record
    raise(ArgumentError, "password or public_key is required for this operation") if !password && !public_key
    options['ssh_public_key'] = public_key if public_key
    options['password'] = password if @password
  end

  if @lb_backends
    options['lb_backends'] = lb_backends
  elsif @lb_services
    options['lb_services'] = lb_services
  elsif @lb_applications
    options['lb_applications'] = lb_applications
  end

  options['username'] = username
  data = connection.create_block(flavor_id, image_id, options)
  merge_attributes(data.body)
  true
end

#setup(credentials = {}) ⇒ Object



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

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