summaryrefslogtreecommitdiffstats
path: root/config-db/slxconfig
blob: cd87e0f9bde70d070a22ff84bd14eeeef2e6ee83 (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
#! /usr/bin/perl
#
# slxconfig -
#
# (c) 2006 - OpenSLX.com
#
# Oliver Tappe <ot@openslx.com>
#
use strict;

my $abstract = q[
slxconfig
    This script can be used to display or change the configuration
    of OpenSLX vendor-OSes. You can create systems that use a
    specific vendor-OS and you can create clients for that system, too.
];

use Getopt::Long qw(:config pass_through);
use Pod::Usage;

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

use lib "$FindBin::RealBin/../config-db";
	# development path to config-db

use OpenSLX::Basics;
use OpenSLX::ConfigDB;
use OpenSLX::DBSchema;

my (
	$helpReq,
	$listClientsReq,
	$listExportsReq,
	$listSystemsReq,
	$listVendorOSesReq,
	$manReq,
	$verbose,
	$versionReq,
);

GetOptions(
	'help|?' => \$helpReq,
	'list-clients' => \$listClientsReq,
	'list-exports' => \$listExportsReq,
	'list-systems' => \$listSystemsReq,
	'list-vendor-oses' => \$listVendorOSesReq,
	'man' => \$manReq,
	'verbose' => \$verbose,
	'version' => \$versionReq,
) or pod2usage(2);
pod2usage(-msg => $abstract, -verbose => 0, -exitval => 1) if $helpReq;
pod2usage(-verbose => 2) if $manReq;
if ($versionReq) {
	system('slxversion');
	exit 1;
}

openslxInit();

my $openslxDB = OpenSLX::ConfigDB->new();
$openslxDB->connect();

if ($listClientsReq) {
	print _tr("List of the clients defined in config-DB:\n");
	print join('', map { "\t$_->{name}\n" }
#				   grep { $_->{id} > 0 }
				   sort { $a->{name} cmp $b->{name} }
				   $openslxDB->fetchClientByFilter());
	exit 1;
}

if ($listExportsReq) {
	print _tr("List of the exports defined in config-DB:\n");
	print join('', map { "\t$_->{name}\n" }
				   sort { $a->{name} cmp $b->{name} }
				   $openslxDB->fetchExportByFilter());
	exit 1;
}

if ($listSystemsReq) {
	print _tr("List of the systems defined in config-DB:\n");
	print join('', map { "\t$_->{name}\n" }
#				   grep { $_->{id} > 0 }
				   sort { $a->{name} cmp $b->{name} }
				   $openslxDB->fetchSystemByFilter());
	exit 1;
}

if ($listVendorOSesReq) {
	print _tr("List of the vendor-OSes defined in config-DB:\n");
	print join('', map { "\t$_->{name}\n" }
				   sort { $a->{name} cmp $b->{name} }
				   $openslxDB->fetchVendorOSByFilter());
	exit 1;
}

my $action = shift @ARGV;
if ($action =~ m[^add-system$]i) {
    addSystemToConfigDB(@ARGV);
} elsif ($action =~ m[^add-client$]i) {
    addClientToConfigDB(@ARGV);
} else {
	print STDERR _tr("You need to specify exactly one of these actions:\n\tadd-system, add-client\n");
	pod2usage(2);
}

$openslxDB->disconnect();

exit;

sub parseKeyValueArgs
{
    my $allowedKeys = shift;
    my $table = shift;

    my %dataHash;
    while (my $param = shift) {
        if ($param !~ m[^\s*(\w+)\s*=(.+)$]) {
            die _tr("value specification %s has unknown format, expected <key>=<value>\n",
                    $param);
        }
        my $key = $1;
        my $value = $2;
        if (!grep { $_ eq $key } @$allowedKeys) {
            die _tr("unknown attribute '%s' specified for %s\n", $key, $table);
        }
        $dataHash{$1} = $2;
    }
    return \%dataHash;
}

sub addSystemToConfigDB
{
    my @systemKeys
		= map { (/^(\w+)\W/) ? $1 : $_; }
		  @{$DbSchema->{tables}->{system}};
	push @systemKeys, 'clients', 'export';
    my $systemData = parseKeyValueArgs(\@systemKeys, 'system', @_);

	if (!length($systemData->{export})) {
        die _tr("you have to specify the export the new system shall be using\n");
	}
    my $exportName = $systemData->{export};
	delete $systemData->{export};
	my $export
		= $openslxDB->fetchExportByFilter({ 'name' => $exportName });
	if (!defined $export) {
		die _tr("export '%s' could not be found in DB, giving up!\n", $exportName);
	}
	$systemData->{export_id} = $export->{id};

	my @clientIDs;
	my $clientNames;
	if (exists $systemData->{clients}) {
		@clientIDs
			= map {
				my $client
					= $openslxDB->fetchClientByFilter({ 'name' => $_ });
				if (!defined $client) {
					die _tr("client '%s' doesn't exist in DB, giving up!\n", $_);
				}
				$client->{id};
			  }
			  split ",", $systemData->{clients};
		$clientNames = $systemData->{clients};
		delete $systemData->{clients};
	}

	if (!length($systemData->{kernel})) {
		$systemData->{kernel} = 'vmlinuz';
	}
	if (!length($systemData->{name})) {
		$systemData->{name} = "$exportName-$systemData->{kernel}";
	}
	if (!length($systemData->{label})) {
		$systemData->{label} = "$exportName-$systemData->{kernel}";
	}
	if (!length($systemData->{ramfs_debug_level})) {
		$systemData->{ramfs_debug_level} = '0';
	}
	if (!length($systemData->{ramfs_use_glibc})) {
		$systemData->{ramfs_use_glibc} = '0';
	}
	if (!length($systemData->{ramfs_use_busybox})) {
		$systemData->{ramfs_use_busybox} = '1';
	}

	if ($openslxDB->fetchSystemByFilter({ 'name' => $systemData->{name} })) {
		die _tr("the system '%s' already exists in the DB, giving up!\n",
				$systemData->{name});
	}
	my $systemID = $openslxDB->addSystem([$systemData]);
	vlog 0, _tr("system '%s' has been successfully added to DB (ID=%s)\n",
				$systemData->{name}, $systemID);

	if (scalar(@clientIDs)) {
	    $openslxDB->addClientIDsToSystem($systemID, \@clientIDs);
		if ($verbose) {
			vlog 0, _tr("clients of this system are:\n\t%s\n", $clientNames);
		}
	}
}

sub addClientToConfigDB
{
    my @clientKeys
		= map { (/^(\w+)\W/) ? $1 : $_; }
		  @{$DbSchema->{tables}->{client}};
	push @clientKeys, 'systems';
    my $clientData = parseKeyValueArgs(\@clientKeys, 'client', @_);

	my @systemIDs;
	my $systemNames;
	if (exists $clientData->{systems}) {
		@systemIDs
			= map {
				my $system
					= $openslxDB->fetchSystemByFilter({ 'name' => $_ });
				if (!defined $system) {
					die _tr("system '%s' doesn't exist!\n", $_);
				}
				$system->{id};
			  }
			  split ",", $clientData->{systems};
		$systemNames = $clientData->{systems};
		delete $clientData->{systems};
	}

	if (!length($clientData->{name})) {
		die _tr("you have to specify the name for the new client\n");
	}
	if (!length($clientData->{mac})) {
		die _tr("you have to specify the MAC for the new client\n");
	}
	if (!length($clientData->{boot_type})) {
		$clientData->{boot_type} = 'pxe';
	}

	if ($openslxDB->fetchClientByFilter({ 'name' => $clientData->{name} })) {
		die _tr("the client '%s' already exists in the DB, giving up!\n",
				$clientData->{name});
	}
	my $clientID = $openslxDB->addClient([$clientData]);
	vlog 0, _tr("client '%s' has been successfully added to DB (ID=%s)\n",
				$clientData->{name}, $clientID);

	if (scalar(@systemIDs)) {
		$openslxDB->addSystemIDsToClient($clientID, \@systemIDs);
		if ($verbose) {
			print _tr("systems for this client are:\n\t%s\n", $systemNames);
		}
	}
}

__END__

=head1 NAME

slxconfig - OpenSLX-script to configure a vendor-OS for use with
OpenSLX. You can create systems that will use the specified vendor-OS
and you can create clients for that system, too.

=head1 SYNOPSIS

=head2 Adding a new System to a Vendor-OS Export

  slxconfig add-system export-name=<export-name> [clients=<client-Names>] [<attribute=value> ...]

  The above syntax is used to add a new system to the config-DB. The new
  system will use the I<export> whose name is given.

  If you specify clients (a comma-separated list of client names), the new system
  will be used by the given clients.

=head2 Adding a new Client

  slxconfig add-client [systems=<system-IDs> [<attribute=value> ...]

  The above syntax is used to add a new client to the config-DB.

  If you specify systems (a comma-separated list of system names), the new client
  will use the given systems.

=head2 General Format

  slxconfig [options] <action> <action-options>

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

  Actions:
      list-systems             clones an existing system via rsync

=head1 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

=cut