clarified 'failed to authenticate' message.
[myplc.git] / db-config
1 #!/usr/bin/env /usr/bin/plcsh
2 #
3 # Bootstraps the PLC database with a default administrator account and
4 # a default site, defines default slice attribute types, and
5 # creates/updates default system slices.
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id$
11 # $HeadURL$
12
13 from plc_config import PLCConfiguration
14 import sys
15
16 def main():
17     cfg = PLCConfiguration()
18     cfg.load()
19     variables = cfg.variables()
20
21     # Load variables into dictionaries
22     for category_id, (category, variablelist) in variables.iteritems():
23         globals()[category_id] = dict(zip(variablelist.keys(),
24                                        [variable['value'] for variable in variablelist.values()]))
25
26     # Create/update the default administrator account (should be
27     # person_id 2).
28     admin = { 'person_id': 2,
29               'first_name': "Default",
30               'last_name': "Administrator",
31               'email': plc['root_user'],
32               'password': plc['root_password'] }
33     persons = GetPersons([admin['person_id']])
34     if not persons:
35         person_id = AddPerson(admin)
36         if person_id != admin['person_id']:
37             # Huh? Someone deleted the account manually from the database.
38             DeletePerson(person_id)
39             raise Exception, "Someone deleted the \"%s %s\" account from the database!" % \
40                   (admin['first_name'], admin['last_name'])
41         UpdatePerson(person_id, { 'enabled': True })
42     else:
43         person_id = persons[0]['person_id']
44         UpdatePerson(person_id, admin)
45
46     # Create/update the default site (should be site_id 1)
47     if plc_www['port'] == '80':
48         url = "http://" + plc_www['host'] + "/"
49     elif plc_www['port'] == '443':
50         url = "https://" + plc_www['host'] + "/"
51     else:
52         url = "http://" + plc_www['host'] + ":" + plc_www['port'] + "/"
53     site = { 'site_id': 1,
54              'name': plc['name'] + " Central",
55              'abbreviated_name': plc['name'],
56              'login_base': plc['slice_prefix'],
57              'is_public': False,
58              'url': url,
59              'max_slices': 100 }
60
61     sites = GetSites([site['site_id']])
62     if not sites:
63         site_id = AddSite(site['name'], site['abbreviated_name'], site['login_base'], site)
64         if site_id != site['site_id']:
65             DeleteSite(site_id)
66             raise Exception, "Someone deleted the \"%s\" site from the database!" % \
67                   site['name']
68         sites = [site]
69
70     # Must call UpdateSite() even after AddSite() to update max_slices
71     site_id = sites[0]['site_id']
72     UpdateSite(site_id, site)
73
74     # The default administrator account must be associated with a site
75     # in order to login.
76     AddPersonToSite(admin['person_id'], site['site_id'])
77     SetPersonPrimarySite(admin['person_id'], site['site_id'])
78
79     # Grant admin and PI roles to the default administrator account
80     AddRoleToPerson(10, admin['person_id'])
81     AddRoleToPerson(20, admin['person_id'])
82
83     # Setup default PlanetLabConf entries
84     default_conf_files = [
85         # NTP configuration
86         {'enabled': True,
87          'source': 'PlanetLabConf/ntp.conf.php',
88          'dest': '/etc/ntp.conf',
89          'file_permissions': '644',
90          'file_owner': 'root',
91          'file_group': 'root',
92          'preinstall_cmd': '',
93          'postinstall_cmd': '/etc/rc.d/init.d/ntpd restart',
94          'error_cmd': '',
95          'ignore_cmd_errors': False,
96          'always_update': False},
97         {'enabled': True,
98          'source': 'PlanetLabConf/ntp/step-tickers.php',
99          'dest': '/etc/ntp/step-tickers',
100          'file_permissions': '644',
101          'file_owner': 'root',
102          'file_group': 'root',
103          'preinstall_cmd': '',
104          'postinstall_cmd': '/etc/rc.d/init.d/ntpd restart',
105          'error_cmd': '',
106          'ignore_cmd_errors': False,
107          'always_update': False},
108
109         # SSH server configuration
110         {'enabled': True,
111          'source': 'PlanetLabConf/sshd_config',
112          'dest': '/etc/ssh/sshd_config',
113          'file_permissions': '600',
114          'file_owner': 'root',
115          'file_group': 'root',
116          'preinstall_cmd': '',
117          'postinstall_cmd': '/etc/init.d/sshd restart',
118          'error_cmd': '',
119          'ignore_cmd_errors': False,
120          'always_update': False},
121
122         # Administrative SSH keys
123         {'enabled': True,
124          'source': 'PlanetLabConf/keys.php?root',
125          'dest': '/root/.ssh/authorized_keys',
126          'file_permissions': '644',
127          'file_owner': 'root',
128          'file_group': 'root',
129          'preinstall_cmd': '',
130          'postinstall_cmd': '/bin/chmod 700 /root/.ssh',
131          'error_cmd': '',
132          'ignore_cmd_errors': False,
133          'always_update': False},
134         {'enabled': True,
135          'source': 'PlanetLabConf/keys.php?site_admin',
136          'dest': '/home/site_admin/.ssh/authorized_keys',
137          'file_permissions': '644',
138          'file_owner': 'site_admin',
139          'file_group': 'site_admin',
140          'preinstall_cmd': 'grep -q site_admin /etc/passwd',
141          'postinstall_cmd': '/bin/chmod 700 /home/site_admin/.ssh',
142          'error_cmd': '',
143          'ignore_cmd_errors': False,
144          'always_update': False},
145         # Log rotation configuration
146         {'enabled': True,
147          'source': 'PlanetLabConf/logrotate.conf',
148          'dest': '/etc/logrotate.conf',
149          'file_permissions': '644',
150          'file_owner': 'root',
151          'file_group': 'root',
152          'preinstall_cmd': '',
153          'postinstall_cmd': '',
154          'error_cmd': '',
155          'ignore_cmd_errors': False,
156          'always_update': False},
157
158         # updatedb/locate nightly cron job
159         {'enabled': True,
160          'source': 'PlanetLabConf/slocate.cron',
161          'dest': '/etc/cron.daily/slocate.cron',
162          'file_permissions': '755',
163          'file_owner': 'root',
164          'file_group': 'root',
165          'preinstall_cmd': '',
166          'postinstall_cmd': '',
167          'error_cmd': '',
168          'ignore_cmd_errors': False,
169          'always_update': False},
170
171         # YUM configuration
172         {'enabled': True,
173          'source': 'PlanetLabConf/f8/yum.conf',
174          'dest': '/etc/yum.conf',
175          'file_permissions': '644', 'file_owner': 'root', 'file_group': 'root',
176          'preinstall_cmd': '', 'postinstall_cmd': '', 'error_cmd': '',
177          'ignore_cmd_errors': False,
178          'always_update': False},
179         {'enabled': True,
180          'source': 'PlanetLabConf/myplc.repo.php?gpgcheck=1',
181          'dest': '/etc/yum.myplc.d/myplc.repo',
182          'file_permissions': '644', 'file_owner': 'root', 'file_group': 'root',
183          'preinstall_cmd': '', 'postinstall_cmd': '', 'error_cmd': '',
184          'ignore_cmd_errors': False,
185          'always_update': False},
186         {'enabled': True,
187          'source': 'PlanetLabConf/f8/yum.myplc.d/fedora.repo',
188          'dest': '/etc/yum.myplc.d/fedora.repo',
189          'file_permissions': '644', 'file_owner': 'root', 'file_group': 'root',
190          'preinstall_cmd': '', 'postinstall_cmd': '', 'error_cmd': '',
191          'ignore_cmd_errors': False,
192          'always_update': False},
193         {'enabled': True,
194          'source': 'PlanetLabConf/f8/yum.myplc.d/fedora-updates.repo',
195          'dest': '/etc/yum.myplc.d/fedora-updates.repo',
196          'file_permissions': '644', 'file_owner': 'root', 'file_group': 'root',
197          'preinstall_cmd': '', 'postinstall_cmd': '', 'error_cmd': '',
198          'ignore_cmd_errors': False,
199          'always_update': False},
200
201         {'enabled': True,
202          'source': 'PlanetLabConf/delete-rpm-list-production',
203          'dest': '/etc/planetlab/delete-rpm-list',
204          'file_permissions': '644',
205          'file_owner': 'root',
206          'file_group': 'root',
207          'preinstall_cmd': '',
208          'postinstall_cmd': '',
209          'error_cmd': '',
210          'ignore_cmd_errors': False,
211          'always_update': False},
212
213         # PLC configuration
214         {'enabled': True,
215          'source': 'PlanetLabConf/get_plc_config.php',
216          'dest': '/etc/planetlab/plc_config',
217          'file_permissions': '644',
218          'file_owner': 'root',
219          'file_group': 'root',
220          'preinstall_cmd': '',
221          'postinstall_cmd': '',
222          'error_cmd': '',
223          'ignore_cmd_errors': False,
224          'always_update': False},
225         {'enabled': True,
226          'source': 'PlanetLabConf/get_plc_config.php?python',
227          'dest': '/etc/planetlab/plc_config.py',
228          'file_permissions': '644',
229          'file_owner': 'root',
230          'file_group': 'root',
231          'preinstall_cmd': '',
232          'postinstall_cmd': '',
233          'error_cmd': '',
234          'ignore_cmd_errors': False,
235          'always_update': False},
236         {'enabled': True,
237          'source': 'PlanetLabConf/get_plc_config.php?perl',
238          'dest': '/etc/planetlab/plc_config.pl',
239          'file_permissions': '644',
240          'file_owner': 'root',
241          'file_group': 'root',
242          'preinstall_cmd': '',
243          'postinstall_cmd': '',
244          'error_cmd': '',
245          'ignore_cmd_errors': False,
246          'always_update': False},
247         {'enabled': True,
248          'source': 'PlanetLabConf/get_plc_config.php?php',
249          'dest': '/etc/planetlab/php/plc_config.php',
250          'file_permissions': '644',
251          'file_owner': 'root',
252          'file_group': 'root',
253          'preinstall_cmd': '',
254          'postinstall_cmd': '',
255          'error_cmd': '',
256          'ignore_cmd_errors': False,
257          'always_update': False},
258
259         # XXX Required for old Node Manager
260         # Proper configuration
261         {'enabled': True,
262          'source': 'PlanetLabConf/propd.conf',
263          'dest': '/etc/proper/propd.conf',
264          'file_permissions': '644',
265          'file_owner': 'root',
266          'file_group': 'root',
267          'preinstall_cmd': '',
268          'postinstall_cmd': '/etc/init.d/proper restart',
269          'error_cmd': '',
270          'ignore_cmd_errors': True,
271          'always_update': False},
272
273         # XXX Required for old Node Manager
274         # Bandwidth cap
275         {'enabled': True,
276          'source': 'PlanetLabConf/bwlimit.php',
277          'dest': '/etc/planetlab/bwcap',
278          'file_permissions': '644',
279          'file_owner': 'root',
280          'file_group': 'root',
281          'preinstall_cmd': '',
282          'postinstall_cmd': '',
283          'error_cmd': '',
284          'ignore_cmd_errors': True,
285          'always_update': False},
286
287         # Proxy ARP setup
288         {'enabled': True,
289          'source': 'PlanetLabConf/proxies.php',
290          'dest': '/etc/planetlab/proxies',
291          'file_permissions': '644',
292          'file_owner': 'root',
293          'file_group': 'root',
294          'preinstall_cmd': '',
295          'postinstall_cmd': '',
296          'error_cmd': '',
297          'ignore_cmd_errors': False,
298          'always_update': False},
299
300         # Firewall configuration
301         {'enabled': True,
302          'source': 'PlanetLabConf/blacklist.php',
303          'dest': '/etc/planetlab/blacklist',
304          'file_permissions': '600',
305          'file_owner': 'root',
306          'file_group': 'root',
307          'preinstall_cmd': '',
308          'postinstall_cmd': '/sbin/iptables-restore --noflush < /etc/planetlab/blacklist',
309          'error_cmd': '',
310          'ignore_cmd_errors': True,
311          'always_update': False},
312
313         # /etc/issue
314         {'enabled': True,
315          'source': 'PlanetLabConf/issue.php',
316          'dest': '/etc/issue',
317          'file_permissions': '644',
318          'file_owner': 'root',
319          'file_group': 'root',
320          'preinstall_cmd': '',
321          'postinstall_cmd': '',
322          'error_cmd': '',
323          'ignore_cmd_errors': False,
324          'always_update': False},
325
326         # Kernel parameters
327         {'enabled': True,
328          'source': 'PlanetLabConf/sysctl.php',
329          'dest': '/etc/sysctl.conf',
330          'file_permissions': '644',
331          'file_owner': 'root',
332          'file_group': 'root',
333          'preinstall_cmd': '',
334          'postinstall_cmd': '/sbin/sysctl -e -p /etc/sysctl.conf',
335          'error_cmd': '',
336          'ignore_cmd_errors': False,
337          'always_update': False},
338
339         # Sendmail configuration
340         {'enabled': True,
341          'source': 'PlanetLabConf/sendmail.mc',
342          'dest': '/etc/mail/sendmail.mc',
343          'file_permissions': '644',
344          'file_owner': 'root',
345          'file_group': 'root',
346          'preinstall_cmd': '',
347          'postinstall_cmd': '',
348          'error_cmd': '',
349          'ignore_cmd_errors': False,
350          'always_update': False},
351         {'enabled': True,
352          'source': 'PlanetLabConf/sendmail.cf',
353          'dest': '/etc/mail/sendmail.cf',
354          'file_permissions': '644',
355          'file_owner': 'root',
356          'file_group': 'root',
357          'preinstall_cmd': '',
358          'postinstall_cmd': 'service sendmail restart',
359          'error_cmd': '',
360          'ignore_cmd_errors': False,
361          'always_update': False},
362
363         # GPG signing keys
364         {'enabled': True,
365          'source': 'PlanetLabConf/RPM-GPG-KEY-fedora',
366          'dest': '/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora',
367          'file_permissions': '644',
368          'file_owner': 'root',
369          'file_group': 'root',
370          'preinstall_cmd': '',
371          'postinstall_cmd': 'rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora',
372          'error_cmd': '',
373          'ignore_cmd_errors': False,
374          'always_update': False},
375         {'enabled': True,
376          'source': 'PlanetLabConf/get_gpg_key.php',
377          'dest': '/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab',
378          'file_permissions': '644',
379          'file_owner': 'root',
380          'file_group': 'root',
381          'preinstall_cmd': '',
382          'postinstall_cmd': 'rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab',
383          'error_cmd': '',
384          'ignore_cmd_errors': False,
385          'always_update': False},
386
387         # Ping of death configuration
388         # the 'restart' postcommand doesn't work, b/c the pod script doesn't support it.
389         {'enabled': True,
390          'source': 'PlanetLabConf/ipod.conf.php',
391          'dest': '/etc/ipod.conf',
392          'file_permissions': '644',
393          'file_owner': 'root',
394          'file_group': 'root',
395          'preinstall_cmd': '',
396          'postinstall_cmd': '/etc/init.d/pod start',
397          'error_cmd': '',
398          'ignore_cmd_errors': False,
399          'always_update': False},
400
401         # sudo configuration
402         {'enabled': True,
403          'source': 'PlanetLabConf/sudoers.php',
404          'dest': '/etc/sudoers',
405          'file_permissions': '440',
406          'file_owner': 'root',
407          'file_group': 'root',
408          'preinstall_cmd': '',
409          'postinstall_cmd': '/usr/sbin/visudo -c',
410          'error_cmd': '',
411          'ignore_cmd_errors': False,
412          'always_update': False}
413         ]
414
415     # Get list of existing (enabled, global) files
416     conf_files = GetConfFiles()
417     conf_files = filter(lambda conf_file: conf_file['enabled'] and \
418                                           not conf_file['node_ids'] and \
419                                           not conf_file['nodegroup_ids'],
420                         conf_files)
421     dests = [conf_file['dest'] for conf_file in conf_files]
422     conf_files = dict(zip(dests, conf_files))
423
424     # Create/update default PlanetLabConf entries
425     for default_conf_file in default_conf_files:
426         if default_conf_file['dest'] not in dests:
427             AddConfFile(default_conf_file)
428         else:
429             conf_file = conf_files[default_conf_file['dest']]
430             UpdateConfFile(conf_file['conf_file_id'], default_conf_file)
431
432     # Setup default slice attribute types
433     default_attribute_types = [
434         # Slice type (only vserver is supported)
435         {'name': "type",
436          'description': "Type of slice (e.g. vserver)",
437          'min_role_id': 20},
438
439         # System slice
440         {'name': "system",
441          'description': "Is a default system slice (1) or not (0 or unset)",
442          'min_role_id': 10},
443
444         # Slice enabled (1) or suspended (0)
445         {'name': "enabled",
446          'description': "Slice enabled (1 or unset) or suspended (0)",
447          'min_role_id': 10},
448
449         # Slice reference image
450         {'name': "vref",
451          'description': "Reference image",
452          'min_role_id': 30},
453
454         # Slice initialization script
455         {'name': "initscript",
456          'description': "Slice initialization script",
457          'min_role_id': 10},
458
459         # CPU share
460         {'name': "cpu_pct",
461          'description': "Reserved CPU percent",
462          'min_role_id': 10},
463         {'name': "cpu_share",
464          'description': "Number of CPU shares",
465          'min_role_id': 10},
466
467         # Bandwidth limits
468         {'name': "net_min_rate",
469          'description': "Minimum bandwidth (kbps)",
470          'min_role_id': 10},
471         {'name': "net_max_rate",
472          'description': "Maximum bandwidth (kbps)",
473          'min_role_id': 10},
474         {'name': "net_i2_min_rate",
475          'description': "Minimum bandwidth over I2 routes (kbps)",
476          'min_role_id': 10},
477         {'name': "net_i2_max_rate",
478          'description': "Maximum bandwidth over I2 routes (kbps)",
479          'min_role_id': 10},
480         {'name': "net_max_kbyte",
481          'description': "Maximum daily network Tx KByte limit.",
482          'min_role_id': 10},
483         {'name': "net_thresh_kbyte",
484          'description': "KByte limit before warning and throttling.",
485          'min_role_id': 10},
486         {'name': "net_i2_max_kbyte",
487          'description': "Maximum daily network Tx KByte limit to I2 hosts.",
488          'min_role_id': 10},
489         {'name': "net_i2_thresh_kbyte",
490          'description': "KByte limit to I2 hosts before warning and throttling.",
491          'min_role_id': 10},
492         {'name': "net_share",
493          'description': "Number of bandwidth shares",
494          'min_role_id': 10},
495         {'name': "net_i2_share",
496          'description': "Number of bandwidth shares over I2 routes",
497          'min_role_id': 10},
498  
499         # Disk quota
500         {'name': "disk_max",
501          'description': "Disk quota (1k disk blocks)",
502          'min_role_id': 10},
503
504         # Proper operations
505         {'name': "proper_op",
506          'description': "Proper operation (e.g. bind_socket)",
507          'min_role_id': 10},
508
509         # VServer capabilities 
510         {'name': "capabilities",
511          'description': "VServer bcapabilities (separate by commas)",
512          'min_role_id': 10},
513
514                 # Vsys
515         {'name': "vsys",
516          'description': "Bind vsys script fd's to a slice's vsys directory.",
517          'min_role_id': 10},
518
519         # CoDemux
520         {'name': "codemux",
521          'description': "Demux HTTP between slices using localhost ports. Value in the form 'host, localhost port'.",
522          'min_role_id': 10},
523
524         ]
525
526     # Get list of existing attribute types
527     attribute_types = GetSliceAttributeTypes()
528     attribute_types = [attribute_type['name'] for attribute_type in attribute_types]
529
530     # Create/update default slice attribute types
531     for default_attribute_type in default_attribute_types:
532         if default_attribute_type['name'] not in attribute_types:
533             AddSliceAttributeType(default_attribute_type)
534         else:
535             UpdateSliceAttributeType(default_attribute_type['name'], default_attribute_type)
536
537     # Default Initscripts
538     default_initscripts = []
539
540     # Find initscripts and add them to the db
541     for (root, dirs, files) in os.walk("/etc/plc_sliceinitscripts"):
542         for f in files:
543             # Read the file
544             file = open(root + "/" + f, "ro")
545             default_initscripts.append({"name": plc['slice_prefix'] + "_" + f,
546                                         "enabled": True,
547                                         "script": file.read().replace("@SITE@", url).replace("@PREFIX@", plc['slice_prefix'])})
548             file.close()
549
550     # Get list of existing initscripts
551     oldinitscripts = GetInitScripts()
552     oldinitscripts = [script['name'] for script in oldinitscripts]
553
554     for initscript in default_initscripts:
555         if initscript['name'] not in oldinitscripts:  AddInitScript(initscript)
556
557     # Setup default slice attribute types
558     default_setting_types = [
559
560         {'category' : "general",
561          'name' : "ifname",
562          'description': "Set interface name, instead of eth0 or the like",
563          'min_role_id' : 40},
564         {'category' : "Multihome",
565          'name' : "alias",
566          'description': "Specifies that the network is used for multihoming",
567          'min_role_id' : 40},
568
569         {'category' : "hidden",
570          'name' : "backdoor",
571          'description': "For testing new settings",
572          'min_role_id' : 10},
573         ] + [
574         { "category" : "WiFi",
575           "name" : x,
576           "description" : "802.11 %s -- see %s"%(y,z),
577           "min_role_id" : 40 } for (x,y,z) in [
578             ("mode","Mode","iwconfig"),
579             ("essid","ESSID","iwconfig"),
580             ("nw","Network Id","iwconfig"),
581             ("freq","Frequency","iwconfig"),
582             ("channel","Channel","iwconfig"),
583             ("sens","sensitivity threshold","iwconfig"),
584             ("rate","Rate","iwconfig"),
585             ("key","key","iwconfig key"),
586             ("key1","key1","iwconfig key [1]"),
587             ("key2","key2","iwconfig key [2]"),
588             ("key3","key3","iwconfig key [3]"),
589             ("key4","key4","iwconfig key [4]"),
590             ("securitymode","Security mode","iwconfig enc"),
591             ("iwconfig","Additional parameters to iwconfig","ifup-wireless"),
592             ("iwpriv","Additional parameters to iwpriv","ifup-wireless"),
593             ]
594         ]
595
596
597     # Get list of existing attribute types
598     setting_types = GetNodeNetworkSettingTypes()
599     setting_types = [setting_type['name'] for setting_type in setting_types]
600
601     # Create/update default slice setting types
602     for default_setting_type in default_setting_types:
603         if default_setting_type['name'] not in setting_types:
604             AddNodeNetworkSettingType(default_setting_type)
605         else:
606             UpdateNodeNetworkSettingType(default_setting_type['name'], default_setting_type)
607
608     # Create/update system slices
609     default_slices = [
610          # PlanetFlow
611         {'name': plc['slice_prefix'] + "_netflow",
612          'description': "PlanetFlow Traffic Auditing Service.  Logs, captured in the root context using fprobe-ulogd, are stored in a directory in the root context which is bind mounted to the planetflow slice.  The Planetflow Central service then periodically rsyncs these logs from the planetflow slice for aggregation.",
613          'url': url,
614          'instantiation': "plc-instantiated",
615          # Renew forever (minus one day, work around date conversion weirdness)
616          'expires': 0x7fffffff - (60 * 60 * 24),
617          'attributes': [('system', "1"),
618                         ('vref', "planetflow"),
619                                                 ('vsys', "pfmount")]},
620           # Sirius
621         {'name': plc['slice_prefix'] + "_sirius",
622          'description': 'The Sirius Calendar Service.\n\nSirius provides system-wide reservations of 25% CPU and 2Mb/s outgoing\nbandwidth.  Sign up for hour-long slots using the Web GUI at the\nPlanetLab website.\n\nThis slice should not generate traffic external to PlanetLab.\n',
623          'url': url + "db/sirius/index.php",
624          'instantiation': "plc-instantiated",
625          # Renew forever (minus one day, work around date conversion weirdness)
626          'expires': 0x7fffffff - (60 * 60 * 24),
627          'attributes': [('system', "1"),
628                         ('net_min_rate', "2000"),
629                         ('cpu_pct', "25"),
630                         ('initscript', plc['slice_prefix'] + "_sirius")]}
631         ]
632     
633     for default_slice in default_slices:
634         slices = GetSlices([default_slice['name']])
635         if slices:
636             slice = slices[0]
637             UpdateSlice(slice['slice_id'], default_slice)
638         else:
639             AddSlice(default_slice)
640             slice = GetSlices([default_slice['name']])[0]
641
642         # Create/update all attributes
643         slice_attributes = []
644         if slice['slice_attribute_ids']:
645             # Delete unknown attributes
646             for slice_attribute in GetSliceAttributes(slice['slice_attribute_ids']):
647                 if (slice_attribute['name'], slice_attribute['value']) \
648                    not in default_slice['attributes']:
649                     DeleteSliceAttribute(slice_attribute['slice_attribute_id'])
650                 else:
651                     slice_attributes.append((slice_attribute['name'], slice_attribute['value']))
652
653         for (name, value) in default_slice['attributes']:
654             if (name, value) not in slice_attributes:
655                 AddSliceAttribute(slice['name'], name, value)
656
657     installfailed = """
658 Once the node meets these requirements, please reinitiate the install
659 by visiting:
660
661 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
662
663 Update the BootState to 'Reinstall', then reboot the node.
664
665 If you have already performed this step and are still receiving this
666 message, please reply so that we may investigate the problem.
667 """
668
669     # Load default message templates
670     message_templates = [
671         {'message_id': 'Verify account',
672          'subject': "Verify account registration",
673          'template': """
674 Please verify that you registered for a %(PLC_NAME)s account with the
675 username %(email)s by visiting:
676
677 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/register.php?id=%(person_id)d&key=%(verification_key)s
678
679 If you did not register for a %(PLC_NAME)s account, please ignore this
680 message, or contact %(PLC_NAME)s Support <%(PLC_MAIL_SUPPORT_ADDRESS)s>.
681 """
682          },
683
684         {'message_id': 'New PI account',
685          'subject': "New PI account registration from %(first_name)s %(last_name)s <%(email)s> at %(site_name)s",
686          'template': """
687 %(first_name)s %(last_name)s <%(email)s> has signed up for a new
688 %(PLC_NAME)s account at %(site_name)s and has requested a PI role. PIs
689 are responsible for enabling user accounts, creating slices, and
690 ensuring that all users abide by the %(PLC_NAME)s Acceptable Use
691 Policy.
692
693 Only %(PLC_NAME)s administrators may enable new PI accounts. If you
694 are a PI at %(site_name)s, please respond and indicate whether this
695 registration is acceptable.
696
697 To view the request, visit:
698
699 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/index.php?id=%(person_id)d
700 """
701          },
702
703         {'message_id': 'New account',
704          'subject': "New account registration from %(first_name)s %(last_name)s <%(email)s> at %(site_name)s",
705          'template': """
706 %(first_name)s %(last_name)s <%(email)s> has signed up for a new
707 %(PLC_NAME)s account at %(site_name)s and has requested the following
708 roles: %(roles)s.
709
710 To deny the request or enable the account, visit:
711
712 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/index.php?id=%(person_id)d
713 """
714          },
715
716         {'message_id': 'Password reset requested',
717          'subject': "Password reset requested",
718          'template': """
719 Someone has requested that the password of your %(PLC_NAME)s account
720 %(email)s be reset. If this person was you, you may continue with the
721 reset by visiting:
722
723 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/reset_password.php?id=%(person_id)d&key=%(verification_key)s
724
725 If you did not request that your password be reset, please contact
726 %(PLC_NAME)s Support <%(PLC_MAIL_SUPPORT_ADDRESS)s>. Do not quote or
727 otherwise include any of this text in any correspondence.
728 """
729          },
730
731         {'message_id': 'Password reset',
732          'subject': "Password reset",
733          'template': """
734 The password of your %(PLC_NAME)s account %(email)s has been
735 temporarily reset to:
736
737 %(password)s
738
739 Please change it at as soon as possible by visiting:
740
741 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/index.php?id=%(person_id)d
742
743 If you did not request that your password be reset, please contact
744 %(PLC_NAME)s Support <%(PLC_MAIL_SUPPORT_ADDRESS)s>. Do not quote or
745 otherwise include any of this text in any correspondence.
746 """
747          },
748
749         # Boot Manager messages
750         {'message_id': "installfinished",
751          'subject': "%(hostname)s completed installation",
752          'template': """
753 %(hostname)s just completed installation.
754
755 The node should be usable in a couple of minutes if installation was
756 successful.
757 """
758          },
759
760         {'message_id': "insufficientdisk",
761          'subject': "%(hostname)s does not have sufficient disk space",
762          'template': """
763 %(hostname)s failed to boot because it does not have sufficent disk
764 space, or because its disk controller was not recognized.
765
766 Please replace the current disk or disk controller or install
767 additional disks to meet the current hardware requirements.
768 """ + installfailed
769          },
770
771         {'message_id': "insufficientmemory",
772          'subject': "%(hostname)s does not have sufficient memory",
773          'template': """
774 %(hostname)s failed to boot because it does not have sufficent
775 memory.
776
777 Please install additional memory to meet the current hardware
778 requirements.
779 """ + installfailed
780          },
781
782         {'message_id': "authfail",
783          'subject': "%(hostname)s failed to authenticate",
784          'template':
785 """
786 %(hostname)s failed to authenticate for the following reason:
787
788 %(fault)s
789
790 The most common reason for authentication failure is that the
791 authentication key stored in the node configuration file, does not
792 match the key stored in our database.  These keys must match in order to
793 authenticate the node successfully.
794
795 Each time the configuration file is downloaded, either as part of an All-in-One
796 BootImage or by downloading the plnode.txt file, the authentication key is
797 RECREATED.  So, which ever file was downloaded most recently is the one we
798 have in our database.  Often, users will download both the All-in-One image as
799 well as the plnode.txt file, and inadvertently break the boot image.
800
801 To repair this problem, simply download your All-in-One BootImage again, and
802 copy it to the appropriate read-only boot media (write-protected USB or
803 CD-ROM).  
804
805 If you have already performed this step and are still receiving this
806 message, please reply so that we can help investigate the problem.
807 """
808          },
809
810         {'message_id': "notinstalled",
811          'subject': "%(hostname)s is not installed",
812          'template':
813 """
814 %(hostname)s failed to boot because it has either never been
815 installed, or the installation is corrupt.
816
817 Please check if the hard drive has failed, and replace it if so. After
818 doing so, visit:
819
820 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
821
822 Change the 'Boot State' to 'Reinstall', and then reboot the node.
823
824 If you have already performed this step and are still receiving this
825 message, please reply so that we may investigate the problem.
826 """
827          },
828
829         {'message_id': "hostnamenotresolve",
830          'subject': "%(hostname)s does not resolve",
831          'template':
832 """
833 %(hostname)s failed to boot because its hostname does not resolve, or
834 does resolve but does not match its configured IP address.
835
836 Please check the network settings for the node, especially its
837 hostname, IP address, and DNS servers, by visiting:
838
839 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
840
841 Correct any errors, and change the 'Boot State' to 'Reinstall', and then
842 reboot the node.
843
844 If you have already performed this step and are still receiving this
845 message, please reply so that we may investigate the problem.
846 """
847          },
848
849         # XXX N.B. I don't think these are necessary, since there's no
850         # way that the Boot Manager would even be able to contact the
851         # API to send these messages.
852
853         {'message_id': "noconfig",
854          'subject': "%(hostname)s does not have a configuration file",
855          'template': """
856 %(hostname)s failed to boot because it could not find a PlanetLab
857 configuration file. To create this file, visit:
858
859 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
860
861 Click the Configuration File link, and save the downloaded file as
862 plnode.txt on either a floppy disk or a USB flash drive.  Change the 
863 'Boot State' to 'Reinstall', and then reboot the node.
864
865 If you have already performed this step and are still receiving this
866 message, please reply so that we may investigate the problem.
867 """
868          },
869
870         {'message_id': "nodetectednetwork",
871          'subject': "%(hostname)s has unsupported network hardware",
872          'template':
873 """
874
875 %(hostname)s failed to boot because it has network hardware that is
876 unsupported by the current production kernel. If it has booted
877 successfully in the past, please try re-installing it by visiting:
878
879 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
880
881 Change the 'Boot State' to 'Reinstall', and then reboot the node.
882
883 If you have already performed this step and are still receiving this
884 message, please reply so that we may investigate the problem.
885 """
886          },
887         ]
888
889     for template in message_templates:
890         messages = GetMessages([template['message_id']])
891         if not messages:
892             AddMessage(template)
893
894     
895     ### Setup Initial PCU information
896     pcu_types = [{'model': 'AP79xx',
897           'name': 'APC AP79xx',
898           'pcu_protocol_types': [{ 'port': 80,
899                                   'protocol': 'APC79xxHttp',
900                                   'supported': False},
901                                  { 'port': 23,
902                                   'protocol': 'APC79xx',
903                                   'supported': True},
904                                  { 'port': 22,
905                                   'protocol': 'APC79xx',
906                                   'supported': True}],
907           },
908          {'model': 'Masterswitch',
909           'name': 'APC Masterswitch',
910           'pcu_protocol_types': [{ 'port': 80,
911                                   'protocol': 'APCMasterHttp',
912                                   'supported': False},
913                                  { 'port': 23,
914                                   'protocol': 'APCMaster',
915                                   'supported': True},
916                                  { 'port': 22,
917                                   'protocol': 'APCMaster',
918                                   'supported': True}],
919           },
920          {'model': 'DS4-RPC',
921           'name': 'BayTech DS4-RPC',
922           'pcu_protocol_types': [{ 'port': 80,
923                                   'protocol': 'BayTechHttp',
924                                   'supported': False},
925                                  { 'port': 23,
926                                   'protocol': 'BayTech',
927                                   'supported': True},
928                                  { 'port': 22,
929                                   'protocol': 'BayTech',
930                                   'supported': True}],
931           },
932          {'model': 'IP-41x_IP-81x',
933           'name': 'Dataprobe IP-41x & IP-81x',
934           'pcu_protocol_types': [ { 'port': 23,
935                                   'protocol': 'IPALTelnet',
936                                   'supported': True},
937                                   { 'port': 80,
938                                   'protocol': 'IPALHttp',
939                                   'supported': False}],
940           },
941          {'model': 'DRAC3',
942           'name': 'Dell RAC Version 3',
943           'pcu_protocol_types': [],
944           },
945          {'model': 'DRAC4',
946           'name': 'Dell RAC Version 4',
947           'pcu_protocol_types': [{ 'port': 443,
948                                   'protocol': 'DRACRacAdm',
949                                   'supported': True},
950                                  { 'port': 80,
951                                   'protocol': 'DRACRacAdm',
952                                   'supported': False},
953                                  { 'port': 22,
954                                   'protocol': 'DRAC',
955                                   'supported': True}],
956           },
957          {'model': 'ePowerSwitch',
958           'name': 'ePowerSwitch 1/4/8x',
959           'pcu_protocol_types': [{ 'port': 80,
960                                   'protocol': 'ePowerSwitch',
961                                   'supported': True}],
962           },
963          {'model': 'ilo2',
964           'name': 'HP iLO2 (Integrated Lights-Out)',
965           'pcu_protocol_types': [{ 'port': 443,
966                                   'protocol': 'HPiLOHttps',
967                                   'supported': True},
968                                  { 'port': 22,
969                                   'protocol': 'HPiLO',
970                                   'supported': True}],
971           },
972          {'model': 'ilo1',
973           'name': 'HP iLO version 1',
974           'pcu_protocol_types': [],
975           },
976          {'model': 'PM211-MIP',
977           'name': 'Infratec PM221-MIP',
978           'pcu_protocol_types': [],
979           },
980          {'model': 'AMT2.5',
981           'name': 'Intel AMT v2.5 (Active Management Technology)',
982           'pcu_protocol_types': [],
983           },
984          {'model': 'AMT3.0',
985           'name': 'Intel AMT v3.0 (Active Management Technology)',
986           'pcu_protocol_types': [],
987           },
988          {'model': 'WTI_IPS-4',
989           'name': 'Western Telematic (WTI IPS-4)',
990           'pcu_protocol_types': [],
991           },
992          {'model': 'unknown',
993           'name': 'Unknown Vendor or Model',
994           'pcu_protocol_types': [{ 'port': 443,
995                                   'protocol': 'UnknownPCU',
996                                   'supported': False},
997                                  { 'port': 80,
998                                   'protocol': 'UnknownPCU',
999                                   'supported': False},
1000                                  { 'port': 23,
1001                                   'protocol': 'UnknownPCU',
1002                                   'supported': False},
1003                                  { 'port': 22,
1004                                   'protocol': 'UnknownPCU',
1005                                   'supported': False}],
1006           }]
1007
1008     # Get all model names
1009     pcu_models = [type['model'] for type in GetPCUTypes()]
1010     for type in pcu_types:
1011         protocol_types = type['pcu_protocol_types']
1012         # Take this value out of the struct.
1013         del type['pcu_protocol_types']
1014         if type['model'] not in pcu_models:
1015             # Add the name/model info into DB
1016             id = AddPCUType(type)
1017             # for each protocol, also add this.
1018             for ptype in protocol_types:
1019                 AddPCUProtocolType(id, ptype)
1020
1021
1022 if __name__ == '__main__':
1023     main()
1024
1025 # Local variables:
1026 # tab-width: 4
1027 # mode: python
1028 # End: