Compare commits
3 Commits
77935b49e8
...
b1878aef1b
Author | SHA1 | Date | |
---|---|---|---|
b1878aef1b | |||
71fe46e881 | |||
d0adb3b0ed |
@ -21,7 +21,7 @@ These are arguments that go between `otherworld` and the command.
|
|||||||
Name to assign to container (default: `otherworld_$USER`)
|
Name to assign to container (default: `otherworld_$USER`)
|
||||||
|
|
||||||
### --image=<image>
|
### --image=<image>
|
||||||
Image to create the container with (default: `debian:stable`)
|
Image to create the container with (default: `image_$CONTAINER_NAME`, or `debian:stable` if that image does not exist)
|
||||||
|
|
||||||
### --sudo
|
### --sudo
|
||||||
Runs command as root, instead of current user.
|
Runs command as root, instead of current user.
|
||||||
@ -29,6 +29,9 @@ Runs command as root, instead of current user.
|
|||||||
### --rm
|
### --rm
|
||||||
Removes the specified container.
|
Removes the specified container.
|
||||||
|
|
||||||
|
### --rmi
|
||||||
|
Removes image generated by the `otherworld` script (`image_$CONTAINER_NAME`)
|
||||||
|
|
||||||
### --commit
|
### --commit
|
||||||
Commits the container to an image named `image_$CONTAINER_NAME`.
|
Commits the container to an image named `image_$CONTAINER_NAME`.
|
||||||
|
|
||||||
|
67
otherworld
Normal file → Executable file
67
otherworld
Normal file → Executable file
@ -7,7 +7,7 @@ import sys
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
CONTAINER_NAME = "otherworld"
|
CONTAINER_NAME = "otherworld"
|
||||||
IMAGE_NAME = "debian:stable"
|
DEFAULT_IMAGE_NAME = "debian:stable"
|
||||||
|
|
||||||
def create_x11_mapping ():
|
def create_x11_mapping ():
|
||||||
home = os.environ["HOME"]
|
home = os.environ["HOME"]
|
||||||
@ -72,6 +72,9 @@ def docker_commit (container, image_name):
|
|||||||
def docker_rm (container):
|
def docker_rm (container):
|
||||||
call(["docker", "rm", "-f", container])
|
call(["docker", "rm", "-f", container])
|
||||||
|
|
||||||
|
def docker_rmi (container):
|
||||||
|
call(["docker", "rmi", "-f", container])
|
||||||
|
|
||||||
def docker_build (build_path, options, image_name):
|
def docker_build (build_path, options, image_name):
|
||||||
check_call(["docker", "build"] + options + ["-t", image_name, build_path])
|
check_call(["docker", "build"] + options + ["-t", image_name, build_path])
|
||||||
return image_name
|
return image_name
|
||||||
@ -110,7 +113,7 @@ def expand_user_volumes (volumes):
|
|||||||
|
|
||||||
user = os.environ["USER"]
|
user = os.environ["USER"]
|
||||||
uid = int(check_output(["id", "-u"]).strip())
|
uid = int(check_output(["id", "-u"]).strip())
|
||||||
image_name = os.environ.get("OW_IMAGE", IMAGE_NAME)
|
image_name = os.environ.get("OW_IMAGE", None)
|
||||||
container_name = os.environ.get("OW_CONTAINER", f"{CONTAINER_NAME}_{user}")
|
container_name = os.environ.get("OW_CONTAINER", f"{CONTAINER_NAME}_{user}")
|
||||||
build_path = None
|
build_path = None
|
||||||
|
|
||||||
@ -119,6 +122,8 @@ command_user = user
|
|||||||
user_volumes = USER_VOLUMES
|
user_volumes = USER_VOLUMES
|
||||||
pull = False
|
pull = False
|
||||||
|
|
||||||
|
actions = []
|
||||||
|
|
||||||
command_env = {}
|
command_env = {}
|
||||||
|
|
||||||
quiet = False
|
quiet = False
|
||||||
@ -128,25 +133,20 @@ while len(command) > 0:
|
|||||||
command = command[1:]
|
command = command[1:]
|
||||||
quiet = True
|
quiet = True
|
||||||
elif arg == "--rm":
|
elif arg == "--rm":
|
||||||
if not quiet:
|
actions.append("rm")
|
||||||
print(f">> Removing container: {container_name}")
|
command = command[1:]
|
||||||
docker_rm(container_name)
|
elif arg == "--rmi":
|
||||||
sys.exit(0)
|
actions.append("rmi")
|
||||||
|
command = command[1:]
|
||||||
elif arg == "--commit":
|
elif arg == "--commit":
|
||||||
image_name = get_generated_image_name(container_name)
|
actions.append("commit")
|
||||||
if not quiet:
|
command = command[1:]
|
||||||
print(f">> Committing container: {container_name} to image: {image_name}")
|
|
||||||
docker_commit(container_name, image_name)
|
|
||||||
sys.exit(0)
|
|
||||||
elif arg == "--sudo":
|
elif arg == "--sudo":
|
||||||
command_user = "root"
|
command_user = "root"
|
||||||
command = command[1:]
|
command = command[1:]
|
||||||
elif arg.startswith("--container="):
|
elif arg.startswith("--container="):
|
||||||
container_name = arg[len("--container="):]
|
container_name = arg[len("--container="):]
|
||||||
command = command[1:]
|
command = command[1:]
|
||||||
elif arg.startswith("--image"):
|
|
||||||
image_name = get_generated_image_name(container_name)
|
|
||||||
command = command[1:]
|
|
||||||
elif arg.startswith("--image="):
|
elif arg.startswith("--image="):
|
||||||
image_name = arg[len("--image="):]
|
image_name = arg[len("--image="):]
|
||||||
command = command[1:]
|
command = command[1:]
|
||||||
@ -167,7 +167,28 @@ while len(command) > 0:
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
if len(command) == 0:
|
if actions:
|
||||||
|
if "commit" in actions:
|
||||||
|
target_image_name = get_generated_image_name(container_name)
|
||||||
|
if not quiet:
|
||||||
|
print(f">> Committing container: {container_name} to image: {target_image_name}")
|
||||||
|
docker_commit(container_name, target_image_name)
|
||||||
|
|
||||||
|
if "rm" in actions:
|
||||||
|
if not quiet:
|
||||||
|
print(f">> Removing container: {container_name}")
|
||||||
|
docker_rm(container_name)
|
||||||
|
|
||||||
|
if "rmi" in actions:
|
||||||
|
target_image_name = get_generated_image_name(container_name)
|
||||||
|
if not quiet:
|
||||||
|
print(f">> Removing image: {target_image_name}")
|
||||||
|
docker_rmi(target_image_name)
|
||||||
|
|
||||||
|
if not command:
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
if not command:
|
||||||
command = DEFAULT_COMMAND
|
command = DEFAULT_COMMAND
|
||||||
|
|
||||||
container = None
|
container = None
|
||||||
@ -177,8 +198,20 @@ except Exception:
|
|||||||
build_path = resolve_build_path(build_path)
|
build_path = resolve_build_path(build_path)
|
||||||
if build_path is not None:
|
if build_path is not None:
|
||||||
image_name = docker_build(build_path, ["--pull"] if pull else [], get_generated_image_name(container_name))
|
image_name = docker_build(build_path, ["--pull"] if pull else [], get_generated_image_name(container_name))
|
||||||
elif pull:
|
else:
|
||||||
docker_pull(image_name)
|
if image_name is None:
|
||||||
|
image_name = get_generated_image_name(container_name)
|
||||||
|
try:
|
||||||
|
docker_inspect(image_name)
|
||||||
|
if not quiet:
|
||||||
|
print(f">> Using default otherworld image {image_name}")
|
||||||
|
except Exception:
|
||||||
|
if not quiet:
|
||||||
|
print(f">> Default otherworld image {image_name} not found, falling back to {DEFAULT_IMAGE_NAME}")
|
||||||
|
image_name = DEFAULT_IMAGE_NAME
|
||||||
|
|
||||||
|
if pull:
|
||||||
|
docker_pull(image_name)
|
||||||
|
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print(f">> Creating container: {container_name} (using image: {image_name})")
|
print(f">> Creating container: {container_name} (using image: {image_name})")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user