-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a dev-room container to simulate a real room locally
- Loading branch information
Luiz Felipe Takakura
committed
Aug 13, 2018
1 parent
c992f35
commit df2c2cd
Showing
18 changed files
with
288 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
env | ||
__pycache__ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM python:3 | ||
|
||
WORKDIR /usr/src/app | ||
ADD . . | ||
|
||
RUN pip install --no-cache-dir -r requirements.txt | ||
CMD ["gunicorn", "-b", "0.0.0.0:8080", "app:app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from api.address import Address | ||
from api.healthcheck import Healthcheck | ||
from api.status import Status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import falcon | ||
import json | ||
import requests | ||
|
||
import app | ||
import constant | ||
|
||
class Address (object): | ||
def on_get(self, req, resp): | ||
"""Handles GET requests""" | ||
app.logger.debug("address") | ||
app.logger.debug("request to {}/{}".format(constant.MAESTRO_ADDR, constant.ROOM_ADDR_ENDPOINT)) | ||
|
||
try: | ||
r = requests.get("{}/{}".format(constant.MAESTRO_ADDR, constant.ROOM_ADDR_ENDPOINT)) | ||
if r.status_code == 200: | ||
resp.status = falcon.HTTP_200 | ||
resp.body = r.text | ||
|
||
except Exception as ex: | ||
resp.status = falcon.HTTP_500 | ||
resp.body = (json.dumps({"error": str(ex)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class WrongStatus(Exception): | ||
def __init__(self, text="Wrong status update. Try one of 'ready' or 'occupied'"): | ||
self.text=text | ||
super().__init__(self) | ||
|
||
def __str__(self): | ||
return str(self.text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import falcon | ||
import json | ||
|
||
import app | ||
|
||
class Healthcheck (object): | ||
def on_get(self, req, resp): | ||
"""Handles GET requests""" | ||
app.logger.debug("healthcheck") | ||
resp.status = falcon.HTTP_200 | ||
resp.body = (json.dumps({"healthy": True})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import falcon | ||
import json | ||
import requests | ||
import time | ||
|
||
import app | ||
import constant | ||
from api.exceptions import WrongStatus | ||
|
||
class Status (object): | ||
def on_put(self, req, resp): | ||
"""Handles PUT requests""" | ||
app.logger.debug("status") | ||
|
||
try: | ||
body = req.stream.read() | ||
status = str.lower(json.loads(body)["status"]) | ||
|
||
if status not in ["ready", "occupied"]: | ||
raise WrongStatus() | ||
|
||
app.logger.debug("request to {}/{} with status={}".format(constant.MAESTRO_ADDR, constant.ROOM_STATUS_ENDPOINT, status)) | ||
r = requests.put("{}/{}".format(constant.MAESTRO_ADDR, constant.ROOM_STATUS_ENDPOINT), json={"timestamp": int(time.time()), "status": status}) | ||
if r.status_code == 200: | ||
resp.status = falcon.HTTP_200 | ||
resp.body = r.text | ||
|
||
except Exception as ex: | ||
if type(ex) == WrongStatus: | ||
resp.status = falcon.HTTP_400 | ||
else: | ||
resp.status = falcon.HTTP_500 | ||
resp.body = (json.dumps({"error": str(ex)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# External deps | ||
import time | ||
import requests | ||
import falcon | ||
import logging | ||
from logging.config import fileConfig | ||
|
||
# Internal deps | ||
import constant | ||
from api import Healthcheck, Address, Status | ||
|
||
|
||
fileConfig('logging_config.ini') | ||
logger = logging.getLogger() | ||
|
||
while True: | ||
try: | ||
print("polling {}/{}".format(constant.MAESTRO_ADDR, constant.ROOM_ADDR_ENDPOINT)) | ||
r = requests.get("{}/{}".format(constant.MAESTRO_ADDR, constant.ROOM_ADDR_ENDPOINT)) | ||
if r.status_code == 200: | ||
r = requests.put("{}/{}".format(constant.MAESTRO_ADDR, constant.ROOM_STATUS_ENDPOINT), json={"timestamp": int(time.time()), "status": "ready"}) | ||
break | ||
except Exception as ex: | ||
print(str(ex)) | ||
pass | ||
time.sleep(constant.POLLING_INTERVAL_IN_SECONDS) | ||
|
||
# Start falcon API | ||
app = falcon.API() | ||
app.add_route('/healthcheck', Healthcheck()) | ||
app.add_route('/address', Address()) | ||
app.add_route('/status', Status()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
|
||
# Config env | ||
MAESTRO_ADDR = "http://{}".format(os.environ.get("MAESTRO_HOST_PORT")) | ||
MAESTRO_SCHEDULER_NAME = os.environ.get("MAESTRO_SCHEDULER_NAME") | ||
MAESTRO_ROOM_ID = os.environ.get("MAESTRO_ROOM_ID") | ||
ROOM_MGMT_ENDPOINT = "scheduler/{}/rooms/{}".format(MAESTRO_SCHEDULER_NAME, MAESTRO_ROOM_ID) | ||
ROOM_ADDR_ENDPOINT = "{}/address".format(ROOM_MGMT_ENDPOINT) | ||
ROOM_STATUS_ENDPOINT = "{}/status".format(ROOM_MGMT_ENDPOINT) | ||
POLLING_INTERVAL_IN_SECONDS = int(os.environ.get("POLLING_INTERVAL_IN_SECONDS")) if os.environ.get("POLLING_INTERVAL_IN_SECONDS") else 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[loggers] | ||
keys=root | ||
|
||
[handlers] | ||
keys=stream_handler | ||
|
||
[formatters] | ||
keys=formatter | ||
|
||
[logger_root] | ||
level=DEBUG | ||
handlers=stream_handler | ||
|
||
[handler_stream_handler] | ||
class=StreamHandler | ||
level=DEBUG | ||
formatter=formatter | ||
args=(sys.stderr,) | ||
|
||
[formatter_formatter] | ||
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
astroid==2.0.2 | ||
certifi==2018.4.16 | ||
chardet==3.0.4 | ||
falcon==1.4.1 | ||
gunicorn==19.9.0 | ||
idna==2.7 | ||
isort==4.3.4 | ||
lazy-object-proxy==1.3.1 | ||
mccabe==0.6.1 | ||
pylint==2.1.0 | ||
python-mimeparse==1.6.0 | ||
requests==2.19.1 | ||
six==1.11.0 | ||
typed-ast==1.1.0 | ||
typing==3.6.4 | ||
urllib3==1.23 | ||
wrapt==1.10.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "scheduler-name", | ||
"game": "game-name", | ||
"image": "maestro-dev-room:latest", | ||
"imagePullPolicy": "Never", | ||
"ports": [ | ||
{ | ||
"containerPort": 8080, | ||
"protocol": "TCP", | ||
"name": "tcp" | ||
} | ||
], | ||
"limits": { | ||
"memory": "128Mi", | ||
"cpu": "20m" | ||
}, | ||
"shutdownTimeout": 10, | ||
"autoscaling": { | ||
"min": 4, | ||
"up": { | ||
"delta": 1, | ||
"trigger": { | ||
"usage": 70, | ||
"time": 1 | ||
}, | ||
"cooldown": 1 | ||
}, | ||
"down": { | ||
"delta": 1, | ||
"trigger": { | ||
"usage": 50, | ||
"time": 1 | ||
}, | ||
"cooldown": 1 | ||
} | ||
}, | ||
"env": [ | ||
{ | ||
"name": "MAESTRO_HOST_PORT", | ||
"value": "192.168.64.1:8080" | ||
}, | ||
{ | ||
"name": "POLLING_INTERVAL_IN_SECONDS", | ||
"value": "20" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.