def api_slice_exists(projectname, slicename): """ Returns the status of the slice """ # Get post values cert = request.headers.get('emulab-s4-cert') # Create and populate temp dir t_dir = tempfile.mkdtemp() cert_path = t_dir + '/s4.cert.xml' with open(cert_path, 'w+') as cert_f: cert_f.write(base64.b64decode(cert)) slice_dir = ARCHIVE_PATH+"/projects/{0}/slices/{1}".format(projectname, slicename) manifest_path = slice_dir + '/manifest.mrspec' if not os.path.isfile(manifest_path): manifest_path = DEFAULT_RSPEC_PATH jfed = JFed(projectname, s4cert=cert_path, properties=PROPERTIES_PATH) # Run command try: expiration = jfed.slice_exists(slicename, manifest_path) resp = Response(json.dumps(expiration), status=200, mimetype='application/json') except NotExistError as err: resp = Response(json.dumps(err.odict), status=404, mimetype='application/json') return resp
def api_slice_create(projectname, slicename): """ Creates new jfed slice using s4 certificate""" # Get request values rspec = request.get_data() cert = request.headers.get('emulab-s4-cert') # Create and populate temp dir t_dir = tempfile.mkdtemp() rspec_path = t_dir + '/rspec.rspec' cert_path = t_dir + '/s4.cert.xml' slice_dir = ARCHIVE_PATH+"/projects/{0}/slices/{1}".format(projectname, slicename) manifest_path = slice_dir + '/manifest.mrspec' with open(rspec_path, 'w+') as rspec_f: rspec_f.write(rspec) with open(cert_path, 'w+') as cert_f: cert_f.write(base64.b64decode(cert)) jfed = JFed(projectname, s4cert=cert_path, properties=PROPERTIES_PATH) # If we already have a manifest, check if experiment exists. If so, exit. # If not so, backup previous dir and create new experiment. if os.path.isfile(manifest_path): slice_exists = jfed.slice_exists(slicename, manifest_path) if slice_exists: resp = Response("Cannot modify existing slice", status=409, mimetype='text/plain') return resp else: shutil.move(slice_dir, '{}.bak{}'.format(slice_dir, time.time())) if not os.path.isdir(slice_dir): os.makedirs(slice_dir) # Run command try: jfed.create_slice(slicename, rspec_path, manifest_path) with open(manifest_path, 'r') as manifest_f: resp = Response(manifest_f.read(), status=201, mimetype='application/xml') return resp except JfedError as ex: resp = Response(json.dumps(ex.odict), status=500, mimetype='application/json') return resp