Class: ChefWorkflow::IPSupport
- Inherits:
-
Object
- Object
- ChefWorkflow::IPSupport
- Extended by:
- AttrSupport
- Includes:
- GenericSupport
- Defined in:
- lib/chef-workflow/support/ip.rb
Overview
IP allocation database. Uses ‘GenericSupport`.
Instance Method Summary collapse
-
#assign_role_ip(role, ip) ⇒ Object
Appends an IP to a role.
- #create_table ⇒ Object
-
#delete_role(role) ⇒ Object
Removes the role and all associated IPs.
-
#get_role_ips(role) ⇒ Object
Gets all the IPs for a role, as an array of strings.
-
#initialize(subnet = ) ⇒ IPSupport
constructor
A new instance of IPSupport.
-
#ip_used?(ip) ⇒ Boolean
Predicate to determine if an IP is in use.
-
#next_ip(arg) ⇒ Object
Gets the next unallocated IP, given an IP to start with.
-
#roles ⇒ Object
Get all the known roles.
-
#seed_vagrant_ips ⇒ Object
Helper method for vagrant.
-
#subnet ⇒ Object
:attr:.
-
#unused_ip ⇒ Object
Gets the next un-used IP.
Methods included from AttrSupport
Methods included from GenericSupport
Constructor Details
#initialize(subnet = ) ⇒ IPSupport
24 25 26 27 28 |
# File 'lib/chef-workflow/support/ip.rb', line 24 def initialize(subnet=ENV["TEST_CHEF_SUBNET"]) @subnet = subnet @db = ChefWorkflow::DatabaseSupport.instance create_table end |
Instance Method Details
#assign_role_ip(role, ip) ⇒ Object
Appends an IP to a role.
75 76 77 |
# File 'lib/chef-workflow/support/ip.rb', line 75 def assign_role_ip(role, ip) @db.execute("insert into ips (role_name, ip_addr) values (?, ?)", [role, ip]) end |
#create_table ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/chef-workflow/support/ip.rb', line 30 def create_table @db.execute " create table if not exists ips (\n id integer not null primary key autoincrement,\n role_name varchar(255) not null,\n ip_addr varchar(255) not null,\n UNIQUE(role_name, ip_addr)\n )\n EOF\nend\n" |
#delete_role(role) ⇒ Object
Removes the role and all associated IPs.
82 83 84 |
# File 'lib/chef-workflow/support/ip.rb', line 82 def delete_role(role) @db.execute("delete from ips where role_name=?", [role]) end |
#get_role_ips(role) ⇒ Object
Gets all the IPs for a role, as an array of strings.
96 97 98 |
# File 'lib/chef-workflow/support/ip.rb', line 96 def get_role_ips(role) @db.execute("select ip_addr from ips where role_name=? order by id", [role]).map(&:first) end |
#ip_used?(ip) ⇒ Boolean
Predicate to determine if an IP is in use.
68 69 70 |
# File 'lib/chef-workflow/support/ip.rb', line 68 def ip_used?(ip) @db.execute("select count(*) from ips where ip_addr=?", [ip]).first.first > 0 rescue nil end |
#next_ip(arg) ⇒ Object
Gets the next unallocated IP, given an IP to start with.
44 45 46 47 48 49 |
# File 'lib/chef-workflow/support/ip.rb', line 44 def next_ip(arg) octets = arg.split(/\./, 4).map(&:to_i) octets[3] += 1 raise "out of ips!" if octets[3] > 255 return octets.map(&:to_s).join(".") end |
#roles ⇒ Object
Get all the known roles
89 90 91 |
# File 'lib/chef-workflow/support/ip.rb', line 89 def roles @db.execute("select distinct role_name from ips").map(&:first); end |
#seed_vagrant_ips ⇒ Object
Helper method for vagrant. Vagrant always occupies .1 of any subnet it configures host-only networking on. This takes care of doing that.
104 105 106 107 108 109 110 |
# File 'lib/chef-workflow/support/ip.rb', line 104 def seed_vagrant_ips # vagrant requires that .1 be used by vagrant. ugh. dot_one_ip = @subnet.gsub(/\.\d+$/, '.1') unless ip_used?(dot_one_ip) assign_role_ip("vagrant-reserved", dot_one_ip) end end |
#subnet ⇒ Object
:attr:
The subnet used for calculating assignable IP addresses. You really want to set ‘TEST_CHEF_SUBNET` in your environment instead of changing this.
22 |
# File 'lib/chef-workflow/support/ip.rb', line 22 fancy_attr :subnet |
#unused_ip ⇒ Object
Gets the next un-used IP. This basically calls ‘next_ip` with knowledge of the database.
55 56 57 58 59 60 61 62 63 |
# File 'lib/chef-workflow/support/ip.rb', line 55 def unused_ip ip = next_ip(@subnet) while ip_used?(ip) ip = next_ip(ip) end return ip end |