Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Docker for local mode (#875)
Browse files Browse the repository at this point in the history
* Reduce image sizes for docker images

* bump version

* Merge branch 'development' into docker_for_local_mode

# Conflicts:
#	version.txt

* working docker in local mode!

* expose external port to aimmo-game

* detach aimmo-game-worker running from aimmo-game process

* delete previously running containers before creating new ones

* make docker container naming consistent with k8s

* make docker host os agnostic

* remove unnecessary log statement

* only re install deps if they have changed

* change hosts

* add Portainer instructions to usage, and small intro paragraph

* try to get host.docker.internal working on both linux and mac

* Merge branch 'docker_for_local_mode' of https://github.com/ocadotechnology/aimmo into docker_for_local_mode

* get port from env variable

* os dependent host

* use a template for container kwargs

* pass CONTAINER_TEMPLATE into worker

* provide default for LOCALHOST_IP

* provide defaults for environment variables to do with docker

* fix tests by mocking docker

* Adding docker install to travis to try and fix tests.

* Remove logs and adjust blank lines.

* Added settings to make Docker for local work for Ubuntu machines.

* Merge branch 'development' into docker_for_local_mode

* Remove extra whitespaces

* Merge branch 'docker_for_local_mode' of https://github.com/ocadotechnology/aimmo into docker_for_local_mode

* Merge branch 'development' of https://github.com/ocadotechnology/aimmo into docker_for_local_mode

* Bumped up version by minor change.

* Move docker logic into a separate file

Since docker setup is not related to minikube anymore

* inject env variables for aimmo-game

* Merge branch 'development' into docker_for_local_mode

# Conflicts:
#	version.txt

* remove newline in version file

* remove docker entrypoint scripts

* change experimental settings back to original values

* add time.sleep back to run script

* Code review changes

* Docstring changes and delete stopped aimmo containers as well

* change port setting back to original value

* fix docker version

* add missing quote

* Don’t fix versions in Pipfile as they are in the Pipfile.lock

* Update aimmo-game/Pipfile.lock

* Conform to pep8

* remove bash and curl from dockerfile

* Merge branch 'development' into docker_for_local_mode

# Conflicts:
#	aimmo-game/Pipfile
#	aimmo-game/Pipfile.lock
#	aimmo-game/service.py
#	version.txt
  • Loading branch information
faucomte97 authored and mrniket committed Oct 31, 2018
1 parent a58bb6e commit 6aedc9c
Show file tree
Hide file tree
Showing 24 changed files with 342 additions and 156 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
install:
- pip install .
- pip install coveralls
- pip install docker
- pushd game_frontend
- yarn
- node djangoBundler.js
Expand Down
6 changes: 4 additions & 2 deletions aimmo-game-creator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ FROM python:2-alpine

MAINTAINER [email protected]

COPY . .

RUN apk add --no-cache gcc musl-dev python-dev libffi-dev openssl-dev

RUN pip install pipenv

COPY ["Pipfile", "Pipfile.lock", "setup.py", "./"]

RUN pipenv install --system --deploy

COPY . .

CMD ["python", "./service.py"]
1 change: 1 addition & 0 deletions aimmo-game-creator/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "pypi"
[packages]
kubernetes = "*"
aimmo-game-creator = {editable = true, path = "."}
docker = "*"

[requires]
python_version = "2.7"
112 changes: 78 additions & 34 deletions aimmo-game-creator/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 24 additions & 13 deletions aimmo-game-creator/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from eventlet.greenpool import GreenPool
from eventlet.semaphore import Semaphore
import kubernetes
import docker
import json


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -131,7 +133,7 @@ def _parallel_map(self, func, *iterable_args):
class LocalGameManager(GameManager):
"""Manages games running on local host"""

host = "127.0.0.1"
host = os.environ.get('LOCALHOST_IP', '127.0.0.1')
game_directory = os.path.join(
os.path.dirname(__file__),
"../aimmo-game/",
Expand All @@ -143,19 +145,27 @@ def __init__(self, *args, **kwargs):
super(LocalGameManager, self).__init__(*args, **kwargs)

def create_game(self, game_id, game_data):
assert(game_id not in self.games)
port = str(6001 + int(game_id) * 1000)
process_args = [
"python",
self.game_service_path,
self.host,
port,
]
env = os.environ.copy()
def setup_container_environment_variables(template, game_data):
template['environment'].update(game_data)
template['environment']['GAME_ID'] = game_id
template['environment']['PYTHONUNBUFFERED'] = 0
template['environment']['WORKER_MANAGER'] = 'local'
template['environment']['EXTERNAL_PORT'] = port
template['environment']['CONTAINER_TEMPLATE'] = os.environ['CONTAINER_TEMPLATE']

assert (game_id not in self.games)
game_data = {str(k): str(v) for k, v in game_data.items()}
env.update(game_data)
env['GAME_ID'] = game_id
self.games[game_id] = subprocess.Popen(process_args, cwd=self.game_directory, env=env)
port = str(6001 + int(game_id) * 1000)
client = docker.from_env()

template = json.loads(os.environ.get('CONTAINER_TEMPLATE', '{}'))
setup_container_environment_variables(template, game_data)
template['ports'] = {"{}/tcp".format(port): ('0.0.0.0', port)}

self.games[game_id] = client.containers.run(
name="aimmo-game-{}".format(game_id),
image='ocadotechnology/aimmo-game:test',
**template)
game_url = "http://{}:{}".format(self.host, port)
LOGGER.info("Game started - {}, listening at {}".format(game_id, game_url))

Expand Down Expand Up @@ -233,6 +243,7 @@ def _create_game_rc(self, game_id, environment_variables):
environment_variables['GAME_URL'] = 'http://game-{}'.format(game_id)
environment_variables['IMAGE_SUFFIX'] = os.environ.get('IMAGE_SUFFIX', 'latest')
environment_variables['K8S_NAMESPACE'] = K8S_NAMESPACE
environment_variables['WORKER_MANAGER'] = 'kubernetes'

rc = self._make_rc(environment_variables, game_id)
self.api.create_namespaced_replication_controller(K8S_NAMESPACE, rc)
Expand Down
3 changes: 2 additions & 1 deletion aimmo-game-creator/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
def main():
logging.basicConfig(level=logging.DEBUG)
game_manager_class = GAME_MANAGERS[os.environ.get('GAME_MANAGER', 'local')]
host = os.environ.get('LOCALHOST_IP', 'localhost')
game_manager = game_manager_class(os.environ.get('GAME_API_URL',
'http://localhost:8000/aimmo/api/games/'))
"http://{}:8000/aimmo/api/games/".format(host)))
game_manager.run()


Expand Down
6 changes: 4 additions & 2 deletions aimmo-game-worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ FROM python:2-alpine

MAINTAINER [email protected]

COPY . .

RUN apk add --no-cache gcc musl-dev python-dev libffi-dev openssl-dev

RUN pip install pipenv

COPY ["Pipfile", "Pipfile.lock", "setup.py", "./"]

RUN pipenv install --system --deploy

COPY . .

CMD python service.py 0.0.0.0 5000 $DATA_URL
1 change: 0 additions & 1 deletion aimmo-game-worker/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


def get_code_and_options():
LOGGER.info('Data url: ' + DATA_URL)
data = requests.get(DATA_URL).json()
return data['code'], data['options']

Expand Down
8 changes: 5 additions & 3 deletions aimmo-game/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ MAINTAINER [email protected]

ENV WORKER_MANAGER=kubernetes

COPY . .

RUN apk add --no-cache gcc musl-dev python-dev libffi-dev openssl-dev

RUN pip install pipenv

COPY ["Pipfile", "Pipfile.lock", "setup.py", "./"]

RUN pipenv install --system --deploy

CMD ["python", "./service.py", "0.0.0.0", "5000"]
COPY . .

CMD ["python", "./service.py", "0.0.0.0"]
3 changes: 2 additions & 1 deletion aimmo-game/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ verify_ssl = true
name = "pypi"

[packages]
kubernetes = "*"
aimmo-game = {editable = true, path = "."}
docker = "*"
kubernetes = "*"
prometheus-client = "*"

[requires]
Expand Down
Loading

0 comments on commit 6aedc9c

Please sign in to comment.