Skip to content

Commit

Permalink
add skeleton of restful application
Browse files Browse the repository at this point in the history
this is a great start that will submit a basic job
to the flux handle running alongside the server.
My next steps are to finish endpoints, work
on user interfaces, and then ensure we have
the automation we need. Ultimately I want to
deploy this alongside the Flux Operator as a
potential MiniCluster controller for jobs!

Signed-off-by: vsoch <[email protected]>
  • Loading branch information
vsoch committed Nov 4, 2022
0 parents commit df03151
Show file tree
Hide file tree
Showing 33 changed files with 1,148 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM fluxrm/flux-sched:focal

LABEL maintainer="Vanessasaurus <@vsoch>"

# Pip not provided in this version
USER root
RUN apt-get update
COPY ./requirements.txt /requirements.txt
COPY ./dev-requirements.txt /dev-requirements.txt

EXPOSE 5000
ENV PYTHONPATH=/usr/lib/flux/python3.8

# For easier Python development.
RUN python3 -m pip install IPython && \
python3 -m pip install -r /requirements.txt && \
python3 -m pip install -r /dev-requirements.txt
29 changes: 29 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "Flux Core Python 3.6",
"dockerFile": "Dockerfile",
"context": "../",

"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",

// Ensure that Python autocomplete works out of the box
"python.autoComplete.extraPaths": [
"/usr/lib/flux/python3.8",
"/usr/lib/python3.8/site-packages",
],
"python.analysis.extraPaths": [
"/usr/lib/flux/python3.8",
"/usr/lib/python3.8/site-packages",
]
},
// Note to Flux Developers! We can add extensions here that you like
"extensions": [
"ms-python.python"
],
},
},
// Needed for git security feature
"postStartCommand": "git config --global --add safe.directory /workspaces/flux-python-api"
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
env
.env
__pycache__
*.tar.gz
163 changes: 163 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
GNU Lesser General Public License
=================================

_Version 3, 29 June 2007_
_Copyright © 2007 Free Software Foundation, Inc. &lt;<http://fsf.org/>&gt;_

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.


This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

### 0. Additional Definitions

As used herein, “this License” refers to version 3 of the GNU Lesser
General Public License, and the “GNU GPL” refers to version 3 of the GNU
General Public License.

“The Library” refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

An “Application” is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

A “Combined Work” is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the “Linked
Version”.

The “Minimal Corresponding Source” for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

The “Corresponding Application Code” for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

### 1. Exception to Section 3 of the GNU GPL

You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

### 2. Conveying Modified Versions

If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

* **a)** under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or

* **b)** under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.

### 3. Object Code Incorporating Material from Library Header Files

The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

* **a)** Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
* **b)** Accompany the object code with a copy of the GNU GPL and this license
document.

### 4. Combined Works

You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

* **a)** Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.

* **b)** Accompany the Combined Work with a copy of the GNU GPL and this license
document.

* **c)** For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.

* **d)** Do one of the following:
- **0)** Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
- **1)** Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that **(a)** uses at run time
a copy of the Library already present on the user's computer
system, and **(b)** will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.

* **e)** Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option **4d0**, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option **4d1**, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)

### 5. Combined Libraries

You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

* **a)** Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
* **b)** Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.

### 6. Revised Versions of the GNU Lesser General Public License

The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License “or any later version”
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
uvicorn app.main:app --host=0.0.0.0 --port=5000 --reload
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Flux RESTFul API

This is a small Flux Python API (using FastAPI) that can be containerized
alongside Flux, and provide an easy means to interact with Flux via the API.
My goals are:

1. provide simple endpoints to submit jobs, list jobs, or get job status
2. eventually support subscribing to events
3. option to kill or stop the server (intended for Flux Operator)
4. allow for start with a user name and token (for basic auth)

The assumption for my use case is that we deploy this on the fly,
alongside a Flux broker, and we only need to authenticate for the single
user case. For the Flux Operator, if we can start a mini-cluster running
a server and then submit jobs to it and have it taken down by API call,
that would be really cool (and is what I plan to test.)

**under development!**

## Usage

Install dependencies:

```bash
$ python -m venv env
$ source env/bin/activate
$ pip install -r requirements.txt
```

Install requirements (note that you also need Flux Python available, which isn't in these requirements as you cannot install from pip).

```bash
$ pip install -r app-requirements.txt
```

There are two ways to start the app! You can either have it be the entry for flux start:

```console
$ flux start uvicorn app.main:app --host=0.0.0.0 --port=5000
```

Or do it separately (two commands):

```bash
$ flux start --test-size=4
$ uvicorn app.main:app --host=0.0.0.0 --port=5000
```

For the latter, you can also use the Makefile:

```bash
$ make
```
And the second might be ideal for development (it's easier).
If you want to require authentication for the user, export the user and token:

```bash
export FLUX_USER=$USER
export FLUX_TOKEN=123456
```

After starting, you can open your browser to [http://127.0.0.1:5000](http://127.0.0.1:5000)
to view the app interface. API endpoints are shown below, and see [examples](examples)
for a full client and example scripts to interact with your server. E.g.,:

```console
$ python3 examples/submit_job.py
```

### API

The following is the spec for the Flux Python RESTful API provided here:

### GET `/jobs`

List jobs owned by the flux executor (the current user).

### GET `/jobs/{uid}`

Get a job with a specific identifer.


### POST `/jobs/submit`

Submit a new job, required metadata in the post is:

- command: a string of the command to run (required)
- runtime: maximum runtime allowed (defaults to 0, unlimited, optional)


## Development

Install development requirements:

```bash
$ pip install -r dev-requirements.txt
```
And then run pre-commit manually, or install:

```bash
$ pre-commit run --all-files
```
Note that you should do this outside of the container!


## TODO

- Finish job submit / status endpoints
- Interface view with nice job table
- Basic authentication
- API endpoint to kill job server
- pre-commit and workflows to run it
- Cute logo with sleeping Flux?
- contributors action!
- We can put additional assets for the server in [data](data), not sure what those are yet!

#### Release

SPDX-License-Identifier: LGPL-3.0

LLNL-CODE-764420
Empty file added app/__init__.py
Empty file.
Empty file added app/core/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions app/core/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pydantic import BaseSettings


class Settings(BaseSettings):
app_name: str = "Flux RESTFul API"

# These map to envars, e.g., FLUX_USER
flux_user: str = None
flux_token: str = None
flux_require_auth: bool = False

settings = Settings()
8 changes: 8 additions & 0 deletions app/core/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import uvicorn

FORMAT: str = "%(levelprefix)s %(asctime)s | %(message)s"


def init_loggers(logger_name: str = "errors-analysis"):
formatter = uvicorn.logging.DefaultFormatter(FORMAT)
assert formatter
Empty file added app/library/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions app/library/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from fastapi import Header, HTTPException


async def get_basic_header(x_token):
print(x_token)
if x_token != "fake-super-secret-token":
raise HTTPException(status_code=400, detail="X-Token header invalid")

20 changes: 20 additions & 0 deletions app/library/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json
import os.path

import markdown


def read_json(filename):
with open(filename, "r") as fd:
content = json.loads(fd.read())
return content


def openfile(filename):
filepath = os.path.join("app/pages/", filename)
with open(filepath, "r", encoding="utf-8") as input_file:
text = input_file.read()

html = markdown.markdown(text)
data = {"text": html}
return data
Loading

0 comments on commit df03151

Please sign in to comment.