Skip to content

Commit

Permalink
Merge pull request #5 from kreneskyp/version_select
Browse files Browse the repository at this point in the history
Version select, setup, etc
  • Loading branch information
kreneskyp authored Sep 12, 2023
2 parents 66115f2 + f155f65 commit f74fae1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 60 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ install: ## Install the project in dev mode.
.PHONY: fmt
fmt: ## Format code using black & isort.
$(ENV_PREFIX)isort agent_ix/
$(ENV_PREFIX)black -l 79 agent_ix/
$(ENV_PREFIX)black -l 79 tests/
$(ENV_PREFIX)black -l 98 agent_ix/
$(ENV_PREFIX)black -l 98 tests/

.PHONY: lint
lint: ## Run pep8, black, mypy linters.
$(ENV_PREFIX)flake8 agent_ix/
$(ENV_PREFIX)black -l 79 --check agent_ix/
$(ENV_PREFIX)black -l 79 --check tests/
$(ENV_PREFIX)black -l 98 --check agent_ix/
$(ENV_PREFIX)black -l 98 --check tests/

.PHONY: test
test: lint ## Run tests and generate coverage report.
Expand Down
2 changes: 1 addition & 1 deletion agent_ix/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.2.0
147 changes: 98 additions & 49 deletions agent_ix/cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import argparse
import os
import subprocess
import pkg_resources

import pkg_resources

DOCKER_COMPOSE_PATH = pkg_resources.resource_filename(
"agent_ix", "docker-compose.yml"
)
IMAGE = "ghcr.io/kreneskyp/ix/sandbox"
DOCKER_COMPOSE_PATH = pkg_resources.resource_filename("agent_ix", "docker-compose.yml")
IX_ENV_TEMPLATE = pkg_resources.resource_filename("agent_ix", "ix.env")
CWD = os.getcwd()
IX_ENV_PATH = os.path.join(CWD, "ix.env")
IX_INIT = os.path.join(CWD, ".ix")


# ==============================
# Setup and environment
# ==============================


def init_env():
Expand All @@ -20,7 +25,31 @@ def init_env():
f2.write(f.read())


def get_env():
def initial_setup(args):
"""Sets up the IX database on the first run
While this is safe to run on every startup, it's not necessary and will
auto-trigger migrations or re-run fixtures. The main concern is that fixtures
will override any changes made to the database.
"""
if os.path.exists(IX_INIT):
return

setup(args)
subprocess.run(["touch", IX_INIT])


def migrate(args):
print("Running IX database migrations")
run_manage_py_command("migrate")


def setup(args):
migrate(args)
run_manage_py_command("setup")


def get_env(env=None):
init_env()
nginx_conf = pkg_resources.resource_filename("agent_ix", "nginx.conf")

Expand All @@ -29,14 +58,19 @@ def get_env():
"IX_IMAGE_TAG": "latest",
"IX_ENV": IX_ENV_PATH,
**os.environ,
**(env or {}),
}


# ==============================
# Compose helpers
# ==============================


def run_docker_compose_command(subcommand, *args, **kwargs):
env = get_env()
cmd = ["docker-compose", "-f", DOCKER_COMPOSE_PATH, subcommand] + list(
args
)
runtime_env = kwargs.get("env", {})
env = get_env(env=runtime_env)
cmd = ["docker-compose", "-f", DOCKER_COMPOSE_PATH, subcommand] + list(args)
subprocess.run(cmd, env=env)


Expand All @@ -47,29 +81,67 @@ def run_manage_py_command(subcommand, *args):
"-f",
DOCKER_COMPOSE_PATH,
"exec",
"app",
"web",
"./manage.py",
subcommand,
] + list(args)
subprocess.run(cmd, env=env)


# ==============================
# Server management
# ==============================


def up(args):
env = {"IX_IMAGE_TAG": "latest"}
if args.version:
env["IX_IMAGE_TAG"] = args.version

print("Starting IX Sandbox")
print(f"image: {IMAGE}:{env['IX_IMAGE_TAG']}")
print(f"env: {IX_ENV_PATH}")
print("------------------------------------------------")

# destroy static on each startup so that it is always pulled fresh from the
# container this avoids stale files from a version prior to what is running.
subprocess.run(["docker", "volume", "rm", "agent_ix_static"])
run_docker_compose_command(
"up",
"-d",
)

# manually pull the image to ensure we have the latest version
subprocess.run(["docker", "pull", f"{IMAGE}:{env['IX_IMAGE_TAG']}"])

# startup the containers
run_docker_compose_command("up", "-d", env=env)

# run initial setup - requires app and database are running
initial_setup(args)

# app is ready!
print_welcome_message(version=env["IX_IMAGE_TAG"])


def print_welcome_message(version):
print("================================================")
print(f"IX Sandbox ({version}) is running on http://localhost:8000")
print()
print("To set global API keys for OpenAI and other services edit ix.env and restart:")
print(IX_ENV_PATH)
print()
print("---- Management Commands ----")
print("stop : ix down")
print("scale : ix scale 3")
print("web log : ix log web nginx")
print("worker log : ix log worker")


def down(args):
print("Stopping IX Sandbox")
run_docker_compose_command("down")


def scale(args):
num = args.num
print(f"Scaling IX agent workers to {num}")
run_docker_compose_command("up", "-d", "--scale", f"worker={num}")


Expand All @@ -78,19 +150,9 @@ def log(args):
run_docker_compose_command("logs", "--tail=50", "--follow", *services)


def migrate(args):
run_manage_py_command("migrate")


def setup(args):
migrate()
# run_manage_py_command('setup')
run_manage_py_command("loaddata", "fake_user")
run_manage_py_command("loaddata", "node_types")
run_manage_py_command(
"loaddata",
"ix_v2 code_v2 pirate_v1 wikipedia_v1 klarna_v1 bot_smith_v1",
)
# ==============================
# Main
# ==============================


def version(args):
Expand All @@ -100,27 +162,22 @@ def version(args):


def main():
parser = argparse.ArgumentParser(
description="Docker-compose and Django CLI wrapper."
)
parser = argparse.ArgumentParser(description="Docker-compose and Django CLI wrapper.")
subparsers = parser.add_subparsers(
title="Subcommands",
description="Valid subcommands",
help="Available operations",
)

# 'up' subcommand
parser_version = subparsers.add_parser(
"version", help="report client version"
)
parser_version = subparsers.add_parser("version", help="report client version")
parser_version.set_defaults(func=version)

# 'up' subcommand
parser_up = subparsers.add_parser(
"up", help="Start services in the background"
parser_up = subparsers.add_parser("up", help="Start services in the background")
parser_up.add_argument(
"--version", type=str, default=None, help="IX sandbox image tag run (e.g. 0.1.1)"
)
# parser_up.add_argument('version', help='Version to load (e.g. 0.1)',
# default="latest")
parser_up.set_defaults(func=up)

# 'down' subcommand
Expand All @@ -132,31 +189,23 @@ def main():

# 'scale' subcommand
parser_scale = subparsers.add_parser("scale", help="Scale agent workers")
parser_scale.add_argument(
"num", type=int, help="Number of agent workers to scale to"
)
parser_scale.add_argument("num", type=int, help="Number of agent workers to scale to")
parser_scale.set_defaults(func=scale)

# 'log' subcommand
parser_log = subparsers.add_parser(
"log",
help="View output from containers [worker, web, nginx, db, redis]",
)
parser_log.add_argument(
"services", nargs="+", help="Names of the services to show logs for"
)
parser_log.add_argument("services", nargs="+", help="Names of the services to show logs for")
parser_log.set_defaults(func=log)

# 'migrate' subcommand
parser_migrate = subparsers.add_parser(
"migrate", help="Run Django database migrations"
)
parser_migrate = subparsers.add_parser("migrate", help="Run Django database migrations")
parser_migrate.set_defaults(func=migrate)

# 'setup' subcommand
parser_setup = subparsers.add_parser(
"setup", help="Initialize database and load fixtures"
)
parser_setup = subparsers.add_parser("setup", help="Initialize database and load fixtures")
parser_setup.set_defaults(func=setup)

args = parser.parse_args()
Expand Down
4 changes: 2 additions & 2 deletions agent_ix/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- db
- redis
env_file:
- ix.env
- ${IX_ENV}
volumes:
- static:/var/static/
environment:
Expand All @@ -44,7 +44,7 @@ services:
- db
- redis
env_file:
- ix.env
- ${IX_ENV}

redis:
image: redis/redis-stack-server:latest
Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ def read_requirements(path):
author="kreneskyp",
packages=find_packages(exclude=["tests", ".github"]),
install_requires=read_requirements("requirements.txt"),
entry_points={
"console_scripts": ["ix = agent_ix.__main__:main"]
},
entry_points={"console_scripts": ["ix = agent_ix.__main__:main"]},
package_data={
'agent_ix': ['docker-compose.yml', 'nginx.conf', 'ix.env', 'VERSION'],
"agent_ix": ["docker-compose.yml", "nginx.conf", "ix.env", "VERSION"],
},
extras_require={"test": read_requirements("requirements-test.txt")},
)

0 comments on commit f74fae1

Please sign in to comment.