Class: Fog::Compute::QingCloud::Servers

Inherits:
Fog::Collection
  • Object
show all
Defined in:
lib/fog/qingcloud/models/compute/servers.rb

Constant Summary collapse

ACTIVE_STATUS =
%w[pending running stopped suspended]

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Servers

Creates a new server



18
19
20
21
22
# File 'lib/fog/qingcloud/models/compute/servers.rb', line 18

def initialize(attributes)
  self.filters ||= {}
  filters['status'] = ACTIVE_STATUS unless filters['status']
  super
end

Instance Method Details

#all(filters = self.filters) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/qingcloud/models/compute/servers.rb', line 24

def all(filters = self.filters)
  self.filters = filters
  data = service.describe_instances(filters).body['instance_set']
  load(data.map do |x| 
      x['vxnet_ids'] = x['vxnets'].map{|v| v['vxnet_id']}
      x['image_id'] = x['image']['image_id']
      x
    end
  )
end

#bootstrap(new_attributes = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/qingcloud/models/compute/servers.rb', line 35

def bootstrap(new_attributes = {})
  server = service.servers.new(new_attributes)

  unless new_attributes[:key_name]
    # first or create fog_#{credential} keypair
    name = Fog.respond_to?(:credential) && Fog.credential || :default
    unless server.key_pair = service.key_pairs.get("fog_#{name}")
      server.key_pair = service.key_pairs.create(
        :name => "fog_#{name}",
        :public_key => server.public_key
      )
    end
  end

  security_group = service.security_groups.get(server.groups.first)
  if security_group.nil?
    raise Fog::Compute::QingCloud::Error, "The security group" \
      " #{server.groups.first} doesn't exist."
  end
  
  # make sure port 22 is open in the first security group
  authorized = security_group.ingress_rules.detect do |rule|
    rule.port_range == (22..22)
  end
  unless authorized
    security_group.add_rule(
      'protocol' => 'tcp',
      'priority' => 1,
      'action' => 'accept',
      'direction' => 0,
      'port_range' => 22,
      'name' => 'ssh',
      'auto_apply' => true
    )
  end

  server.save
  server.wait_for { ready? }
  server.setup(:key_data => [server.private_key])
  server
end

#get(server_id) ⇒ Object

Used to retrieve a server

server_id is required to get the associated server information.

You can run the following command to get the details: QingCloud.servers.get(“i-5c973972”)



84
85
86
87
88
89
90
# File 'lib/fog/qingcloud/models/compute/servers.rb', line 84

def get(server_id)
  if server_id
    self.class.new(:service => service).all('instance-id' => server_id).first
  end
rescue Fog::QingCloud::Errors::NotFound
  nil
end