~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to spm/connection.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
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.
7
 
 *
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.
12
 
 *
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
16
 
 * 
17
 
 * $Id: connection.pike,v 1.9 2006/07/01 02:14:50 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: connection.pike,v 1.9 2006/07/01 02:14:50 exodusd Exp $";
21
 
 
22
 
 
23
 
/* 
24
 
 * installation script
25
 
 * the pike call will include the pathnames to the server location
26
 
 */ 
27
 
inherit "client_base";
28
 
inherit "base/xml_parser";
29
 
 
30
 
#include <coal.h>
31
 
#include <client.h>
32
 
#include <classes.h>
33
 
#include <attributes.h>
34
 
 
35
 
//#define CHECK_ONLY
36
 
 
37
 
static object fsystem; // the filesystem to use
38
 
static string _fs;
39
 
 
40
 
static int debug_output = 0;
41
 
 
42
 
private static mapping mFiles = ([ ]);
43
 
object _docfactory, _contfactory, _filepath;
44
 
int iInstall, iUpdate, iError;
45
 
 
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] );
50
 
  }
51
 
  else werror( "Error: %O\n", err );
52
 
 
53
 
  if ( debug_output ) {
54
 
    werror( "Debug output:\n%O\n", err );
55
 
    throw( ({ "", backtrace() }) );
56
 
  }
57
 
  exit(1);
58
 
}
59
 
 
60
 
object open_file(string fname, string fs)
61
 
{
62
 
    return Filesystem.Tar(fs)->open(fname, "r");
63
 
}
64
 
 
65
 
array(string) get_directory(string dname)
66
 
{
67
 
    return fsystem->get_dir(dname);
68
 
}
69
 
 
70
 
void set_debug ( int debug )
71
 
{
72
 
  debug_output = debug;
73
 
}
74
 
 
75
 
int get_debug ()
76
 
{
77
 
  return debug_output;
78
 
}
79
 
 
80
 
/*****************************************************************************
81
 
 * XML stuff
82
 
 */
83
 
mapping xmlMap(NodeXML n)
84
 
{
85
 
  mapping res = ([ ]);
86
 
  if ( !objectp(n) )
87
 
    return res;
88
 
 
89
 
  foreach ( n->children, NodeXML children) {
90
 
    if ( children->name == "member" ) {
91
 
      mixed key,value;
92
 
      foreach(children->children, object o) {
93
 
 
94
 
        if ( o->name == "key" )
95
 
          key = unserialize(o->children[0]);
96
 
        else if ( o->name == "value" )
97
 
          value = unserialize(o->children[0]);
98
 
      }
99
 
      res[key] = value;
100
 
    }
101
 
  }
102
 
  return res;
103
 
}
104
 
 
105
 
array xmlArray(NodeXML n)
106
 
{
107
 
  array res = ({ });
108
 
  foreach ( n->children, NodeXML children) {
109
 
    res += ({ unserialize(children) });
110
 
  }
111
 
  return res;
112
 
}
113
 
 
114
 
mixed unserialize(NodeXML n) 
115
 
{
116
 
  switch ( n->name ) {
117
 
  case "struct":
118
 
    return xmlMap(n);
119
 
    break;
120
 
  case "array":
121
 
    return xmlArray(n);
122
 
    break;
123
 
  case "int":
124
 
    return (int)n->data;
125
 
    break;
126
 
  case "float":
127
 
    return (float)n->data;
128
 
    break;
129
 
  case "string":
130
 
    return n->data;
131
 
    break;
132
 
  case "object":
133
 
    string type = n->children[0]->data;
134
 
    string id = n->children[1]->data;
135
 
    object obj;
136
 
    int    oid;
137
 
 
138
 
    switch(type) {
139
 
    case "Group":
140
 
      oid = set_object(mVariables["groups"]);
141
 
      break;
142
 
    case "User":
143
 
      oid = set_object(mVariables["users"]);
144
 
      break;
145
 
    case "Module":
146
 
      oid = set_object(0);
147
 
      obj = send_command(COAL_COMMAND, ({ "get_module", ({ id }) }));
148
 
      set_object(oid);
149
 
      return obj;
150
 
      break;
151
 
    default:
152
 
 
153
 
        
154
 
      oid = set_object(mVariables["filepath:tree"]);
155
 
      obj = send_command(COAL_COMMAND, ({ "path_to_object", ({ id }) }));
156
 
      set_object(oid);
157
 
      return obj;
158
 
      break;
159
 
    }
160
 
 
161
 
    obj = send_command(COAL_COMMAND, ({ "lookup", ({ id }) }));
162
 
    set_object(oid);
163
 
    return obj;
164
 
  }
165
 
  return 0;
166
 
}
167
 
 
168
 
void save_xml(NodeXML n, object obj)
169
 
{
170
 
  set_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"));
177
 
 
178
 
 
179
 
  foreach(indices(sanction), object sanc) {
180
 
      if ( objectp(sanc) ) {
181
 
          send_command(COAL_COMMAND, ({ "sanction_object", 
182
 
                                            ({ sanc, sanction[sanc] }) }) );
183
 
      }
184
 
  }
185
 
  foreach(indices(msanction), object msanc) {
186
 
      if ( objectp(msanc) ) {
187
 
          send_command(COAL_COMMAND, ({ "sanction_object_meta", 
188
 
                                            ({ msanc, msanction[msanc] }) }) );
189
 
      }
190
 
  }
191
 
  mixed key;
192
 
 
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", 
197
 
                                    ({ key, 1 }) }));
198
 
    else if ( !intp(a_acquire[key]) )
199
 
      send_command(COAL_COMMAND, ({ "set_acquire_attribute", 
200
 
                                    ({ key, a_acquire[key] }) }));
201
 
  }
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 }) }));
206
 
      }
207
 
  }
208
 
 
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 }) }));
213
 
 
214
 
}
215
 
 
216
 
  
217
 
void object_to_server(object obj)
218
 
{
219
 
  string path = "xml";
220
 
 
221
 
  if ( obj->get_object_class() & CLASS_USER )
222
 
    return;
223
 
 
224
 
  
225
 
  if ( !objectp(_filepath) )
226
 
  _filepath = send_cmd(0, "get_module", "filepath:tree");
227
 
 
228
 
  object pname = send_cmd(_filepath, "object_to_filename", obj);  
229
 
  path += pname;
230
 
 
231
 
  set_object(obj);
232
 
 
233
 
  werror("\r"+(" "*79));
234
 
  werror("\rreading... " + path+".xml");
235
 
  Stdio.File f;
236
 
  mixed err = catch {
237
 
    f = open_file(path+".xml", _fs);
238
 
  };
239
 
  // if the file does not exist its not part of the reference 
240
 
  // server installation
241
 
  if ( !objectp(f) ) {
242
 
#if 0
243
 
      werror("\r"+path+".xml ... file not found !\n");
244
 
#endif
245
 
    return;
246
 
  }
247
 
  string xml = f->read();
248
 
  f->close();
249
 
  NodeXML n = parse_data(xml);
250
 
  save_xml(n, obj);
251
 
}
252
 
 
253
 
object upload_file(object dir, string fname, object f)
254
 
{
255
 
    if ( search(fname, ".") == 0 || search(fname, "#") >= 0 ) {
256
 
        return 0;
257
 
    }
258
 
    object obj = send_cmd(dir, "get_object_byname", fname);
259
 
    if ( !objectp(obj) ) {
260
 
        werror(" (Created)");
261
 
        iInstall++;
262
 
        obj = send_cmd(_docfactory, "execute", ([ "name": fname, ]));
263
 
        send_cmd(obj, "move", dir);
264
 
    }
265
 
    else
266
 
        iUpdate++;
267
 
    
268
 
    
269
 
    send_cmd(obj, "set_content", f->read());
270
 
 
271
 
    string filename=send_cmd(_filepath, "object_to_filename", obj);
272
 
    mFiles[filename] = obj;
273
 
    return obj;
274
 
}                
275
 
 
276
 
object create_folder(string inCont, string folderName)
277
 
{
278
 
    return send_cmd(_filepath,"make_directory", ({ inCont, folderName }) );
279
 
}
280
 
 
281
 
void upload_directory(object location, string dir, mapping vars)
282
 
{
283
 
    object f, cont;
284
 
    
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");
290
 
        return 0;
291
 
    }
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", 
300
 
               basename(dir));
301
 
#ifdef CHECK_ONLY
302
 
        return;
303
 
#endif
304
 
        cont = create_folder(path, basename(dir));
305
 
    }
306
 
    path = path + (path[-1] !='/' ?"/":"") + basename(dir);
307
 
    mFiles[path] = cont;
308
 
    werror("\r%s", " "*79); //clear the line
309
 
    werror("\rDIRECTORY: %s", path);
310
 
    array dirs = ({ });
311
 
 
312
 
    foreach( files, string fname ) {
313
 
        if ( basename(fname)=="CVS" ||
314
 
             search(fname, ".")==0 ||
315
 
             search(fname, "#")>= 0 )
316
 
            continue;
317
 
 
318
 
        if ( fsystem->stat(fname)->isdir() )
319
 
            dirs += ({ fname });
320
 
        else
321
 
        {
322
 
            werror("\r%s", " "*79); //clear the line
323
 
            werror("\rUploading: (%s)/%s", path, basename(fname));
324
 
            f = open_file(fname, vars->fs);
325
 
#ifndef CHECK_ONLY
326
 
            upload_file(cont, basename(fname), f);
327
 
#endif
328
 
            f->close();
329
 
        }
330
 
    }
331
 
    foreach ( dirs, string dir_name ) {
332
 
        upload_directory(cont, dir_name, vars);
333
 
    }
334
 
}
335
 
 
336
 
 
337
 
//! upload the package on the server
338
 
//! And run update routines...
339
 
void upload_package(mapping vars)
340
 
{
341
 
 
342
 
    object f;
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);
357
 
    }
358
 
 
359
 
    
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");
365
 
        return;
366
 
      }
367
 
    }
368
 
    foreach( files, string fname ) {
369
 
        if ( basename(fname) == "CVS" )
370
 
            continue;
371
 
       
372
 
        object stat = fsystem->stat(fname);
373
 
        if ( stat->isdir() ) {
374
 
          upload_directory(dest, "/"+fname, vars);
375
 
        }
376
 
        else {
377
 
            f = Filesystem.Tar(vars->fs)->open(fname,"r");
378
 
            upload_file(dest, basename(fname), f); 
379
 
            f->close();
380
 
        }
381
 
    }
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 
385
 
    // server
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");
392
 
    }
393
 
    
394
 
    object exe;
395
 
    int isUpgrade = 0;
396
 
    
397
 
    foreach ( files, string package ) {
398
 
        object file = Filesystem.Tar(vars->fs)->open(package,"r");
399
 
        object o = upload_file(pdir, basename(package), file);
400
 
        file->close();
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");
408
 
              return;
409
 
            }
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 }));
414
 
            
415
 
            additional = send_cmd(exe, "spm_install_package");
416
 
        }
417
 
        else {
418
 
            werror("\n\nFound previous package Version=" +
419
 
                   send_cmd(exe, "get_version") + "\n");
420
 
            // upgrade!
421
 
            isUpgrade = 1;
422
 
            werror("Upgrading Installation...\n");
423
 
            // return additional files
424
 
            additional = send_cmd(exe, "spm_upgrade_package");
425
 
            send_cmd(exe, "check_package_integrity");
426
 
        }
427
 
        // check if module is registered
428
 
        werror("\nChecking Registration: "+ vars->package+"...");
429
 
 
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 }));
434
 
        }
435
 
        else
436
 
            werror("found.\n");
437
 
    }
438
 
    if ( !objectp(exe) ) {
439
 
      werror("Unable to find package main component !\nFILE DUMP=%O\n", files);
440
 
      return;
441
 
    }
442
 
    if(arrayp(additional))
443
 
      foreach(additional, object installed ) {
444
 
        if ( !objectp(installed) ) {
445
 
            werror("Error on Installation: Script failed to upgrade.\n");
446
 
            continue;
447
 
        }
448
 
        string installedP = send_cmd(_filepath,"object_to_filename",installed);
449
 
        mFiles[installedP] = installed;
450
 
        werror("Installed Script = " + installedP + "\n");
451
 
      }
452
 
    if ( !isUpgrade ) {
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]);
457
 
      }
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"));
463
 
      }
464
 
 
465
 
    }
466
 
    else
467
 
      werror("Skipping XML on UPGRADING\n");
468
 
 
469
 
    send_cmd(exe, "set_attribute", ({ "package:components", mFiles }));
470
 
}
471
 
 
472
 
void configure_web()
473
 
{
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]);
480
 
  }
481
 
}
482
 
 
483
 
array(object) list_packages(int|void quiet)
484
 
{
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");
489
 
    if ( quiet != 1 ) {
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");
494
 
        }
495
 
    }
496
 
    return packages;
497
 
}
498
 
 
499
 
string get_package(string pck_name)
500
 
{
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");
506
 
        }
507
 
    return 0;
508
 
}
509
 
 
510
 
void set_fsystem(object fs, mapping vars)
511
 
{
512
 
    iInstall = 0;
513
 
    iUpdate  = 0;
514
 
    iError   = 0;
515
 
    fsystem  = fs;
516
 
    _fs = vars->fs;
517
 
}
518
 
 
519
 
int start(string server, int port, string user, string pw)
520
 
{
521
 
    int start_time = time();
522
 
 
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()}));
527
 
        }
528
 
        werror("Failed to connect... still trying ... (server running ?)\n");
529
 
        sleep(10);
530
 
    }
531
 
    
532
 
    mixed err = catch {
533
 
     if ( !stringp(login(user, pw,CLIENT_STATUS_CONNECTED)))
534
 
          throw(({"Wrong Password !", backtrace()}));
535
 
    };
536
 
    if ( err != 0 ) {
537
 
        werror("Error on installation: \n"+sprintf("%O", err));
538
 
        throw(({"Wrong Password !", backtrace()}) );
539
 
        return 0;
540
 
    } 
541
 
    mVariables["filepath:tree"] = send_cmd(0, "get_module", "filepath:tree");
542
 
    return 1;
543
 
}
544
 
 
545
 
 
546