def includeme(config): Environment.initialize(config.registry.settings, None) config.include(add_routes) config.add_renderer('taskresponse', 'aybu.manager.rest.renderers.TaskResponseRender') config.add_renderer(None, 'pyramid.renderers.json_renderer_factory') config.scan()
def create(context, request): try: name = request.params['name'] virtualenv_name = request.params.get('virtualenv_name') # try to get the env from db, as it must not exists Environment.get(request.db_session, name) except KeyError as e: raise ParamsError(e) except NoResultFound: # no env found, submit the task return request.submit_task('environment.create', name=name, virtualenv_name=virtualenv_name) else: # found instance, give conflict error = 'environment {} already exists'.format(name) raise HTTPConflict(headers={'X-Request-Error': error})
def __init__(self, context, config): super(AybuManagerDaemonWorker, self).__init__(name='worker') self.config = config self.log = logging.getLogger(__name__) self.context = context self.config["sqlalchemy.poolclass"] = NullPool self.engine = engine_from_config(self.config, 'sqlalchemy.') self.Session = scoped_session(sessionmaker(bind=self.engine)) Base.metadata.bind = self.engine Base.metadata.create_all() redis_opts = {k.replace('redis.', ''): self.config[k] for k in self.config if k.startswith('redis.')} if 'port' in redis_opts: redis_opts['port'] = int(redis_opts['port']) self.redis = redis.StrictRedis(**redis_opts) self.pub_socket = self.context.socket(zmq.PUB) self.pub_socket.bind(self.config['zmq.status_pub_addr']) Environment.initialize(self.config, section=None)
def test_create(self): with self.assertRaises(ValidationError): env = Environment.create(self.session, 'test-env', config=self.config) env = Environment.create(self.session, 'testenv', config=self.config) keys = {k.replace('paths.', ''): self.config[k] for k in self.config if k.startswith('paths.')} for key, path in keys.iteritems(): if key.startswith('virtualenv'): continue if key.startswith('cgroups'): continue self.log.debug("%s: %s", key, path) self.assertTrue(os.path.isdir(path)) self.session.commit() self.assert_env_paths(env)
def switch_environment(session, task, id, environment): instance = Instance.get(session, id) new_env = Environment.get(session, environment) reenable = False if instance.enabled: reenable = True instance.enabled = False instance.environment = new_env if reenable: instance.enabled = True
def test_create_single_cgroup(self): controllers = self.config['paths.cgroups.controllers'] rel_path = self.config['paths.cgroups.relative_path'] del self.config['paths.cgroups.controllers'] self.config['paths.cgroups.relative_path'] = '/' env = Environment.create(self.session, 'testenv', config=self.config) self.session.commit() self.assert_env_paths(env) #restore options self.config['paths.cgroups.controllers'] = controllers self.config['paths.cgroups.realtive_path'] = rel_path
def deploy(session, task, domain, owner_email, environment_name, technical_contact_email, theme_name=None, default_language=u'it', database_password=None, enabled=True): env = Environment.get(session, environment_name) theme = None if not theme_name else Theme.get(session, theme_name) owner = User.get(session, owner_email) technical_contact = User.get(session, technical_contact_email) instance = Instance.deploy(session, domain, owner, env, technical_contact, theme, default_language, database_password, enabled) return instance.id
def test_delete(self): env = Environment.create(self.session, 'testenv', config=self.config) env.delete()
def rewrite(context, request): env = Environment.get(request.db_session, request.matchdict['name']) taskname = "environment.rewrite" return request.submit_task(taskname, name=env.name)
def info(context, request): env = Environment.get(request.db_session, request.matchdict['name']) return env.to_dict(paths=True, instances=True)
def list(context, request): return {e.name: e.to_dict() for e in Environment.all(request.db_session)}
def deploy(context, request): try: params = dict( domain=request.params['domain'], owner_email=request.params['owner_email'], environment_name=request.params['environment_name'], technical_contact_email=request.params.get( 'technical_contact_email', request.params['owner_email']), theme_name=request.params.get('theme_name'), default_language=request.params.get('default_language', u'it'), database_password=request.params.get('database_password'), enabled=True if request.params.get('enabled') else False, verbose=True if request.params.get('verbose') else False, ) check_domain_not_used(request, params['domain']) params['domain'] = validate_hostname(params['domain']) # try to get the instance, as it MUST not exists Instance.get_by_domain(request.db_session, params['domain']) except KeyError as e: log.exception("Error validating params") raise ParamsError(e) except NoResultFound: # no instance found, validate relations. try: field = 'owner_email' cls = 'user' User.log.debug("Validating owner %s", params[field]) owner = User.get(request.db_session, params[field]) if not owner: raise NoResultFound() if params['technical_contact_email'] != params['owner_email']: field = 'technical_contact_email' User.log.debug("Validating contact %s", params[field]) ctc = User.get(request.db_session, params[field]) if not ctc: raise NoResultFound() field = 'environment_name' cls = 'environment' Environment.log.debug("validating environment %s", params[field]) env = Environment.get(request.db_session, params[field]) if not env: raise NoResultFound() field = 'theme_name' cls = 'theme' if params[field]: Theme.log.debug("Validating theme %s", params[field]) theme = Theme.get(request.db_session, params[field]) if not theme: raise NoResultFound() except NoResultFound: raise ParamsError('{} "{}" for {} not found'\ .format(cls.title(), params[field], field)) else: log.info("Submitting task") # relations exists, submit tasks return request.submit_task('instance.deploy', **params) else: # found instance, conflict error = 'instance for domain {} already exists'\ .format(params['domain']) raise HTTPConflict(headers={'X-Request-Error': error})
def test_deploy(self): self.import_data() owner = self.session.query(User)\ .filter(User.email == '*****@*****.**').one() theme = self.session.query(Theme)\ .filter(Theme.name == 'uffizi').one() env = Environment.create(self.session, 'testenv', config=self.config) instance = Instance.deploy(self.session, 'www.example.com', owner, env, owner, theme=theme) # vassal config is created only upon session commit self.assertFalse(os.path.exists(instance.paths.vassal_config)) self.session.commit() self.assertTrue(os.path.exists(instance.paths.vassal_config)) # various asserts for consistency with self.assertRaises(NotSupported): instance.domain = 'www.newdomain.com' with self.assertRaises(NotImplementedError): nt = self.session.query(Theme)\ .filter(Theme.name == 'moma').one() instance.theme = nt # test reload os.unlink(instance.paths.vassal_config) instance.reload() self.assertFalse(os.path.exists(instance.paths.vassal_config)) self.session.commit() self.assertTrue(os.path.exists(instance.paths.vassal_config)) self.assertTrue(instance.enabled) # test disable instance.enabled = False self.session.commit() self.assertFalse(os.path.exists(instance.paths.vassal_config)) with self.assertRaises(OperationalError): instance.reload() # disable an already disable instance is a no-op self.assertFalse(instance.enabled) instance.enabled = False self.session.rollback() self.assertFalse(instance.enabled) # test enable instance.enabled = True self.assertFalse(os.path.exists(instance.paths.vassal_config)) self.session.commit() self.assertTrue(os.path.exists(instance.paths.vassal_config)) # enabling an already enabled instance is a no-op self.assertTrue(instance.enabled) instance.enabled = True self.session.rollback() self.assertTrue(instance.enabled) # test change environment newenv = Environment.create(self.session, 'testenv2', config=self.config) with self.assertRaises(OperationalError): instance.environment = newenv self.assertEqual(instance.environment, env) instance.enabled = False self.session.commit() instance.environment = newenv self.assertFalse(hasattr(instance, "_paths")) self.session.commit() self.assertEqual(instance.environment, newenv) instance.enabled = True self.session.commit() # "test" migration: call it at least once :( instance.upgrade_schema('head') # create an archive instance.archive(archive_name='test') p = os.path.join(instance.environment.paths.archives, "test.tar.gz") self.assertTrue(os.path.exists(p)) # test restore and delete with self.assertRaises(OperationalError): instance.delete() with self.assertRaises(OperationalError): instance.restore(archive_name='test') instance.enabled = False self.session.commit() #instance.restore(archive_name='test') instance.delete() self.assertFalse(os.path.exists(instance.paths.dir)) self.session.commit() self.assertFalse(os.path.exists(instance.paths.dir)) # todo assert database is not present """
def rewrite(session, task, name): env = Environment.get(session, name) env.rewrite()
def delete(session, task, name): env = Environment.get(session, name) env.delete()
def create(session, task, name, virtualenv_name=None): env = Environment.create(session, name, virtualenv_name) return env.name