summaryrefslogtreecommitdiffstats
path: root/deploy.pl
blob: 67c618c3a951c1e093c13b6ff26b32aaa21ca720 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env perl
#
# Copyright (c) 2019-2020 Mischa Peters <mischa @ openbsd.amsterdam>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# vmm(4)/vmd(8) VM deploy script for OpenBSD Amsterdam
# 2018/12/17 version 3 - Perl again! :)
# 2018/12/19 added: default disk and memory size VM options
# 2018/12/20 added: extra interface option for a VM
# 2019/04/14 changed: $dir in conf path load
# 2019/05/07 added: disk format option, img or qcow2
# 2019/05/31 added: check for OpenBSD 6.6 vmctl command line syntax change
# 2019/09/29 added: doas.conf for pkill option
# 2020/05/10 changed: includes all install sets, inline with sysupgrade
# 2020/05/22 changed: set the hosts password for the users
# 2020/05/24 changed: add staggered option to vm.conf, leave disable for now
#
use 5.024;
use strict;
use warnings;
use autodie;
use Cwd qw(cwd);
use User::pwent;

# fuction to parse _deploy.conf and vm*.txt files
# all variables are stripped and added to either %vms or %conf
sub get_variables {
	my ($hash_name, @files)	= @_;
	my %hash;
	my $filename;
	my $vm_name;
	my $vm_number;

	for my $file (@files) {
		# When hash is 'vms' use the vm_name as key
		# Otherwise use 'conf' as key
		if ($hash_name eq "vms") {
			($filename = $file) =~ s/.*\///;
			($vm_name = $filename) =~ s/\.txt//;
			($vm_number = $vm_name) =~ s/^vm//;
			$hash{$vm_name}{'vm_number'} = $vm_number;
		}

		open my $fh, "<", "$file";
		while (my $row = <$fh>) {
			next if ($row =~ /^\s*($|#)/);
			chomp($row);
			(my $key, my $val) = split(/=/, $row, 2);
			if ($hash_name eq "vms") {
				($hash{$vm_name}{$key} .= $val) =~ s/^"+|"+$//g;
			} else {
				($hash{$hash_name}{$key} .= $val) =~ s/^"+|"+$//g;
			}
		}
		close $fh;
	}
	return %hash;
}

# function to render the vm.conf(5) file
# if the disk image file doesn't exist "boot bsd.rd" is used
# if the disk image file exists "boot bsd.rd" won't be used
sub render_vm_conf {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};
	my $_etc = $conf{'conf'}{'ETC'};

	open my $fh_vm, ">", "$_etc/vm.conf";
	printf $fh_vm "#\n# File generated by deploy.pl\n#\n";
	printf $fh_vm "socket owner :%s\n", $conf{'conf'}{'VMDUSERS'};
	printf $fh_vm "staggered start parallel 2 delay 90\n\n";
	printf $fh_vm "switch \"%s\" {\n", $conf{'conf'}{'SWITCH'};
	printf $fh_vm "\tinterface %s\n", $conf{'conf'}{'INTERFACE'};
	printf $fh_vm "}\n\n";

	if ($conf{'conf'}{'SWITCH2'}) {
		printf $fh_vm "switch \"%s\" {\n", $conf{'conf'}{'SWITCH2'};
		printf $fh_vm "\tinterface %s\n", $conf{'conf'}{'INTERFACE2'};
		printf $fh_vm "}\n\n";
	}

	for my $vm_name (sort keys %vms) {
		my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
		my $_disk_format = $vms{$vm_name}{'format'} || $conf{'conf'}{'FORMAT'};
		my $_disk = $conf{'conf'}{'IMAGES'} . "/" . $_instance . "." . $_disk_format;
		my $_disk2 = $conf{'conf'}{'IMAGES'} . "/" . $_instance . "_extra." . $_disk_format if $vms{$vm_name}{'disk2'};
		my $_owner = $vms{$vm_name}{'owner'} || $vms{$vm_name}{'username'};
		my $_memory = $vms{$vm_name}{'memory'} || $conf{'conf'}{'MEMORY'};
		my $_boot = $conf{'conf'}{'IMAGES'} . "/bsd.rd";
		my $_switch = $vms{$vm_name}{'switch'} || $conf{'conf'}{'SWITCH'};
		my $_switch2 = $vms{$vm_name}{'switch2'};
		my $_mac = $vms{$vm_name}{'mac'} || $conf{'conf'}{'MAC_PREFIX'} . ":" . $vms{$vm_name}{'vm_number'};

		printf $fh_vm "vm \"%s\" {\n", $_instance;
		printf $fh_vm "\tdisable\n";
		printf $fh_vm "\towner %s\n", $_owner;
		printf $fh_vm "\tmemory %s\n", $_memory if $_memory;
		printf $fh_vm "\tboot \"%s\"\n", $_boot if (! -e $_disk);
		printf $fh_vm "\tdisk \"%s\"\n", $_disk;
		printf $fh_vm "\tdisk \"%s\"\n", $_disk2 if $_disk2;
		printf $fh_vm "\tinterface tap {\n";
		printf $fh_vm "\t\tswitch \"%s\"\n", $_switch;
		printf $fh_vm "\t\tlladdr %s\n", $_mac;
		printf $fh_vm "\t}\n";
		printf $fh_vm "\tinterface tap { switch \"%s\" }\n", $_switch2 if $_switch2;
		printf $fh_vm "}\n";
	}
	close $fh_vm;
}

# function to render the dhcpd.conf(5) file
# if the disk image file doesn't exist "auto_install" is used as filename
# if the disk image file exists "auto_upgrade" is used as filename
sub render_dhcpd_conf {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};
	my $_etc = $conf{'conf'}{'ETC'};

	open my $fh_dhcpd, ">", "$_etc/dhcpd.conf";
	printf $fh_dhcpd "#\n# File generated by deploy.pl\n#\n";
	printf $fh_dhcpd "option domain-name \"%s\";\n", $conf{'conf'}{'DOMAIN'};
	printf $fh_dhcpd "option domain-name-servers %s;\n\n", $conf{'conf'}{'DNS'};
	printf $fh_dhcpd "subnet %s netmask %s {\n", $conf{'conf'}{'SUBNET'}, $conf{'conf'}{'NETMASK'};
	printf $fh_dhcpd "\toption routers %s;\n", $conf{'conf'}{'ROUTER'};
	printf $fh_dhcpd "\tserver-name \"%s.%s\";\n", $conf{'conf'}{'SERVER'}, $conf{'conf'}{'DOMAIN'};

	for my $vm_name (sort keys %vms) {
		my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
		my $_disk_format = $vms{$vm_name}{'format'} || $conf{'conf'}{'FORMAT'};
		my $_disk = $conf{'conf'}{'IMAGES'} . "/" . $_instance . "." . $_disk_format;
		my $_mac = $vms{$vm_name}{'mac'} || $conf{'conf'}{'MAC_PREFIX'} . ":" . $vms{$vm_name}{'vm_number'};
		my $_ip = $vms{$vm_name}{'ip'} || $conf{'conf'}{'IP_PREFIX'} . "." . ($conf{'conf'}{'IP_START'} + $vms{$vm_name}{'vm_number'});
		my $_hostname = $vms{$vm_name}{'hostname'};

		printf $fh_dhcpd "\thost %s {\n", $_instance;
		printf $fh_dhcpd "\t\thardware ethernet %s;\n", $_mac;
		printf $fh_dhcpd "\t\tfixed-address %s;\n", $_ip;
		if (! -e $_disk) {
			printf $fh_dhcpd "\t\tfilename \"auto_install\";\n";
		} else {
			printf $fh_dhcpd "\t\tfilename \"auto_upgrade\";\n";
		}
		printf $fh_dhcpd "\t\toption host-name \"%s\";\n", $_hostname;
		printf $fh_dhcpd "\t}\n";
	}
	printf $fh_dhcpd "}\n";
	close $fh_dhcpd;
}

# function to render the <MAC>-install.conf file for initial
# provisioning using autoinstall(8)
sub render_install_conf {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};

	printf "autoinstall(8) files:\n";
	for my $vm_name (sort keys %vms) {
		my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
		my $_disk_format = $vms{$vm_name}{'format'} || $conf{'conf'}{'FORMAT'};
		my $_disk = $conf{'conf'}{'IMAGES'} . "/" . $_instance . "." . $_disk_format;
		my $_mac = $vms{$vm_name}{'mac'} || $conf{'conf'}{'MAC_PREFIX'} . ":" . $vms{$vm_name}{'vm_number'};
		my $_htdocs = $conf{'conf'}{'HTDOCS'};

		if (! -e $_disk) {
			my $_hostname = $vms{$vm_name}{'hostname'};
			my $jot_pass = qx(jot -rcs '' 20 33 126);
			chomp($jot_pass);
			my $_ipv6 = $conf{'conf'}{'IPV6_PREFIX'} . ":" . ($conf{'conf'}{'IPV6_START'} + $vms{$vm_name}{'vm_number'}) . "::" . ($conf{'conf'}{'IP_START'} + $vms{$vm_name}{'vm_number'});
			my $_ipv6_gateway = $conf{'conf'}{'IPV6_PREFIX'} . ":" . ($conf{'conf'}{'IPV6_START'} + $vms{$vm_name}{'vm_number'}) . "::1";
			my $_username = $vms{$vm_name}{'username'};
			my $_sshkey = $vms{$vm_name}{'sshkey'};

			open my $fh_install, ">", "$_htdocs/$_mac-install.conf";
			printf $fh_install "#\n# File generated by deploy.pl\n#\n";
			printf $fh_install "System hostname = %s\n", $_hostname;
			printf $fh_install "Password for root = %s\n", $jot_pass;
			printf $fh_install "Which speed should com0 = 115200\n";
			printf $fh_install "Network interfaces = vio0\n";
			printf $fh_install "IPv4 address for vio0 = dhcp\n";
			printf $fh_install "IPv6 address for vio0 = %s\n", $_ipv6;
			printf $fh_install "IPv6 default router = %s\n", $_ipv6_gateway;
			printf $fh_install "Setup a user = %s\n", $_username;
			printf $fh_install "Password for user = %s\n", $jot_pass;
			printf $fh_install "Public ssh key for user = %s %s\n", $_sshkey, $jot_pass;
			printf $fh_install "Which disk is the root disk = sd0\n";
			printf $fh_install "What timezone are you in = Europe/Amsterdam\n";
			printf $fh_install "Location of sets = http\n";
			printf $fh_install "Server = openbsd.amsterdam\n";
			printf $fh_install "Set name(s) = +site*\n";
			printf $fh_install "Continue anyway = yes\n";
			printf $fh_install "Continue without verification = yes\n";
			close $fh_install;
			printf "%16s %s created\n", $_instance, $_htdocs . "/" . $_mac . "-install.conf";
		} elsif (-e $_disk && -e "$_htdocs/$_mac-install.conf") {
			unlink "$_htdocs/$_mac-install.conf" or warn "Unable to unlink file: $!\n";
			printf "%16s %s deleted\n", $_instance, $_htdocs . "/" . $_mac . "-install.conf";
		}
	}
}

# function to render the doas.conf(5) file
sub render_doas_conf {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};
	my $_etc = $conf{'conf'}{'ETC'};

	open my $fh_doas, ">", "$_etc/doas.conf";
	printf $fh_doas "permit nopass setenv { ENV PS1 SSH_AUTH_SOCK PATH=\$PATH HOME=\$HOME USER=\$USER } mischa\n";
	printf $fh_doas "permit nopass keepenv root as root\n";

	for my $vm_name (sort keys %vms) {
		my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
		my $_owner = $vms{$vm_name}{'owner'} || $vms{$vm_name}{'username'};
		printf $fh_doas "permit nopass %s as root cmd pkill args -9 -f %s\n", $_owner, $_instance;
	}
	close $fh_doas;
}


# function to create accounts on the host for vmctl(8) access
sub create_accounts {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};

	printf "useradd(8) users:\n";
	for my $vm_name (sort keys %vms) {
		my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
		my $_owner = $vms{$vm_name}{'owner'} || $vms{$vm_name}{'username'};
		my $_group = $conf{'conf'}{'VMDUSERS'};
		my $_sshkey = $vms{$vm_name}{'sshkey'};
		my $id = getpwnam("$_owner");

		if (! $id) {
			my $jot_pass = qx(jot -rcs '' 20 43 125);
			chomp($jot_pass);
			my $encrypt_pass = qx(encrypt '${jot_pass}');
			chomp($encrypt_pass);
			my $output = qx(/usr/sbin/useradd -m -G $_group -p '${encrypt_pass}' $_owner);
			open my $fh_authorized, ">>", "/home/$_owner/.ssh/authorized_keys";
			printf $fh_authorized "%s\n", $_sshkey;
			close $fh_authorized;
			printf "%16s %s account created\n", $_instance, $_owner;
		}
	}
}

# function to create the disk image files for vmm(4)/vmd(8)
sub create_img_files {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};
	my $_VERSION = qx(uname -r);
	my $vmctl_create;

	printf "vmm(4)/vmd(8) files:\n";
	for my $vm_name (sort keys %vms) {
		my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
		my $_disk_format = $vms{$vm_name}{'format'} || $conf{'conf'}{'FORMAT'};
		my $_disk = $conf{'conf'}{'IMAGES'} . "/" . $_instance . "." . $_disk_format;
		my $_disk_size = $vms{$vm_name}{'disk'} || $conf{'conf'}{'DISKSIZE'};
		my $_disk2 = $conf{'conf'}{'IMAGES'} . "/" . $_instance . "_extra." . $_disk_format if $vms{$vm_name}{'disk2'};
		my $_disk2_size = $vms{$vm_name}{'disk2'} if $vms{$vm_name}{'disk2'};

		if (! -e $_disk) {
			if ($_VERSION < 6.6) {
				$vmctl_create= "vmctl create $_disk -s $_disk_size 2>&1";
			} else {
				$vmctl_create= "vmctl create -s $_disk_size $_disk 2>&1";
			}
			my $output = qx($vmctl_create);
			if ($? == 0) {
				printf "%16s %s created (size %s)\n", $_instance, $_disk, $_disk_size;
			} else {
				printf "%16s %s NOT created!!!\n", $_instance, $_disk;
			}
		}
		if ($_disk2) {
			if (! -e $_disk2) {
				if ($_VERSION < 6.6) {
					$vmctl_create = "vmctl create $_disk2 -s $_disk2_size 2>&1";
				} else {
					$vmctl_create = "vmctl create -s $_disk2_size $_disk2 2>&1";
				}
				my $output = qx($vmctl_create);
				if ($? == 0) {
					printf "%16s %s created (size %s)\n", $_instance, $_disk2, $_disk2_size;
				} else {
					printf "%16s %s NOT created (size %s)!!!\n", $_instance, $_disk2, $_disk2_size;
				}
			}
		}
	}
}

# function to print all keys & values for debug purposes
sub debug_parse {
	my %conf = %{$_[0]};
	my %vms = %{$_[1]};

	for my $vm_name (sort keys %vms) {
		for my $key (keys %{$vms{$vm_name}}) {
			printf "VMS: %s %s = %s\n", $vm_name, $key, $vms{$vm_name}{$key};
		}
	}
}

# check if _deploy.conf exists in current working directory
my %conf;
my $dir = cwd;
if (-e "$dir/_deploy.conf") {
	%conf = get_variables('conf', "$dir/_deploy.conf");
} else {
	printf "Unable to find config file in current directory (%s).\n", $dir;
	printf "Create the config file _deploy.conf in %s.\n", $dir;
	exit 1;
}

# parse all vm*.txt files in the VMS directory
my %vms;
my @files = glob "$conf{'conf'}{'VMS'}/*.txt";
%vms = get_variables('vms', @files);

# run all functions
#debug_parse(\%conf, \%vms);
render_vm_conf(\%conf, \%vms);
render_dhcpd_conf(\%conf, \%vms);
render_install_conf(\%conf, \%vms);
create_accounts(\%conf, \%vms);
create_img_files(\%conf, \%vms);
render_doas_conf(\%conf, \%vms);