Skip to content

Commit

Permalink
Merge pull request #76 from AI-Planning/dependency-fix
Browse files Browse the repository at this point in the history
Moving around the remote calls to fix the dependency.
  • Loading branch information
haz authored Jun 2, 2022
2 parents 2d883e6 + 683a42a commit 7b80522
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 99 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install build
python -m pip install -r requirements.txt
pip install build
- name: Build package
run: python -m build
- name: Publish package
Expand Down
3 changes: 3 additions & 0 deletions environments/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Build the Planutils image and install the selected packages
FROM aiplanning/planutils:latest

# Needed for the server
RUN pip3 install flask

# FD Planner
RUN planutils install -f -y lama-first

Expand Down
4 changes: 2 additions & 2 deletions planutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def main():
run(args.package, args.options)

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

elif 'remote-list' == args.command:
from planutils.package_installation import package_remote_list
from planutils.server import package_remote_list
package_remote_list()

elif 'list' == args.command:
Expand Down
94 changes: 1 addition & 93 deletions planutils/package_installation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

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

def package_remote_list():

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

print("\nDeployed:")
for p in remote_packages:
arguments = " ".join(f'{{{a["name"]}}} ' for a in p['endpoint']['services']['solve']['args'])
print(" %s: %s\n\tArguments: %s" % (p['package_name'], p['name'], arguments ))

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(args):
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'])
95 changes: 94 additions & 1 deletion planutils/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import glob, os, subprocess, tempfile
import glob, os, subprocess, tempfile, requests, time, sys

from planutils.package_installation import PACKAGES, check_installed
from planutils import settings
Expand Down Expand Up @@ -103,3 +103,96 @@ def runPackage(package, service):

app.run(port=port)


def package_remote_list():

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

print("\nDeployed:")
for p in remote_packages:
arguments = " ".join(f'{{{a["name"]}}} ' for a in p['endpoint']['services']['solve']['args'])
print(" %s: %s\n\tArguments: %s" % (p['package_name'], p['name'], arguments ))

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(args):
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'])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
long_description = fh.read()

setuptools.setup(name='planutils',
version='0.5.4',
version='0.5.5',
description='General library for setting up linux-based environments for developing, running, and evaluating planners.',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 7b80522

Please sign in to comment.