ba9b0befa44a1242f441fbf08d2e4d6be451fe86
[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/iptables',
303          'dest': '/etc/sysconfig/iptables',
304          'file_permissions': '600',
305          'file_owner': 'root',
306          'file_group': 'root',
307          'preinstall_cmd': '',
308          'postinstall_cmd': '',
309          'error_cmd': '',
310          'ignore_cmd_errors': False,
311          'always_update': False},
312         {'enabled': True,
313          'source': 'PlanetLabConf/blacklist.php',
314          'dest': '/etc/planetlab/blacklist',
315          'file_permissions': '600',
316          'file_owner': 'root',
317          'file_group': 'root',
318          'preinstall_cmd': '',
319          'postinstall_cmd': '/sbin/iptables-restore --noflush < /etc/planetlab/blacklist',
320          'error_cmd': '',
321          'ignore_cmd_errors': True,
322          'always_update': False},
323
324         # /etc/issue
325         {'enabled': True,
326          'source': 'PlanetLabConf/issue.php',
327          'dest': '/etc/issue',
328          'file_permissions': '644',
329          'file_owner': 'root',
330          'file_group': 'root',
331          'preinstall_cmd': '',
332          'postinstall_cmd': '',
333          'error_cmd': '',
334          'ignore_cmd_errors': False,
335          'always_update': False},
336
337         # Kernel parameters
338         {'enabled': True,
339          'source': 'PlanetLabConf/sysctl.php',
340          'dest': '/etc/sysctl.conf',
341          'file_permissions': '644',
342          'file_owner': 'root',
343          'file_group': 'root',
344          'preinstall_cmd': '',
345          'postinstall_cmd': '/sbin/sysctl -e -p /etc/sysctl.conf',
346          'error_cmd': '',
347          'ignore_cmd_errors': False,
348          'always_update': False},
349
350         # Sendmail configuration
351         {'enabled': True,
352          'source': 'PlanetLabConf/sendmail.mc',
353          'dest': '/etc/mail/sendmail.mc',
354          'file_permissions': '644',
355          'file_owner': 'root',
356          'file_group': 'root',
357          'preinstall_cmd': '',
358          'postinstall_cmd': '',
359          'error_cmd': '',
360          'ignore_cmd_errors': False,
361          'always_update': False},
362         {'enabled': True,
363          'source': 'PlanetLabConf/sendmail.cf',
364          'dest': '/etc/mail/sendmail.cf',
365          'file_permissions': '644',
366          'file_owner': 'root',
367          'file_group': 'root',
368          'preinstall_cmd': '',
369          'postinstall_cmd': 'service sendmail restart',
370          'error_cmd': '',
371          'ignore_cmd_errors': False,
372          'always_update': False},
373
374         # GPG signing keys
375         {'enabled': True,
376          'source': 'PlanetLabConf/RPM-GPG-KEY-fedora',
377          'dest': '/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora',
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-fedora',
383          'error_cmd': '',
384          'ignore_cmd_errors': False,
385          'always_update': False},
386         {'enabled': True,
387          'source': 'PlanetLabConf/get_gpg_key.php',
388          'dest': '/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab',
389          'file_permissions': '644',
390          'file_owner': 'root',
391          'file_group': 'root',
392          'preinstall_cmd': '',
393          'postinstall_cmd': 'rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab',
394          'error_cmd': '',
395          'ignore_cmd_errors': False,
396          'always_update': False},
397
398         # Ping of death configuration
399         # the 'restart' postcommand doesn't work, b/c the pod script doesn't support it.
400         {'enabled': True,
401          'source': 'PlanetLabConf/ipod.conf.php',
402          'dest': '/etc/ipod.conf',
403          'file_permissions': '644',
404          'file_owner': 'root',
405          'file_group': 'root',
406          'preinstall_cmd': '',
407          'postinstall_cmd': '/etc/init.d/pod start',
408          'error_cmd': '',
409          'ignore_cmd_errors': False,
410          'always_update': False},
411
412         # sudo configuration
413         {'enabled': True,
414          'source': 'PlanetLabConf/sudoers.php',
415          'dest': '/etc/sudoers',
416          'file_permissions': '440',
417          'file_owner': 'root',
418          'file_group': 'root',
419          'preinstall_cmd': '',
420          'postinstall_cmd': '/usr/sbin/visudo -c',
421          'error_cmd': '',
422          'ignore_cmd_errors': False,
423          'always_update': False}
424         ]
425
426     # Get list of existing (enabled, global) files
427     conf_files = GetConfFiles()
428     conf_files = filter(lambda conf_file: conf_file['enabled'] and \
429                                           not conf_file['node_ids'] and \
430                                           not conf_file['nodegroup_ids'],
431                         conf_files)
432     dests = [conf_file['dest'] for conf_file in conf_files]
433     conf_files = dict(zip(dests, conf_files))
434
435     # Create/update default PlanetLabConf entries
436     for default_conf_file in default_conf_files:
437         if default_conf_file['dest'] not in dests:
438             AddConfFile(default_conf_file)
439         else:
440             conf_file = conf_files[default_conf_file['dest']]
441             UpdateConfFile(conf_file['conf_file_id'], default_conf_file)
442
443     # Setup default slice attribute types
444     default_attribute_types = [
445         # Slice type (only vserver is supported)
446         {'name': "type",
447          'description': "Type of slice (e.g. vserver)",
448          'min_role_id': 20},
449
450         # System slice
451         {'name': "system",
452          'description': "Is a default system slice (1) or not (0 or unset)",
453          'min_role_id': 10},
454
455         # Slice enabled (1) or suspended (0)
456         {'name': "enabled",
457          'description': "Slice enabled (1 or unset) or suspended (0)",
458          'min_role_id': 10},
459
460         # Slice reference image
461         {'name': "vref",
462          'description': "Reference image",
463          'min_role_id': 30},
464
465         # Slice initialization script
466         {'name': "initscript",
467          'description': "Slice initialization script",
468          'min_role_id': 10},
469
470         # CPU share
471         {'name': "cpu_pct",
472          'description': "Reserved CPU percent",
473          'min_role_id': 10},
474         {'name': "cpu_share",
475          'description': "Number of CPU shares",
476          'min_role_id': 10},
477
478         # Bandwidth limits
479         {'name': "net_min_rate",
480          'description': "Minimum bandwidth (kbps)",
481          'min_role_id': 10},
482         {'name': "net_max_rate",
483          'description': "Maximum bandwidth (kbps)",
484          'min_role_id': 10},
485         {'name': "net_i2_min_rate",
486          'description': "Minimum bandwidth over I2 routes (kbps)",
487          'min_role_id': 10},
488         {'name': "net_i2_max_rate",
489          'description': "Maximum bandwidth over I2 routes (kbps)",
490          'min_role_id': 10},
491         {'name': "net_max_kbyte",
492          'description': "Maximum daily network Tx KByte limit.",
493          'min_role_id': 10},
494         {'name': "net_thresh_kbyte",
495          'description': "KByte limit before warning and throttling.",
496          'min_role_id': 10},
497         {'name': "net_i2_max_kbyte",
498          'description': "Maximum daily network Tx KByte limit to I2 hosts.",
499          'min_role_id': 10},
500         {'name': "net_i2_thresh_kbyte",
501          'description': "KByte limit to I2 hosts before warning and throttling.",
502          'min_role_id': 10},
503         {'name': "net_share",
504          'description': "Number of bandwidth shares",
505          'min_role_id': 10},
506         {'name': "net_i2_share",
507          'description': "Number of bandwidth shares over I2 routes",
508          'min_role_id': 10},
509  
510         # Disk quota
511         {'name': "disk_max",
512          'description': "Disk quota (1k disk blocks)",
513          'min_role_id': 10},
514
515         # Proper operations
516         {'name': "proper_op",
517          'description': "Proper operation (e.g. bind_socket)",
518          'min_role_id': 10},
519
520         # VServer capabilities 
521         {'name': "capabilities",
522          'description': "VServer bcapabilities (separate by commas)",
523          'min_role_id': 10},
524
525                 # Vsys
526         {'name': "vsys",
527          'description': "Bind vsys script fd's to a slice's vsys directory.",
528          'min_role_id': 10},
529
530         # CoDemux
531         {'name': "codemux",
532          'description': "Demux HTTP between slices using localhost ports. Value in the form 'host, localhost port'.",
533          'min_role_id': 10},
534
535         ]
536
537     # Get list of existing attribute types
538     attribute_types = GetSliceAttributeTypes()
539     attribute_types = [attribute_type['name'] for attribute_type in attribute_types]
540
541     # Create/update default slice attribute types
542     for default_attribute_type in default_attribute_types:
543         if default_attribute_type['name'] not in attribute_types:
544             AddSliceAttributeType(default_attribute_type)
545         else:
546             UpdateSliceAttributeType(default_attribute_type['name'], default_attribute_type)
547
548     # Default Initscripts
549     default_initscripts = []
550
551     # Find initscripts and add them to the db
552     for (root, dirs, files) in os.walk("/etc/plc_sliceinitscripts"):
553         for f in files:
554             # Read the file
555             file = open(root + "/" + f, "ro")
556             default_initscripts.append({"name": plc['slice_prefix'] + "_" + f,
557                                         "enabled": True,
558                                         "script": file.read().replace("@SITE@", url).replace("@PREFIX@", plc['slice_prefix'])})
559             file.close()
560
561     # Get list of existing initscripts
562     oldinitscripts = GetInitScripts()
563     oldinitscripts = [script['name'] for script in oldinitscripts]
564
565     for initscript in default_initscripts:
566         if initscript['name'] not in oldinitscripts:  AddInitScript(initscript)
567
568     # Setup default slice attribute types
569     default_setting_types = [
570
571         {'category' : "general",
572          'name' : "ifname",
573          'description': "Set interface name, instead of eth0 or the like",
574          'min_role_id' : 40},
575         {'category' : "Multihome",
576          'name' : "alias",
577          'description': "Specifies that the network is used for multihoming",
578          'min_role_id' : 40},
579
580         {'category' : "hidden",
581          'name' : "backdoor",
582          'description': "For testing new settings",
583          'min_role_id' : 10},
584         ] + [
585         { "category" : "WiFi",
586           "name" : x,
587           "description" : "802.11 %s -- see %s"%(y,z),
588           "min_role_id" : 40 } for (x,y,z) in [
589             ("mode","Mode","iwconfig"),
590             ("essid","ESSID","iwconfig"),
591             ("nw","Network Id","iwconfig"),
592             ("freq","Frequency","iwconfig"),
593             ("channel","Channel","iwconfig"),
594             ("sens","sensitivity threshold","iwconfig"),
595             ("rate","Rate","iwconfig"),
596             ("key","key","iwconfig key"),
597             ("key1","key1","iwconfig key [1]"),
598             ("key2","key2","iwconfig key [2]"),
599             ("key3","key3","iwconfig key [3]"),
600             ("key4","key4","iwconfig key [4]"),
601             ("securitymode","Security mode","iwconfig enc"),
602             ("iwconfig","Additional parameters to iwconfig","ifup-wireless"),
603             ("iwpriv","Additional parameters to iwpriv","ifup-wireless"),
604             ]
605         ]
606
607
608     # Get list of existing attribute types
609     setting_types = GetNodeNetworkSettingTypes()
610     setting_types = [setting_type['name'] for setting_type in setting_types]
611
612     # Create/update default slice setting types
613     for default_setting_type in default_setting_types:
614         if default_setting_type['name'] not in setting_types:
615             AddNodeNetworkSettingType(default_setting_type)
616         else:
617             UpdateNodeNetworkSettingType(default_setting_type['name'], default_setting_type)
618
619     # Create/update system slices
620     default_slices = [
621          # PlanetFlow
622         {'name': plc['slice_prefix'] + "_netflow",
623          '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.",
624          'url': url,
625          'instantiation': "plc-instantiated",
626          # Renew forever (minus one day, work around date conversion weirdness)
627          'expires': 0x7fffffff - (60 * 60 * 24),
628          'attributes': [('system', "1"),
629                         ('vref', "planetflow"),
630                                                 ('vsys', "pfmount")]},
631           # Sirius
632         {'name': plc['slice_prefix'] + "_sirius",
633          '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',
634          'url': url + "db/sirius/index.php",
635          'instantiation': "plc-instantiated",
636          # Renew forever (minus one day, work around date conversion weirdness)
637          'expires': 0x7fffffff - (60 * 60 * 24),
638          'attributes': [('system', "1"),
639                         ('net_min_rate', "2000"),
640                         ('cpu_pct', "25"),
641                         ('initscript', plc['slice_prefix'] + "_sirius")]}
642         ]
643     
644     for default_slice in default_slices:
645         slices = GetSlices([default_slice['name']])
646         if slices:
647             slice = slices[0]
648             UpdateSlice(slice['slice_id'], default_slice)
649         else:
650             AddSlice(default_slice)
651             slice = GetSlices([default_slice['name']])[0]
652
653         # Create/update all attributes
654         slice_attributes = []
655         if slice['slice_attribute_ids']:
656             # Delete unknown attributes
657             for slice_attribute in GetSliceAttributes(slice['slice_attribute_ids']):
658                 if (slice_attribute['name'], slice_attribute['value']) \
659                    not in default_slice['attributes']:
660                     DeleteSliceAttribute(slice_attribute['slice_attribute_id'])
661                 else:
662                     slice_attributes.append((slice_attribute['name'], slice_attribute['value']))
663
664         for (name, value) in default_slice['attributes']:
665             if (name, value) not in slice_attributes:
666                 AddSliceAttribute(slice['name'], name, value)
667
668     installfailed = """
669 Once the node meets these requirements, please reinitiate the install
670 by visiting:
671
672 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
673
674 Update the BootState to 'Reinstall', then reboot the node.
675
676 If you have already performed this step and are still receiving this
677 message, please reply so that we may investigate the problem.
678 """
679
680     # Load default message templates
681     message_templates = [
682         {'message_id': 'Verify account',
683          'subject': "Verify account registration",
684          'template': """
685 Please verify that you registered for a %(PLC_NAME)s account with the
686 username %(email)s by visiting:
687
688 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/register.php?id=%(person_id)d&key=%(verification_key)s
689
690 If you did not register for a %(PLC_NAME)s account, please ignore this
691 message, or contact %(PLC_NAME)s Support <%(PLC_MAIL_SUPPORT_ADDRESS)s>.
692 """
693          },
694
695         {'message_id': 'New PI account',
696          'subject': "New PI account registration from %(first_name)s %(last_name)s <%(email)s> at %(site_name)s",
697          'template': """
698 %(first_name)s %(last_name)s <%(email)s> has signed up for a new
699 %(PLC_NAME)s account at %(site_name)s and has requested a PI role. PIs
700 are responsible for enabling user accounts, creating slices, and
701 ensuring that all users abide by the %(PLC_NAME)s Acceptable Use
702 Policy.
703
704 Only %(PLC_NAME)s administrators may enable new PI accounts. If you
705 are a PI at %(site_name)s, please respond and indicate whether this
706 registration is acceptable.
707
708 To view the request, visit:
709
710 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/index.php?id=%(person_id)d
711 """
712          },
713
714         {'message_id': 'New account',
715          'subject': "New account registration from %(first_name)s %(last_name)s <%(email)s> at %(site_name)s",
716          'template': """
717 %(first_name)s %(last_name)s <%(email)s> has signed up for a new
718 %(PLC_NAME)s account at %(site_name)s and has requested the following
719 roles: %(roles)s.
720
721 To deny the request or enable the account, visit:
722
723 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/index.php?id=%(person_id)d
724 """
725          },
726
727         {'message_id': 'Password reset requested',
728          'subject': "Password reset requested",
729          'template': """
730 Someone has requested that the password of your %(PLC_NAME)s account
731 %(email)s be reset. If this person was you, you may continue with the
732 reset by visiting:
733
734 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/reset_password.php?id=%(person_id)d&key=%(verification_key)s
735
736 If you did not request that your password be reset, please contact
737 %(PLC_NAME)s Support <%(PLC_MAIL_SUPPORT_ADDRESS)s>. Do not quote or
738 otherwise include any of this text in any correspondence.
739 """
740          },
741
742         {'message_id': 'Password reset',
743          'subject': "Password reset",
744          'template': """
745 The password of your %(PLC_NAME)s account %(email)s has been
746 temporarily reset to:
747
748 %(password)s
749
750 Please change it at as soon as possible by visiting:
751
752 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/persons/index.php?id=%(person_id)d
753
754 If you did not request that your password be reset, please contact
755 %(PLC_NAME)s Support <%(PLC_MAIL_SUPPORT_ADDRESS)s>. Do not quote or
756 otherwise include any of this text in any correspondence.
757 """
758          },
759
760         # Boot Manager messages
761         {'message_id': "installfinished",
762          'subject': "%(hostname)s completed installation",
763          'template': """
764 %(hostname)s just completed installation.
765
766 The node should be usable in a couple of minutes if installation was
767 successful.
768 """
769          },
770
771         {'message_id': "insufficientdisk",
772          'subject': "%(hostname)s does not have sufficient disk space",
773          'template': """
774 %(hostname)s failed to boot because it does not have sufficent disk
775 space, or because its disk controller was not recognized.
776
777 Please replace the current disk or disk controller or install
778 additional disks to meet the current hardware requirements.
779 """ + installfailed
780          },
781
782         {'message_id': "insufficientmemory",
783          'subject': "%(hostname)s does not have sufficient memory",
784          'template': """
785 %(hostname)s failed to boot because it does not have sufficent
786 memory.
787
788 Please install additional memory to meet the current hardware
789 requirements.
790 """ + installfailed
791          },
792
793         {'message_id': "authfail",
794          'subject': "%(hostname)s failed to authenticate",
795          'template':
796 """
797 %(hostname)s failed to authenticate for the following reason:
798
799 %(fault)s
800
801 The most common reason for authentication failure is that the
802 authentication key stored in the node configuration file, does not
803 match the key on record. 
804
805 There are two possible steps to resolve the problem.
806
807 1. If you have used an All-in-one BootCD that includes the plnode.txt file,
808     then please check your machine for any old boot media, either in the
809     floppy drive, or on a USB stick.  It is likely that an old configuration
810     is being used instead of the new configuration stored on the BootCD.
811 Or, 
812 2. If you are using Generic BootCD image, then regenerate the node 
813     configuration file by visiting:
814
815     https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
816
817     Under 'Download', follow the 'Download plnode.txt file for %(hostname)s'
818     option, and save the downloaded file as plnode.txt on either a floppy 
819     disk or a USB flash drive.  Be sure the 'Boot State' is set to 'Boot', 
820     and, then reboot the node.
821
822 If you have already performed this step and are still receiving this
823 message, please reply so that we can help investigate the problem.
824 """
825          },
826
827         {'message_id': "notinstalled",
828          'subject': "%(hostname)s is not installed",
829          'template':
830 """
831 %(hostname)s failed to boot because it has either never been
832 installed, or the installation is corrupt.
833
834 Please check if the hard drive has failed, and replace it if so. After
835 doing so, visit:
836
837 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
838
839 Change the 'Boot State' to 'Reinstall', and then reboot the node.
840
841 If you have already performed this step and are still receiving this
842 message, please reply so that we may investigate the problem.
843 """
844          },
845
846         {'message_id': "hostnamenotresolve",
847          'subject': "%(hostname)s does not resolve",
848          'template':
849 """
850 %(hostname)s failed to boot because its hostname does not resolve, or
851 does resolve but does not match its configured IP address.
852
853 Please check the network settings for the node, especially its
854 hostname, IP address, and DNS servers, by visiting:
855
856 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
857
858 Correct any errors, and change the 'Boot State' to 'Reinstall', and then
859 reboot the node.
860
861 If you have already performed this step and are still receiving this
862 message, please reply so that we may investigate the problem.
863 """
864          },
865
866         # XXX N.B. I don't think these are necessary, since there's no
867         # way that the Boot Manager would even be able to contact the
868         # API to send these messages.
869
870         {'message_id': "noconfig",
871          'subject': "%(hostname)s does not have a configuration file",
872          'template': """
873 %(hostname)s failed to boot because it could not find a PlanetLab
874 configuration file. To create this file, visit:
875
876 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
877
878 Click the Configuration File link, and save the downloaded file as
879 plnode.txt on either a floppy disk or a USB flash drive.  Change the 
880 'Boot State' to 'Reinstall', and then reboot the node.
881
882 If you have already performed this step and are still receiving this
883 message, please reply so that we may investigate the problem.
884 """
885          },
886
887         {'message_id': "nodetectednetwork",
888          'subject': "%(hostname)s has unsupported network hardware",
889          'template':
890 """
891
892 %(hostname)s failed to boot because it has network hardware that is
893 unsupported by the current production kernel. If it has booted
894 successfully in the past, please try re-installing it by visiting:
895
896 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
897
898 Change the 'Boot State' to 'Reinstall', and then reboot the node.
899
900 If you have already performed this step and are still receiving this
901 message, please reply so that we may investigate the problem.
902 """
903          },
904         ]
905
906     for template in message_templates:
907         messages = GetMessages([template['message_id']])
908         if not messages:
909             AddMessage(template)
910
911     
912     ### Setup Initial PCU information
913     pcu_types = [{'model': 'AP79xx',
914           'name': 'APC AP79xx',
915           'pcu_protocol_types': [{ 'port': 80,
916                                   'protocol': 'APC79xxHttp',
917                                   'supported': False},
918                                  { 'port': 23,
919                                   'protocol': 'APC79xx',
920                                   'supported': True},
921                                  { 'port': 22,
922                                   'protocol': 'APC79xx',
923                                   'supported': True}],
924           },
925          {'model': 'Masterswitch',
926           'name': 'APC Masterswitch',
927           'pcu_protocol_types': [{ 'port': 80,
928                                   'protocol': 'APCMasterHttp',
929                                   'supported': False},
930                                  { 'port': 23,
931                                   'protocol': 'APCMaster',
932                                   'supported': True},
933                                  { 'port': 22,
934                                   'protocol': 'APCMaster',
935                                   'supported': True}],
936           },
937          {'model': 'DS4-RPC',
938           'name': 'BayTech DS4-RPC',
939           'pcu_protocol_types': [{ 'port': 80,
940                                   'protocol': 'BayTechHttp',
941                                   'supported': False},
942                                  { 'port': 23,
943                                   'protocol': 'BayTech',
944                                   'supported': True},
945                                  { 'port': 22,
946                                   'protocol': 'BayTech',
947                                   'supported': True}],
948           },
949          {'model': 'IP-41x_IP-81x',
950           'name': 'Dataprobe IP-41x & IP-81x',
951           'pcu_protocol_types': [ { 'port': 23,
952                                   'protocol': 'IPALTelnet',
953                                   'supported': True},
954                                   { 'port': 80,
955                                   'protocol': 'IPALHttp',
956                                   'supported': False}],
957           },
958          {'model': 'DRAC3',
959           'name': 'Dell RAC Version 3',
960           'pcu_protocol_types': [],
961           },
962          {'model': 'DRAC4',
963           'name': 'Dell RAC Version 4',
964           'pcu_protocol_types': [{ 'port': 443,
965                                   'protocol': 'DRACRacAdm',
966                                   'supported': True},
967                                  { 'port': 80,
968                                   'protocol': 'DRACRacAdm',
969                                   'supported': False},
970                                  { 'port': 22,
971                                   'protocol': 'DRAC',
972                                   'supported': True}],
973           },
974          {'model': 'ePowerSwitch',
975           'name': 'ePowerSwitch 1/4/8x',
976           'pcu_protocol_types': [{ 'port': 80,
977                                   'protocol': 'ePowerSwitch',
978                                   'supported': True}],
979           },
980          {'model': 'ilo2',
981           'name': 'HP iLO2 (Integrated Lights-Out)',
982           'pcu_protocol_types': [{ 'port': 443,
983                                   'protocol': 'HPiLOHttps',
984                                   'supported': True},
985                                  { 'port': 22,
986                                   'protocol': 'HPiLO',
987                                   'supported': True}],
988           },
989          {'model': 'ilo1',
990           'name': 'HP iLO version 1',
991           'pcu_protocol_types': [],
992           },
993          {'model': 'PM211-MIP',
994           'name': 'Infratec PM221-MIP',
995           'pcu_protocol_types': [],
996           },
997          {'model': 'AMT2.5',
998           'name': 'Intel AMT v2.5 (Active Management Technology)',
999           'pcu_protocol_types': [],
1000           },
1001          {'model': 'AMT3.0',
1002           'name': 'Intel AMT v3.0 (Active Management Technology)',
1003           'pcu_protocol_types': [],
1004           },
1005          {'model': 'WTI_IPS-4',
1006           'name': 'Western Telematic (WTI IPS-4)',
1007           'pcu_protocol_types': [],
1008           },
1009          {'model': 'unknown',
1010           'name': 'Unknown Vendor or Model',
1011           'pcu_protocol_types': [{ 'port': 443,
1012                                   'protocol': 'UnknownPCU',
1013                                   'supported': False},
1014                                  { 'port': 80,
1015                                   'protocol': 'UnknownPCU',
1016                                   'supported': False},
1017                                  { 'port': 23,
1018                                   'protocol': 'UnknownPCU',
1019                                   'supported': False},
1020                                  { 'port': 22,
1021                                   'protocol': 'UnknownPCU',
1022                                   'supported': False}],
1023           }]
1024
1025     # Get all model names
1026     pcu_models = [type['model'] for type in GetPCUTypes()]
1027     for type in pcu_types:
1028         protocol_types = type['pcu_protocol_types']
1029         # Take this value out of the struct.
1030         del type['pcu_protocol_types']
1031         if type['model'] not in pcu_models:
1032             # Add the name/model info into DB
1033             id = AddPCUType(type)
1034             # for each protocol, also add this.
1035             for ptype in protocol_types:
1036                 AddPCUProtocolType(id, ptype)
1037
1038
1039 if __name__ == '__main__':
1040     main()
1041
1042 # Local variables:
1043 # tab-width: 4
1044 # mode: python
1045 # End: