add vim-minimal as requirement of myplc-native
[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 on record. 
793
794 There are two possible steps to resolve the problem.
795
796 1. If you have used an All-in-one BootCD that includes the plnode.txt file,
797     then please check your machine for any old boot media, either in the
798     floppy drive, or on a USB stick.  It is likely that an old configuration
799     is being used instead of the new configuration stored on the BootCD.
800 Or, 
801 2. If you are using Generic BootCD image, then regenerate the node 
802     configuration file by visiting:
803
804     https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
805
806     Under 'Download', follow the 'Download plnode.txt file for %(hostname)s'
807     option, and save the downloaded file as plnode.txt on either a floppy 
808     disk or a USB flash drive.  Be sure the 'Boot State' is set to 'Boot', 
809     and, then reboot the node.
810
811 If you have already performed this step and are still receiving this
812 message, please reply so that we can help investigate the problem.
813 """
814          },
815
816         {'message_id': "notinstalled",
817          'subject': "%(hostname)s is not installed",
818          'template':
819 """
820 %(hostname)s failed to boot because it has either never been
821 installed, or the installation is corrupt.
822
823 Please check if the hard drive has failed, and replace it if so. After
824 doing so, visit:
825
826 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
827
828 Change the 'Boot State' to 'Reinstall', and then reboot the node.
829
830 If you have already performed this step and are still receiving this
831 message, please reply so that we may investigate the problem.
832 """
833          },
834
835         {'message_id': "hostnamenotresolve",
836          'subject': "%(hostname)s does not resolve",
837          'template':
838 """
839 %(hostname)s failed to boot because its hostname does not resolve, or
840 does resolve but does not match its configured IP address.
841
842 Please check the network settings for the node, especially its
843 hostname, IP address, and DNS servers, by visiting:
844
845 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
846
847 Correct any errors, and change the 'Boot State' to 'Reinstall', and then
848 reboot the node.
849
850 If you have already performed this step and are still receiving this
851 message, please reply so that we may investigate the problem.
852 """
853          },
854
855         # XXX N.B. I don't think these are necessary, since there's no
856         # way that the Boot Manager would even be able to contact the
857         # API to send these messages.
858
859         {'message_id': "noconfig",
860          'subject': "%(hostname)s does not have a configuration file",
861          'template': """
862 %(hostname)s failed to boot because it could not find a PlanetLab
863 configuration file. To create this file, visit:
864
865 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
866
867 Click the Configuration File link, and save the downloaded file as
868 plnode.txt on either a floppy disk or a USB flash drive.  Change the 
869 'Boot State' to 'Reinstall', and then reboot the node.
870
871 If you have already performed this step and are still receiving this
872 message, please reply so that we may investigate the problem.
873 """
874          },
875
876         {'message_id': "nodetectednetwork",
877          'subject': "%(hostname)s has unsupported network hardware",
878          'template':
879 """
880
881 %(hostname)s failed to boot because it has network hardware that is
882 unsupported by the current production kernel. If it has booted
883 successfully in the past, please try re-installing it by visiting:
884
885 https://%(PLC_WWW_HOST)s:%(PLC_WWW_SSL_PORT)d/db/nodes/?id=%(node_id)d
886
887 Change the 'Boot State' to 'Reinstall', and then reboot the node.
888
889 If you have already performed this step and are still receiving this
890 message, please reply so that we may investigate the problem.
891 """
892          },
893         ]
894
895     for template in message_templates:
896         messages = GetMessages([template['message_id']])
897         if not messages:
898             AddMessage(template)
899
900     
901     ### Setup Initial PCU information
902     pcu_types = [{'model': 'AP79xx',
903           'name': 'APC AP79xx',
904           'pcu_protocol_types': [{ 'port': 80,
905                                   'protocol': 'APC79xxHttp',
906                                   'supported': False},
907                                  { 'port': 23,
908                                   'protocol': 'APC79xx',
909                                   'supported': True},
910                                  { 'port': 22,
911                                   'protocol': 'APC79xx',
912                                   'supported': True}],
913           },
914          {'model': 'Masterswitch',
915           'name': 'APC Masterswitch',
916           'pcu_protocol_types': [{ 'port': 80,
917                                   'protocol': 'APCMasterHttp',
918                                   'supported': False},
919                                  { 'port': 23,
920                                   'protocol': 'APCMaster',
921                                   'supported': True},
922                                  { 'port': 22,
923                                   'protocol': 'APCMaster',
924                                   'supported': True}],
925           },
926          {'model': 'DS4-RPC',
927           'name': 'BayTech DS4-RPC',
928           'pcu_protocol_types': [{ 'port': 80,
929                                   'protocol': 'BayTechHttp',
930                                   'supported': False},
931                                  { 'port': 23,
932                                   'protocol': 'BayTech',
933                                   'supported': True},
934                                  { 'port': 22,
935                                   'protocol': 'BayTech',
936                                   'supported': True}],
937           },
938          {'model': 'IP-41x_IP-81x',
939           'name': 'Dataprobe IP-41x & IP-81x',
940           'pcu_protocol_types': [ { 'port': 23,
941                                   'protocol': 'IPALTelnet',
942                                   'supported': True},
943                                   { 'port': 80,
944                                   'protocol': 'IPALHttp',
945                                   'supported': False}],
946           },
947          {'model': 'DRAC3',
948           'name': 'Dell RAC Version 3',
949           'pcu_protocol_types': [],
950           },
951          {'model': 'DRAC4',
952           'name': 'Dell RAC Version 4',
953           'pcu_protocol_types': [{ 'port': 443,
954                                   'protocol': 'DRACRacAdm',
955                                   'supported': True},
956                                  { 'port': 80,
957                                   'protocol': 'DRACRacAdm',
958                                   'supported': False},
959                                  { 'port': 22,
960                                   'protocol': 'DRAC',
961                                   'supported': True}],
962           },
963          {'model': 'ePowerSwitch',
964           'name': 'ePowerSwitch 1/4/8x',
965           'pcu_protocol_types': [{ 'port': 80,
966                                   'protocol': 'ePowerSwitch',
967                                   'supported': True}],
968           },
969          {'model': 'ilo2',
970           'name': 'HP iLO2 (Integrated Lights-Out)',
971           'pcu_protocol_types': [{ 'port': 443,
972                                   'protocol': 'HPiLOHttps',
973                                   'supported': True},
974                                  { 'port': 22,
975                                   'protocol': 'HPiLO',
976                                   'supported': True}],
977           },
978          {'model': 'ilo1',
979           'name': 'HP iLO version 1',
980           'pcu_protocol_types': [],
981           },
982          {'model': 'PM211-MIP',
983           'name': 'Infratec PM221-MIP',
984           'pcu_protocol_types': [],
985           },
986          {'model': 'AMT2.5',
987           'name': 'Intel AMT v2.5 (Active Management Technology)',
988           'pcu_protocol_types': [],
989           },
990          {'model': 'AMT3.0',
991           'name': 'Intel AMT v3.0 (Active Management Technology)',
992           'pcu_protocol_types': [],
993           },
994          {'model': 'WTI_IPS-4',
995           'name': 'Western Telematic (WTI IPS-4)',
996           'pcu_protocol_types': [],
997           },
998          {'model': 'unknown',
999           'name': 'Unknown Vendor or Model',
1000           'pcu_protocol_types': [{ 'port': 443,
1001                                   'protocol': 'UnknownPCU',
1002                                   'supported': False},
1003                                  { 'port': 80,
1004                                   'protocol': 'UnknownPCU',
1005                                   'supported': False},
1006                                  { 'port': 23,
1007                                   'protocol': 'UnknownPCU',
1008                                   'supported': False},
1009                                  { 'port': 22,
1010                                   'protocol': 'UnknownPCU',
1011                                   'supported': False}],
1012           }]
1013
1014     # Get all model names
1015     pcu_models = [type['model'] for type in GetPCUTypes()]
1016     for type in pcu_types:
1017         protocol_types = type['pcu_protocol_types']
1018         # Take this value out of the struct.
1019         del type['pcu_protocol_types']
1020         if type['model'] not in pcu_models:
1021             # Add the name/model info into DB
1022             id = AddPCUType(type)
1023             # for each protocol, also add this.
1024             for ptype in protocol_types:
1025                 AddPCUProtocolType(id, ptype)
1026
1027
1028 if __name__ == '__main__':
1029     main()
1030
1031 # Local variables:
1032 # tab-width: 4
1033 # mode: python
1034 # End: