Skip to content

Commit

Permalink
added side car proxy controle servers (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepCowProductions committed Sep 6, 2024
1 parent aaa79b8 commit 3b5e811
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/jupyter_command_launcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Description
A simple start command for launching anything via some proxy

## Install
We have not published this package to pypi, but you can install it with pip directly from github:
```
pip install git+https://github.com/fhswf/Jupyterhub-K8s.git@main#subdirectory=packages/jupyter_command_launcher
```
Using pip and requirements.txt, simple put the above link into the file:
```
package-one==1.9.4
git+https://github.com/fhswf/Jupyterhub-K8s.git@main#subdirectory=packages/jupyter_command_launcher
package-three==2.0.0
numpy
...
```
## copy right notice
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import shutil
from typing import List

def resolve_env(s:str) -> str:
[s := s.replace("$"+k, v) for k, v in dict(os.environ).items()]
return s

def resolve_envs(ss:List[str]) -> List[str]:
return list(map(resolve_env, ss))

def start():
def launch_control_gui(port):
executable = "py_cmd_launcher_gui"
cmd = [
executable,
"--port=" + str(port),
]
print("launching app", cmd)
return cmd

return {
"command": launch_control_gui,
"timeout": 300,
"new_browser_tab": False,
"launcher_entry": {
"title": "Sidecar Control GUI",
}
}

1 change: 1 addition & 0 deletions packages/jupyter_command_launcher/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jupyter-server-proxy
33 changes: 33 additions & 0 deletions packages/jupyter_command_launcher/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from setuptools import find_packages, setup

with open("README.md", encoding="utf8") as f:
readme = f.read()

setup_args = dict(
name="jupyter_vscodeserver_proxy",
packages=find_packages(),
version='0.1.0',
description="JupyterHub jupyter server proxy for code-server",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
author="Max Kuhmichel, FH SWF",
author_email="[email protected]",
url="https://github.com/fhswf",
license="MIT",
python_requires=">=3.8",
include_package_data=True,
#package_data={"jupyter_vscodeserver_proxy": ["icons/*"]},
entry_points={"jupyter_serverproxy_servers": ["start_sidecar = jupyter_command_launcher:start"],
"jupyter_serverproxy_servers": ["stop_sidecar = jupyter_command_launcher:stop"] }
)

setup_args['install_requires'] = install_requires = []
with open('requirements.txt') as f:
for line in f.readlines():
req = line.strip()
if not req or req.startswith(('-e', '#')):
continue
install_requires.append(req)

if __name__ == '__main__':
setup(**setup_args)
18 changes: 18 additions & 0 deletions packages/py_cmd_launcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## What
Simple flask server to launch a system command. Used to API-ify container run scripts.
This is not for usage in an unsave production setup, as there is not auth or encryption.
Please only use this for internal networks such as contained kubernets pods, without external excess.

## Install
We have not published this package to pypi, but you can install it with pip directly from github:
```
pip install git+https://github.com/fhswf/Jupyterhub-K8s.git@main#subdirectory=packages/py_cmd_launcher
```
Using pip and requirements.txt, simple put the above link into the file:
```
package-one==1.9.4
git+https://github.com/fhswf/Jupyterhub-K8s.git@main#subdirectory=packages/py_cmd_launcher
package-three==2.0.0
numpy
...
```
Empty file.
78 changes: 78 additions & 0 deletions packages/py_cmd_launcher/py_cmd_launcher/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import logging
from flask import Flask, jsonify
import subprocess
#import signal
#import threading
from argparse import ArgumentParser

# Prozess-Handler
process = None

def create_app(script_path):
app = Flask(__name__)
#def run_script():
# global process
# process = subprocess.Popen(script_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

@app.route('/start', methods=['GET'])
def start():
global process
if process is None:
# Start the script (in a new thread?)
#thread = threading.Thread(target=run_script)
#thread.start()
process = subprocess.Popen(script_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
return jsonify({'status': 'Script started!'}), 201
else:
return jsonify({'status': 'Script is already running!'}), 204

@app.route('/stop', methods=['GET'])
def stop():
global process
if process is not None:
# Terminate the process
#os.kill(process.pid, signal.SIGTERM)
process.terminate()
process.wait()
process = None
return jsonify({'status': 'Script stopped!'}), 200
else:
return jsonify({'status': 'No script is running!'}), 204

@app.route('/status', methods=['GET'])
def status():
global process
if process is not None:
return jsonify({'status': 'Live'}), 200
else:
return jsonify({'status': 'Process was lost'}), 204

return app

def main():
logging.captureWarnings(True)
log = logging.getLogger("py_cmd_launcher_main")
exit_code=0
try:
parser = ArgumentParser(description='Config')
parser.add_argument('--host', type=str, default='127.0.0.1',
help='Host (default is 127.0.0.1)')
parser.add_argument('--port', type=int, default=4990,
help='Port (default is 4990)')
parser.add_argument(name_or_flags='--script', help="command inside PATH to launch", default="bash -c 'echo no_command'")
parser.add_argument(name_or_flags='--debug', help="debug", default=False, type=bool)

args = parser.parse_args()

app = create_app(args.script)
app.run(host=args.host, port=args.port,debug=args.debug)

except Exception as e:
exit_code = 1 # generic error
if exit_code is not None and exit_code > 0:
log.error(f"Exited with Status Code: {str(exit_code)}")
exit(exit_code)
log.info(f"Exited with Status Code: {str(exit_code)}")

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions packages/py_cmd_launcher/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask
32 changes: 32 additions & 0 deletions packages/py_cmd_launcher/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from setuptools import find_packages, setup

setup_args = dict(
name='py_cmd_launcher',
packages=find_packages(),
version='1.0.0',
description="Simple flask server to launch a system command. Used to API-ify container run scripts",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
author="Max Kuhmichel, FH SWF",
author_email="[email protected]",
url="https://github.com/fhswf",
license="MIT",
python_requires=">=3.6",
#include_package_data=True,
entry_points={
'console_scripts': [
'py_cmd_launcher = py_cmd_launcher.app:main',
]
},
)

setup_args['install_requires'] = install_requires = []
with open('requirements.txt') as f:
for line in f.readlines():
req = line.strip()
if not req or req.startswith(('-e', '#')):
continue
install_requires.append(req)

if __name__ == '__main__':
setup(**setup_args)
18 changes: 18 additions & 0 deletions packages/py_cmd_launcher_gui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## What
Simple flask server to launch a system command. Used to API-ify container run scripts.
This is not for usage in an unsave production setup, as there is not auth or encryption.
Please only use this for internal networks such as contained kubernets pods, without external excess.

## Install
We have not published this package to pypi, but you can install it with pip directly from github:
```
pip install git+https://github.com/fhswf/Jupyterhub-K8s.git@main#subdirectory=packages/py_cmd_launcher
```
Using pip and requirements.txt, simple put the above link into the file:
```
package-one==1.9.4
git+https://github.com/fhswf/Jupyterhub-K8s.git@main#subdirectory=packages/py_cmd_launcher
package-three==2.0.0
numpy
...
```
Empty file.
Loading

0 comments on commit 3b5e811

Please sign in to comment.