2019-01-22 20:25:04 +00:00
|
|
|
import click
|
|
|
|
|
2019-03-18 16:26:37 +00:00
|
|
|
from . import config as tutor_config
|
2019-05-11 19:20:09 +00:00
|
|
|
from .. import env as tutor_env
|
|
|
|
from .. import fmt
|
|
|
|
from .. import opts
|
|
|
|
from .. import utils
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
@click.group(short_help="Manage docker images")
|
2019-04-23 07:57:55 +00:00
|
|
|
def images_command():
|
2019-01-22 20:25:04 +00:00
|
|
|
pass
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
OPENEDX_IMAGES = ["openedx", "forum", "notes", "xqueue", "android"]
|
2019-05-05 09:45:24 +00:00
|
|
|
VENDOR_IMAGES = [
|
|
|
|
"elasticsearch",
|
|
|
|
"memcached",
|
|
|
|
"mongodb",
|
|
|
|
"mysql",
|
|
|
|
"nginx",
|
|
|
|
"rabbitmq",
|
|
|
|
"smtp",
|
|
|
|
]
|
2019-03-18 21:39:35 +00:00
|
|
|
argument_openedx_image = click.argument(
|
2019-05-05 09:45:24 +00:00
|
|
|
"image", type=click.Choice(["all"] + OPENEDX_IMAGES)
|
2019-03-18 21:39:35 +00:00
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
argument_image = click.argument(
|
2019-05-05 09:45:24 +00:00
|
|
|
"image", type=click.Choice(["all"] + OPENEDX_IMAGES + VENDOR_IMAGES)
|
2019-01-22 20:25:04 +00:00
|
|
|
)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
@click.command(
|
|
|
|
short_help="Build docker images",
|
2019-05-05 09:45:24 +00:00
|
|
|
help="Build the docker images necessary for an Open edX platform.",
|
2019-02-13 19:18:47 +00:00
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
@opts.root
|
2019-03-18 21:39:35 +00:00
|
|
|
@argument_openedx_image
|
2019-05-22 17:17:54 +00:00
|
|
|
@click.option(
|
|
|
|
"--no-cache", is_flag=True, help="Do not use cache when building the image"
|
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
@click.option(
|
2019-05-05 09:45:24 +00:00
|
|
|
"-a",
|
|
|
|
"--build-arg",
|
|
|
|
multiple=True,
|
|
|
|
help="Set build-time docker ARGS in the form 'myarg=value'. This option may be specified multiple times.",
|
2019-01-22 20:25:04 +00:00
|
|
|
)
|
2019-05-22 17:17:54 +00:00
|
|
|
def build(root, image, no_cache, build_arg):
|
2019-03-18 16:26:37 +00:00
|
|
|
config = tutor_config.load(root)
|
2019-04-25 19:58:06 +00:00
|
|
|
for img in openedx_image_names(config, image):
|
|
|
|
tag = get_tag(config, img)
|
2019-01-22 20:25:04 +00:00
|
|
|
click.echo(fmt.info("Building image {}".format(tag)))
|
2019-05-05 09:45:24 +00:00
|
|
|
command = ["build", "-t", tag, tutor_env.pathjoin(root, "build", img)]
|
2019-05-22 17:17:54 +00:00
|
|
|
if no_cache:
|
|
|
|
command.append("--no-cache")
|
2019-01-22 20:25:04 +00:00
|
|
|
for arg in build_arg:
|
2019-05-05 09:45:24 +00:00
|
|
|
command += ["--build-arg", arg]
|
2019-01-22 20:25:04 +00:00
|
|
|
utils.docker(*command)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
@click.command(short_help="Pull images from the Docker registry")
|
2019-03-18 16:26:37 +00:00
|
|
|
@opts.root
|
2019-02-13 19:18:47 +00:00
|
|
|
@argument_image
|
2019-03-18 20:53:18 +00:00
|
|
|
def pull(root, image):
|
2019-03-18 16:26:37 +00:00
|
|
|
config = tutor_config.load(root)
|
2019-04-23 07:57:55 +00:00
|
|
|
for img in image_names(config, image):
|
|
|
|
tag = get_tag(config, img)
|
2019-02-13 19:18:47 +00:00
|
|
|
click.echo(fmt.info("Pulling image {}".format(tag)))
|
|
|
|
utils.execute("docker", "pull", tag)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-03-18 20:53:18 +00:00
|
|
|
@click.command(short_help="Push images to the Docker registry")
|
2019-03-18 16:26:37 +00:00
|
|
|
@opts.root
|
2019-03-18 21:39:35 +00:00
|
|
|
@argument_openedx_image
|
2019-03-18 20:53:18 +00:00
|
|
|
def push(root, image):
|
2019-03-18 16:26:37 +00:00
|
|
|
config = tutor_config.load(root)
|
2019-04-23 07:57:55 +00:00
|
|
|
for tag in openedx_image_tags(config, image):
|
2019-01-22 20:25:04 +00:00
|
|
|
click.echo(fmt.info("Pushing image {}".format(tag)))
|
|
|
|
utils.execute("docker", "push", tag)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-03-18 20:53:18 +00:00
|
|
|
def get_tag(config, name):
|
|
|
|
image = config["DOCKER_IMAGE_" + name.upper()]
|
2019-05-05 09:45:24 +00:00
|
|
|
return "{registry}{image}".format(registry=config["DOCKER_REGISTRY"], image=image)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
def image_names(config, image):
|
|
|
|
return openedx_image_names(config, image) + vendor_image_names(config, image)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
|
|
|
def openedx_image_tags(config, image):
|
|
|
|
for img in openedx_image_names(config, image):
|
|
|
|
yield get_tag(config, img)
|
|
|
|
|
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
def openedx_image_names(config, image):
|
2019-03-18 16:26:37 +00:00
|
|
|
if image == "all":
|
2019-03-18 21:39:35 +00:00
|
|
|
images = OPENEDX_IMAGES[:]
|
2019-05-05 09:45:24 +00:00
|
|
|
if not config["ACTIVATE_XQUEUE"]:
|
|
|
|
images.remove("xqueue")
|
|
|
|
if not config["ACTIVATE_NOTES"]:
|
|
|
|
images.remove("notes")
|
2019-03-18 16:26:37 +00:00
|
|
|
return images
|
|
|
|
return [image]
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
def vendor_image_names(config, image):
|
2019-03-20 17:59:09 +00:00
|
|
|
if image == "all":
|
|
|
|
images = VENDOR_IMAGES[:]
|
2019-05-05 09:45:24 +00:00
|
|
|
if not config["ACTIVATE_ELASTICSEARCH"]:
|
|
|
|
images.remove("elasticsearch")
|
|
|
|
if not config["ACTIVATE_MEMCACHED"]:
|
|
|
|
images.remove("memcached")
|
|
|
|
if not config["ACTIVATE_MONGODB"]:
|
|
|
|
images.remove("mongodb")
|
|
|
|
if not config["ACTIVATE_MYSQL"]:
|
|
|
|
images.remove("mysql")
|
|
|
|
if not config["ACTIVATE_RABBITMQ"]:
|
|
|
|
images.remove("rabbitmq")
|
2019-03-21 13:08:47 +00:00
|
|
|
return images
|
2019-03-20 17:59:09 +00:00
|
|
|
return [image]
|
2019-03-18 21:39:35 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
|
|
|
images_command.add_command(build)
|
|
|
|
images_command.add_command(pull)
|
|
|
|
images_command.add_command(push)
|