Commit f0aaf02a authored by Your Name's avatar Your Name
Browse files

template's / ipfromhostid / ipfromhost

parent 4f5f410c
*.swp
*.swo
Gemfile.lock
manifests/grdeploy.pp
manifests/vm/grinit.pp
manifests/vm/storage.pp
spec/functions/lala.rb
......@@ -12,38 +12,12 @@ Libvirt / kvm / qemu setup
# Example hiera:
````
"domain": "example.org",
"vms": {
"ldap": {
"dns": "1.1.1.1",
"external_gw": "192.168.185.1",
"external_ip": "192.168.185.10",
"external_net": "external",
"external_nm": "255.255.255.0",
"internal_gw": "192.168.23.1",
"internal_ip": "192.168.23.10",
"internal_net": "net-internal",
"internal_nm": "255.255.255.0",
"name": "ldap"
}
},
"networks": {
"external": {
"dns": "8.8.8.8",
"gateway": "192.168.185.1",
"netmask": "255.255.255.0",
"network": "192.168.185.0/24"
},
"net-internal": {
"dns": "1.1.1.1.1",
"gateway": "192.168.23.1",
"netmask": "255.255.255.0",
"network": "192.168.23.0/24"
}
},
You can find example hiera data in: ./spec/fixtures/hieradata/*.[json|yaml]
# to test
bundle install
rspec spec
# experimental
......
......@@ -3,7 +3,7 @@ require 'hiera/util'
require 'optparse'
require 'pp'
require 'ipaddr'
require_relative './putcommon.rb'
# generates the network interfaces (using info in hiera) for cirrax/libvirt
Puppet::Functions.create_function(:'gen_interfaces') do
......@@ -25,7 +25,7 @@ Puppet::Functions.create_function(:'gen_interfaces') do
out = {}
if not intf['ip'] then
# calculate ip from host
out['ip'] = ipfromhost(networks, intf['network'], intf['host'])
out['ip'] = ipfromhostid(intf['network'], intf['host'])
else
out['ip'] = intf['ip']
end
......@@ -44,21 +44,7 @@ Puppet::Functions.create_function(:'gen_interfaces') do
end
<<-DOC
gives full ip from host id, by using allocation_subnet
DOC
def ipfromhostid(networks, network, host)
gateway = IPAddr.new networks[network]['gateway']
netmask = IPAddr.new networks[network]['netmask']
end
if networks[network].key?('allocation_subnet') then
net = IPAddr.new networks[network]['allocation_subnet']
else
net = IPAddr.new networks[network]['network']
end
net.to_range.first(host + 1).last().to_s()
end
end
require_relative './putcommon.rb'
Puppet::Functions.create_function(:'put_ipfromhostid') do
def put_ipfromhostid(*args)
ipfromhostid(*args)
end
end
require_relative './putcommon.rb'
Puppet::Functions.create_function(:'put_ipfromhostname') do
def put_ipfromhostname(*args)
ipfromhostname(*args)
end
end
require_relative './putcommon.rb'
Puppet::Functions.create_function(:'put_loaddata') do
def put_loaddata(*args)
loaddata(*args)
end
end
require 'yaml'
#gives full ip from host id, by using allocation_subnet
def ipfromhostid(network, host)
networks = call_function('lookup', "networks")
gateway = IPAddr.new networks[network]['gateway']
netmask = IPAddr.new networks[network]['netmask']
if ! (networks[network].key?('allocation_network') or networks[network].key?('network')) then
raise Puppet::Error.new "no network info in network definition, can't find ip like that"
end
# a p2p network, has an allocation subnet
if networks[network].key?('allocation_network') then
net = IPAddr.new networks[network]['allocation_network']
if networks[network]['netmask'] != '255.255.255.255' then
raise Puppet::Error.new "allocation_network is for p2p networks, and your netmask != 32"
end
else
net = IPAddr.new networks[network]['network']
end
net.to_range.first(host + 1).last().to_s()
end
# get ip from interface definition
def ipfrominterface(interface)
if ! ( interface.key?('ip') or interface.key?('host') ) then
raise Puppet::Error.new "interface needs ip or host"
end
if interface.key?('ip') then
return interface['ip']
else
return ipfromhostid(interface['network'], interface['host'])
end
end
# gives full ip from hostname + network
def ipfromhostname(hostname, network)
vms = call_function('lookup', "vms")
admins = call_function('lookup', "admins")
networks = call_function('lookup', "networks")
interfaces = vms[hostname]['net_interfaces']
count = 0
for interface in interfaces do
if interface['network'] == network then
count = count + 1
ip = ipfrominterface(interface)
end
end
if count > 1 then
raise Puppet::Error.new "multiple interfaces in same network, not sure what ip to give you"
end
return ip
end
def fqdnfromhostname(hostname)
domain = call_function('lookup', "domain")
if hostname.include? '.' then
hostname
else
hostname + '.' + domain
end
end
def loaddata()
data = {}
data['domain'] = call_function('lookup', "domain")
data['vms'] = applytemplatesondict(call_function('lookup', "vms"))
data['admins'] = applytemplatesondict(call_function('lookup', "admins"))
data['networks'] = applytemplatesondict(call_function('lookup', "networks"))
puts data.to_yaml
#puts data.to_json
puts JSON.pretty_generate(data)
return data
end
def applytemplatesondict(dict)
# works on dict of dicts
# if a dict in the dict has a field template, then it will be merged with another dict in that dict
puts dict.class
out = {}
for key, value in dict do
if value.key?('istemplate') then
next
end
if value.key?('template') then
tp = dict[value['template']]
if tp.key?('template') then
raise Puppet::Error.new "only 1 level of templates supported"
end
#{ "23" => [0,3] }.merge({ "23" => [2,3] }) do |key, oldval, newval|
merged = tp.merge(value).reject { |k, v| ["template", "istemplate"].include? k }
#merged = tp.merge(value) do |key, oldval, newval|
# oldval | newval
#end
#.reject { |k, v| ["template", "istemplate"].include? k }
out[key] = merged
else
# no template used
out[key] = value
end
end
return out
end
def getservicesforinterface(hostname, network)
FALSE
end
def getportsforinterface(hostname, network)
FALSE
end
def getnetgroupsforinterface(hostname, network)
FALSE
end
{
"wtf": "werkt dit?",
"networks": {
"external": {
"allocation_network": "192.168.185.0/24",
"bridge": "external",
"dns": "8.8.8.8",
"bridge": "net-ext",
"gateway": "192.168.185.1",
"netmask": "255.255.255.255",
"network": "192.168.185.0"
},
"external123": {
"allocation_network": "192.168.123.0/24",
"bridge": "external",
"dns": "8.8.8.8",
"bridge": "net-ext",
"gateway": "192.168.123.1",
"netmask": "255.255.255.255",
"network": "192.168.123.0"
"netmask": "255.255.255.255"
},
"internal": {
"allocation_network": "192.168.23.0/24",
"bridge": "internal",
"dns": "1.1.1.1.1",
"gateway": "192.168.23.1",
"netmask": "255.255.255.0",
"network": "192.168.23.0/24"
}
},
"net-int": {
"bridge": "net-int",
"allocation_network": "192.168.1.0/24",
"gateway": "192.168.1.1",
"netmask": "255.255.255.255"
},
"net-nat": {
"bridge": "net-nat",
"gateway": "192.168.123.1",
"netmask": "255.255.255.0",
"network": "192.168.123.0/24"
},
"net-ext245": {
"bridge": "net-ext",
"allocation_network": "94.142.245.0/24",
"gateway": "192.168.123.1",
"netmask": "255.255.255.255"
}
}
}
......@@ -27,13 +27,25 @@ profile::hypervisor::nat_dhcp_start: 10.13.12.42
profile::hypervisor::nat_dhcp_end: 10.13.12.254
admins:
- email: testkey@example.com
tmpl:
istemplate: true
email: testkey@example.com
gpg_keys:
- 7E35E1F539500F6451C79B3E48714B2316943D93
name: testkey
- 7E35E1F539500F6451C79B3E48714B2316943D93
password: "$s$16384$8$1$c479e8eb722f1b071efea7826ccf9c20$96d63ebed0c64afb746026f56f71b2a1f8796c73141d2d6b1958d4ea26c60a0b"
ssh_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2HNSlzSGdyvdOoIZ93Xjmvs8hi+B84jVXjgz2RMM8s testkey
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2HNSlzSGdyvdOoIZ93Xjmvs8hi+B84jVXjgz2RMM8s testkey
- lalala
test1:
template: tmpl
name: test account 1
test2:
template: tmpl
name: test account 2
vms:
puppetmaster:
......@@ -42,17 +54,34 @@ vms:
net_interfaces:
-
network: net-nat
mac: 52:54:00:ac:ab:03
filter: clean-traffic
-
network: net-ext
mac: 52:54:00:ac:ab:04
filter: clean-traffic
-
network: net-int
mac: 52:54:00:ac:ab:05
filter: clean-traffic
host: 200
ip: 192.168.1.200
test:
net_interfaces:
-
network: net-nat
mac: 52:54:00:ac:ab:06
filter: clean-traffic
-
network: net-int
host: 150
test2:
net_interfaces:
-
network: net-int
host: 123
-
network: net-int
host: 124
-
network: net-int
host: 159
janus:
rootsize: 30G
memory: 3072000
......
require 'spec_helper'
describe 'put_ipfromhostname' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
context 'with parameters' do
it {
is_expected.to run.with_params('proksie', 'net-ext').and_return('94.142.245.192')
is_expected.to run.with_params('puppetmaster', 'net-int').and_return('192.168.1.200')
is_expected.to run.with_params('test', 'net-int').and_return('192.168.1.150')
is_expected.to run.with_params('test', 'net-nat').and_raise_error(Puppet::Error, /interface needs ip or host/)
is_expected.to run.with_params('test2', 'net-nat').and_return(nil)
is_expected.to run.with_params('test2', 'net-int').and_raise_error(Puppet::Error, /multiple interfaces in same network, not sure what ip to give you/)
}
end
end
end
end
require 'spec_helper'
describe 'put_loaddata' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
context 'with parameters' do
it {
is_expected.to run.with_params().and_return({})
}
end
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment