Skip to content

Commit

Permalink
Fix Read only shadowing read-write mounts
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Neff <andy@visionsystemsinc.com>
  • Loading branch information
andyneff committed Sep 7, 2023
1 parent 594d44d commit 0de1948
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 32 additions & 2 deletions linux/just_files/docker_compose_override
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,40 @@ function generate_docker_compose_override()
over_volumes+=(" - ${volume_host}:${volume_docker}${volume_flags}")
IFS="${OLD_IFS}"
done
IFS=$'\n'
# Remove duplicates (https://unix.stackexchange.com/q/159695) because
# docker compose doesn't like them
IFS=$'\n'
over_volumes=($(awk '!count[$0]++' <<< "${over_volumes[*]+${over_volumes[*]}}"))
# Originally this just had to remove duplicate entries, but the nfs code could
# easily create pairs of mounts that were some :ro and some :rw. Depending on
# the order, the read only would take precedence, and you would be unable to
# write where you needed to, and get unexpected failures due to using NFS.
# So the solution for now is to just prefer the read-write over read-only.
over_volumes=($(awk -F: '
# Deduplicate fully identical lines
!count[$0]++ {
# Save the order of the original lines.
original_order[NR]=$0
key=$1":"$2
deduplicate_keys[NR]=key
if ( key in deduplicate ) {
# Do nothing if already there, Always prefer not :ro
if (deduplicate[key] != $0 && deduplicate[key] ~ /:ro/) {
deduplicate[key]=$0
}
} else {
deduplicate[key]=$0
}
}
END{
# Print out deduplicated list in order
for (key in original_order) {
# If the line matches exactly, print it
if ( deduplicate[deduplicate_keys[key]] == original_order[key] ) {
print original_order[key]
}
}
}' <<< "${over_volumes[*]}"))

if [ " ""${over_volumes[@]+set}" = " set" ]; then
echo " volumes:"
echo "${over_volumes[*]+${over_volumes[*]}}"
Expand Down
11 changes: 10 additions & 1 deletion tests/test-docker_compose_override.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ begin_test "Just docker compose dynamic volumes"
fi
TEST_VOLUMES=(".:/trash:z")
override="$(generate_docker_compose_override TEST test1)"
[ "${override}" = "${ans1}" ]
assert_str_eq "${override}" "${ans1}"

TEST_VOLUMES=("/tmp:/temp:ro")
ans+="$(vol "$(real_path "/tmp")" /temp:ro)"
Expand Down Expand Up @@ -180,6 +180,15 @@ begin_test "Just docker compose dynamic volumes"
else
assert_str_eq "${override}" "${ans}"
fi

# Make sure readonly doesn't shadow a read-write
TEST_TEST1_VOLUME_3="${TESTDIR}/bar2:/foo5:ro"
override="$(generate_docker_compose_override TEST test1)"
if [ "${OS-}" = "Windows_NT" ]; then
[ "${override}" = "${ans}$(environment)$(envi "JUST_HOST_WINDOWS=1")" ]
else
assert_str_eq "${override}" "${ans}"
fi
)
end_test

Expand Down

0 comments on commit 0de1948

Please sign in to comment.