pec 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff9692ce800f020d84648166b9b0efd0dd706850
4
- data.tar.gz: 1bc2f21a9975f5b961e6a7546a1bfc39047b79aa
3
+ metadata.gz: 35cad45133a8df40934711c27364efb26b8508a1
4
+ data.tar.gz: 55cef3480870581c1b2eddc60fdfa805ce9216e5
5
5
  SHA512:
6
- metadata.gz: 516b5d1eb608325daa9896903b29998c8475b69455336bc5872ca67877774a9a7f4b2a2e47c1a6b64535014e55e0b066affbdc703b52840f75a01ded22aeff57
7
- data.tar.gz: d279871aacbbde8bdf9f7df4994531c755abca182486e3c8fd55d3cbadfd1e34c5eab43b75514328e0ad06d41b665cf7e5f294fea2ff0445d0787c98b7a2d59f
6
+ metadata.gz: de7ec22cd43e85a97ecf264875c5c021bd2c99529ad4c455ab70a72f9d967c2775817319b5e4b861fa2dacab2ecb1a0ab5f725376bac967e378cbd9f031312f8
7
+ data.tar.gz: 7e34e2de84215bbf238a70eabeba9a23c2fd8363afbf21f423fc75424a2d4a3172954c5298bfd1a59a2e822697ffa510db90879475f31cf2be3bc0192788d418
@@ -6,13 +6,56 @@ module Pec
6
6
  @name = config[0];
7
7
  @bootproto = config[1]["bootproto"];
8
8
  @ip_address = config[1]["ip_address"];
9
- @options = config[1].select do |k,v|
10
- { k => v } if k != "bootproto" && k != "ip_address"
9
+ @options = config[1].reject do |k,v|
10
+ { k => v } if k == "bootproto" || k == "ip_address"
11
11
  end
12
12
  end
13
13
 
14
- def find_port(ports)
15
- ports.find { |p| p.device_name == @name }
14
+ def get_port_content(ports)
15
+ base = {
16
+ "bootproto" => @bootproto,
17
+ "name" => config_name,
18
+ "device" => device_name,
19
+ "type" => type,
20
+ "onboot" => onboot,
21
+ "hwaddr" => mac_address(ports),
22
+ }
23
+ base.merge!({ "netmask" => netmask(ports), "ipaddr" => port_ip_address(ports) }) if static?
24
+ base.merge!(@options) if @options
25
+ base.map {|k,v| "#{k.upcase}=#{v}"}.join("\n")
26
+ end
27
+
28
+
29
+ def config_name
30
+ @options["name"] || @name
31
+ end
32
+
33
+ def device_name
34
+ @options["device"] || @name
35
+ end
36
+
37
+ def type
38
+ @options["type"] || 'Ethernet'
39
+ end
40
+
41
+ def onboot
42
+ @options["onboot"] || 'yes'
43
+ end
44
+
45
+ def mac_address(ports)
46
+ ports.find { |p| p.device_name == @name }.mac_address
47
+ end
48
+
49
+ def netmask(ports)
50
+ ports.find { |p| p.device_name == @name }.netmask
51
+ end
52
+
53
+ def port_ip_address(ports)
54
+ ports.find { |p| p.device_name == @name }.ip_address
55
+ end
56
+
57
+ def static?
58
+ @bootproto == "static"
16
59
  end
17
60
 
18
61
  class << self
@@ -21,23 +21,9 @@ module Pec
21
21
 
22
22
  def make_port_content(config, ports)
23
23
  config.networks.map do |ether|
24
- ifcfg_content = {}
25
- ifcfg_content["bootproto"] = ether.bootproto
26
- ifcfg_content["name"] = ether.options["name"] || ether.name
27
- ifcfg_content["device"] = ether.options["device"] || ether.name
28
- ifcfg_content["type"] = ether.options['type'] ||'Ethernet'
29
- ifcfg_content["onboot"] = ether.options['onboot'] || 'yes'
30
- ifcfg_content["hwaddr"] = ether.find_port(ports).mac_address
31
- if ether.bootproto == "static"
32
- ifcfg_content["netmask"] = ether.find_port(ports).netmask
33
- ifcfg_content["ipaddr"] = ether.find_port(ports).ip_address
34
- end
35
-
36
- ifcfg_content.merge!(ether.options)
37
-
38
24
  path = ether.options['path'] || "/etc/sysconfig/network-scripts/ifcfg-#{ether.name}"
39
25
  {
40
- 'content' => ifcfg_content.map {|k,v| "#{k.upcase}=#{v}"}.join("\n"),
26
+ 'content' => ether.get_port_content(ports),
41
27
  'owner' => "root:root",
42
28
  'path' => path,
43
29
  'permissions' => "0644"
@@ -27,8 +27,8 @@ module Pec
27
27
  response = Pec::Resource.get.create_port(subnet["network_id"], options)
28
28
 
29
29
  raise(Pec::Errors::Port, "ip:#{ip.to_addr} is not created!") unless response
30
+ append_assigned_ip(response)
30
31
 
31
- @@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
32
32
  response.data[:body]["port"]
33
33
  end
34
34
 
@@ -40,6 +40,14 @@ module Pec
40
40
  ip.to_s != subnet["cidr"] ? options.merge({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) : options
41
41
  end
42
42
 
43
+ def append_assigned_ip(response)
44
+ @@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
45
+ end
46
+
47
+ def assigned_ip?(port)
48
+ @@use_ip_list.include?(port["fixed_ips"][0]["ip_address"])
49
+ end
50
+
43
51
  def get_free_port_ip(ip, subnet)
44
52
  port = fetch_free_port(subnet)
45
53
  port ? IP.new("#{port["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") : ip
@@ -67,7 +75,7 @@ module Pec
67
75
  p["fixed_ips"][0]["subnet_id"] == subnet["id"] &&
68
76
  p["device_owner"].empty? &&
69
77
  p["admin_state_up"] &&
70
- !@@use_ip_list.include?(p["fixed_ips"][0]["ip_address"])
78
+ !assigned_ip?
71
79
  end
72
80
  end
73
81
  end
@@ -1,3 +1,3 @@
1
1
  module Pec
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
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.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamashita