summaryrefslogtreecommitdiffstats
path: root/config-db/OpenSLX/Basics.pm
blob: 5a7970341cb13e329076df1417afb9449604cb54 (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
package OpenSLX::Basics;

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

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

@EXPORT = qw(
	&openslxInit %openslxConfig
	&_tr &trInit
	&vlog
);

use vars qw(%openslxConfig);

################################################################################
### Module implementation
################################################################################
use Carp;
use FindBin;
use Getopt::Long;

my %translations;
my $loadedTranslationModule;

# this hash will hold the active openslx configuration,
# it is populated from config files and/or cmdline arguments:
%openslxConfig = (
	'db-name' => 'openslx',
	'db-type' => 'CSV',
	'locale' => $ENV{LANG},
		# TODO: may need to be improved in order to be portable
	'private-basepath' => '/var/lib/openslx',
	'public-basepath' => '/srv/openslx',
	'shared-basepath' => '/usr/share/openslx',
	'temp-basepath' => '/tmp',
);
$openslxConfig{'db-basepath'} = "$openslxConfig{'private-basepath'}/db",

# specification of cmdline arguments that are shared by all openslx-scripts:
my %openslxCmdlineArgs = (
	'db-basepath=s' => \$openslxConfig{'db-basepath'},
		# basic path to openslx database, defaults to "$private-basepath/db"
	'db-datadir=s' => \$openslxConfig{'db-datadir'},
		# data folder created under db-basepath, default depends on db-type
	'db-spec=s' => \$openslxConfig{'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-name=s' => \$openslxConfig{'db-name'},
		# name of database, defaults to 'openslx'
	'db-type=s' => \$openslxConfig{'db-type'},
		# type of database to connect to (CSV, SQLite, ...), defaults to 'CSV'
	'locale=s' => \$openslxConfig{'locale'},
		# locale to use for translations
	'logfile=s' => \$openslxConfig{'locale'},
		# file to write logging output to, defaults to STDERR
	'private-basepath=s' => \$openslxConfig{'private-basepath'},
		# basic path to private data (which is accessible for clients and
		# contains all data required for booting the clients)
	'public-basepath=s' => \$openslxConfig{'public-basepath'},
		# basic path to public data (which contains database, vendorOSes
		# and all local extensions [system specific scripts])
	'shared-basepath=s' => \$openslxConfig{'shared-basepath'},
		# basic path to shared data (functionality templates and distro-specs)
	'temp-basepath=s' => \$openslxConfig{'temp-basepath'},
		# basic path to temporary data (used during demuxing)
	'verbose-level=i' => \$openslxConfig{'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'};
	print $openslxLog '-'x$minLevel, @_, "\n";
}

# ------------------------------------------------------------------------------
sub openslxInit
{
	# try to read and evaluate config files:
	foreach my $f ("OpenSLX/openslxrc", "$ENV{HOME}/.openslxrc") {
		next unless open(CONFIG, "<$f");
		while(<CONFIG>) {
			chomp;
			s/#.*//;
			s/^\s+//;
			s/\s+$//;
			next unless length;
			my ($key, $value) = split(/\s*=\s*/, $_, 2);
			$openslxConfig{$key} = $value;
		}
		close CONFIG;
	}

	# push any cmdline argument directly into our config hash:
	GetOptions(%openslxCmdlineArgs);

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

	# setup translation "engine":
	trInit();
}

# ------------------------------------------------------------------------------
sub trInit
{
	my $locale = $openslxConfig{'locale'};
	$locale =~ tr[A-Z.\-][a-z__];

	my $trModule = "OpenSLX::Translations::$locale";
	if ($loadedTranslationModule eq $trModule) {
		# requested translations have already been loaded
		return;
	}

	# 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 {
		carp "Unable to load translations module 'posix' ($!).";
	}

	if ($locale ne 'posix') {
		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;
		} else {
			carp "Unable to load translations module '$locale' ($!).";
		}
	}

}

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

	my $formatStr = $translations{$trKey};
	if (!defined $formatStr) {
#		carp "Translation key '$trKey' not found.";
		$formatStr = $trKey;
	}
	return sprintf($formatStr, @_);
}

1;