|
| 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