Class: Pec::Compute::Server

Inherits:
Object
  • Object
show all
Includes:
Query
Defined in:
lib/pec/compute/server.rb

Instance Method Summary collapse

Methods included from Query

#fetch, #get_adapter, #get_ref, #list

Instance Method Details

#create(name, image_ref, flavor_ref, ports, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pec/compute/server.rb', line 6

def create(name, image_ref, flavor_ref, ports, options)
  if exists?(name)
    puts "skip create server! name:#{name} is exists!"
    return true
  end

  networks = ports.map do |port|
    if port.used?
      puts "port-id:#{port.id} ip-addr:#{port.ip_address} in used"
      return false
    end
    { net_id: port.network_id, fixed_ip: port.ip_address, port_id: port.id }
  end if ports

  options.merge!({ 'nics' =>  networks })
  response = Fog::Compute[:openstack].create_server(name, image_ref, flavor_ref, options)
  if response[:status] == 202
    puts "success create for server_name:#{name}"
  end
  true
  rescue Excon::Errors::Conflict,Excon::Errors::BadRequest => e
    JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
    false
end

#destroy!(server_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pec/compute/server.rb', line 35

def destroy!(server_name)
  server = fetch(server_name)
  unless server
    puts "server_name:#{server_name} is not fond!"
    return
  end

  response = Fog::Compute[:openstack].delete_server(server["id"]) if server
  if response && response[:status] == 204
    puts "server_name:#{server_name} is deleted!"
  end
end

#exists?(server_name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/pec/compute/server.rb', line 31

def exists?(server_name)
  fetch(server_name)
end