コード例 #1
0
ファイル: __init__.py プロジェクト: jrmsdev/pysadm
	def deploy(self, mockCfg = 'build.ini'):
		from _sadm.env.settings import Settings
		cfgfn = path.join('tdata', 'plugin',
			self._p.name.replace('.', path.sep), 'config', mockCfg)
		cfg = Settings()
		with open(cfgfn, 'r') as fh:
			cfg.read_file(fh)
		with mock.utils(cfg, 'deploy'):
			self._envAction.run(self._env, 'deploy', sumode = self._p.sumode)
コード例 #2
0
ファイル: __init__.py プロジェクト: jrmsdev/pysadm
 def __init__(self, profile, name, config=None):
     self.profile = Profile(profile, config)
     if name == 'default':
         name = self.profile.config.get('default', 'env')
     self._name = name
     self._profName = self.profile.name()
     self._logtag = ''
     self._run = {}
     self.settings = Settings()
     self.session = Session()
     self._load()
     self._check()
コード例 #3
0
def test_merge_filter(testing_settings):
    def optfilter(opt):
        if opt == 'opt1' or opt == 'opt3':
            return opt
        return None

    cfgfn = path.join('tdata', 'testing', 'config-merge.ini')
    s = testing_settings()
    cfg = Settings()
    with open(cfgfn, 'r') as fh:
        cfg.read_file(fh)
    assert s.options('testing') == []
    s.merge(cfg, 'testing', optfilter)
    assert sorted(s.options('testing')) == ['opt1', 'opt3']
コード例 #4
0
ファイル: configure.py プロジェクト: jrmsdev/pysadm
def configure(env, cfg):
    env.settings.merge(cfg, 'os', (
        'users.config.dir',
        'users.config.file',
        'users.dirmode',
        'users.filemode',
    ))

    udir = env.settings.get('os', 'users.config.dir')
    fn = env.settings.get('os', 'users.config.file')

    db = Settings()
    with env.assets.open(udir, fn) as fh:
        env.debug("users config %s" % fh.name)
        db.read_file(fh)

    _addgroups(env, db, cfg)
    _addusers(env, db, cfg)
コード例 #5
0
def test_merge(testing_settings):
    cfgfn = path.join('tdata', 'testing', 'config-merge.ini')
    s = testing_settings()
    assert sorted(s.sections()) == ['os', 'os.pkg', 'sadm', 'sync', 'testing']
    assert s.options('testing') == []
    cfg = Settings()
    with open(cfgfn, 'r') as fh:
        cfg.read_file(fh)
    s.merge(cfg, 'nosection', ('opt0', 'opt1'))
    assert sorted(s.sections()) == ['os', 'os.pkg', 'sadm', 'sync', 'testing']
    s.merge(cfg, 'newsection', ('opt0', 'opt1'))
    assert sorted(s.sections()) == [
        'newsection', 'os', 'os.pkg', 'sadm', 'sync', 'testing'
    ]
    s.merge(cfg, 'testing', ('noopt0', 'noopt1', 'noopt2'))
    assert s.options('testing') == []
    s.merge(cfg, 'testing', ('opt0', 'opt1', 'opt2'))
    assert sorted(s.options('testing')) == ['opt0', 'opt1', 'opt2']
コード例 #6
0
def _pcheck(p, fn):
    pcfg = Settings()
    with open(fn, 'r') as fh:
        pcfg.read_file(fh)
    if pcfg.has_section('_sadmtest.check'):
        for opt, val in pcfg.items('_sadmtest.check'):
            if opt.startswith('builddir.file.'):
                _checkBuilddirFile(p, val)
コード例 #7
0
ファイル: __init__.py プロジェクト: jrmsdev/pysadm
class Env(object):
    _name = None
    _cfgfile = None
    _profile = None
    _profName = None
    _logtag = None
    _run = None
    _lockfn = None
    _err = None
    profile = None
    assets = None
    settings = None
    session = None
    build = None

    def __init__(self, profile, name, config=None):
        self.profile = Profile(profile, config)
        if name == 'default':
            name = self.profile.config.get('default', 'env')
        self._name = name
        self._profName = self.profile.name()
        self._logtag = ''
        self._run = {}
        self.settings = Settings()
        self.session = Session()
        self._load()
        self._check()

    def __str__(self):
        return "%s/%s" % (self._profName, self._name)

    def _load(self, fn=None, pdir=None):
        self.debug('env load')
        # env self.config.ini
        opt = "env.%s" % self._name
        if fn is None:
            fn = self.profile.config.get(self._profName, opt, fallback=None)
            if fn is None:
                raise self.error("'%s/%s' env not found" %
                                 (self._profName, self._name))
        fn = path.normpath(fn.strip())
        if fn == '.':
            raise self.error('config file not set')
        self._cfgfile = fn
        # profile dir
        if pdir is None:
            pdir = self.profile.config.get(self._profName, 'dir')
        pdir = path.normpath(pdir.strip())
        if not path.exists(pdir):
            raise self.error("%s directory not found" % pdir)
        if not path.isdir(pdir):
            raise self.error("%s is not a directory" % pdir)
        # env assets
        _rootdir = path.realpath(pdir)
        self.assets = asset.Manager(_rootdir)
        self.debug("assets %s" % _rootdir)
        # env builddir
        _builddir = path.join(path.dirname(_rootdir), 'build', self._profName,
                              self._name)
        self.build = build.Manager(_builddir)
        self.debug("builddir %s" % self.build.rootdir())

    def _check(self):
        # check env dist setting
        if self.dist() not in dist.SUPPORTED.keys():
            raise self.error("unsupported dist %s" % self.dist())

    def configure(self, cfgfile=None):
        self.debug('session start')
        self.session.start()
        plugins.configure(self, cfgfile=cfgfile)

    def name(self):
        return self._name

    def cfgfile(self):
        return self._cfgfile

    def _log(self, func, msg):
        func("%s/%s%s %s" % (self._profName, self._name, self._logtag, msg))

    def log(self, msg):
        self._log(log.msg, msg)

    def info(self, msg):
        self._log(log.info, msg)

    def warn(self, msg):
        self._log(log.warn, msg)

    def debug(self, msg):
        tag = "%s/%s%s" % (self._profName, self._name, self._logtag)
        log.debug("%s" % msg, depth=4, tag=tag)

    def error(self, msg):
        self._err = EnvError(msg)
        self._log(log.error, msg)
        return self._err

    def getError(self):
        return self._err

    def start(self, action, msg=''):
        if self._run.get(action, None) is not None:
            raise self.error("%s action already started" % action)
        prev = self._logtag
        self._logtag = " %s" % action
        sep = ''
        if msg != '':
            sep = ' '
        self.info("start%s%s" % (sep, msg))
        self._run[action] = {'tag.prev': prev, 'start': time()}

    def end(self, action):
        if self._run.get(action, None) is None:
            raise self.error("%s action was not started" % action)
        self._run[action]['end'] = time()
        self.info("end (%fs)" %
                  (self._run[action]['end'] - self._run[action]['start']))
        self._logtag = self._run[action]['tag.prev']

    def report(self, action, startTime=None):
        actno = 0
        noend = []
        for n, d in self._run.items():
            actno += 1
            if d.get('end', None) is None:
                noend.append(n)
        took = ''
        if startTime is not None:
            took = " in %f seconds" % (time() - startTime)
        self.log("%s %d/%d actions%s" % (action,
                                         (actno - len(noend)), actno, took))
        if len(noend) > 0:
            raise self.error("not finished action(s): %s" % ','.join(noend))

    def plugins(self, action, revert=False):
        for p in self.settings.plugins(revert=revert):
            yield getPlugin(p, action)

    def dist(self):
        return self.settings.get('sadm', 'env.dist', fallback='debian')

    @contextmanager
    def lock(self):
        try:
            yield _lock(self)
        finally:
            _unlock(self)