Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRIVERS-2497 Fix paths on Cygwin and Python package dependencies #244

Merged
merged 15 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 72 additions & 3 deletions .evergreen/csfle/activate-kmstlsvenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,93 @@
# Usage:
# . ./activate-kmstlsvenv.sh
#
# This file creates and activates the kmstlsvenv virtual environment in the
# This file creates and/or activates the kmstlsvenv virtual environment in the
# current working directory. This file must be invoked from within the
# .evergreen/csfle directory in the Drivers Evergreen Tools repository.
#
# If a kmstlsvenv virtual environment already exists, it will be activated and
# no further action will be taken. If a kmstlsvenv virtual environment must be
# created, required packages will also be installed.

# If an error occurs during creation, activation, or installation of packages,
# the kmstlsvenv virtual environment will be deactivated and activate_kmstlsvenv
# will return a non-zero value.
rcsanchez97 marked this conversation as resolved.
Show resolved Hide resolved

# Automatically invoked by activate-kmstlsvenv.sh.
activate_kmstlsvenv() {
# shellcheck source=.evergreen/venv-utils.sh
. ../venv-utils.sh || return

if [[ -d kmstlsvenv ]]; then
venvactivate kmstlsvenv
venvactivate kmstlsvenv || return
else
# shellcheck source=.evergreen/find-python3.sh
. ../find-python3.sh || return

venvcreate "$(find_python3)" kmstlsvenv || return

CRYPTOGRAPHY_DONT_BUILD_RUST=1 python -m pip install --upgrade boto3~=1.19 cryptography~=3.4.8 pykmip~=0.10.0
local packages=(
"boto3~=1.19.0"
"pykmip~=0.10.0"
)

if [[ "$OSTYPE" == darwin16 && "$HOSTTYPE" == x86_64 ]]; then
# Avoid `error: thread-local storage is not supported for the current
# target` on macos-1012.
packages+=("greenlet<2.0")
fi

if [[ "$OSTYPE" == cygwin && "$HOSTTYPE" == x86_64 ]]; then
local -r is_win_2016="$(systeminfo.exe /FO LIST | perl -lne 'print $1 if m/^OS Name:\s+(.*)$/' || true)"

if [[ "$is_win_2016" =~ 2016 ]]; then
# Avoid `RuntimeError: Could not determine home directory.` on
# windows-64-2016. See BUILD-16233.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this only windows-64-2016? What about windows-64-vsMulti-small (Microsoft Windows Server 2019 Datacenter)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I reproduced the same issue there. This probably hits all windows hosts on evergreen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the patch testing windows-64-2019, there appeared to be no issue. I was not aware of windows-64-vsMulti-small. It is unclear to me what the difference between these distros may be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why windows-64-2019 works but windows-64-vsMulti-small doesn't. Either way the issue needs to be fixed on windows-64-vsMulti-small too because that's what we test on in pymongo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just documenting what we discussed via other channels that the windows-64-vsMulti-small was added to the test suite but did not demonstrate failure that was observed when testing on a spawn host, and that this issue could be related to BUILD-12392.

python -m pip install -U "setuptools<65.0" || {
local -r ret="$?"
deactivate || return 1 # Deactivation should never fail!
return "$ret"
}
fi
fi

# Avoid `error: can't find Rust compiler`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if instead of trying to pinpoint which platforms need cryptography<3.4 we just try to install the latest version and if that fails fallback to cryptography<3.4? Like this:

python -m pip install cryptography || python -m pip install 'cryptography<3.4' || ...

python -m pip install -U "${packages[@]}" || ...

This is simpler and should work on more platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed simpler, but I deliberately opted for the current approach in order to be very explicit about conditions that require workarounds and as narrow as possible in the application of said workarounds.

This was motivated by the status quo where generally-applied workarounds such as pinning cryptography to ~=3.4.8 or using CRYPTOGRAPHY_DONT_BUILD_RUST=1 continued to demonstrate unexpected failures, and the conditions for said failures appeared to be inconsistent and opaque. It was unclear to me whenever I encountered such a failure whether it was already known, a new problem, or where the blame should be assigned (did I break it, or did the environment change without my knowing?).

My hope was that being explicit in this manner would make it easier to maintain this script moving forward, with simplifications/removals of special-casing being applied in a controlled and targeted manner.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the generic one to avoid needing to tweak and maintain these 30 extra lines which may or may not cover all the hosts drivers test on. I think a good compromise would be to use the generic approach but add an informative comment that specifically explains why the workaround exists like:

# Installing newer versions of cryptography requires rust when a wheel is not available.
# Fallback to an older version that does not require rust if the install fails. This is needed
# for at least the RHEL 6.2, powerpc64le, zSeries, and power8 hosts.
python -m pip install cryptography || python -m pip install 'cryptography<3.4' || ...

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is an acceptable compromise. Would appeciate other reviewers' thoughts on this before committing to the refactor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference for the compromise. That may require less changes to this script as distros undergo changes or more distros are added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Verified by this patch.

if [[ "$OSTYPE" =~ linux && ! -f /etc/os-release ]]; then
# rhel62-* is the only suppoerted Linux-like distro that does not provide
# /etc/os-release. Remove this condition once support for rhel62 is
# dropped.
packages+=("cryptography<3.4")
elif [[ "$OSTYPE" =~ linux ]]; then
local -r os_id="$(perl -lne 'print $1 if m/^ID="?([^"]+)"?/' /etc/os-release || true)"
local -r os_ver="$(perl -lne 'print $1 if m/^VERSION_ID="?([^"]+)"?/' /etc/os-release || true)"

case "$os_id" in
rhel)
if [[ "$HOSTTYPE" =~ (powerpc64le|s390x) ]]; then
# rhelXY-power8-* and rhelXY-zseries-*
packages+=("cryptography<3.4")
fi
;;
sles)
if [[ "$os_ver" == 12.3 && "$HOSTTYPE" == s390x ]]; then
# suse12-zseries-*
packages+=("cryptography<3.4")
fi
;;
ubuntu)
if [[ "$os_ver" == 18.04 && "$HOSTTYPE" =~ (s390x|powerpc64le) ]]; then
# ubuntu1804-power8-* and ubuntu1804-zseries-*
packages+=("cryptography<3.4")
fi
;;
esac
fi

python -m pip install -U "${packages[@]}" || {
local -r ret="$?"
deactivate || return 1 # Deactivation should never fail!
return "$ret"
}
fi
}

Expand Down
40 changes: 35 additions & 5 deletions .evergreen/find-python3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ is_python3() (

# Expect an output of the form: "Python x.y.z".
# Note: Python 2 binaries output to stderr rather than stdout.
local -r version_output="$("$bin" -V 2>&1)"
local -r version_output="$("$bin" -V 2>&1 | tr -d '\n')"

# For diagnostic purposes.
echo " - $bin: $version_output"
Expand Down Expand Up @@ -81,15 +81,30 @@ is_venv_capable() (
local -r tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

"$bin" -m venv "$tmp" || return
if [[ "$OSTYPE" == cygwin ]]; then
local -r real_path="$(cygpath -aw "$tmp")" || return
"$bin" -m venv "$real_path" || return
else
"$bin" -m venv "$tmp" || return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be refactored to avoid duplicating "$bin" -m venv "$tmp" || return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for a dedicated real_path variable only when required, but I can refactor it to reduce duplication instead.

fi

# Sanity check: on some environments (such as Cygwin) creation of the virtual
# environment may succeed but place the environment in an unexpected location.
if [[ -n "$(find "$tmp" -maxdepth 0 -type d -empty 2>/dev/null)" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show an example of this happening? Regardless can we remove this check because it's already handled by the if/elif/else below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example is as described in the PR description under "Paths on Cygwin".

I suppose it could be considered redundant due to the checks below. The intent of this check was to test if there are any files placed in the intended directory at all, which I felt to be different enough from whether or not an activation script could be found. I can remove/simplify if preferable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I would prefer removing it because it simplifies the script and we don't do anything special for an empty dir.

echo "$tmp is empty despite successful creation of virtual environment!"
return 1
fi

if [[ -f "$tmp/bin/activate" ]]; then
# shellcheck source=/dev/null
. "$tmp/bin/activate"
else
elif [[ -f "$tmp/Scripts/activate" ]]; then
dos2unix "$tmp/Scripts/activate" || return
# shellcheck source=/dev/null
. "$tmp/Scripts/activate"
else
echo "Could not find an activation script in $tmp!"
return 1
fi
) 1>&2

Expand Down Expand Up @@ -121,15 +136,30 @@ is_virtualenv_capable() (
local -r tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

"$bin" -m virtualenv -p "$bin" "$tmp" || return
if [[ "$OSTYPE" == cygwin ]]; then
local -r real_path="$(cygpath -aw "$tmp")" || return
"$bin" -m virtualenv -p "$bin" "$real_path" || return
else
"$bin" -m virtualenv -p "$bin" "$tmp" || return
fi

# Sanity check: on some environments (such as Cygwin) creation of the virtual
# environment may succeed but place the environment in an unexpected location.
if [[ -n "$(find "$tmp" -maxdepth 0 -type d -empty 2>/dev/null)" ]]; then
echo "$tmp is empty despite successful creation of virtual environment!"
return 1
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above.


if [[ -f "$tmp/bin/activate" ]]; then
# shellcheck source=/dev/null
. "$tmp/bin/activate"
else
elif [[ -f "$tmp/Scripts/activate" ]]; then
dos2unix "$tmp/Scripts/activate" || return
# shellcheck source=/dev/null
. "$tmp/Scripts/activate"
else
echo "Could not find an activation script in $tmp!"
return 1
fi
) 1>&2

Expand Down
71 changes: 53 additions & 18 deletions .evergreen/venv-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,73 @@
# "$2": The path to the virtual environment (directory) to create.
#
# Return 0 (true) if the virtual environment has been successfully created,
# activated, and the pip package upgraded.
# Return a non-zero value (false) otherwise.
# activated, and all seed packages are successfully installed in the new
# virtual environment.
# Return a non-zero value (false) in a deactivated state otherwise.
#
# The "seed" packages pip, setuptools, and wheel are automatically installed
# into the virtual environment. All packages must be successfully installed for
# venvcreate to be considered a success.
#
# If a file or directory exists at the given path to the virtual environment,
# they may be deleted as part of virtual environment creation.
venvcreate() {
local -r bin="${1:?'venvcreate requires a Python binary to use for the virtual environment'}"
local -r path="${2:?'venvcreate requires a path to the virtual environment to create'}"

if [[ "$OSTYPE" == cygwin ]]; then
local -r real_path="$(cygpath -aw "$path")" || return
else
local -r real_path="$path" || return
fi

# Prefer venv, but fallback to virtualenv if venv fails.
for mod in "venv" "virtualenv"; do
# Ensure a clean directory before attempting to create a virtual environment.
# Ensure a clean directory before attempting to create a virtual
# environment.
rm -rf "$path"

if "$bin" -m "$mod" "$path"; then
# Workaround https://bugs.python.org/issue32451:
# mongovenv/Scripts/activate: line 3: $'\r': command not found
if [[ -f "$path/Scripts/activate" ]]; then
dos2unix "$path/Scripts/activate" || return
fi
case "$mod" in
venv)
"$bin" -m "$mod" --system-site-packages "$real_path" || continue
;;
virtualenv)
# -p: ensure correct Python binary is used by virtual environment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is -p actually needed here? -p defaults to the current version of python so this seems redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is required, as some old versions of virtualenv do not correctly select the Python binary used to create the virtual environment. This is documented by this comment in the old utils.sh script, but I observed it to be an issue on more than just Debian 10 distros. I wanted to link to a relevant bug report, but could not find one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, can you add the comment from the old script? It's much more informative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. 👍

"$bin" -m "$mod" -p "$bin" --system-site-packages "$real_path" || continue
;;
*)
echo "Unexpected virtual environment module $mod!"
return 1
;;
esac

if venvactivate "$path"; then
# Use --no-cache-dir to ensure ensure the *actual* latest pip is
# correctly installed.
if python -m pip install --no-cache-dir --upgrade pip; then
# Only consider success if activation + pip upgrade was successful.
return
fi
# Workaround https://bugs.python.org/issue32451:
# mongovenv/Scripts/activate: line 3: $'\r': command not found
if [[ -f "$path/Scripts/activate" ]]; then
dos2unix "$path/Scripts/activate" || continue
fi

deactivate
fi
venvactivate "$path" || continue

if ! python -m pip install -U pip; then
deactivate || return 1 # Deactivation should never fail!
continue
fi

# Ensure setuptools and wheel are installed in the virtual environment.
# virtualenv only guarantees "one or more of" the seed packages are
# installed. venv only guarantees pip is installed via ensurepip.
#
# These packages must be upgraded *after* pip, *separately*, as some old
# versions of pip do not handle their simultaneous installation properly.
# See: https://github.com/pypa/pip/issues/4253
if ! python -m pip install -U setuptools wheel; then
deactivate || return 1 # Deactivation should never fail!
continue
fi

# Success only if both activation and package upgrades are successful.
return 0
done

echo "Could not use either venv or virtualenv with $bin to create a virtual environment at $path!" 1>&2
Expand Down