Example #1
0
def index():
    log.debug("index")
    config = cfg.new()
    return {
        'cfgfile': config.filename(),
        'cfg': _getCfg(config),
    }
Example #2
0
def index():
    log.debug("index")
    config = cfg.new()
    return {
        'cfgfile': config.filename(),
        'cfg': _getCfg(config),
        'user': getenv('USER', 'nouser?'),
    }
Example #3
0
 def mock(self, tag='cmd'):
     mockcfg = cfg.new(self.cfgfile)
     program = flags.program
     with mock.log(), mock.utils(mockcfg, tag=tag) as ctx:
         try:
             flags.program = 'sadm'
             yield ctx
         finally:
             flags.program = program
Example #4
0
 def __init__(self, name, config=None):
     log.debug("profile init %s" % name)
     if config is None:
         config = cfg.new()
     if name == 'default':
         name = config.get('default', 'profile')
     self._name = name
     self._load(config)
     self.config = config
Example #5
0
def _check(envfn):
    if not envfn.endswith('.env'):
        log.error("invalid file name: %s" % envfn)
        return 1
    env = path.basename(envfn)[:-4]
    config = cfg.new(deploy.cfgfile)
    if not env in config.listEnvs('deploy'):
        log.error("%s env not found" % env)
        return 2
    return 0
Example #6
0
 def mock(self, tag='webapp'):
     mockcfg = None
     if path.isfile(self.cfgfn):
         mockcfg = cfg.new(self.cfgfn)
     with mock.log(), mock.utils(mockcfg, tag=tag):
         ctx = Mock()
         ctx.orig = _orig
         ctx.view = _mock.view
         ctx.wapp = _mock.wapp
         ctx.tpl = _mock.tpl
         try:
             yield ctx
         finally:
             pass
Example #7
0
def run(profile, env, action, cfgfile=None, sumode='user'):
    err = None
    e = None
    try:
        if isinstance(env, Env):
            e = env
        else:
            e = Env(profile, env, cfg.new(cfgfile=cfgfile))
        envAction.run(e, action, sumode=sumode)
    except EnvError as err:
        return (1, err)
    except Error as err:
        log.error("%s" % err)
        return (2, err)
    return (0, err)
Example #8
0
def main(args, sumode):
    log.debug("deploy %s sumode=%s" % (args.env, sumode))
    if sumode == 'not':
        config = cfg.new(cfgfile=cfgfile)
        dn = config.get('deploy',
                        'rundir',
                        fallback=path.join('~', '.local', 'sadm', 'deploy'))
        sh.makedirs(dn, mode=0o750, exists_ok=True)
        with sh.lockd(dn):
            env = Env('deploy', args.env, config)
            try:
                _check(_sumode(env, 'pre'))
                _check(_usermode(env))
                _check(_sumode(env, 'post'))
            except CmdError as err:
                return err.code
    else:
        return cmd.run(args.env, sumode)
    return 39
Example #9
0
def test_cfg():
	assert cfg._DEFAULT['name'] == ''
	assert cfg._DEFAULT['profile'] == 'default'
	assert cfg._DEFAULT['env'] == 'default'
	c = cfg.new()
	assert isinstance(c, ConfigParser)
	assert c.name() == 'sadmtest'
	assert c.filename().endswith(path.join('tdata', 'sadm.cfg'))
	assert c.get('default', 'name') == 'sadmtest'
	assert c.get('default', 'profile') == 'testing'
	assert c.get('default', 'env') == 'testing'
	assert c.get('default', 'dir') == '.'
	assert c.listPlugins('testing') == ['sadm', 'os', 'testing']
	assert sorted(c.listProfiles()) == ['cmd', 'envsetup', 'plugin', 'testing']
	assert c.has_section('testing')
	assert c.get('testing', 'dir') == './tdata'
	assert c.get('testing', 'env.testing') == 'testing/config.ini'
	assert sorted(c.listEnvs('testing')) == [
		'testing', 'testing.include', 'testing.include.error', 'testing.nodir']
Example #10
0
def _newPlugin(name='testing',
               ns='_sadm',
               cfgfn=None,
               deploy=False,
               buildDeploy=True,
               buildCfg='build.ini',
               cfgdir='config'):
    pdir = name.replace('.', path.sep)
    profile = 'plugin'
    config = None
    if deploy:
        profile = 'deploy'
        config = cfg.new(cfgfile=path.join('tdata', 'deploy.cfg'))
    env = _newEnv(profile=profile, name=name, config=config)
    if cfgfn is not None:
        env._cfgfile = path.join(pdir, cfgdir, cfgfn)
    p = Plugin(name, env, ns=ns)
    if deploy and buildDeploy:
        _buildDeploy(name, ns=ns, cfgfn=buildCfg)
    return p
Example #11
0
 def mock(self, tag='devops'):
     mockcfg = None
     if path.isfile(self.cfgfn):
         mockcfg = cfg.new(self.cfgfn)
     bup = Mock()
     # bup bottle
     bup.bottle = bup.mock.bottle
     bup.bottle.template = bottle.template
     # bup wapp
     bup.wapp = _sadm.devops.wapp.wapp.wapp
     # bup tpl
     bup.tpl = bup.mock.tpl
     bup.tpl.parse = _sadm.devops.wapp.tpl.tpl.parse
     bup.tpl.version = _sadm.devops.wapp.tpl.tpl.version
     # bup session
     bup.session = bup.mock.session
     bup.session._secret = _sadm.devops.wapp.session.session._secret
     with mock.log(), mock.utils(mockcfg, tag=tag):
         ctx = Mock()
         ctx.orig = bup
         try:
             # mock bottle
             ctx.bottle = ctx.mock.bottle
             ctx.bottle.template = ctx.mock.bottle.template
             bottle.template = ctx.bottle.template
             # test session db dir
             sessdbdir = path.join('tdata', 'tmp', 'devops', 'wapp',
                                   'session')
             if path.isdir(sessdbdir):
                 print('mock.devops.wapp rmtree', sessdbdir)
                 rmtree(sessdbdir)
             # mock wapp
             print('mock.devops.wapp init')
             _sadm.devops.wapp.wapp.init(cfgfn=self.cfgfn)
             ctx.wapp = ctx.mock.wapp
             _sadm.devops.wapp.wapp.wapp = ctx.wapp
             # mock config
             ctx.config = _sadm.devops.wapp.cfg.config
             # mock tpl
             ctx.tpl = ctx.mock.tpl
             ctx.tpl.parse = ctx.mock.tpl.parse
             _sadm.devops.wapp.tpl.tpl.parse = ctx.tpl.parse
             ctx.tpl.version = ctx.mock.tpl.version
             ctx.tpl.version.string = ctx.mock.tpl.version.string
             ctx.tpl.version.string.return_value = 'testing'
             _sadm.devops.wapp.tpl.tpl.version = ctx.tpl.version
             yield ctx
         finally:
             print('mock.devops.wapp.restore')
             # restore bottle
             del bottle.template
             bottle.template = bup.bottle.template
             # restore wapp
             del _sadm.devops.wapp.wapp.wapp
             _sadm.devops.wapp.wapp.wapp = bup.wapp
             # restore config
             del _sadm.devops.wapp.cfg.config
             _sadm.devops.wapp.cfg.config = None
             # restore tpl
             del _sadm.devops.wapp.tpl.tpl.parse
             _sadm.devops.wapp.tpl.tpl.parse = bup.tpl.parse
             del _sadm.devops.wapp.tpl.tpl.version
             _sadm.devops.wapp.tpl.tpl.version = bup.tpl.version
             # restore session
             del _sadm.devops.wapp.session.session._secret
             _sadm.devops.wapp.session.session._secret = bup.session._secret
Example #12
0
def _getallProfiles():
    config = cfg.new()
    l = []
    for p in config.listProfiles():
        l.append(_getProfile(p, config))
    return l
Example #13
0
def test_noname():
	fn = path.join('tdata', 'testing', 'config-noname.ini')
	c = cfg.new(cfgfile = fn)
	assert c.name() == 'testing'
Example #14
0
def test_file_not_found():
	fn = path.join('tdata', 'testing', 'noconfig.ini')
	with raises(ProfileError, match = 'noconfig.ini file not found'):
		c = cfg.new(cfgfile = fn)
Example #15
0
def test_name():
	c = cfg.new()
	assert c.name() == 'sadmtest'
	assert c._getName() == 'tdata'
Example #16
0
def test_profile_error():
	c = cfg.new()
	with raises(ProfileError, match = 'config profile noprofile not found'):
		c.listEnvs('noprofile')