Commit 31853daf authored by Your Name's avatar Your Name
Browse files

calculate ip from host_id

parent 11882538
module Puppet::Parser::Functions
newfunction(:hypervisor_interfaces, :type => :rvalue) do |args|
require 'hiera'
require 'hiera/util'
require 'optparse'
require 'pp'
require 'ipaddr'
# generates the network interfaces (using info in hiera) for cirrax/libvirt
Puppet::Functions.create_function(:'gen_interfaces') do
def gen_interfaces(*args)
Puppet::Parser::Functions.function('generate_mac')
in_if = args[0]
out_if = Array.new
type = 'virtio'
vms = call_function('lookup', "vms")
admins = call_function('lookup', "admins")
networks = call_function('lookup', "networks")
puts networks
in_if.each do |intf|
out = {}
if not intf['ip'] then
# calculate ip from host
out['ip'] = '1.2.3.4'
out['ip'] = ipfromhost(networks, intf['network'], intf['host'])
else
out['ip'] = intf['ip']
end
if not intf['mac'] then
# gen mac
out['mac'] = function_libvirt_generate_mac([out['ip']])
out['mac'] = call_function('libvirt_generate_mac',[out['ip']])
else
out['mac'] = intf['mac']
end
......@@ -29,4 +43,22 @@ module Puppet::Parser::Functions
out_if
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']
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
#
module Puppet::Parser::Functions
newfunction(:libvirt_generate_mac, type: :rvalue, doc: <<-'ENDHEREDOC') do |args|
compute sha1 hash of all keys concatenated, only the first 6
hex digits will be used
@return a mac address
ENDHEREDOC
#generate mac
# compute sha1 hash of all keys concatenated, only the first 6
# hex digits will be used
Puppet::Functions.create_function(:libvirt_generate_mac) do
def libvirt_generate_mac(*args)
sha1 = Digest::SHA1.hexdigest(args.join(''))
# generate address in the QEMU/KVM MAC OID
'52:54:00:' + "#{sha1[0..1]}:#{sha1[2..3]}:#{sha1[4..5]}"
end
end
---
version: 5
defaults:
# datadir: "./spec/fixtures/hieradata"
#datadir: "/home/user/notes/automation/puppet/code/modules/hypervisor/spec/fixtures/hieradata/"
datadir: "../fixtures/hieradata/"
data_hash: yaml_data
hierarchy:
- name: 'yaml'
path: 'data.yaml'
- name: 'json'
path: 'data.json'
data_hash: json_data
{
"wtf": "werkt dit?",
"networks": {
"external": {
"allocation_network": "192.168.185.0/24",
"bridge": "external",
"dns": "8.8.8.8",
"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",
"gateway": "192.168.123.1",
"netmask": "255.255.255.255",
"network": "192.168.123.0"
},
"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"
}
}
}
---
wtf: "lala"
domain: example.com
vm::rootsize: 10G
vm::autostart: false
vm::memory: 1024000
vm::cpus: 1
vm::kernel: /var/lib/libvirt/boot/vmlinuz.current
vm::cmdline: root=/dev/vda rw elevator=noop net.ifnames=0 biosdevname=0
vm::osconf:
os:
values:
type:
attrs:
arch: 'x86_64'
values: 'hvm'
bootmenu:
attrs:
enable: 'yes'
boot:
attrs:
dev: 'hd'
profile::hypervisor::nat_ip: 10.13.12.1
profile::hypervisor::nat_netmask: 255.255.255.0
profile::hypervisor::nat_dhcp_start: 10.13.12.42
profile::hypervisor::nat_dhcp_end: 10.13.12.254
admins:
- email: testkey@example.com
gpg_keys:
- 7E35E1F539500F6451C79B3E48714B2316943D93
name: testkey
password: "$s$16384$8$1$c479e8eb722f1b071efea7826ccf9c20$96d63ebed0c64afb746026f56f71b2a1f8796c73141d2d6b1958d4ea26c60a0b"
ssh_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2HNSlzSGdyvdOoIZ93Xjmvs8hi+B84jVXjgz2RMM8s testkey
vms:
puppetmaster:
rootsize: 15G
memory: 2048000
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
ip: 192.168.1.200
janus:
rootsize: 30G
memory: 3072000
cpus: 2
net_interfaces:
-
network: net-nat
mac: 52:54:00:ac:ab:06
filter: clean-traffic
-
network: net-int
mac: 52:54:00:ac:ab:07
filter: clean-traffic
ip: 192.168.1.150
proksie:
memory: 2048000
cpus: 2
net_interfaces:
-
network: net-ext
mac: 52:54:00:ac:ab:08
filter: clean-traffic
ip: 94.142.245.192
-
network: net-int
mac: 52:54:00:ac:ab:09
filter: clean-traffic
ip: 192.168.1.155
require 'spec_helper'
describe 'hypervisor_interfaces' do
describe 'gen_interfaces' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
......@@ -16,7 +16,13 @@ describe 'hypervisor_interfaces' do
"network" => "internal",
"mac" => "52:54:00:11:22:33"
},
{
"host" => 12,
"network" => "internal",
"mac" => "52:54:00:11:22:33"
}
]
out_if = [
{
......@@ -31,7 +37,15 @@ describe 'hypervisor_interfaces' do
"type" => "virtio",
"mac" => "52:54:00:11:22:33"
},
{
"ip" => "192.168.23.12",
"network" => "internal",
"type" => "virtio",
"mac" => "52:54:00:11:22:33"
}
]
......
......@@ -10,7 +10,20 @@ require 'rspec-puppet-facts'
require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb'))
include RspecPuppetFacts
## hiera
#
RSpec.configure do |c|
# c.hiera_config = File.expand_path(File.join(__FILE__, '../fixtures/hiera.yaml'))
c.hiera_config = File.expand_path(File.join(__FILE__, '../fixtures/hiera.yaml'))
puts "spec helper hiera: " + c.hiera_config
end
##
default_facts = {
puppetversion: Puppet.version,
facterversion: Facter.version,
......
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