5.629 Python Samples

These samples were tested with version 2.9.1 of the requests package.

Get List of Active Jobs

#!/usr/bin/env python

from __future__ import print_function

import json
import sys

import requests

session = requests.Session()
session.auth = ('moab-admin', 'changeme!')
response = session.request(
    method='GET',
    url='http://localhost:8080/mws/rest/jobs',
    params={
        'query': json.dumps({'isActive': True}),
        'sort': json.dumps({'credentials.user': 1}),
        'fields': 'name,queueStatus,priorities.user,credentials.user',
        'max': 10,
        'api-version': 3,
    },
)
if response.ok:
    print(json.dumps(response.json(), sort_keys=True, indent=4))
else:
    try:
        print("Error: " + response.json()['messages'][0], file=sys.stderr)
    except ValueError:
        print("Error: status code is " + str(response.status_code), file=sys.stderr)

Submit Job

#!/usr/bin/env python

from __future__ import print_function

import base64
import sys

import requests

session = requests.Session()
session.auth = ('moab-admin', 'changeme!')
script = base64.b64encode("""
#!/bin/sh
/bin/date
sleep 600
/bin/date
""")
response = session.request(
    method='POST',
    url='http://localhost:8080/mws/rest/jobs',
    params={'api-version': 3},
    json={
        'commandScript': script,
        'initialWorkingDirectory': '/tmp',
        'credentials': {
            'group': 'adaptive',
            'user': 'adaptive'
        },
        'requirements': [{'taskCount': 4}]
    })
if response.ok:
    print("Submitted job " + response.json()['name'])
else:
    try:
        print("Error: " + response.json()['messages'][0], file=sys.stderr)
    except ValueError:
        print("Error: status code is " + str(response.status_code), file=sys.stderr)

Create Principal

#!/usr/bin/env python
from __future__ import print_function

import sys

import requests

session = requests.Session()
session.auth = ('moab-admin', 'changeme!')
response = session.request(
    method='POST',
    url='http://localhost:8080/mws/rest/principals',
    params={'api-version': 3},
    json={
        "name": "name_of_principal",
        "description": "Short description of principal",
        "attachedRoles": [
            {"name": "HPCUser"},
            {"name": "NitroUser"},
            {"name": "RemoteVizUser"}
        ],
        "groups": [
            {"name": "group1", "type": "PAMGROUP"},
            {"name": "group2", "type": "PAMGROUP"},
            {"name": "group3", "type": "PAMGROUP"},
            {"name": "group4", "type": "PAMGROUP"},
            {"name": "group5", "type": "PAMGROUP"},
            {"name": "group6", "type": "PAMGROUP"}
        ],
        "users": [
            {"name": "user1", "type": "PAM"},
            {"name": "user2", "type": "PAM"},
            {"name": "user3", "type": "PAM"},
            {"name": "user4", "type": "PAM"},
            {"name": "user5", "type": "PAM"},
            {"name": "user6", "type": "PAM"},
            {"name": "user7", "type": "PAM"}
        ]
    }
)

if response.ok:
    print("Created principal " + response.json()['name'])
else:
    try:
        print("Error: " + response.json()['messages'][0], file=sys.stderr)
    except ValueError:
        print("Error: status code is " + str(response.status_code), file=sys.stderr)

Related Topics 

© 2017 Adaptive Computing