1
/* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: connection.pike,v 1.9 2006/07/01 02:14:50 exodusd Exp $
20
constant cvs_version="$Id: connection.pike,v 1.9 2006/07/01 02:14:50 exodusd Exp $";
25
* the pike call will include the pathnames to the server location
27
inherit "client_base";
28
inherit "base/xml_parser";
33
#include <attributes.h>
37
static object fsystem; // the filesystem to use
40
static int debug_output = 0;
42
private static mapping mFiles = ([ ]);
43
object _docfactory, _contfactory, _filepath;
44
int iInstall, iUpdate, iError;
46
int handle_error(mixed err) {
47
if ( arrayp(err) && sizeof(err)>1 ) {
48
if ( stringp(err[1]) ) werror( "Error: %s\n", err[1] );
49
else werror( "Error: %O\n", err[1] );
51
else werror( "Error: %O\n", err );
54
werror( "Debug output:\n%O\n", err );
55
throw( ({ "", backtrace() }) );
60
object open_file(string fname, string fs)
62
return Filesystem.Tar(fs)->open(fname, "r");
65
array(string) get_directory(string dname)
67
return fsystem->get_dir(dname);
70
void set_debug ( int debug )
80
/*****************************************************************************
83
mapping xmlMap(NodeXML n)
89
foreach ( n->children, NodeXML children) {
90
if ( children->name == "member" ) {
92
foreach(children->children, object o) {
94
if ( o->name == "key" )
95
key = unserialize(o->children[0]);
96
else if ( o->name == "value" )
97
value = unserialize(o->children[0]);
105
array xmlArray(NodeXML n)
108
foreach ( n->children, NodeXML children) {
109
res += ({ unserialize(children) });
114
mixed unserialize(NodeXML n)
127
return (float)n->data;
133
string type = n->children[0]->data;
134
string id = n->children[1]->data;
140
oid = set_object(mVariables["groups"]);
143
oid = set_object(mVariables["users"]);
147
obj = send_command(COAL_COMMAND, ({ "get_module", ({ id }) }));
154
oid = set_object(mVariables["filepath:tree"]);
155
obj = send_command(COAL_COMMAND, ({ "path_to_object", ({ id }) }));
161
obj = send_command(COAL_COMMAND, ({ "lookup", ({ id }) }));
168
void save_xml(NodeXML n, object obj)
171
mapping attributes = xmlMap(n->get_node("/Object/attributes/struct"));
172
mapping a_acquire = xmlMap(n->get_node("/Object/attributes-acquire/struct"));
173
mapping sanction = xmlMap(n->get_node("/Object/sanction/struct"));
174
mapping msanction = xmlMap(n->get_node("/Object/sanction-meta/struct"));
175
mixed acquire = unserialize(n->get_node("/Object/acquire/*"));
176
mapping a_lock = xmlMap(n->get_node("/Object/attributes-locked/struct"));
179
foreach(indices(sanction), object sanc) {
180
if ( objectp(sanc) ) {
181
send_command(COAL_COMMAND, ({ "sanction_object",
182
({ sanc, sanction[sanc] }) }) );
185
foreach(indices(msanction), object msanc) {
186
if ( objectp(msanc) ) {
187
send_command(COAL_COMMAND, ({ "sanction_object_meta",
188
({ msanc, msanction[msanc] }) }) );
193
send_command(COAL_COMMAND, ({ "unlock_attributes" }));
194
foreach(indices(a_acquire), key) {
195
if ( stringp(a_acquire[key]) ) // environment only, may not work *g*
196
send_command(COAL_COMMAND, ({ "set_acquire_attribute",
198
else if ( !intp(a_acquire[key]) )
199
send_command(COAL_COMMAND, ({ "set_acquire_attribute",
200
({ key, a_acquire[key] }) }));
202
send_command(COAL_COMMAND, ({ "set_attributes", ({ attributes }) }) );
203
foreach(indices(a_lock), key ) {
204
if ( a_lock[key] != 0 ) {
205
send_command(COAL_COMMAND, ({ "lock_attribute", ({ key }) }));
209
// acquire string should be environment function, but thats default ...
210
// cannot handle functions yet
211
if ( objectp(acquire) || acquire == 0 )
212
send_command(COAL_COMMAND, ({ "set_acquire", ({ acquire }) }));
217
void object_to_server(object obj)
221
if ( obj->get_object_class() & CLASS_USER )
225
if ( !objectp(_filepath) )
226
_filepath = send_cmd(0, "get_module", "filepath:tree");
228
object pname = send_cmd(_filepath, "object_to_filename", obj);
233
werror("\r"+(" "*79));
234
werror("\rreading... " + path+".xml");
237
f = open_file(path+".xml", _fs);
239
// if the file does not exist its not part of the reference
240
// server installation
243
werror("\r"+path+".xml ... file not found !\n");
247
string xml = f->read();
249
NodeXML n = parse_data(xml);
253
object upload_file(object dir, string fname, object f)
255
if ( search(fname, ".") == 0 || search(fname, "#") >= 0 ) {
258
object obj = send_cmd(dir, "get_object_byname", fname);
259
if ( !objectp(obj) ) {
260
werror(" (Created)");
262
obj = send_cmd(_docfactory, "execute", ([ "name": fname, ]));
263
send_cmd(obj, "move", dir);
269
send_cmd(obj, "set_content", f->read());
271
string filename=send_cmd(_filepath, "object_to_filename", obj);
272
mFiles[filename] = obj;
276
object create_folder(string inCont, string folderName)
278
return send_cmd(_filepath,"make_directory", ({ inCont, folderName }) );
281
void upload_directory(object location, string dir, mapping vars)
285
if ( !objectp(location) )
286
error("Dont know where to install - location non-object !");
287
array files = fsystem->get_dir(dir);
288
if ( !arrayp(files) ) {
289
werror("Directory "+ dir + " is empty .. skipping...\n");
292
string path = send_cmd(_filepath, "object_to_filename", location);
293
if ( !stringp(path) )
294
error("Path is not resolvable (no string)!");
295
if ( strlen(path) == 0 )
296
error("Path has zero length !");
297
cont = send_cmd(location, "get_object_byname", basename(dir));
298
if ( !objectp(cont) ) {
299
werror("\rFolder %s does not exist on server, creating...\n",
304
cont = create_folder(path, basename(dir));
306
path = path + (path[-1] !='/' ?"/":"") + basename(dir);
308
werror("\r%s", " "*79); //clear the line
309
werror("\rDIRECTORY: %s", path);
312
foreach( files, string fname ) {
313
if ( basename(fname)=="CVS" ||
314
search(fname, ".")==0 ||
315
search(fname, "#")>= 0 )
318
if ( fsystem->stat(fname)->isdir() )
322
werror("\r%s", " "*79); //clear the line
323
werror("\rUploading: (%s)/%s", path, basename(fname));
324
f = open_file(fname, vars->fs);
326
upload_file(cont, basename(fname), f);
331
foreach ( dirs, string dir_name ) {
332
upload_directory(cont, dir_name, vars);
337
//! upload the package on the server
338
//! And run update routines...
339
void upload_package(mapping vars)
343
write("Register package...\n");
344
_docfactory = send_cmd(0, "get_factory", CLASS_DOCUMENT);
345
if ( !objectp(_docfactory) )
346
throw(({"Document Factory not found in server !"}));
347
_contfactory = send_cmd(0, "get_factory", CLASS_CONTAINER);
348
if ( !objectp(_contfactory) )
349
throw( ({ "Container factory not found inside server !" }));
350
_filepath = send_cmd(0, "get_module", "filepath:tree");
351
if ( !objectp(_filepath) )
352
error("Unable to find filepath on server !");
353
object _rootroom = send_cmd(_filepath, "path_to_object", "/");
354
object dest = send_cmd(_filepath, "path_to_object", vars->dest);
355
if ( !objectp(dest) ) {
356
dest = send_cmd(_filepath, "make_directory",vars->dest);
360
array files = fsystem->get_dir("/files");
361
if ( !arrayp(files) || sizeof(files) == 0 ) {
362
files = fsystem->get_dir("files");
363
if ( !arrayp(files) || sizeof(files) == 0 ) {
364
werror("Invalid SPM Archive: Empty files/ Directory !\n");
368
foreach( files, string fname ) {
369
if ( basename(fname) == "CVS" )
372
object stat = fsystem->stat(fname);
373
if ( stat->isdir() ) {
374
upload_directory(dest, "/"+fname, vars);
377
f = Filesystem.Tar(vars->fs)->open(fname,"r");
378
upload_file(dest, basename(fname), f);
382
// now some script needs to be called on the server...
383
// they first have to be uploaded from the package/ directory
384
// and will be installed in the package/ directory of the
386
array additional = ({ });
387
files = get_directory("/package");
388
object pdir = send_cmd(_rootroom, "get_object_byname", "packages");
389
if ( !objectp(pdir) ) {
390
werror("Creating Packages Folder !\n");
391
pdir = create_folder("/", "packages");
397
foreach ( files, string package ) {
398
object file = Filesystem.Tar(vars->fs)->open(package,"r");
399
object o = upload_file(pdir, basename(package), file);
401
exe = send_cmd(o, "get_instance");
402
if ( !objectp(exe) ) {
403
exe = send_cmd(o, "execute", ([ "name":vars->package,]));
404
if ( !objectp(exe) ) {
405
array errlist = o->get_errors();
406
werror("Failed to install package main component. Errors are:\n"+
407
(arrayp(errlist) ? (errlist * "\n") : "No errors can be found ?!")+"\n");
410
send_cmd(exe, "move", pdir);
411
werror("\n\nSetting up " + package + "\n");
412
// return additional files
413
send_cmd(exe, "set_attribute", ({ "package:components", mFiles }));
415
additional = send_cmd(exe, "spm_install_package");
418
werror("\n\nFound previous package Version=" +
419
send_cmd(exe, "get_version") + "\n");
422
werror("Upgrading Installation...\n");
423
// return additional files
424
additional = send_cmd(exe, "spm_upgrade_package");
425
send_cmd(exe, "check_package_integrity");
427
// check if module is registered
428
werror("\nChecking Registration: "+ vars->package+"...");
430
object reg = send_cmd(0, "get_module", vars->package);
431
if ( !objectp(reg) ) {
432
werror("registered.\n");
433
send_cmd(1, "register_module", ({ vars->package, exe }));
438
if ( !objectp(exe) ) {
439
werror("Unable to find package main component !\nFILE DUMP=%O\n", files);
442
if(arrayp(additional))
443
foreach(additional, object installed ) {
444
if ( !objectp(installed) ) {
445
werror("Error on Installation: Script failed to upgrade.\n");
448
string installedP = send_cmd(_filepath,"object_to_filename",installed);
449
mFiles[installedP] = installed;
450
werror("Installed Script = " + installedP + "\n");
453
werror("Reading XML Object Descriptions !\n");
454
// finally do the xml settings
455
foreach(indices(mFiles), string comp) {
456
object_to_server(mFiles[comp]);
458
// after loading all xml descriptions ...
459
foreach(indices(mFiles), string comp) {
460
if ( mFiles[comp]->get_object_class() & CLASS_DOCXSL &&
461
objectp(mFiles[comp+".xml"]) )
462
catch(send_cmd(mFiles[comp], "load_xml_structure"));
467
werror("Skipping XML on UPGRADING\n");
469
send_cmd(exe, "set_attribute", ({ "package:components", mFiles }));
474
werror("Configure Web Package!\n");
475
object _filepath = send_cmd(0, "get_module", "filepath:tree");
476
object web = send_cmd(_filepath, "path_to_object", "/packages/package:web");
477
mFiles = send_cmd(web, "query_attribute", "package:components");
478
foreach(indices(mFiles), string comp) {
479
object_to_server(mFiles[comp]);
483
array(object) list_packages(int|void quiet)
485
_filepath = send_cmd(0, "get_module", "filepath:tree");
486
object _packages = send_cmd(_filepath, "path_to_object",
487
"/home/admin/packages");
488
array packages = send_cmd(_packages, "get_inventory");
490
werror("List of Packages on sTeam server:\n");
491
foreach(packages, object pck) {
492
werror(" " + pck->get_identifier() + ":\t"+
493
send_cmd(pck, "query_attribute", OBJ_DESC)+"\n");
499
string get_package(string pck_name)
501
array(object) packages = list_packages(1);
502
foreach ( packages, object pck )
503
if ( pck->get_identifier() == pck_name ) {
504
werror("Retrieving "+pck_name+"\n");
505
return send_cmd(pck, "get_content");
510
void set_fsystem(object fs, mapping vars)
519
int start(string server, int port, string user, string pw)
521
int start_time = time();
523
werror("Connecting to sTeam server...\n");
524
while ( !connect_server(server, port) ) {
525
if ( time() - start_time > 120 ) {
526
throw (({" Couldn't connect to server. Please check steam.log for details! \n", backtrace()}));
528
werror("Failed to connect... still trying ... (server running ?)\n");
533
if ( !stringp(login(user, pw,CLIENT_STATUS_CONNECTED)))
534
throw(({"Wrong Password !", backtrace()}));
537
werror("Error on installation: \n"+sprintf("%O", err));
538
throw(({"Wrong Password !", backtrace()}) );
541
mVariables["filepath:tree"] = send_cmd(0, "get_module", "filepath:tree");