summaryrefslogtreecommitdiffstats
path: root/lib/OpenSLX/Basics.pm
blob: 3159afa0974b6c74a30ed62979a9dcc4c06c5e55 (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
# Copyright (c) 2006, 2007 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# Basics.pm
#	- provides basic functionality of the OpenSLX config-db.
# -----------------------------------------------------------------------------
package OpenSLX::Basics;

use strict;
use vars qw(@ISA @EXPORT $VERSION);

use Exporter;
$VERSION = 1.01;
@ISA = qw(Exporter);

@EXPORT = qw(
	&openslxInit %openslxConfig %cmdlineConfig
	&_tr &trInit
	&warn &die &executeInSubprocess &slxsystem
	&vlog
);

use vars qw(%openslxConfig %cmdlineConfig);

################################################################################
### Module implementation
################################################################################
use Carp;
use Carp::Heavy;
	# use it here to have it loaded immediately, not at the time when
	# carp() is being invoked (which might be at a point in time where
	# the script executes in a chrooted environment, such that the module
	# can't be loaded anymore). 
use FindBin;
use Getopt::Long;
use POSIX qw(locale_h);

my %translations;

# this hash will hold the active openslx configuration,
# the initial content is based on environment variables or default values.
# Each value may be overrided from config files and/or cmdline arguments.
%openslxConfig = (
	'croak' => '0',
	'db-datadir' => $ENV{SLX_DB_DATADIR},
	'db-name' => $ENV{SLX_DB_NAME} || 'openslx',
	'db-spec' => $ENV{SLX_DB_SPEC},
	'db-type' => $ENV{SLX_DB_TYPE} || 'SQLite',
	'locale' => setlocale(LC_MESSAGES),
	'locale-charmap' => `locale charmap`,
	'base-path' => $ENV{SLX_BASE_PATH} || '/opt/openslx',
	'config-path' => $ENV{SLX_CONFIG_PATH} || '/etc/opt/openslx',
	'private-path' => $ENV{SLX_PRIVATE_PATH} || '/var/opt/openslx',
	'public-path' => $ENV{SLX_PUBLIC_PATH} || '/srv/openslx',
	'temp-path' => $ENV{SLX_TEMP_PATH} || '/tmp',
	'verbose-level' => $ENV{SLX_VERBOSE_LEVEL} || '0',
);
chomp($openslxConfig{'locale-charmap'});
$openslxConfig{'bin-path'}
	= $ENV{SLX_BIN_PATH} || "$openslxConfig{'base-path'}/bin",
$openslxConfig{'db-basepath'}
	= $ENV{SLX_DB_PATH} || "$openslxConfig{'private-path'}/db",
$openslxConfig{'export-path'}
	= $ENV{SLX_EXPORT_PATH} || "$openslxConfig{'public-path'}/export",
$openslxConfig{'share-path'}
	= $ENV{SLX_SHARE_PATH} || "$openslxConfig{'base-path'}/share",
$openslxConfig{'stage1-path'}
	= $ENV{SLX_STAGE1_PATH} || "$openslxConfig{'private-path'}/stage1",
$openslxConfig{'tftpboot-path'}
	= $ENV{SLX_TFTPBOOT_PATH} || "$openslxConfig{'public-path'}/tftpboot",

# specification of cmdline arguments that are shared by all openslx-scripts:
%cmdlineConfig;
my %openslxCmdlineArgs = (
	'base-path=s' => \$cmdlineConfig{'base-path'},
		# basic path to project files (binaries, functionality templates and
		# distro-specs)
	'bin-path=s' => \$cmdlineConfig{'bin-path'},
		# path to binaries and scripts
	'config-path=s' => \$cmdlineConfig{'config-path'},
		# path to configuration files
	'croak' => \$cmdlineConfig{'croak'},
		# activates debug mode, this will show the lines where any error occured
	'db-basepath=s' => \$cmdlineConfig{'db-basepath'},
		# basic path to openslx database, defaults to "${private-path}/db"
	'db-datadir=s' => \$cmdlineConfig{'db-datadir'},
		# data folder created under db-basepath, default depends on db-type
	'db-name=s' => \$cmdlineConfig{'db-name'},
		# name of database, defaults to 'openslx'
	'db-spec=s' => \$cmdlineConfig{'db-spec'},
		# full specification of database, a special string defining the
		# precise database to connect to (the contents of this string
		# depend on db-type)
	'db-type=s' => \$cmdlineConfig{'db-type'},
		# type of database to connect to (CSV, SQLite, ...), defaults to 'CSV'
	'export-path=s' => \$cmdlineConfig{'export-path'},
		# path to root of all exports, each different export-type (e.g. nfs, nbd)
		# has a separate subfolder in here.
	'locale=s' => \$cmdlineConfig{'locale'},
		# locale to use for translations
	'locale-charmap=s' => \$cmdlineConfig{'locale-charmap'},
		# locale-charmap to use for I/O (iso-8859-1, utf-8, etc.)
	'logfile=s' => \$cmdlineConfig{'locale'},
		# file to write logging output to, defaults to STDERR
	'private-path=s' => \$cmdlineConfig{'private-path'},
		# path to private data (which is *not* accesible by clients and contains
		# database, vendorOSes and all local extensions [system specific scripts])
	'public-path=s' => \$cmdlineConfig{'public-path'},
		# path to public data (which is accesible by clients and contains
		# PXE-configurations, kernels, initramfs and client configurations)
	'share-path=s' => \$cmdlineConfig{'share-path'},
		# path to sharable data (functionality templates and distro-specs)
	'stage1-path=s' => \$cmdlineConfig{'stage1-path'},
		# path to stage1 systems
	'temp-path=s' => \$cmdlineConfig{'temp-path'},
		# path to temporary data (used during demuxing)
	'tftpboot-path=s' => \$cmdlineConfig{'tftpboot-path'},
		# path to root of tftp-server, tftpable data will be stored there
	'verbose-level=i' => \$cmdlineConfig{'verbose-level'},
		# level of logging verbosity (0-3)
);

# filehandle used for logging:
my $openslxLog = *STDERR;

# ------------------------------------------------------------------------------
sub vlog
{
	my $minLevel = shift;
	return if $minLevel > $openslxConfig{'verbose-level'};
	my $str = join("", '-'x$minLevel, @_);
	if (substr($str,-1,1) ne "\n") {
		$str .= "\n";
	}
	print $openslxLog $str;
}

# ------------------------------------------------------------------------------
sub openslxInit
{
	# evaluate cmdline arguments:
	Getopt::Long::Configure('no_pass_through');
	GetOptions(%openslxCmdlineArgs) or return 0;

	# try to read and evaluate config files:
	my $configPath = $cmdlineConfig{'config-path'}
						|| $openslxConfig{'config-path'};
	foreach my $f ("$configPath/settings.default",
				   "$configPath/settings.local",
				   "$ENV{HOME}/.openslx/settings") {
		next unless open(CONFIG, "<$f");
		if ($cmdlineConfig{'verbose-level'} >= 2) {
			vlog 0, "reading config-file $f...";
		}
		while(<CONFIG>) {
			chomp;
			s/#.*//;
			s/^\s+//;
			s/\s+$//;
			next unless length;
			if (! /^(\w+)=(.*)$/) {
				die _tr("config-file <%s> has incorrect syntax here:\n\t%s\n",
						$f, $_);
			}
			my ($key, $value) = ($1, $2);
			# N.B.: the config files are used by shell-scripts, too, so in
			# order to comply with shell-style, the config files use shell
			# syntax and an uppercase, underline-as-separator format.
			# Internally, we use lowercase, minus-as-separator format, so we
			# need to convert the environment variable names to our own
			# internal style here (e.g. 'SLX_BASE_PATH' to 'base-path'):
			$key =~ s[^SLX_][];
			$key =~ tr/[A-Z]_/[a-z]-/;
			$openslxConfig{$key} = $value;
		}
		close CONFIG;
	}

	# push any cmdline argument into our config hash, possibly overriding any
	# setting from the config files:
	while(my ($key, $val) = each(%cmdlineConfig)) {
		next unless defined $val;
		$openslxConfig{$key} = $val;
	}

	if (defined $openslxConfig{'logfile'}
	&& open(LOG, ">>$openslxConfig{'logfile'}")) {
		$openslxLog
	}
	if ($openslxConfig{'verbose-level'} >= 2) {
		foreach my $k (sort keys %openslxConfig) {
			vlog 2, "config-dump: $k = $openslxConfig{$k}";
		}
	}

	# setup translation "engine":
	trInit();

	return 1;
}

# ------------------------------------------------------------------------------
sub trInit
{
	# set the specified locale...
	setlocale('LC_ALL', $openslxConfig{'locale'});

	# ...and activate automatic charset conversion on all I/O streams:
	binmode(STDIN, ":encoding($openslxConfig{'locale-charmap'})");
	binmode(STDOUT, ":encoding($openslxConfig{'locale-charmap'})");
	binmode(STDERR, ":encoding($openslxConfig{'locale-charmap'})");
	use open ':locale';

	my $locale = $openslxConfig{'locale'};
	if (lc($locale) eq 'c') {
		# treat locale 'c' as equivalent for 'posix':
		$locale = 'posix';
	}

	# load Posix-Translations first in order to fall back to English strings
	# if a specific translation isn't available:
	if (eval "require OpenSLX::Translations::posix") {
		%translations = %OpenSLX::Translations::posix::translations;
	} else {
		vlog 1, "unable to load translations module 'posix' ($@).";
	}

	if (lc($locale) ne 'posix') {
		# parse locale and canonicalize it (e.g. to 'de_DE') and generate
		# two filenames from it (language+country and language only):
		if ($locale !~ m{^\s*([^_]+)(?:_(\w+))?}) {
			die "locale $locale has unknown format!?!";
		}
		my @locales;
		if (defined $2) {
			push @locales, lc($1).'_'.uc($2);
		}
		push @locales, lc($1);

		# try to load any of the Translation modules (starting with the more
		# specific one [language+country]):
		my $loadedTranslationModule;
		foreach my $trName (@locales) {
			my $trModule = "OpenSLX::Translations::$trName";
			if (eval "require $trModule") {
				# Access OpenSLX::Translations::<locale>::translations
				# via a symbolic reference...
				no strict 'refs';
				my $translationsRef	= \%{ "${trModule}::translations" };
				# ...and copy the available translations into our hash:
				foreach my $k (keys %{$translationsRef}) {
					$translations{$k} = $translationsRef->{$k};
				}
				$loadedTranslationModule = $trModule;
				vlog 1, _tr("translations module %s loaded successfully",
							$trModule);
				last;
			}
		}
		if (!defined $loadedTranslationModule) {
			vlog 1, "unable to load any translations module for locale '$locale' ($!).";
		}
	}
}

# ------------------------------------------------------------------------------
sub _tr
{
	my $trOrig = shift;

	my $trKey = $trOrig;
	$trKey =~ s[\n][\\n]g;
	$trKey =~ s[\t][\\t]g;

	my $formatStr = $translations{$trKey};
	if (!defined $formatStr) {
		$formatStr = $trOrig;
	}
	return sprintf($formatStr, @_);
}

# ------------------------------------------------------------------------------
sub executeInSubprocess
{
	my $childFunc = shift;

	my $pid = fork();
	if (!$pid) {
		# child...
		# ...execute the given function and exit:
		&$childFunc();
		exit 0;
	}

	# parent...
	# ...pass on interrupt- and terminate-signals to child...
	local $SIG{INT}
		= sub { kill 'INT', $pid; waitpid($pid, 0); exit $? };
	local $SIG{TERM}
		= sub { kill 'TERM', $pid; waitpid($pid, 0); exit $? };
	# ...and wait for child to do its work:
	waitpid($pid, 0);
	if ($?) {
		exit $?;
	}
}

# ------------------------------------------------------------------------------
sub slxsystem
{
	vlog 2, _tr("executing: %s", join ' ', @_);
	my $res = system(@_);
	if ($res > 0 && $res & 127) {
		# child got killed, so we stop, too
		die _tr("child-process reveived signal '%s', parent stops!", $res & 127);
		exit;
	}
	return $res;
}

# ------------------------------------------------------------------------------
sub warn
{
	my $msg = shift;
	if ($openslxConfig{'croak'}) {
		print STDERR "*** ";
		carp $msg;
	} else {
		$msg =~ s[^][*** ]igms;
		chomp $msg;
		print STDERR "$msg\n";
	}
}

# ------------------------------------------------------------------------------
sub die
{
	my $msg = shift;
	if ($openslxConfig{'croak'}) {
		print STDERR "*** ";
		croak $msg;
	} else {
		$msg =~ s[^][*** ]igms;
		chomp $msg;
		print STDERR "$msg\n";
		exit 5 unless ($!);
		exit $!;
	}
}

1;