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

Commit

Permalink
more suggestions, move some build cli stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaudill committed Jul 15, 2024
1 parent 7f17900 commit 8d0befc
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 101 deletions.
2 changes: 1 addition & 1 deletion bin/ch-checkns.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void fatal_(const char *file, int line, int errno_, const char *str)
char *url = "https://github.com/hpc/charliecloud/blob/master/bin/ch-checkns.c";
printf("error: %s: %d: %s\n", file, line, str);
printf("errno: %d\nsee: %s\n", errno_, url);
exit(ERR_CHRUN);
exit(EXIT_CHRUN);
}

int main(int argc, char *argv[])
Expand Down
16 changes: 15 additions & 1 deletion bin/ch-image.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,25 @@ def main():
sp = ap.add_parser("modify", "foo")
add_opts(sp, modify.main, deps_check=True, stog_init=True)
sp.add_argument("-c", metavar="CMD", action="append", default=[], nargs=1, help="foo")
sp.add_argument("-S", "--shell", metavar="shell", help="foo")
sp.add_argument("-i", "--interactive", action="store_true", help="foo")
sp.add_argument("-t", "--test", action="store_true", help="foo")
sp.add_argument("-S", "--shell", metavar="shell", default="/bin/sh", help="foo")
sp.add_argument("image_ref", metavar="IMAGE_REF", help="image to modify")
sp.add_argument("out_image", metavar="OUT_IMAGE", help="destination of modified image")
# Optional positional argument? https://stackoverflow.com/a/4480202
sp.add_argument("script", metavar="SCRIPT", help="foo", nargs='?')
sp.add_argument("-b", "--bind", metavar="SRC[:DST]",
action="append", default=[],
help="mount SRC at guest DST (default: same as SRC)")
sp.add_argument("--build-arg", metavar="ARG[=VAL]",
action="append", default=[],
help="set build-time variable ARG to VAL, or $ARG if no VAL")
sp.add_argument("--force", metavar="MODE", nargs="?", default="seccomp",
type=ch.Force_Mode, const="seccomp",
help="inject unprivileged build workarounds")
sp.add_argument("--force-cmd", metavar="CMD,ARG1[,ARG2...]",
action="append", default=[],
help="command arg(s) to add under --force=seccomp")

# pull
sp = ap.add_parser("pull",
Expand Down
6 changes: 3 additions & 3 deletions bin/ch-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
#ifdef HAVE_FNM_EXTMATCH
exit(0);
#else
exit(ERR_CHRUN);
exit(EXIT_CHRUN);
#endif
} else if (!strcmp(arg, "overlayfs")) {
#ifdef HAVE_OVERLAYFS
Expand All @@ -457,13 +457,13 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
#ifdef HAVE_SECCOMP
exit(0);
#else
exit(ERR_CHRUN);
exit(EXIT_CHRUN);
#endif
} else if (!strcmp(arg, "squash")) {
#ifdef HAVE_LIBSQUASHFUSE
exit(0);
#else
exit(ERR_CHRUN);
exit(EXIT_CHRUN);
#endif
} else if (!strcmp(arg, "tmpfs-xattrs")) {
#ifdef HAVE_TMPFS_XATTRS
Expand Down
2 changes: 1 addition & 1 deletion bin/ch_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ void run_user_command(char *argv[], const char *initial_dir)
T_ (freopen("/dev/null", "w", stderr));
execvp(argv[0], argv); // only returns if error
ERROR(errno, "can't execve(2): %s", argv[0]);
exit(ERR_CMD);
exit(EXIT_CMD);
}

/* Set up the fake-syscall seccomp(2) filter. This computes and installs a
Expand Down
2 changes: 1 addition & 1 deletion bin/ch_fuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int sq_loop(void)

// Clean up zombie child if exit signal was SIGCHLD.
if (!sigchld_received)
exit_code = ERR_SQUASH;
exit_code = EXIT_SQUASH;
else {
Tf (wait(&child_status) >= 0, "can't wait for child");
if (WIFEXITED(child_status)) {
Expand Down
2 changes: 1 addition & 1 deletion bin/ch_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ noreturn void msg_fatal(const char *file, int line, int errno_,
msgv(LL_FATAL, file, line, errno_, fmt, ap);
va_end(ap);

exit(ERR_CHRUN);
exit(EXIT_CHRUN);
}

/* va_list form of msg(). */
Expand Down
6 changes: 3 additions & 3 deletions bin/ch_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#define WARNINGS_SIZE (4*1024)

/* Exit codes (see also: test/common.bash, lib/build.py). */
#define ERR_CHRUN 31
#define ERR_CMD 49
#define ERR_SQUASH 84
#define EXIT_CHRUN 31
#define EXIT_CMD 49
#define EXIT_SQUASH 84

/* Test some value, and if it's not what we expect, exit with a fatal error.
These are macros so we have access to the file and line number.
Expand Down
2 changes: 1 addition & 1 deletion doc/ch-image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ Delete all images and cache from ch-image builder storage.


:code:`modify`
=============
==============

Modify an image with shell commands, possibly interactively.

Expand Down
64 changes: 33 additions & 31 deletions lib/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def main(cli_):
tree = parse_dockerfile(text)

global image_ct
ml = traverse_parse_tree(tree, image_ct, cli)
ml = parse_tree_traverse(tree, image_ct, cli)

## Functions ##

Expand All @@ -144,35 +144,6 @@ def main(cli_):
# reference, so changes made to mutable objects (which “cli” is) will persist in
# the scope of the caller.'
def cli_process(cli):
# Infer input file if needed.
if (cli.file is None):
cli.file = cli.context + "/Dockerfile"

# Infer image name if needed.
if (cli.tag is None):
path = os.path.basename(cli.file)
if ("." in path):
(base, ext_all) = str(path).split(".", maxsplit=1)
(base_all, ext_last) = str(path).rsplit(".", maxsplit=1)
else:
base = None
ext_last = None
if (base == "Dockerfile"):
cli.tag = ext_all
ch.VERBOSE("inferring name from Dockerfile extension: %s" % cli.tag)
elif (ext_last in ("df", "dockerfile")):
cli.tag = base_all
ch.VERBOSE("inferring name from Dockerfile basename: %s" % cli.tag)
elif (os.path.abspath(cli.context) != "/"):
cli.tag = os.path.basename(os.path.abspath(cli.context))
ch.VERBOSE("inferring name from context directory: %s" % cli.tag)
else:
assert (os.path.abspath(cli.context) == "/")
cli.tag = "root"
ch.VERBOSE("inferring name with root context directory: %s" % cli.tag)
cli.tag = re.sub(r"[^a-z0-9_.-]", "", cli.tag.lower())
ch.INFO("inferred image name: %s" % cli.tag)

# --force and friends.
if (cli.force_cmd and cli.force == ch.Force_Mode.FAKEROOT):
ch.FATAL("--force-cmd and --force=fakeroot are incompatible")
Expand Down Expand Up @@ -206,6 +177,37 @@ def build_arg_get(arg):
ch.FATAL("--build-arg: %s: no value and not in environment" % kv[0])
return (kv[0], v)
cli.build_arg = dict( build_arg_get(i) for i in cli.build_arg )

# Infer input file if needed.
if (cli.file is None):
cli.file = cli.context + "/Dockerfile"

# Infer image name if needed.
if (cli.tag is None):
path = os.path.basename(cli.file)
if ("." in path):
(base, ext_all) = str(path).split(".", maxsplit=1)
(base_all, ext_last) = str(path).rsplit(".", maxsplit=1)
else:
base = None
ext_last = None
if (base == "Dockerfile"):
cli.tag = ext_all
ch.VERBOSE("inferring name from Dockerfile extension: %s" % cli.tag)
elif (ext_last in ("df", "dockerfile")):
cli.tag = base_all
ch.VERBOSE("inferring name from Dockerfile basename: %s" % cli.tag)
elif (os.path.abspath(cli.context) != "/"):
cli.tag = os.path.basename(os.path.abspath(cli.context))
ch.VERBOSE("inferring name from context directory: %s" % cli.tag)
else:
assert (os.path.abspath(cli.context) == "/")
cli.tag = "root"
ch.VERBOSE("inferring name with root context directory: %s" % cli.tag)
cli.tag = re.sub(r"[^a-z0-9_.-]", "", cli.tag.lower())
ch.INFO("inferred image name: %s" % cli.tag)


ch.DEBUG(cli)

# Guess whether the context is a URL, and error out if so. This can be a
Expand Down Expand Up @@ -280,7 +282,7 @@ def parse_dockerfile(text):
# [1]: https://lark-parser.readthedocs.io/en/latest/visitors/#visitors
# [2]: https://github.com/lark-parser/lark/blob/445c8d4/lark/visitors.py#L211
# [3]: https://lark-parser.readthedocs.io/en/latest/classes/#tree
def traverse_parse_tree(tree, image_ct_, cli_):
def parse_tree_traverse(tree, image_ct_, cli_):
global cli
global image_ct
cli = cli_
Expand Down
6 changes: 3 additions & 3 deletions lib/charliecloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class Build_Mode(enum.Enum):

# ch-run exit codes (see also: bin/ch_misc.h)
class Ch_Run_Retcode(enum.Enum):
ERR_CHRUN = 31
ERR_CMD = 49
ERR_SQUASH = 84
EXIT_CHRUN = 31
EXIT_CMD = 49
EXIT_SQUASH = 84

# Download cache mode.
class Download_Mode(enum.Enum):
Expand Down
Loading

0 comments on commit 8d0befc

Please sign in to comment.