def write(self, entries): shard_string_index = str(random.randint(0, NUM_SHARDS - 1)) slice = Slice.get_by_id(shard_string_index) if slice is None: slice = Slice(id=shard_string_index) if slice.content: entries.append(slice.content) slice.content = '\r\n'.join(entries) slice.put() logging.info(shard_string_index)
def get(self, id): lab = Lab.fetchone(id=id) if lab is None: return jsonify(message="Lab doesn't existed"), 410 if lab.status == 'inactive': return jsonify({ 'id': lab.id, 'name': lab.name, 'status': lab.status }) else: slices = [] for sl in Slice.fetchall(lab_id=lab.id): user = User.fetchone(id=sl.user_id) slices.append({ 'id': sl.id, 'name': sl.name, 'status': sl.status, 'username': user.fullname }) return jsonify({ 'id': lab.id, 'name': lab.name, 'status': lab.status, 'slices': slices, 'errors': lab.error_msgs })
def read(self): contents=[] for slice in Slice.query(): contents.append(slice.content) slice.content=[] slice.put() content='\r\n'.join(contents) if WRITE_BLOCK: Block(content=contents) return content
def deploy(self, id): cloudconfig_id = request.get_json()['cloudConfigId'] cloudconfig = CloudConfig.fetchone(id=cloudconfig_id) users = request.get_json()['users'] lab = Lab.fetchone(id=id) lab.update(status='deploying') """Chances are you are redeploying a lab, whose slices are already created""" slices = Slice.fetchall(lab_id=lab.id) """Otherwise, create slices for the lab""" if len(slices) == 0: for index, user in enumerate(users): new_slice = Slice.insert(lab_id=lab.id, status='deploying', user_id=user['id'], name=lab.name + ' / slice_' + str(index), cloud_attrs={}) slices.append(new_slice) scenario = Scenario.fetchone(id=lab.scenario_id) if cloudconfig.provider == 'Openstack': return _deploy_openstack(id, cloudconfig, slices, scenario)
def index(self): """Get labs as a owner""" labs = Lab.fetchall(owner_id=g.user['id']) ret = [] for l in labs: ret.append({ 'id': l.id, 'name': l.name, 'description': l.description, 'status': l.status, 'slices': len(Slice.fetchall(lab_id=l.id)) }) return jsonify(sorted(ret, key=lambda i: i['id'], reverse=True))
def _get_slices(db=None, config=None, slices=None): if slices is None: return [] if db is None: db = get_mongo_db(config) return [Slice(db, slice['start'], slice['num_of_samples'], None, slice.get('id', None)) for slice in slices if not slice['is_sparse']] + \ [SparseSlice(db, slice['start'], slice['num_of_samples'], None, slice.get('id', None)) for slice in slices if slice['is_sparse']]
def create_sec_group(cloudconfig: CloudConfig, lab_id, lab_slice: Slice, scenario): try: openstack = Openstack(cloudconfig.detail['openstackAuthURL'], cloudconfig.detail['openstackProject'], cloudconfig.detail['openstackUser'], cloudconfig.detail['openstackPassword']) sec_group_id = openstack.create_security_group(lab_slice.name) new_cloud_attrs = lab_slice.cloud_attrs new_cloud_attrs['sec_group_id'] = sec_group_id lab_slice.update(cloud_attrs=new_cloud_attrs.value) for rule in scenario.sg_rules: _add_security_group_rule(openstack, sec_group_id, rule) return sec_group_id except Exception as ex: error_type = 'Create security group error' error_msgs = [error_type + ': ' + str(ex)] lab = Lab.fetchone(id=lab_id) lab.update(status='deployfailed', error_msgs=lab.error_msgs + error_msgs) raise Exception( error_type ) # Raise exception to not execute the next job in the dependency link
def _destroy_openstack(lab_id, cloudconfig): last_jobs = [] last_jobs_ids = [] for lab_slice in Slice.fetchall(lab_id=lab_id): delete_routers_job = queue.enqueue(openstackjobs.delete_routers, cloudconfig, lab_id, lab_slice) delete_instances_job = queue.enqueue(openstackjobs.delete_instances, cloudconfig, lab_id, lab_slice, depends_on=delete_routers_job) delete_networks_job = queue.enqueue(openstackjobs.delete_networks, cloudconfig, lab_id, lab_slice, depends_on=delete_instances_job) # Destroy security group delete_sec_group_job = queue.enqueue(openstackjobs.delete_sec_group, cloudconfig, lab_id, lab_slice, depends_on=delete_networks_job, result_ttl=5000) last_jobs.append(delete_sec_group_job) last_jobs_ids.append(delete_sec_group_job.get_id()) queue.enqueue(openstackjobs.delete_lab, lab_id, last_jobs_ids, depends_on=last_jobs[-1]) return jsonify(message="ok")
def get(self, id): sl = Slice.fetchone(id=id) # only user or the lab creator can access if sl.user_id != g.user['id']: lab = Lab.fetchone(id=sl.lab_id) if lab.owner_id != g.user['id']: return jsonify( message="Access denied to this slice resource"), 403 # get networks with Select(NetworkNode, 'slice_id=%s and status!=%s') as select: select.execute((sl.id, 'inactive')) networks = [{ 'id': n.id, 'name': n.name, 'cidr': n.cidr, 'status': n.status, 'x': n.x, 'y': n.y, 'type': 'NetworkNode' } for n in select.fetchall()] instances = [] links = [] # get instances with Select( Instance, 'instances.slice_id=%s and instances.status!=%s') as select: select.execute((sl.id, 'inactive')) results = select.fetchall() for res in results: instance = res instances.append({ 'id': instance.id, 'name': instance.name, 'public_ip': instance.public_ip, 'status': instance.status, 'password': instance.password, 'x': instance.x, 'y': instance.y, 'configurations': instance.configurations. value, # TODO: remove .value here after implement it in postgrespy 'type': 'Instance' }) for link in instance.links: links.append({ 'gid': link['gid'], 'ip': link['ip'], 'network': link['network'], 'target': link['target'], 'type': 'NetworkLink' }) # get routers routers = [] with Select(Router, 'routers.slice_id=%s and routers.status!=%s') as select: select.execute((sl.id, 'inactive')) results = select.fetchall() for res in results: router = res routers.append({ 'id': router.id, 'name': router.name, 'public_ip': router.public_ip, 'status': router.status, 'password': router.password, 'x': router.x, 'y': router.y, 'configurations': router.configurations. value, # TODO: remove .value here after implement it in postgrespy 'type': 'Router' }) for link in router.links: links.append({ 'gid': link['gid'], 'ip': link['ip'], 'network': link['network'], 'target': link['target'], 'type': 'NetworkLink' }) user = User.fetchone(id=sl.user_id) return jsonify({ 'status': sl.status, 'name': sl.name, 'username': user.fullname, 'networks': networks, 'instances': instances, 'routers': routers, 'links': links })