Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality to run things on the remote server #72

Merged
merged 5 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions planutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def main():
parser_run.add_argument('package', help='package name')
parser_run.add_argument('options', help='commandline options for the package', nargs="*")

parser_run = subparsers.add_parser('remote', help='run package remotely')
parser_run.add_argument('package', help='package name')
parser_run.add_argument('options', help='commandline options for the package', nargs="*")

haz marked this conversation as resolved.
Show resolved Hide resolved
parser_checkinstalled = subparsers.add_parser('check-installed', help='check if a package is installed')
parser_checkinstalled.add_argument('package', help='package name')

Expand Down Expand Up @@ -116,6 +120,10 @@ def main():
from planutils.package_installation import run
run(args.package, args.options)

elif 'remote' == args.command:
from planutils.package_installation import remote
remote(args.package, args.options)

elif 'list' == args.command:
from planutils.package_installation import package_list
package_list()
Expand Down
86 changes: 84 additions & 2 deletions planutils/package_installation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

import json, os, glob, subprocess, sys
import json, os, glob, subprocess, sys, requests, time
from collections import defaultdict
from pathlib import Path

from planutils import settings
from planutils import manifest_converter

PACKAGES = {}

Expand Down Expand Up @@ -193,3 +192,86 @@ def run(target, options):
if not PACKAGES[target]["runnable"]:
sys.exit(f"Package {target} is not executable")
subprocess.run([Path(settings.PLANUTILS_PREFIX) / "packages" / target / "run"] + options)


def remote(target, options):

# 1. check if the target is deployed

package_url = settings.PAAS_SERVER + "/package"
r = requests.get(package_url)
remote_packages = r.json()

remote_package = None
for p in remote_packages:
if p['package_name'] == target:
remote_package = p
break

if (remote_package is None) or ('solve' not in remote_package['endpoint']['services']):
sys.exit(f"Package {target} is not remotely deployed")


# 2. unpack the options and target to the right json

json_options = {}
remote_call = remote_package['endpoint']['services']['solve']['call']
call_parts = remote_call.split(' ')

args = {arg['name']: arg for arg in remote_package['endpoint']['services']['solve']['args']}

if len(options) != len(call_parts)-1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to compare with the number of arguments, not the number of call parts, as some manifests include predefined call arguments.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to push a commit fixing this line but I get the following error:

$ git push
remote: Permission to AI-Planning/planutils.git denied to nirlipo.
fatal: unable to access 'https://github.com/AI-Planning/planutils.git/': The requested URL returned error: 403

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Permissions fixed.

But I think the comparison is correct. options is the command-line options, and call_parts are what will be called on the remote. They should match the # of arguments, but there's no guarantee (it's up to the manifest maintainer). Here, we assume that the local call (filling out options) will match precisely the remote call.

Copy link
Collaborator

@nirlipo nirlipo May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Yes, but it should check against number of args instead. See line 228, we iterate only over the arguments of the call defined in the manifest by looking for the { and } symbols. I'll make the commit so you can test it. see 30a8426

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye, I see the change, but I was just trying to align the command-line call with the remote call string -- should mirror closely, as otherwise there is no way to align. I get that this fixes the ... plan issue, but I think that may be more of an issue with the package -- should be able to specify the plan name. A more direct regex would be better, but meh...I think this is good enough for now.

#75

sys.exit(f"Call string does not match the remote call: {remote_package['endpoint']['services']['solve']['call']}")

call_map = {}
for i, step in enumerate(call_parts[1:]):
if '{' == step[0] and '}' == step[-1]:
option = step[1:-1]
call_map[option] = options[i]
if option not in args:
sys.exit(f"Option {option} from call string is not defined in the remote call: {remote_call}")
if args[option]['type'] == 'file':
with open(options[i], 'r') as f:
json_options[option] = f.read()
else:
json_options[option] = options[i]

rcall = remote_call
for k, v in call_map.items():
rcall = rcall.replace('{' + k + '}', v)
print("\nMaking remote call: %s" % rcall)


# 3. run the remote command

solve_url = '/'.join([settings.PAAS_SERVER, 'package', target, 'solve'])
r = requests.post(solve_url, json=json_options)
if r.status_code != 200:
sys.exit(f"Error running remote call: {r.text}")

result_url = f"{settings.PAAS_SERVER}/{r.json()['result']}"

# call every 0.5s until the result is ready
result = None
for _ in range(settings.PAAS_SERVER_LIMIT):
r = requests.get(result_url)
if (r.status_code == 200) and ('status' in r.json()) and (r.json()['status'] == 'ok'):
result = r.json()['result']
break
time.sleep(0.5)

if result is None:
sys.exit(f"Error running remote call: {r.text}")


# 4. unpack the results back to the client, including any new files

result = r.json()['result']
for fn in result['output']:
with open(fn, 'w') as f:
f.write(result['output'][fn])

print("\n\t{stdout}\n")
print(result['stdout'])
print("\n\t{stderr}\n")
print(result['stderr'])
3 changes: 3 additions & 0 deletions planutils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

SETTINGS_FILE = os.path.join(PLANUTILS_PREFIX, 'settings.json')

PAAS_SERVER = 'http://45.113.232.43:5001'
PAAS_SERVER_LIMIT = 100

def load():
with open(SETTINGS_FILE, 'r') as f:
settings = json.loads(f.read())
Expand Down