def get_profile_by_uuid(uuid):
    # BUG: does not gurantee more than one with same uuid
    """get_profile_by_uuid
    Returns a general description of a user  # noqa: E501
    :param uuid: uuid of user
    :type id: str
    :rtype: PROFILE
    """
    for element in profiles.find({'uuid': uuid}):
        return (element['uuid'],
                element['username'],
                element['context'],
                element['description'],
                element['firstname'],
                element['lastname'],
                element['publickey'],
                element['email'])


    return Profile(element[0],
                   element[1],
                   element[2],
                   element[3],
                   element[4],
                   element[5],
                   element[6],
                   element[7])
def parse_resources(body):  # noqa: E501
    """Parse resources

    Parse resources # noqa: E501

    :param body: Profile Object
    :type body: dict | bytes

    :rtype: Resource
    """
    if connexion.request.is_json:
        profile = Profile.from_dict(connexion.request.get_json())  # noqa: E501
        script = profile.script
    else:
        abort(405, "Invalid input")

    if script is "":
        abort(405, "No script is provided")

    fd, path = tempfile.mkstemp(suffix='.py')
    with open(path, 'w') as f:
        f.write(script)
    # with open(path, 'r') as f:
    #    print(f.read())
    cmd = 'python {}'.format(path)
    rspec = subprocess.check_output(cmd, stderr=subprocess.STDOUT,
                                    shell=True).decode()
    logger.info(rspec)
    os.unlink(path)

    return parse_rspec_profile(rspec=rspec)
def get_profiles(username=None):  # noqa: E501
    """get profiles under user

    get profiles under user # noqa: E501

    :param username: creator of the profile
    :type username: str

    :rtype: List[Profile]
    """
    if username is None:
        username = emulab.EMULAB_EXPERIMENT_USER
        logger.info('user default user!')

    emulab_cmd = '{} sudo python /root/aerpaw/querydb.py {} list_profiles'.format(
        emulab.SSH_BOSS, username)
    emulab_stdout = emulab.send_request(emulab_cmd)
    profiles = []
    if emulab_stdout:
        results = json.loads(emulab_stdout)
        logger.info(results)
        for record in results:
            for k in list(record):
                if not getattr(Profile, k, None):
                    logger.info(k + ":" + str(record[k]) + " is ignored")
                    del record[k]
            profile = Profile(**record)
            profiles.append(profile)

    return profiles
def create_profile(body):  # noqa: E501
    """create profile

    Create Profile # noqa: E501

    :param body: Profile Object
    :type body: dict | bytes

    :rtype: ApiResponse
    """
    if connexion.request.is_json:
        req = Profile.from_dict(connexion.request.get_json())  # noqa: E501

    if req.creator is None:
        req.creator = emulab.EMULAB_EXPERIMENT_USER
    if req.project is None:
        req.project = emulab.EMULAB_PROJ

    xmlfile = emulab.write_profile_xml(req.project, req.name, req.script,
                                       req.repourl)
    xmlpath = emulab.send_file(xmlfile)

    emulab_cmd = '{} sudo -u {} manage_profile create {}'.format(
        emulab.SSH_BOSS, req.creator, xmlpath)
    emulab.send_request(emulab_cmd)

    # clean up the temporary files
    os.unlink(xmlfile)
    emulab_cmd = '{} sudo rm {}'.format(emulab.SSH_BOSS, xmlpath)
    emulab.send_request(emulab_cmd)

    response = ApiResponse(
        code=0,
        output="Please use getProfile to check whether success or fail")
    return response
def query_profile(profile, username=None, project=None):  # noqa: E501
    """query specific profile

    query specific profile # noqa: E501

    :param profile: profile name to query
    :type profile: str
    :param username: creator of the profile
    :type username: str
    :param project: project name
    :type project: str

    :rtype: Profile
    """
    if username is None:
        username = emulab.EMULAB_EXPERIMENT_USER
    if project is None:
        project = emulab.EMULAB_PROJ

    emulab_cmd = '{} sudo python /root/aerpaw/querydb.py {} query_profile {} {}'.format(
        emulab.SSH_BOSS, username, project, profile)
    emulab_stdout = emulab.send_request(emulab_cmd)
    found_profile = None
    if emulab_stdout:
        results = json.loads(emulab_stdout)
        logger.info(results)
        for record in results:
            for k in list(record):
                if not getattr(Profile, k, None):
                    logger.info(k + ":" + str(record[k]) + " is ignored")
                    del record[k]
            found_profile = Profile(**record)
    if found_profile is None:
        abort(404, description="No such profile")
    return found_profile
Beispiel #6
0
    def test_parse_resources(self):
        """Test case for parse_resources

        Parse resources
        """
        body = Profile()
        response = self.client.open(
            '/aerpawgateway/1.0.0/resources/parse_script',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
Beispiel #7
0
def add_profile(profile=None):  # noqa: E501
    """Create a new profile

     # noqa: E501

    :param profile: The new profile to create
    :type profile: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        profile = Profile.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
    def test_create_profile(self):
        """Test case for create_profile

        create profile
        """
        body = Profile(
            creator='erikafu',
            name='unittest',
            project='TestProject1',
            script=
            "\"\"\"One raw PC running the default OS.\n\nInstructions:\nLog into your PC and poke around. You have root access via `sudo`. Any work you do on your PC will be lost when it terminates.\"\"\"\n\n# Import the Portal object.\nimport geni.portal as portal\n# Import the ProtoGENI library.\nimport geni.rspec.pg as pg\n# Import the Emulab specific extensions.\nimport geni.rspec.emulab as emulab\n\n# Create a portal object,\npc = portal.Context()\n\n# Create a Request object to start building the RSpec.\nrequest = pc.makeRequestRSpec()\n\n# Node node1\nnode1 = request.RawPC('node1')\n\n# Print the generated rspec\npc.printRequestRSpec(request)\n"
        )
        response = self.client.open('/aerpawgateway/1.0.0/profile',
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
def profiles_get():  # noqa: E501
    """profiles_get

    Returns a list of general description of users  # noqa: E501

    :rtype: List[PROFILE]
    """
    listOfProfile = []
    items = get_profile()
    for element in items:
        listOfProfile.append(Profile(element[0],
                                     element[1],
                                     element[2],
                                     element[3],
                                     element[4],
                                     element[5],
                                     element[6],
                                     element[7]))
    return listOfProfile
Beispiel #10
0
def add_profile(profile=None):
    # ok
    uid = str(uuid.uuid4())
    profile["uuid"] = uid
    if connexion.request.is_json:
        profile = Profile.from_dict(profile)

    profiles.insert(profile.to_dict())
    """
    db.Profile.insert({"uuid": profile.uuid,
                       "username": profile.username,
                       "context": profile.context,
                       "description": profile.description,
                       "firstname": profile.firstname,
                       "lastname": profile.lastname,
                       "publickey": profile.publickey,
                       "email": profile.email})
    """
    return profile