def test_instantiate_service(self): services = [ { 'template': 'github.com/threefoldtech/0-robot/node/0.0.1', 'service': 'name', }, { 'template': 'github.com/threefoldtech/0-robot/vm/0.0.1', 'service': 'name', }, ] service_created, err_code, err_msg = instantiate_services(services) assert err_code is None assert err_msg is None self.assertEqual(len(scol.list_services()), 2) self.assertEqual( len( scol.find( template_uid='github.com/threefoldtech/0-robot/node/0.0.1') ), 1) self.assertEqual( len( scol.find( template_uid='github.com/threefoldtech/0-robot/vm/0.0.1')), 1)
def listServicesHandler(): ''' List all the services known by the ZeroRobot. It is handler for GET /services ''' kwargs = {} for x in [ "name", "template_uid", "template_host", "template_account", "template_repo", "template_name", "template_version" ]: val = request.args.get(x) if val: kwargs[x] = val if auth.god_jwt.check_god_token(request): # god token passed, we list all the services services = [service_view(s) for s in scol.find(**kwargs)] else: # normal flow, only return service for which the user has the secret allowed_services = auth.user_jwt.extract_service_guid(request) services = [ service_view(s) for s in scol.find(**kwargs) if s.guid in allowed_services or scol.is_service_public(s.guid) is True ] return json.dumps(services), 200, {"Content-type": 'application/json'}
def test_instantiate_service_validation_fail(self): assert len(scol.find()) == 0 Validate = self.load_template('validate') with pytest.raises(ValidationError) as err: srv = tcol.instantiate_service(Validate) assert len(scol.find()) == 0
def _create_node_service(): service_found = scol.find(template_host='github.com', template_account='zero-os', template_name='node') if not service_found: template_found = tcol.find(host='github.com', account='zero-os', name='node') if not template_found: tcol.add_repo("https://github.com/zero-os/0-templates") template_found = tcol.find(host='github.com', account='zero-os', name='node') if not template_found: raise RuntimeError( "cannot create node service and --mode node is set") logger.info("create node service because --mode node is net") node = tcol.instantiate_service(template_found[0], 'local', {}) else: node = service_found[0] try: node.state.check('actions', 'install', 'ok') except: node.schedule_action('install') node.schedule_action('_register')
def _find_services_to_be_scheduled(actions): services_guids = [] for action_item in actions: template_uid = None template = action_item.get("template") if template: template_uid = TemplateUID.parse(template) service = action_item.get("service") candidates = [] kwargs = {"name": service} if template_uid: kwargs.update({ "template_host": template_uid.host, "template_account": template_uid.account, "template_repo": template_uid.repo, "template_name": template_uid.name, "template_version": template_uid.version, }) # filter out None value kwargs = {k: v for k, v in kwargs.items() if v is not None} if len(kwargs) > 0: candidates = scol.find(**kwargs) else: candidates = scol.list_services() services_guids.extend([s.guid for s in candidates]) return services_guids
def checkout_repo(url, revision='master'): logger = j.logger.get('zerorobot') logger.info("checkout %s for repo %s", revision, url) dir_path = git.url.git_path(url) if not os.path.exists(dir_path): raise TemplateNotFoundError() repo = git.repo.Repo(dir_path) repo.fetch() repo.repo.checkout(revision) t, _ = repo.branch_or_tag() if t == 'branch': repo.pull() # load the new templates logger.info("reload templates") updated_templates = add_repo(url) # pool of greenlet used to upgrade service concurrently pool = Pool(25) for template in updated_templates: for service in scol.find(template_host=template.template_uid.host, template_account=template.template_uid.account, template_repo=template.template_uid.repo, template_name=template.template_uid.name): pool.spawn(scol.upgrade, service, template, True) pool.join(raise_error=True)
def test_add_get_service(self): service = FakeService('1234567890', 's1') scol.add(service) self.assertEqual(service, scol.get_by_guid('1234567890')) self.assertEqual(service, scol.get_by_name('s1')) self.assertEqual(service, scol.find(name='s1')[0]) with self.assertRaises(scol.ServiceNotFoundError): scol.get_by_name("nan")
def test_search_services(self): s1 = FakeService('1111', 's1') s2 = FakeService('2222', 's2') s3 = FakeService2('3333', 's3') scol.add(s1) scol.add(s2) scol.add(s3) results = scol.find(template_uid='github.com/threefoldtech/0-robot/fakeservice/0.0.1') self.assertEqual(len(results), 2) guids = [s1.guid, s2.guid] for s in results: self.assertIn(s.guid, guids)
def find(self, **kwargs): """ Search for services and filter results from kwargs. You can filter on: "name", "template_uid", "template_host", "template_account", "template_repo", "template_name", "template_version" example: to list all services with name foo: find(name='foo') """ services = {} for service in scol.find(**kwargs): services[service.guid] = service return list(services.values())
def instantiate_service(template, name=None, data=None): if isinstance(template, str): template = get(template) existing = scol.find(template_uid=str(template.template_uid), name=name) if name and len(existing) > 0: raise ServiceConflictError(message="a service with name=%s already exist" % name, service=existing[0]) service = template(data=data, name=name) try: service.validate() except Exception as err: service.delete() raise ValidationError("fail to create service %s %s" % (name, template.template_uid), err) service.save() scol.add(service) return service
def listServicesHandler(): ''' List all the services known by the ZeroRobot. It is handler for GET /services ''' kwargs = {} for x in [ "name", "template_uid", "template_host", "template_account", "template_repo", "template_name", "template_version" ]: val = request.args.get(x) if val: kwargs[x] = val allowed_services = extract_guid_from_headers(request.headers) services = [ service_view(s) for s in scol.find(**kwargs) if s.guid in allowed_services or scol.is_service_public(s.guid) is True ] return json.dumps(services), 200, {"Content-type": 'application/json'}
def _schedule_action(action_item): template_uid = None template = action_item.get("template") if template: template_uid = TemplateUID.parse(template) service = action_item.get("service") action = action_item.get("action") args = action_item.get("args") if args and not isinstance(args, dict): raise TypeError("args should be a dict not %s" % type(args)) candidates = [] kwargs = {"name": service} if template_uid: kwargs.update({ "template_host": template_uid.host, "template_account": template_uid.account, "template_repo": template_uid.repo, "template_name": template_uid.name, "template_version": template_uid.version, }) # filter out None value kwargs = {k: v for k, v in kwargs.items() if v is not None} if len(kwargs) > 0: candidates = scol.find(**kwargs) else: candidates = scol.list_services() tasks = [] for service in candidates: t = service.schedule_action(action, args=args) tasks.append((t, service)) return tasks