split plc.d/ and db-config.d between myplc and plcapi modules as a first step
[myplc.git] / db-config
index ac230cd..b1a073b 100755 (executable)
--- a/db-config
+++ b/db-config
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: db-config,v 1.4 2006/11/08 23:34:28 mlhuang Exp $
-#
+# $Id$
+# $HeadURL$
+
+import os,sys
+from optparse import OptionParser
 
 from plc_config import PLCConfiguration
-import sys
+
+def GetSnippets(directory):
+    filenames = []
+    if os.path.exists(directory):
+        try:
+            filenames = os.listdir(directory)
+        except OSError, e:
+            raise Exception, "Error when opening %s (%s)" % \
+                  (os.path.join(dir, file), e)
+            
+    ignored = (".bak","~",".rpmsave",".rpmnew",".orig")
+    numberedfiles = {}
+    for filename in filenames:
+        shouldIgnore = False
+        for ignore in ignored:
+            if filename.endswith(ignore):
+                shouldIgnore = True
+                break
+
+        if not shouldIgnore:
+            parts = filename.split('-')
+            if len(parts)>=2:
+                name = '-'.join(parts)
+                try:
+                    number = int(parts[0])
+                    entry = numberedfiles.get(number,[])
+                    entry.append(name)
+                    numberedfiles[number]=entry
+                except ValueError:
+                    shouldIgnore = True
+            else:
+                shouldIgnore = True
+
+        if shouldIgnore:
+            print "db-config: ignoring %s snippet" % filename
+
+    filenames = []
+    keys = numberedfiles.keys()
+    keys.sort()
+    for k in keys:
+        for filename in numberedfiles[k]:
+            filenames.append(filename)
+    return filenames
 
 def main():
     cfg = PLCConfiguration()
     cfg.load()
     variables = cfg.variables()
 
+    usage="%prog [-- options] [steps]"
+    release_url = "$URL$"
+    parser = OptionParser(usage=usage, version="%prog " + release_url )
+    parser.add_option("-l","--list",dest="list_steps",action="store_true",default=False,
+                      help="Lists available steps")
+    parser.add_option("-v","--verbose",dest="verbose",action="store_true",default=False,
+                      help="Run verbosely")
+
+    (options,args) = parser.parse_args()
+    
     # Load variables into dictionaries
     for category_id, (category, variablelist) in variables.iteritems():
         globals()[category_id] = dict(zip(variablelist.keys(),
-                                       [variable['value'] for variable in variablelist.values()]))
-
-    # Create/update the default administrator account (should be
-    # person_id 2).
-    admin = { 'person_id': 2,
-              'first_name': "Default",
-              'last_name': "Administrator",
-              'email': plc['root_user'],
-              'password': plc['root_password'] }
-    persons = GetPersons([admin['person_id']])
-    if not persons:
-        person_id = AddPerson(admin)
-        if person_id != admin['person_id']:
-            # Huh? Someone deleted the account manually from the database.
-            DeletePerson(person_id)
-            raise Exception, "Someone deleted the \"%s %s\" account from the database!" % \
-                  (admin['first_name'], admin['last_name'])
-        UpdatePerson(person_id, { 'enabled': True })
-    else:
-        person_id = persons[0]['person_id']
-        UpdatePerson(person_id, admin)
-
-    # Create/update the default site (should be site_id 1)
-    if plc_www['port'] == '80':
-        url = "http://" + plc_www['host'] + "/"
-    elif plc_www['port'] == '443':
-        url = "https://" + plc_www['host'] + "/"
-    else:
-        url = "http://" + plc_www['host'] + ":" + plc_www['port'] + "/"
-    site = { 'site_id': 1,
-             'name': plc['name'] + " Central",
-             'abbreviated_name': plc['name'],
-             'login_base': plc['slice_prefix'],
-             'is_public': False,
-             'url': url,
-             'max_slices': 100 }
-
-    sites = GetSites([site['site_id']])
-    if not sites:
-        site_id = AddSite(site['name'], site['abbreviated_name'], site['login_base'], site)
-        if site_id != site['site_id']:
-            DeleteSite(site_id)
-            raise Exception, "Someone deleted the \"%s\" site from the database!" % \
-                  site['name']
-        sites = [site]
-
-    # Must call UpdateSite() even after AddSite() to update max_slices
-    site_id = sites[0]['site_id']
-    UpdateSite(site_id, site)
-
-    # The default administrator account must be associated with a site
-    # in order to login.
-    AddPersonToSite(admin['person_id'], site['site_id'])
-    SetPersonPrimarySite(admin['person_id'], site['site_id'])
-
-    # Grant admin and PI roles to the default administrator account
-    AddRoleToPerson(10, admin['person_id'])
-    AddRoleToPerson(20, admin['person_id'])
-
-    # Setup default PlanetLabConf entries
-    default_conf_files = [
-        # NTP configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/ntp.conf.php',
-         'dest': '/etc/ntp.conf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/etc/rc.d/init.d/ntpd restart',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/ntp/step-tickers.php',
-         'dest': '/etc/ntp/step-tickers',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/etc/rc.d/init.d/ntpd restart',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # SSH server configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/sshd_config',
-         'dest': '/etc/ssh/sshd_config',
-         'file_permissions': '600',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/etc/init.d/sshd restart',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # Administrative SSH keys
-        {'enabled': True,
-         'source': 'PlanetLabConf/keys.php?root',
-         'dest': '/root/.ssh/authorized_keys',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/keys.php?site_admin',
-         'dest': '/home/site_admin/.ssh/authorized_keys',
-         'file_permissions': '644',
-         'file_owner': 'site_admin',
-         'file_group': 'site_admin',
-         'preinstall_cmd': 'grep -q site_admin /etc/passwd',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/keys.php?role=admin',
-         'dest': '/home/pl_admin/.ssh/authorized_keys',
-         'file_permissions': '644',
-         'file_owner': 'pl_admin',
-         'file_group': 'pl_admin',
-         'preinstall_cmd': 'grep -q pl_admin /etc/passwd',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # Log rotation configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/logrotate.conf',
-         'dest': '/etc/logrotate.conf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # updatedb/locate nightly cron job
-        {'enabled': True,
-         'source': 'PlanetLabConf/slocate.cron',
-         'dest': '/etc/cron.daily/slocate.cron',
-         'file_permissions': '755',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # YUM configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/yum.conf.php?gpgcheck=1',
-         'dest': '/etc/yum.conf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/delete-rpm-list-production',
-         'dest': '/etc/planetlab/delete-rpm-list',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # PLC configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/get_plc_config.php',
-         'dest': '/etc/planetlab/plc_config',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/get_plc_config.php?python',
-         'dest': '/etc/planetlab/plc_config.py',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/get_plc_config.php?perl',
-         'dest': '/etc/planetlab/plc_config.pl',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/get_plc_config.php?php',
-         'dest': '/etc/planetlab/php/plc_config.php',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # Proper configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/propd.conf',
-         'dest': '/etc/proper/propd.conf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/etc/init.d/proper restart',
-         'error_cmd': '',
-         'ignore_cmd_errors': True,
-         'always_update': False},
-
-        # Proxy ARP setup
-        {'enabled': True,
-         'source': 'PlanetLabConf/proxies.php',
-         'dest': '/etc/planetlab/proxies',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # Firewall configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/iptables',
-         'dest': '/etc/sysconfig/iptables',
-         'file_permissions': '600',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/blacklist.php',
-         'dest': '/etc/planetlab/blacklist',
-         'file_permissions': '600',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/sbin/iptables-restore --noflush < /etc/planetlab/blacklist',
-         'error_cmd': '',
-         'ignore_cmd_errors': True,
-         'always_update': True},
-
-        # /etc/issue
-        {'enabled': True,
-         'source': 'PlanetLabConf/issue.php',
-         'dest': '/etc/issue',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # Kernel parameters
-        {'enabled': True,
-         'source': 'PlanetLabConf/sysctl.php',
-         'dest': '/etc/sysctl.conf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/sbin/sysctl -e -p /etc/sysctl.conf',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': True},
-
-        # Sendmail configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/sendmail.mc',
-         'dest': '/etc/mail/sendmail.mc',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/sendmail.cf',
-         'dest': '/etc/mail/sendmail.cf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': 'service sendmail restart',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # GPG signing keys
-        {'enabled': True,
-         'source': 'PlanetLabConf/RPM-GPG-KEY-fedora',
-         'dest': '/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': 'rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-        {'enabled': True,
-         'source': 'PlanetLabConf/get_gpg_key.php',
-         'dest': '/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': 'rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # Ping of death configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/ipod.conf.php',
-         'dest': '/etc/ipod.conf',
-         'file_permissions': '644',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False},
-
-        # sudo configuration
-        {'enabled': True,
-         'source': 'PlanetLabConf/sudoers',
-         'dest': '/etc/sudoers',
-         'file_permissions': '440',
-         'file_owner': 'root',
-         'file_group': 'root',
-         'preinstall_cmd': '',
-         'postinstall_cmd': '/usr/sbin/visudo -c',
-         'error_cmd': '',
-         'ignore_cmd_errors': False,
-         'always_update': False}
-        ]
-
-    # Get list of existing (enabled, global) files
-    conf_files = GetConfFiles()
-    conf_files = filter(lambda conf_file: conf_file['enabled'] and \
-                                          not conf_file['node_ids'] and \
-                                          not conf_file['nodegroup_ids'],
-                        conf_files)
-    dests = [conf_file['dest'] for conf_file in conf_files]
-    conf_files = dict(zip(dests, conf_files))
-
-    # Create/update default PlanetLabConf entries
-    for default_conf_file in default_conf_files:
-        if default_conf_file['dest'] not in dests:
-            AddConfFile(default_conf_file)
-        else:
-            conf_file = conf_files[default_conf_file['dest']]
-            UpdateConfFile(conf_file['conf_file_id'], default_conf_file)
-
-    # Setup default slice attribute types
-    default_attribute_types = [
-        # Slice type (only vserver is supported)
-        {'name': "type",
-         'description': "Type of slice (e.g. vserver)",
-         'min_role_id': 20},
-
-        # Slice enabled (1) or suspended (0)
-        {'name': "enabled",
-         'description': "Slice enabled (1) or suspended (0)",
-         'min_role_id': 10},
-
-        # Slice reference image
-        {'name': "vref",
-         'description': "Reference image",
-         'min_role_id': 30},
-
-        # Slice initialization script
-        {'name': "initscript",
-         'description': "Slice initialization script",
-         'min_role_id': 10},
-
-        # CPU share
-        {'name': "cpu_min",
-         'description': "Minimum CPU share (ms/s)",
-         'min_role_id': 10},
-        {'name': "cpu_share",
-         'description': "Number of CPU shares",
-         'min_role_id': 10},
-
-        # Bandwidth limits
-        {'name': "net_min",
-         'description': "Minimum bandwidth (bps)",
-         'min_role_id': 10},
-        {'name': "net_max",
-         'description': "Maximum bandwidth (bps)",
-         'min_role_id': 10},
-        {'name': "net_avg",
-         'description': "Average bandwidth (bps)",
-         'min_role_id': 10},
-        {'name': "net_share",
-         'description': "Number of bandwidth shares",
-         'min_role_id': 10},
-        {'name': "net2_min",
-         'description': "Minimum bandwidth over routes exempt from node bandwidth limits (bps)",
-         'min_role_id': 10},
-        {'name': "net2_max",
-         'description': "Maximum bandwidth over routes exempt from node bandwidth limits (bps)",
-         'min_role_id': 10},
-        {'name': "net2_avg",
-         'description': "Average bandwidth over routes exempt from node bandwidth limits (bps)",
-         'min_role_id': 10},
-        {'name': "net2_share",
-         'description': "Number of bandwidth shares over routes exempt from node bandwidth limits",
-         'min_role_id': 10},
-
-        # Disk quota
-        {'name': "disk_max",
-         'description': "Disk quota (bytes)",
-         'min_role_id': 10},
-        ]
-
-    # Get list of existing attribute types
-    attribute_types = GetSliceAttributeTypes()
-    attribute_types = [attribute_type['name'] for attribute_type in attribute_types]
-
-    # Create/update default slice attribute types
-    for default_attribute_type in default_attribute_types:
-        if default_attribute_type['name'] not in attribute_types:
-            AddSliceAttributeType(default_attribute_type)
-        else:
-            UpdateSliceAttributeType(default_attribute_type['name'], default_attribute_type)
-
-    # Create/update system slices
-    default_slices = [
-        {'name': plc['slice_prefix'] + "_netflow",
-         'description': "PlanetFlow Traffic Auditing Service",
-         'instantiation': "plc-instantiated",
-         # Renew forever
-         'expires': sys.maxint,
-         'attributes': {'reference': "planetflow"}},
-        ]
-
-    for default_slice in default_slices:
-        slices = GetSlices([default_slice['name']])
-        if slices:
-            slice = slices[0]
-            UpdateSlice(slice['slice_id'], default_slice)
-        else:
-            AddSlice(default_slice)
-            slice = GetSlices([default_slice['name']])[0]
-
-        # Create/update all attributes
-        slice_attributes = {}
-        if slice['slice_attribute_ids']:
-            for slice_attribute in GetSliceAttributes(slice['slice_attribute_ids']):
-                slice_attributes[slice_attribute['name']] = slice_attribute
-
-        for name, value in default_slice['attributes'].iteritems():
-            if name not in slice_attributes:
-                AddSliceAttribute(slice['name'], name, value)
-            else:
-                UpdateSliceAttribute(slice_attributes[name]['slice_attribute_id'], value)
+                                          [variable['value'] for variable in variablelist.values()]))
+
+    directory="/etc/planetlab/db-config.d"
+    snippets = GetSnippets(directory)
+
+    if options.list_steps:
+        for snippet in snippets:
+            if not options.verbose: 
+                print snippet
+            else: 
+                print "Found step %s/%s"%(directory,snippet)
+                os.system("rpm -qf %s/%s"%(directory,snippet))
+        sys.exit(0)
+    
+    for snippet in snippets:
+        fullpath = os.path.join(directory, snippet)
+        if options.verbose:
+            print "Running step %s"%fullpath
+        execfile(fullpath)
 
 if __name__ == '__main__':
     main()