pec 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pec.rb +1 -0
- data/lib/pec/configure/ethernet.rb +4 -4
- data/lib/pec/director/helper.rb +1 -1
- data/lib/pec/network/port.rb +53 -76
- data/lib/pec/network/port_state.rb +40 -0
- data/lib/pec/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40647fc979435e780b1ed75ef3d5d5c69cef8430
|
4
|
+
data.tar.gz: bd561098ca4192c67f9ddb18f2b0f1dd0874ad7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caaf7f0b8c4b9f453734816999f23241ed1a319abc73c4c08b9cd545b08176f9fdc4c74aea9f400c07ec91244913ca13b26ef5046b10ee2a3cf2cd72aa5c6cdd
|
7
|
+
data.tar.gz: 2e70c4393bbb7cad5c0ab3b2485ddd78e4e59f7412ab9edeb9465ea51277be691387611937022528fb9f457b2189bb4102ef2873c23e22c7a9cb270c2f156d6a
|
data/lib/pec.rb
CHANGED
@@ -3,16 +3,16 @@ module Pec
|
|
3
3
|
class Ethernet
|
4
4
|
attr_reader :name, :bootproto, :ip_address, :options
|
5
5
|
def initialize(config)
|
6
|
-
@name
|
7
|
-
@bootproto
|
6
|
+
@name = config[0];
|
7
|
+
@bootproto = config[1]["bootproto"];
|
8
8
|
@ip_address = config[1]["ip_address"];
|
9
|
-
@options
|
9
|
+
@options = config[1].select do |k,v|
|
10
10
|
{ k => v } if k != "bootproto" && k != "ip_address"
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def find_port(ports)
|
15
|
-
ports.find { |p| p.
|
15
|
+
ports.find { |p| p.device_name == @name }
|
16
16
|
end
|
17
17
|
|
18
18
|
class << self
|
data/lib/pec/director/helper.rb
CHANGED
@@ -14,7 +14,7 @@ module Pec
|
|
14
14
|
port_subnet = Pec::Network::Subnet.fetch(ip.network.to_s)
|
15
15
|
raise(Pec::Errors::Subnet, "subnet:#{ip.network.to_s} is not fond!") unless port_subnet
|
16
16
|
|
17
|
-
port = Pec::Network::Port.
|
17
|
+
port = Pec::Network::Port.assign(ether.name, ip, port_subnet, get_security_group_id(host.security_group))
|
18
18
|
raise(Pec::Errors::Port, "ip addess:#{ip.to_addr} can't create port!") unless port
|
19
19
|
|
20
20
|
puts "#{host.name}: assingn ip #{port.ip_address}".green
|
data/lib/pec/network/port.rb
CHANGED
@@ -3,96 +3,73 @@ module Pec
|
|
3
3
|
class Network
|
4
4
|
class Port
|
5
5
|
extend Query
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
6
|
+
class << self
|
7
|
+
@@use_ip_list = []
|
8
|
+
|
9
|
+
def assign(name, ip, subnet, security_group_ids)
|
10
|
+
ip = get_free_port_ip(ip, subnet) if request_any_address?(ip, subnet)
|
11
|
+
port_state = Pec::Network::PortState.new(name, fetch_by_ip(ip.to_addr))
|
12
|
+
|
13
|
+
assign_port = case
|
14
|
+
when port_state.exists? && !port_state.used?
|
15
|
+
recreate(ip, subnet, security_group_ids)
|
16
|
+
when !port_state.exists?
|
17
|
+
create(ip, subnet, security_group_ids)
|
18
|
+
when port_state.used?
|
19
|
+
raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
|
20
|
+
end
|
21
|
+
Pec::Network::PortState.new(name, assign_port)
|
17
22
|
end
|
18
|
-
case
|
19
|
-
when exists? && !used?
|
20
|
-
recreate(ip, subnet, security_group_ids)
|
21
|
-
when !exists?
|
22
|
-
create(ip, subnet, security_group_ids)
|
23
|
-
when used?
|
24
|
-
raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
|
25
|
-
end
|
26
|
-
self
|
27
|
-
end
|
28
23
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
response = Pec::Resource.get.create_port(subnet["network_id"], options)
|
33
|
-
if response
|
34
|
-
@port = response.data[:body]["port"]
|
35
|
-
@@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
|
36
|
-
response.data[:body]["port"]["id"]
|
24
|
+
def get_free_port_ip(ip, subnet)
|
25
|
+
port = fetch_free_port(subnet)
|
26
|
+
port ? IP.new("#{port["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") : ip
|
37
27
|
end
|
38
|
-
end
|
39
28
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
29
|
+
def create(ip, subnet, security_group_ids)
|
30
|
+
options = set_security_group(security_group_ids)
|
31
|
+
options = set_fixed_ip(options, subnet, ip)
|
32
|
+
response = Pec::Resource.get.create_port(subnet["network_id"], options)
|
44
33
|
|
45
|
-
|
46
|
-
create(ip, subnet, security_group_ids) if delete(ip)
|
47
|
-
end
|
34
|
+
raise(Pec::Errors::Port, "ip:#{ip.to_addr} is not created!") unless response
|
48
35
|
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def list
|
54
|
-
Pec::Resource.get.port_list
|
55
|
-
end
|
56
|
-
|
57
|
-
def fetch_by_ip(ip_addr)
|
58
|
-
list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
|
59
|
-
end
|
60
|
-
|
61
|
-
def fetch_free_port
|
62
|
-
list.find do |p|
|
63
|
-
p["fixed_ips"][0]["subnet_id"] == @subnet["id"] &&
|
64
|
-
p["device_owner"].empty? &&
|
65
|
-
p["admin_state_up"] &&
|
66
|
-
!@@use_ip_list.include?(p["fixed_ips"][0]["ip_address"])
|
36
|
+
@@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
|
37
|
+
response.data[:body]["port"]
|
67
38
|
end
|
68
|
-
end
|
69
39
|
|
70
|
-
|
71
|
-
|
72
|
-
|
40
|
+
def set_security_group(security_group_ids)
|
41
|
+
{ security_groups: security_group_ids }
|
42
|
+
end
|
73
43
|
|
74
|
-
|
75
|
-
|
76
|
-
|
44
|
+
def set_fixed_ip(options, subnet, ip)
|
45
|
+
ip.to_s != subnet["cidr"] ? options.merge({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) : options
|
46
|
+
end
|
77
47
|
|
78
|
-
|
79
|
-
|
80
|
-
|
48
|
+
def delete(ip)
|
49
|
+
target_port = fetch_by_ip(ip.to_addr)
|
50
|
+
response = Pec::Resource.get.delete_port(target_port["id"]) if target_port
|
51
|
+
end
|
81
52
|
|
82
|
-
|
83
|
-
|
84
|
-
|
53
|
+
def recreate(ip, subnet, security_group_ids)
|
54
|
+
create(ip, subnet, security_group_ids) if delete(ip)
|
55
|
+
end
|
85
56
|
|
86
|
-
|
87
|
-
|
88
|
-
|
57
|
+
def request_any_address?(ip, subnet)
|
58
|
+
ip.to_s == subnet["cidr"]
|
59
|
+
end
|
89
60
|
|
90
|
-
|
91
|
-
|
92
|
-
|
61
|
+
def fetch_by_ip(ip_addr)
|
62
|
+
list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
|
63
|
+
end
|
93
64
|
|
94
|
-
|
95
|
-
|
65
|
+
def fetch_free_port(subnet)
|
66
|
+
list.find do |p|
|
67
|
+
p["fixed_ips"][0]["subnet_id"] == subnet["id"] &&
|
68
|
+
p["device_owner"].empty? &&
|
69
|
+
p["admin_state_up"] &&
|
70
|
+
!@@use_ip_list.include?(p["fixed_ips"][0]["ip_address"])
|
71
|
+
end
|
72
|
+
end
|
96
73
|
end
|
97
74
|
end
|
98
75
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'json'
|
2
|
+
module Pec
|
3
|
+
class Network
|
4
|
+
class PortState
|
5
|
+
attr_reader :device_name
|
6
|
+
def initialize(device_name, port)
|
7
|
+
@device_name = device_name
|
8
|
+
@port = port
|
9
|
+
end
|
10
|
+
|
11
|
+
def exists?
|
12
|
+
!@port.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def used?
|
16
|
+
@port && !@port["device_owner"].empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def id
|
20
|
+
@port["id"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def mac_address
|
24
|
+
@port["mac_address"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def ip_address
|
28
|
+
@port["fixed_ips"][0]["ip_address"]
|
29
|
+
end
|
30
|
+
|
31
|
+
def network_id
|
32
|
+
@port["network_id"]
|
33
|
+
end
|
34
|
+
|
35
|
+
def netmask
|
36
|
+
IP.new(@port["fixed_ips"][0]["ip_address"]).netmask.to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/pec/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kazuhiko yamashita
|
@@ -162,6 +162,7 @@ files:
|
|
162
162
|
- lib/pec/errors.rb
|
163
163
|
- lib/pec/init.rb
|
164
164
|
- lib/pec/network/port.rb
|
165
|
+
- lib/pec/network/port_state.rb
|
165
166
|
- lib/pec/network/subnet.rb
|
166
167
|
- lib/pec/query.rb
|
167
168
|
- lib/pec/resource.rb
|