This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkvm.pp
104 lines (95 loc) · 3.85 KB
/
kvm.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# have some functions do deal with virtual machines
class kvm {
package { ["qemu-kvm", "python-vm-builder", "ubuntu-vm-builder", "libvirt-bin", "bridge-utils", "lvm2", "virt-viewer"]:
ensure => latest,
}
file { "/root/run-puppet-at-boot":
owner => root,
group => root,
mode => 0755,
source => "puppet:///files/root/run-puppet-at-boot",
}
file { "/root/add-serial-to-grub":
owner => root,
group => root,
mode => 0755,
source => "puppet:///files/root/add-serial-to-grub",
}
file { "/etc/libvirt/qemu/autostart":
ensure => directory,
}
sysctl::value { "net.ipv4.ip_forward":
value => "1",
}
sysctl::value { "net.ipv6.conf.all.forwarding":
value => "1",
}
package { ["virt-goodies"]:
ensure => latest,
}
}
define kvm::virtual_machine ($fqdn, $ip, $netmask, $dns="8.8.8.8", $gateway, $memory, $rootsize, $disksize, $bridge, $ensure, $container) {
case $ensure {
present: {
exec { "create_vm_${name}":
path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
timeout => 3600,
command => "lvcreate -n $name -L ${disksize}G ${container}; virsh destroy ${fqdn}; virsh undefine ${fqdn}; \
/usr/bin/vmbuilder \
kvm ubuntu --raw /dev/mapper/${container}-$name -v -m $memory --cpus=1 --rootsize=$rootsize \
--swapsize=512 --domain=ring.nlnog.net --ip=$ip --mask=$netmask --gw=$gateway --dns=$dns \
--hostname=$fqdn --suite=precise \
--libvirt=qemu:///system \
--components=main,restricted,universe,multiverse \
--debug \
--verbose \
--timezone=UTC \
--lang=en_US.UTF-8 \
--tmpfs=- \
--addpkg=puppet \
--addpkg=openssh-server \
--addpkg=dnsutils \
--addpkg=traceroute \
--addpkg=vim \
--bridge=$bridge \
--firstboot=/root/run-puppet-at-boot \
&& virsh start $fqdn && virsh autostart ${fqdn}; rm -rf ubuntu-kvm ",
unless => "/usr/bin/test -L /dev/mapper/${container}-$name",
}
# I removed this out of the above container
# because all in all this is not the right way to do it
# --execscript=/root/add-serial-to-grub \
# file { "/etc/init/ttyS0.conf":
# owner => root,
# group => root,
# mode => 0755,
# source => "puppet:///files/etc/init/ttyS0.conf",
# }
}
absent: {
exec { "destroy_vm_${name}":
path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
timeout => 3600,
command => "virsh destroy $fqdn; virsh undefine $fqdn; sleep 10; lvremove -f /dev/mapper/${container}-$name",
}
@@exec { "clean_cert_on_master_${name}":
path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
timeout => 3600,
command => "puppetca --clean ${fqdn}",
tag => "destroy_virtual_machines",
}
@@exec { "clean_storedconfigs_on_master_${name}":
path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
timeout => 3600,
command => "puppet node clean ${fqdn}",
tag => "destroy_virtual_machines",
}
file { "/etc/libvirt/qemu/autostart/${fqdn}.xml":
ensure => absent,
}
}
default: {
fail "Invalid 'ensure' value '$ensure' for kvm::virtual_machine"
}
}
}