Ejemplo n.º 1
0
def submit(job, headers, inputs=[]):
    """
    Submits a job to the given URL, which can be the ".../jobs" URL
    or a ".../sites/site_name/" URL
    If inputs is not empty, the listed input data files are
    uploaded to the job's working directory, and a "start" command is sent
    to the job.
    """
    data = {}
    my_headers = headers.copy()
    my_headers['Content-Type'] = "application/json"
    if len(inputs) > 0:
        # make sure UNICORE does not start the job
        # before we have uploaded data
        job['haveClientStageIn'] = 'true'

    r = requests.post(url=api.JOBS_URL,
                      data=json.dumps(job),
                      headers=my_headers,
                      auth=api.get_credential(),
                      verify=False)

    if r.status_code == 201:
        job_url = r.headers['Location']
        data['job_id'] = get_job_id(job_url)

        #  upload input data and explicitely start job
        if len(inputs) > 0:
            working_directory = get_working_directory(job_url, headers)
            # working_directory = api.get_working_directory_url(data['job_id'].lower())
            for i in inputs:
                upload(working_directory + "/files", i, headers)
        invoke_action(job_url, "start", headers)

        headers_2 = {"Accept": "application/json"}
        r = requests.get(url=job_url,
                         headers=headers_2,
                         auth=api.get_credential(),
                         verify=False)

        if r.status_code == 200:
            print(r.content)
            json_job = r.json()
            data.update({
                'stage': json_job['status'],
                'init_date': json_job['submissionTime']
            })

    else:
        data = r.content

    return data, r.status_code
Ejemplo n.º 2
0
def invoke_action(job_url, action, headers, data={}):
    my_headers = headers.copy()
    my_headers['Content-Type'] = "application/json"
    properties = get_properties(job_url, headers)
    action_url = properties['_links']['action:' + action]['href']
    return requests.post(action_url,
                         headers=my_headers,
                         data=json.dumps(data),
                         auth=api.get_credential(),
                         verify=False)
Ejemplo n.º 3
0
def advance_endpoint(method,
                     headers,
                     url=None,
                     append_url=None,
                     data=None,
                     json=None):
    new_headers = headers.copy()
    if append_url:
        URL = api.ROOT_URL + append_url
    elif url:
        URL = url
    if method == 'GET':
        r = requests.get(url=URL,
                         headers=new_headers,
                         data=data,
                         json=json,
                         auth=api.get_credential(),
                         verify=False)
    elif method == 'POST':
        r = requests.post(url=URL,
                          headers=new_headers,
                          data=data,
                          json=json,
                          auth=api.get_credential(),
                          verify=False)
    elif method == 'PUT':
        r = requests.put(url=URL,
                         headers=new_headers,
                         data=data,
                         json=json,
                         auth=api.get_credential(),
                         verify=False)
    elif method == 'DELETE':
        r = requests.delete(url=URL,
                            headers=new_headers,
                            data=data,
                            json=json,
                            auth=api.get_credential(),
                            verify=False)

    return r
Ejemplo n.º 4
0
def upload(destination, file_desc, headers):
    my_headers = headers.copy()
    my_headers['Content-Type'] = "application/octet-stream"
    name = file_desc['To']
    data = file_desc['Data']
    # TODO file_desc could refer to local file
    r = requests.put(destination + "/" + name,
                     data=data,
                     headers=my_headers,
                     auth=api.get_credential(),
                     verify=False)
    return r
Ejemplo n.º 5
0
def get_properties(resource, headers={}):
    """ get JSON properties of a resource """
    my_headers = headers.copy()
    my_headers['Accept'] = "application/json"
    r = requests.get(resource,
                     headers=my_headers,
                     auth=api.get_credential(),
                     verify=False)
    if r.status_code != 200:
        raise RuntimeError
    else:
        return r.json()