示例#1
0
def systemd_service_install():
    for name,user,group in [("strato","strato-ddns-updater","strato-ddns-updater"),
	("openafs","root","root") # has to be run as root in order to be able to create dummy interfaces
    ]:
        systemd_unit_name = "%s-ddns-updater" % (name,)

        # @TODO: adjust to deal with setuptools options
        systemd_service_file_path = "/lib/systemd/system/%s-ddns-updater.service" % (name,)
        config_file_dir_path = "/usr/local/%s-ddns-updater/etc/" % (name,)
        config_file_name = "%s-ddns-updater.conf" % (name,)
        config_file_path = os.path.join(config_file_dir_path, config_file_name)
        log_file_dir_path = "/var/log/%s-ddns-updater" % (name,)
        log_file_path = os.path.join(log_file_dir_path, ddns_updater_globals.log_file_name)

        sp.call([systemctl, "stop", systemd_unit_name]) # might fail if service doesn't exist
        from Cheetah.Template import Template
        t = Template(file="ddns-updater.service.tmpl")
        t.systemd_unit_name = systemd_unit_name
        t.app_name = ddns_updater_globals.app_name
        t.user = user
        t.group = group
        t.mode = name
        template_helper.write_template_file(str(t), systemd_service_file_path)
        logger.info("created systemd unit '%s'" % (systemd_service_file_path,))
        # pwd.getpwnam doesn't return a decent value if the user doesn't exist, but raises KeyError -> in order to express the condition in one line use a list aggreator
        if not user in [i.pw_name for i in pwd.getpwall()]:
            sp.check_call([adduser, "--system", user])
        if not group in [i.gr_name for i in grp.getgrall()]:
            sp.check_call([addgroup, "--system", group])
        if not os.path.exists(log_file_dir_path):
            os.makedirs(log_file_dir_path)
        os.chown(log_file_dir_path, pwd.getpwnam(user).pw_uid, grp.getgrnam(group).gr_gid)
        if os.path.exists(log_file_path):
            os.chown(log_file_path, pwd.getpwnam(user).pw_uid, grp.getgrnam(group).gr_gid)
        sp.check_call([systemctl, "daemon-reload"])
        sp.check_call([systemctl, "start", systemd_unit_name])
示例#2
0
    def install(self):
        """ install config fo pound """

        # configures pound
        # loading templates
        curdir = os.path.dirname(__file__)
        tpl = Template(open(os.path.join(curdir, 'pound.cfg.tpl')).read())
        try:
            tpl.daemon = int(self.options.get('daemon',1))
        except ValueError:
            raise zc.buildout.UserError("Deamon is invalid")
        tpl.log_facility = self.options.get('log_facility','daemon')
        try:
            tpl.log_level = int(self.options.get('log_level',1))
        except ValueError:
            raise zc.buildout.UserError("LogLevel is invalid")
        try:
            tpl.alive = int(self.options.get('alive',30))
        except ValueError:
            raise zc.buildout.UserError("Alive is invalid")
        try:
            tpl.dynscale = int(self.options.get('dynscale',0))
        except ValueError:
            raise zc.buildout.UserError("Dynscale is invalid")

        try:
            tpl.client = int(self.options.get('client',10))
        except ValueError:
            raise zc.buildout.UserError("Client is invalid")
        try:
            tpl.timeout = int(self.options.get('timeout',15))
        except ValueError:
            raise zc.buildout.UserError("Timeout is invalid")

        try:
            tpl.grace = int(self.options.get('grace',30))
        except ValueError:
            raise zc.buildout.UserError("Grace is invalid")
        try:
            tpl.owner = self.buildoptions.get('owner', utils.get_sysuser())
        except ValueError:
            raise zc.buildout.UserError("Owner is invalid")
        try:
            tpl.group = self.buildoptions.get('group', utils.get_sysgroup())
        except ValueError:
            raise zc.buildout.UserError("Group is invalid")
        ## session management
        try:
            tpl.sticky = self.options.get('sticky', 'on')
        except ValueError:
            raise zc.buildout.UserError("Sticky is invalid")
        try:
            tpl.sessiontype = self.options.get('sessiontype', 'COOKIE')
        except ValueError:
            raise zc.buildout.UserError("Sessiontype is invalid")
        try:
            tpl.sessioncookie = self.options.get('sessioncookie', '__ac')
        except ValueError:
            raise zc.buildout.UserError("Sessioncookie is invalid")
        try:
            tpl.sessiontimeout = int(self.options.get('sessiontimeout', 300))
        except ValueError:
            raise zc.buildout.UserError("SessionTimeout is invalid")

        tpl.socket = self.options.get('socket', '')

        # creating balancers
        balancer_cfg = []

        for balancer in self.options.get('balancers', '').split('\n'):

            balancer = balancer.strip()
            if balancer == '':
                continue
            balancer = balancer.split()
            name = balancer[0]
            try:
                (adress, port) = balancer[1].split(':')
            except ValueError:
                raise zc.buildout.UserError("balancer syntax is not correct %s" \
                                            % balancer)

            backends = balancer[2:]
            backends_cfg = []

            for backend in backends:
                backend = backend.split(':')
                host = backend[0]
                port_backend = backend[1]
                priority = 0
                timeout = 0
                if ',' in port_backend:
                    l =  port_backend.split(',')
                    if len(l) == 3:
                        (port_backend, priority, timeout) = l

                    elif len(l) == 2:
                        (port_backend, priority) = l
                    else:
                        raise zc.buildout.UserError("Backend configuration is invalid")
                backends_cfg.append({'host': host,
                                    'port': port_backend,
                                    'priority' : priority,
                                    'timeout': timeout})

            balancer_dict = {'name': name,
                             'port': port,
                             'adress': adress,
                             'backends': backends_cfg}

            balancer_cfg.append(balancer_dict)
        tpl.balancers = balancer_cfg


        # writing the file
        if not  os.path.exists(self.options['location']):
            os.mkdir(self.options['location'])
        etc_dir = os.path.join(self.options['location'], 'etc')
        if not os.path.exists(etc_dir):
            os.mkdir(etc_dir)
        var_dir = os.path.join(self.options['location'], 'var')
        if not os.path.exists(var_dir):
            os.mkdir(var_dir)

        filename = os.path.join(etc_dir, 'pound.cfg')
        f = open(filename, 'w')

        try:
            print >>f, tpl
        finally:
            f.close()

        if self.buildoptions:
            self.options['executable'] = os.path.join(self.buildoptions['location'],
                                                      'sbin','pound')
        else:
            if not self.options.get('executable'):
                raise zc.buildout.UserError("path to executable is required")


        pid = os.path.join(var_dir, 'pound.pid')
        scripts = []
        for x in ('poundctl', 'poundrun', 'poundcontrol'):
            ctl = Template(open(os.path.join(curdir, x+'.tpl')).read())
            ctl.poundbin = self.options['executable']
            ctl.socket = self.options.get('socket', '')
            ctl.poundcontrol = os.path.join(
                os.path.dirname(self.options['executable']),
                'poundctl'
            )
            ctl.logfilename = self.options.get('log_file', os.path.join(var_dir, 'pound.log'))
            ctl.poundcfg = filename
            ctl.poundpid = pid
            bin_dir = self.buildout['buildout']['bin-directory']
            script_name = os.path.join(bin_dir, self.options.get('%s-binary' % x, x))
            try:
                os.remove(script_name)
            except OSError:
                pass
            f = open(script_name, 'w')
            try:
                print >>f, ctl
            finally:
                f.close()
            mode = os.stat(script_name).st_mode
            os.chmod(script_name, mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
            scripts.append(script_name)

        return [etc_dir] + scripts