summaryrefslogtreecommitdiffstats
path: root/config-db/slxconfig-demuxer.pl
blob: 74c7fba0a813edb999677474a5e8490bb49c68a3 (plain) (blame)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#! /usr/bin/perl
use strict;

# add the lib-folder and the folder this script lives in to perl's search
# path for modules:
use FindBin;
use lib "$FindBin::RealBin/../lib";
	# production path
use lib "$FindBin::RealBin";
	# development path

use Fcntl qw(:DEFAULT :flock);
use File::Basename;
use Getopt::Long qw(:config pass_through);
use Pod::Usage;

use OpenSLX::Basics;
use OpenSLX::ConfigDB qw(:access :aggregation :support);

my $pxeConfigDefaultTemplate = q[# generated by openslx
NOESCAPE 0
PROMPT 0
TIMEOUT 10
DEFAULT menu.c32
IMPLICIT 1
ALLOWOPTIONS 1
MENU TITLE What would you like to do? (use cursor to select)
MENU MASTER PASSWD secret
];

my (
	$dryRun,
		# dryRun won't touch any file
	$systemConfCount,
		# number of system configurations written
	$clientSystemConfCount,
		# number of (system-specific) client configurations written
	$helpReq,
	$manReq,
	$versionReq,
);

GetOptions(
	'dry-run' => \$dryRun,
		# dry-run doesn't write anything, just prints statistic about what
		# would have been written
	'help|?' => \$helpReq,
	'man' => \$manReq,
	'version' => \$versionReq,
) or pod2usage(2);
pod2usage(1) if $helpReq;
pod2usage(-verbose => 2) if $manReq;
if ($versionReq) {
	system('slxversion');
	exit 1;
}

openslxInit();

my $openslxDB = connectConfigDB();

my $clientConfigPath = "$openslxConfig{'private-path'}/config";
if (!-d $clientConfigPath) {
	die _tr("Unable to access client-config-path '%s'!", $clientConfigPath);
}
my $tempPath = "$openslxConfig{'temp-path'}/slxconfig-demuxer";
if (!$dryRun) {
	mkdir $tempPath;
	if (!-d $tempPath) {
		die _tr("Unable to create or access temp-path '%s'!", $tempPath);
	}
}
my $tftpbootPath = $openslxConfig{'tftpboot-path'};
if (!$dryRun) {
	system("rm -rf $tftpbootPath/client-config/* $tftpbootPath/pxe/*");
	system("mkdir -p $tftpbootPath/client-config $tftpbootPath/pxe/pxelinux.cfg");
	if (!-d $tftpbootPath) {
		die _tr("Unable to create or access tftpboot-path '%s'!", $tftpbootPath);
	}
}

my $lockFile = "$tftpbootPath/config-demuxer.lock";
lockScript($lockFile);

writeConfigurations();

my $wr = ($dryRun ? "would have written" : "wrote");
print "$wr $systemConfCount systems and $clientSystemConfCount client-configurations to $tftpbootPath/client-config\n";

disconnectConfigDB($openslxDB);

system("rm -rf $tempPath")		unless $dryRun || length($tempPath) < 12;

unlockScript($lockFile);

exit;

################################################################################
###
################################################################################
sub lockScript
{
	my $lockFile = shift;

	return		if $dryRun;

	# use a lock-file to singularize execution of this script:
	if (-e $lockFile) {
		my $ctime = (stat($lockFile))[10];
		my $now = time();
		if ($now - $ctime > 15*60) {
			# existing lock file is older than 15 minutes, wipe it:
			unlink $lockFile;
		}
	}
	sysopen(LOCKFILE, $lockFile, O_RDWR|O_CREAT|O_EXCL)
		or die _tr(qq[Lock-file <%s> exists, script is already running.\nPlease remove the logfile and try again if you are sure that no one else is executing this script.], $lockFile);
}

sub unlockScript
{
	my $lockFile = shift;

	return		if $dryRun;

	unlink $lockFile;
}

sub writeAttributesToFile
{
	my $attrHash = shift;
	my $fileName = shift;

	return		if $dryRun;

	open(ATTRS, "> $fileName")		or die "unable to write to $fileName";
	my @attrs = sort grep { isAttribute($_) } keys %$attrHash;
	foreach my $attr (@attrs) {
		if (length($attrHash->{$attr}) > 0) {
			my $externalAttrName = externalAttrName($attr);
			print ATTRS "$externalAttrName=$attrHash->{$attr}\n";
		}
	}
	close(ATTRS);
}

sub copyExternalSystemConfig
{	# copies local configuration extensions of given system from private
	# config folder (var/lib/openslx/config/...) into a temporary folder
	my $systemName = shift;
	my $targetPath = shift;

	return		if $dryRun;

	if ($targetPath !~ m[$tempPath]) {
		die _tr("system-error: illegal target-path <%s>!", $targetPath);
	}
	system("rm -rf $targetPath");
	mkdir $targetPath;

	# first copy default files...
	my $defaultConfigPath = "$clientConfigPath/default";
	if (-d $defaultConfigPath) {
		system("cp -r $defaultConfigPath/* $targetPath");
	}
	# now pour system-specific configuration on top (if any):
	my $systemConfigPath = "$clientConfigPath/$systemName";
	if (-d $systemConfigPath) {
		system("cp -r $systemConfigPath/* $targetPath");
	}
}

sub createTarOfPath
{
	my $buildPath = shift;
	my $tarName = shift;
	my $destinationPath = shift;

	my $tarFile = "$destinationPath/$tarName";
	vlog 1, _tr('creating tar %s', $tarFile);
	return		if $dryRun;

	mkdir $destinationPath;
	my $tarCmd = "cd $buildPath && tar czf $tarFile *";
	if (system("$tarCmd") != 0) {
		die _tr("unable to execute shell-command:\n\t%s \n\t($!)", $tarCmd);
	}
}

################################################################################
###
################################################################################
sub writePXEMenus
{
	my $pxePath = "$tftpbootPath/pxe";
	my $pxeConfigPath = "$tftpbootPath/pxe/pxelinux.cfg";

	if (!-e "$pxePath/pxelinux.0") {
		my $pxelinux0Path
			= "$openslxConfig{'share-path'}/tftpboot/pxelinux.0";
		system(qq[cp -a "$pxelinux0Path" $pxePath/]);
	}
	if (!-e "$pxePath/menu.c32") {
		my $menuc32Path
			= "$openslxConfig{'share-path'}/tftpboot/menu.c32";
		system(qq[cp -a "$menuc32Path" $pxePath/]);
	}

	my @clients = fetchClientsByFilter($openslxDB);
	foreach my $client (@clients) {
		my $externalClientID = externalIDForClient($client);
		my $pxeFile = "$pxeConfigPath/$externalClientID";
		my $clientAppend = $client->{kernel_params};
		vlog 1, _tr("writing PXE-file %s", $pxeFile);
		open(PXE, "> $pxeFile")		or die "unable to write to $pxeFile";
		print PXE $pxeConfigDefaultTemplate;
		my @systemIDs = aggregatedSystemIDsOfClient($openslxDB, $client);
		my @systems = fetchSystemsByID($openslxDB, \@systemIDs);
		foreach my $system (@systems) {
			my @infos = aggregatedSystemFileInfosOfSystem($openslxDB, $system);
			foreach my $info (@infos) {
				my $extSysID = externalIDForSystem($info);
				my $append = $system->{kernel_params};
				$append .= " initrd=$extSysID/initramfs";
				$append .= " $clientAppend";
				$append .= " rootfs=$info->{'export-uri'} file";
				print PXE "LABEL openslx-$extSysID\n";
#				print PXE "\tMENU DEFAULT\n";
				print PXE "\tMENU LABEL ^$info->{label}\n";
				print PXE "\tKERNEL $extSysID/kernel\n";
				print PXE "\tAPPEND $append\n";
				print PXE "\tIPAPPEND 1\n";
			}
		}
		close(PXE);
 	}
}

sub generateInitalRamFS
{
	my $setup = shift;
	my $vendorOS = shift;
	my $pxeSysPath = shift;

	vlog 1, _tr('generating initialramfs %s/initramfs', $pxeSysPath);
	my $cmd = "$openslxConfig{'bin-path'}/slxmkramfs ";
	if ($setup->{ramfs_use_glibc}) {
		$cmd .= '-g ';
	}
	if ($setup->{ramfs_use_busybox}) {
		$cmd .= '-b ';
	}
	my $debugLevel = $setup->{ramfs_debug_level};
	if ($debugLevel) {
		$cmd .= qq[-d $debugLevel ];
	}
	if (length($setup->{ramfs_nicmods}) > 0) {
		$cmd .= qq[-n "$setup->{ramfs_nicmods}" ];
	}
	if (length($setup->{ramfs_fsmods}) > 0) {
		$cmd .= qq[-f "$setup->{ramfs_fsmods}" ];
	}
	my $rootPath
		= "$openslxConfig{'private-path'}/stage1/$vendorOS->{path}";
	$cmd .= "-i $pxeSysPath/initramfs -r $rootPath";

	$ENV{'SLX_PRIVATE_PATH'} = $openslxConfig{'private-path'};
	$ENV{'SLX_PUBLIC_PATH'} = $openslxConfig{'public-path'};
	$ENV{'SLX_SHARE_PATH'} = $openslxConfig{'share-path'};
	vlog 2, _tr('executing %s', $cmd);
	system($cmd)		unless $dryRun;
}

sub writeSystemPXEFiles
{
	my $system = shift;

	my $pxePath = "$tftpbootPath/pxe";

	my $vendorOS = fetchVendorOSesByID($openslxDB, $system->{vendor_os_id});
	my @infos = aggregatedSystemFileInfosOfSystem($openslxDB, $system);
	foreach my $info (@infos) {
		my $kernelFile = $info->{'kernel-file'};
		my $extSysID = externalIDForSystem($info);
		my $pxeSysPath = "$pxePath/$extSysID";
		if (-e $pxeSysPath) {
			die _tr('PXE-system %s already exists!', $pxeSysPath);
		}
		mkdir $pxeSysPath;
		vlog 1, _tr('copying kernel %s to %s/kernel', $kernelFile, $pxeSysPath);
		system(qq[cp -a "$kernelFile" $pxeSysPath/kernel])		unless $dryRun;

		generateInitalRamFS($info, $vendorOS, $pxeSysPath);
	}
}

sub writeClientConfigurationsForSystem
{
	my $system = shift;
	my $buildPath = shift;
	my $attrFile = shift;

	my @clientIDs = aggregatedClientIDsOfSystem($openslxDB, $system);
	my @clients = fetchClientsByID($openslxDB, \@clientIDs);
	foreach my $client (@clients) {
		vlog 2, _tr("exporting client %d:%s", $client->{id}, $client->{name});
		$clientSystemConfCount++;

		# merge configurations of client, it's groups, default client and
		# system and write the resulting attributes to a configuration file:
		mergeDefaultAndGroupAttributesIntoClient($openslxDB, $client);
		mergeAttributes($client, $system);
		writeAttributesToFile($client, $attrFile);

		# create tar containing external system configuration
		# and client attribute file:
		my $externalClientID = externalIDForClient($client);
		my $externalSystemID = externalIDForSystem($system);
		createTarOfPath($buildPath, "${externalClientID}.tgz",
						"$tftpbootPath/client-config/$externalSystemID");
	}
}

sub writeSystemConfigurations
{
	my @systems = fetchSystemsByFilter($openslxDB);
	foreach my $system (@systems) {
		next 	unless $system->{id} > 0;

		vlog 2, _tr('exporting system %d:%s', $system->{id}, $system->{name});
		$systemConfCount++;

		my $buildPath = "$tempPath/build";
		copyExternalSystemConfig($system->{name}, $buildPath);

		my $attrFile = "$buildPath/initramfs/machine-setup";
		mergeDefaultAttributesIntoSystem($openslxDB, $system);
		writeAttributesToFile($system, $attrFile);

		my $externalSystemID = externalIDForSystem($system);
		my $systemPath = "$tftpbootPath/client-config/$externalSystemID";
		createTarOfPath($buildPath, "default.tgz", $systemPath);

		writeSystemPXEFiles($system);

		writeClientConfigurationsForSystem($system, $buildPath, $attrFile);

		system("rm -rf $buildPath")		unless $dryRun;
	}
}

sub writeConfigurations
{
	$systemConfCount = $clientSystemConfCount = 0;
	writeSystemConfigurations();
	writePXEMenus();
}

__END__

=head1 NAME

slxconfig-demuxer.pl - OpenSLX configuration demultiplexer

=head1 SYNOPSIS

slxconfig-demuxer.pl [options]

  Script Options:
      --dry-run           avoids writing anything, for testing

  OpenSLX Options:
      --base-path=s       basic path to project files
      --bin-path=s        path to binaries and scripts
      --config-path=s     path to configuration files
      --db-basepath=s     basic path to openslx database
      --db-datadir=s      data folder created under db-basepath
      --db-name=s         name of database
      --db-spec=s         full DBI-specification of database
      --db-type=s         type of database to connect to
      --export-path=s     path to root of all exported filesystems
      --locale=s          locale to use for translations
      --logfile=s         file to write logging output to
      --private-path=s    path to private data
      --public-path=s     path to public (client-accesible) data
      --share-path=s      path to sharable data
      --temp-path=s       path to temporary data
      --tftpboot-path=s   path to root of tftp-server
      --verbose-level=i   level of logging verbosity (0-3)

  General Options:
      --help              brief help message
      --man               full documentation
      --version           show version

=head1 OPTIONS

=head3 Script Options

=over 8

=item B<--dry-run>

Runs the script but avoids writing anything. This is useful for testing, as
you can learn from the logging output what would have been done.

=back

=head3 OpenSLX Options

=over 8

=item B<--base-path=s>

Sets basic path to project files.

Default is $SLX_BASE_PATH (usually F</opt/openslx>).

=item B<--bin-path=s>

Sets path to binaries and scripts.

Default is $SLX_BASE_PATH/bin (usually F</opt/openslx/bin>).

=item B<--config-path=s>

Sets path to configuration files.

Default is $SLX_CONFIG_PATH (usually F</etc/opt/openslx>).

=item B<--db-basepath=s>

Sets basic path to openslx database.

Default is $SLX_DB_PATH (usually F</var/opt/openslx/db>).

=item B<--db-datadir=s>

Sets data folder created under db-basepath.

Default is $SLX_DB_DATADIR (usually empty as it depends on db-type
whether or not such a directory is required at all).

=item B<--db-name=s>

Gives the name of the database to connect to.

Default is $SLX_DB_NAME (usually C<openslx>).

=item B<--db-spec=s>

Gives the full DBI-specification of database to connect to. Content depends
on the db-type.

Default is $SLX_DB_SPEC (usually empty as it will be built automatically).

=item B<--db-type=s>

Sets the type of database to connect to (CSV, SQLite, mysql, ...).

Default $SLX_DB_TYPE (usually C<CSV>).

=item B<--export-path=s>

Sets path to root of all exported filesystems. For each type of export (NFS,
NBD, ...) a separate folder will be created in here.

Default is $SLX_EXPORT_PATH (usually F</srv/openslx/export>.

=item B<--locale=s>

Sets the locale to use for translations.

Defaults to the system's standard locale.

=item B<--logfile=s>

Specifies a file where logging output will be written to.

Default is to log to STDERR.

=item B<--private-path=s>

Sets path to private data, where the config-db, vendor_oses and configurational
extensions will be stored.

Default is $SLX_PRIVATE_PATH (usually F</var/opt/openslx>.

=item B<--public-path=s>

Sets path to public (client-accesible) data.

Default is $SLX_PUBLIC_PATH (usually F</srv/openslx>.

=item B<--share-path=s>

Sets path to sharable data, where distro-specs and functionality templates
will be stored.

Default is $SLX_SHARE_PATH (usually F</opt/openslx/share>.

=item B<--temp-path=s>

Sets path to temporary data.

Default is $SLX_TEMP_PATH (usually F</tmp>.

=item B<--tftpboot-path=s>

Sets path to root of tftp-server from which clients will access their files.

Default is $SLX_TFTPBOOT_PATH (usually F</srv/openslx/tftpboot>.

=item B<--verbose-level=i>

Sets the level of logging verbosity (0-3).

Default is $SLX_VERBOSE_LEVEL (usually 0, no logging).

=back

=head3 General Options

=over 8

=item B<--help>

Prints a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=item B<--version>

Prints the version and exits.

=back

=head1 DESCRIPTION

B<slxconfig-demuxer.pl> will read information about all systems, clients and
groups from the OpenSLX configuration database, mix & match the individual
configurational attributes and then demultiplex the resulting information
to a set of configuration files. These files are used by any OpenSLX-client
during boot to find out which systems to offer for booting.

The resulting files will be put into the OpenSLX-tftpboot-path (you can use the
option B<--tftpboot-path> to override the default taken from the OpenSLX
default settings).

=head2 FILE CREATION

The following set of files will be created:

=over 8

=item B<Basic PXE Setup>

The basic PXE files (F<menu.c32>, F<pxelinux.0>) will be copied into
F<$SLX_TFTPBOOT_PATH/pxe> to make them available to any PXE-client via tftp.

=item B<PXE Client Configurations>

For each client, a PXE configuration file will be generated and written to
F<$SLX_TFTPBOOT_PATH/pxe/pxelinux.cfg/01-<MAC-of-client>>. This file will
contain information about the systems this client shall offer for booting.
For each of these systems, the kernel cmdline options required for that
particular system setup is specified (via PXE's APPEND option).

=item B<System Kernels and Initialram-Filesystems>

For each bootable system, that system's kernel will be copied to
F<$SLX_TFTPBOOT_PATH/pxe/<system-name>/kernel and an OpenSLX-specific initramfs
required for booting that particular system is generated (by means of
slxmkramfs) and put into F<$SLX_TFTPBOOT_PATH/pxe/<system-name>/initramfs.

These two files (kernel & initramfs) will be referenced by the PXE client
configuration of all clients that offer this specific system for booting.

=item B<OpenSLX Client Configurations>

For each combination of system and client, an OpenSLX configuration archive
will be generated and written to
F<$SLX_TFTPBOOT_PATH/client-config/<system-name>/01-<MAC-of-client>.tgz>.
This archive will contain the file F<initramfs/machine-setup>, specifying all
the attributes of that particular client (e.g. whether or not it should start
the X-server).

Furthermore, the archive may contain additional system files that are
required for the client (e.g. a special PAM-module required for LDAP
authentication). These files are copied from F<$SLX_PRIVATE_PATH/config/default>
and F<$SLX_PROVATE_PATH/config/<system-name>>.

=back

=head2 MIXING & MATCHING (THE DEMUXER)

In the OpenSLX configuration database, each system, group and client may have
several configurational attributes set to a specific value that will cause
a client booting that system to behave in a certain way. The mixing of all
these different attributes into one set that is relevant for a specific client
booting one specific system is one important task of the slxconfig-demuxer.

As an example, let's assume one system setup that is configured to boot directly
into a special application that demands a rather low screen-resolution of
1024x768 pixels, as otherwise the text would be unreadable due to very small
fonts being used by that app. In order to achieve this, the administrator can
set the I<hw_monitor>-attribute of the B<system> to '1024x768'.
Let's say one of the clients, however, is connected to a very old monitor that
has problems with this resolution and only supports 800x600 pixels. In that
case, the administrator can set the I<hw_monitor>-attribute of that B<client> to
'800x600'. The mixing & matching process would make sure that this specific
client would run that system with a resolution of 800x600, while all other
clients would run that system in 1024x768.

So the slxconfig-demuxer demultiplexes the individual configurational attributes
into a concrete set of configuration settings for specific clients and their
offered systems, making sure that each client/system combination uses the
appropriate settings.

=cut