Skip to content

Commit acb2a40

Browse files
authored
add local tutorial and expose flux restful host to env (#52)
* add local tutorial and expose flux restful host to env * typo in logs * version bump Signed-off-by: vsoch <[email protected]>
1 parent 1653c2d commit acb2a40

File tree

10 files changed

+259
-5
lines changed

10 files changed

+259
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
flux-restful.db
22
env
3+
migrations/versions/*.py
34
.env
45
__pycache__
56
*.tar.gz

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
all:
2-
uvicorn app.main:app --host=0.0.0.0 --port=5000 --reload
2+
flux start uvicorn app.main:app --host=0.0.0.0 --port=5000 --workers=2
3+
4+
init:
5+
alembic revision --autogenerate -m "Create intital tables"
6+
alembic upgrade head
7+
/bin/bash ./init_db.sh

clients/python/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/flux-framework/flux-restful-api/tree/main) (0.0.x)
17+
- Expose host to environment and bug fix for logs (0.1.14)
18+
- Ensure we update flux environment for user (0.1.13)
19+
- Add better multi-user mode - running jobs on behalf of user (0.1.12)
20+
- Restore original rpc to get job info (has more information) (0.1.11)
1721
- refactor to require secret key, oauth2 flow (0.1.0)
1822
- add simple retry to requests (0.0.16)
1923
- support for adding option flags to submit (0.0.15)

clients/python/flux_restful_client/client/logs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def main(args, parser, extra, subparser):
1212
for line in cli.stream_output(jobid=args.jobid, stream=args.stream):
1313
print(line.strip())
1414
else:
15-
logs = cli.output(jobid=args.jobid, stream=args.stream)
15+
logs = cli.output(jobid=args.jobid)
1616
if "Output" in logs:
1717
for line in logs["Output"]:
1818
# Ensure we only have one newline!

clients/python/flux_restful_client/client/submit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def main(args, parser, extra, subparser):
1515

1616
# The command is the "extra"
1717
kwargs = {"command": extra}
18+
logger.debug(f"Command: {extra}")
1819
for arg in [
1920
"num_tasks",
2021
"cores_per_task",

clients/python/flux_restful_client/main/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(
4444
if not hasattr(self, "settings"):
4545
self.settings = Settings(settings_file)
4646

47-
self.host = host or self.settings.host
47+
self.host = host or os.environ.get("FLUX_RESTFUL_HOST") or self.settings.host
4848
self.user = user or os.environ.get("FLUX_USER") or self.settings.flux_user
4949
self.token = token or os.environ.get("FLUX_TOKEN") or self.settings.flux_token
5050
self.secret_key = secret_key or os.environ.get("FLUX_SECRET_KEY")

clients/python/flux_restful_client/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.13"
1+
__version__ = "0.1.14"
22
AUTHOR = "Vanessa Sochat"
33
44
NAME = "flux-restful-client"

docs/getting_started/developer-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ The following variables are available (with their defaults):
193193
|FLUX_OPTION_FLAGS | Option flags to give to flux, in the same format you'd give on the command line | unset |
194194
|FLUX_SECRET_KEY | secret key to be shared between user and server (required) | unset |
195195
|FLUX_ACCESS_TOKEN_EXPIRES_MINUTES| number of minutes to expire an access token | 600 |
196+
|FLUX_RESTFUL_HOST| Host for command line client | http://127.0.0.1:5000 |
196197

197-
Note that we currently use a global secret key, and this could be improved to use a user-specific one.
198198

199199
### Flux Option Flags
200200

docs/tutorials/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ any questions or issues, please [let us know](https://github.com/flux-framework/
1616
:maxdepth: 3
1717
../auto_examples/index
1818
container
19+
local
1920
```

docs/tutorials/local.md

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
# Local Install
2+
3+
This tutorial will show running the Flux Restful API on an allocation where you have flux
4+
(so you are the instance owner). This is arguably unnecessary, because you have the Flux
5+
command line client to interact with, but we show the example to demonstrate that it's
6+
possible.
7+
8+
## Get an Allocation.
9+
10+
We want to start with an allocation so we are already running in our own Flux
11+
instance. Let's say we ask for the allocation:
12+
13+
```bash
14+
$ flux alloc -N 2
15+
16+
# Older versions of flux
17+
$ flux mini alloc -N 2
18+
```
19+
20+
We can then clone the flux-restful API:
21+
22+
```bash
23+
git clone --depth 1 https://github.com/flux-framework/flux-restful-api
24+
cd flux-restful-api
25+
```
26+
27+
and create an environment for it:
28+
29+
```bash
30+
python -m venv env
31+
source env/bin/activate
32+
pip install -r requirements.txt
33+
```
34+
35+
## Database
36+
37+
Prepare the database. Note that this can also be done with `make init`.
38+
39+
```bash
40+
alembic revision --autogenerate -m "Create intital tables"
41+
alembic upgrade head
42+
```
43+
44+
Export your desired flux token and user, and create the database.
45+
46+
```bash
47+
export FLUX_USER=dinosaur
48+
export FLUX_TOKEN=dinosaur
49+
50+
./init_db.sh
51+
```
52+
53+
## Start
54+
55+
And run! This can also be done by running `make` (and inspect the Makefile first
56+
for port variables, etc):
57+
58+
```bash
59+
$ flux start uvicorn app.main:app --host=0.0.0.0 --port=16798 --workers=2
60+
```
61+
62+
Note that we are using a very large port number.
63+
64+
## Interact
65+
66+
At this point you'd want to shell into another terminal, and return to the same cloned
67+
directory! The easiest thing to do (to test quickly) is to shell into the node where
68+
you have the allocation. E.g.,:
69+
70+
```bash
71+
$ ssh corona194
72+
```
73+
74+
And return to that directory. We can cd into the python client and install:
75+
76+
```bash
77+
$ source env/bin/activate
78+
$ cd clients/python
79+
$ pip install -e .
80+
```
81+
82+
### Python
83+
84+
We can now derive an interaction via example in the examples directory.
85+
I like to use ipython when I'm developing or testing like this, but you
86+
could use python or just a script. First, submit the job:
87+
88+
89+
```python
90+
from flux_restful_client.main import get_client
91+
92+
# You can also again export these in the environment.
93+
cli = get_client(host="http://127.0.0.1:16798", user="dinosaur", token="dinosaur")
94+
95+
cli.submit(command=["whoami"])
96+
# {'Message': 'Job submit.', 'id': 8245884223488}
97+
```
98+
99+
Note the id! Let's get it back to see the result.
100+
101+
```
102+
# see all jobs
103+
res = jobs = cli.jobs()
104+
# {'jobs': [{'id': 8245884223488}]}
105+
106+
# Or get the specific job
107+
job = cli.jobs(res['id'])
108+
```
109+
```console
110+
{'id': 8245884223488,
111+
'userid': 34633,
112+
'urgency': 16,
113+
'priority': 16,
114+
't_submit': 1678216131.163,
115+
't_depend': 1678216131.163,
116+
't_run': 1678216131.1762564,
117+
't_cleanup': 1678216131.234502,
118+
't_inactive': 1678216131.2362921,
119+
'state': 'INACTIVE',
120+
'name': 'whoami',
121+
'ntasks': 1,
122+
'ncores': 1,
123+
'duration': 0.0,
124+
'nnodes': 1,
125+
'ranks': '0',
126+
'nodelist': 'corona194',
127+
'success': True,
128+
'exception_occurred': False,
129+
'result': 'COMPLETED',
130+
'expiration': 4831816131.0,
131+
'waitstatus': 0,
132+
'returncode': 0,
133+
'runtime': 0.05824565887451172,
134+
'exception': {'occurred': False, 'severity': '', 'type': '', 'note': ''}}
135+
```
136+
137+
And finally, get the log:
138+
139+
```bash
140+
out = cli.output()
141+
# {'Output': ['dinosaur1\n']}
142+
```
143+
144+
And that's it! You've successfully used the flux restful API in single user
145+
mode, of course running as yourself.
146+
147+
### Command Line
148+
149+
Now let's produce the same thing from the command line! This time we will export
150+
our credentials and the host for the client to find:
151+
152+
```bash
153+
export FLUX_USER=dinosaur
154+
export FLUX_TOKEN=dinosaur
155+
export FLUX_RESTFUL_HOST=http://127.0.0.1:16798
156+
```
157+
158+
And then submit the job:
159+
160+
```bash
161+
$ flux-restful-cli submit whoami
162+
```
163+
```console
164+
{
165+
"Message": "Job submit.",
166+
"id": 1944496111616
167+
}
168+
```
169+
```console
170+
{
171+
"id": 1944496111616,
172+
"userid": 34633,
173+
"urgency": 16,
174+
"priority": 16,
175+
"t_submit": 1678217369.1978858,
176+
"t_depend": 1678217369.1978858,
177+
"t_run": 1678217369.211171,
178+
"t_cleanup": 1678217369.2724185,
179+
"t_inactive": 1678217369.2742176,
180+
"state": "INACTIVE",
181+
"name": "whoami",
182+
"ntasks": 1,
183+
"ncores": 1,
184+
"duration": 0.0,
185+
"nnodes": 1,
186+
"ranks": "0",
187+
"nodelist": "corona194",
188+
"success": true,
189+
"exception_occurred": false,
190+
"result": "COMPLETED",
191+
"expiration": 4831817369.0,
192+
"waitstatus": 0,
193+
"returncode": 0,
194+
"runtime": 0.06124758720397949,
195+
"exception": {
196+
"occurred": false,
197+
"severity": "",
198+
"type": "",
199+
"note": ""
200+
}
201+
}
202+
```
203+
204+
Or list jobs...
205+
206+
```bash
207+
$ flux-restful-cli list-jobs
208+
```
209+
```console
210+
{
211+
"jobs": [
212+
{
213+
"id": 1944496111616
214+
}
215+
]
216+
}
217+
```
218+
or nodes..
219+
220+
```bash
221+
$ flux-restful-cli list-nodes
222+
```
223+
```console
224+
{
225+
"nodes": [
226+
"corona194"
227+
]
228+
}
229+
```
230+
231+
Or finally, get the output!
232+
233+
```bash
234+
$ flux-restful-cli logs 1944496111616
235+
```
236+
```console
237+
dinosaur1
238+
```
239+
240+
And that's it! This is entirely not needed for a cluster since the Flux
241+
command line tool is available, but it's a nice proof of concept to show
242+
that it works.

0 commit comments

Comments
 (0)