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

Commit

Permalink
more work on non-interactive script case
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaudill committed Jun 4, 2024
1 parent 26ce9e6 commit dbbf74d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/ch-image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,7 @@ Non-interactive mode, commands specified with :code:`-c`

The following are equivalent::

$ ch-image modify -S /bin/bash -c 'echo hello' -c 'echo world' foo bar
$ ch-image modify -S /bin/ash -c 'echo hello' -c 'echo world' foo bar

and::

Expand Down
20 changes: 20 additions & 0 deletions lib/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ def modify(cli_):
# (if specified).
cli.tag = str(out_image)

if ((not sys.stdin.isatty()) and (commands == [])):
# https://stackoverflow.com/a/6482200
stdin = sys.stdin.read()
# We use “decode("utf-8")” here because stdout seems default to a bytes
# object, which is not a valid type for an argument for “Path”.
tmpfile = ch.Path(subprocess.run(["mktemp", "-d"],capture_output=True).stdout.decode("utf-8"))
with open(tmpfile, "w") as outfile:
outfile.write(stdin)
# By default, the file is seemingly created with its execute bit
# unassigned. This is problematic for the RUN instruction.
os.chmod(tmpfile, 0o755)
cli.script = str(tmpfile)

if (cli.shell is not None):
shell = cli.shell
else:
Expand Down Expand Up @@ -376,6 +389,10 @@ def modify_tree_make(src_img, cmds):
meta.line = -1
#df_children.append(im.Tree(lark.Token('RULE', 'from_'), [im.Tree(lark.Token('RULE', 'image_ref'),[lark.Token('IMAGE_REF', src_img.name)], meta)], meta))
df_children.append(im.Tree(lark.Token('RULE', 'from_'), [im.Tree(lark.Token('RULE', 'image_ref'),[lark.Token('IMAGE_REF', str(src_img))], meta)], meta))
if (cli.shell is not None):
#df_children.append(im.Tree(lark.Token('RULE', 'shell'), [lark.Token('STRING_QUO', "/bin/sh"),lark.Token('STRING_QUO', "-c")], meta))
#ch.ILLERI("HERE")
df_children.append(im.Tree(lark.Token('RULE', 'shell'), [lark.Token('STRING_QUOTED', '"%s"' % cli.shell),lark.Token('STRING_QUOTED', '"-c"')],meta))
for cmd in cmds:
df_children.append(im.Tree(lark.Token('RULE', 'run'), [im.Tree(lark.Token('RULE', 'run_shell'),[lark.Token('LINE_CHUNK', cmd)], meta)],meta))
return im.Tree(lark.Token('RULE', 'start'), [im.Tree(lark.Token('RULE','dockerfile'), df_children)], meta)
Expand Down Expand Up @@ -412,6 +429,9 @@ def modify_tree_make_script(src_img, path):
meta = lark.tree.Meta()
meta.line = -1
df_children.append(im.Tree(lark.Token('RULE', 'from_'), [im.Tree(lark.Token('RULE', 'image_ref'),[lark.Token('IMAGE_REF', str(src_img))], meta)], meta))
if (cli.shell is not None):
#df_children.append(im.Tree(lark.Token('RULE', 'from_'), [im.Tree(lark.Token('RULE', 'shell'),[lark.Token('STRING_QUO', "%s" % cli.shell),lark.Token('STRING_QUO', "-c")], meta)], meta))
df_children.append(im.Tree(lark.Token('RULE', 'shell'), [lark.Token('STRING_QUOTED', '"%s"' % cli.shell),lark.Token('STRING_QUOTED', '"-c"')],meta))
df_children.append(im.Tree(lark.Token('RULE', 'copy'), [im.Tree(lark.Token('RULE', 'copy_shell'),[lark.Token('WORD', path),lark.Token('WORD', '/ch/script.sh')], meta)],meta))
df_children.append(im.Tree(lark.Token('RULE', 'run'), [im.Tree(lark.Token('RULE', 'run_shell'),[lark.Token('LINE_CHUNK', '/ch/script.sh')], meta)],meta))
return im.Tree(lark.Token('RULE', 'start'), [im.Tree(lark.Token('RULE','dockerfile'), df_children)], meta)
Expand Down
14 changes: 14 additions & 0 deletions test/build/50_ch-image.bats
Original file line number Diff line number Diff line change
Expand Up @@ -982,18 +982,21 @@ EOF

@test "ch-image modify" {

# -c success, echo
run ch-image modify -c "echo foo" -c "echo bar" -- alpine:3.17 tmpimg
echo "$output"
[[ $status -eq 0 ]]
[[ $output = *'foo'* ]]
[[ $output = *'bar'* ]]

# -c success, create file
ch-image modify -c "touch /home/foo" -- alpine:3.17 tmpimg
run ch-run tmpimg -- ls /home
echo "$output"
[[ $status -eq 0 ]]
[[ $output = *'foo'* ]]

# non-interactive, script
echo "touch /home/bar" >> "${BATS_TMPDIR}/modify-script.sh"
chmod 755 "${BATS_TMPDIR}/modify-script.sh"
ch-image modify alpine:3.17 tmpimg "${BATS_TMPDIR}/modify-script.sh"
Expand All @@ -1002,11 +1005,22 @@ EOF
[[ $status -eq 0 ]]
[[ $output = *'bar'* ]]

# non-interactive, here doc
ch-image modify alpine:3.17 tmpimg <<'EOF'
touch /home/foobar
EOF
run ch-run tmpimg -- ls /home
echo "$output"
[[ $status -eq 0 ]]
[[ $output = *'foobar'* ]]

# -c fail
run ch-image modify -c 'echo foo' -- alpine:3.17 alpine:3.17
echo "$output"
[[ $status -eq 1 ]]
[[ $output = *'output must be different from source image'* ]]

# non-existant shell
run ch-image modify -S "doesnotexist" -- alpine:3.17 tmpimg
echo "$output"
[[ $status -eq 1 ]]
Expand Down

0 comments on commit dbbf74d

Please sign in to comment.