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

FIX Support default tofu version #41

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/47.txt
Copy link
Contributor

Choose a reason for hiding this comment

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

could you please also rename 47.txt to 41.txt - filename should point to pull request number

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
feature: support `TOFUENV_TOFU_DEFAULT_VERSION` env variable to provide a default version for all operations ([#34](https://github.com/tofuutils/tofuenv/issues/34))
```
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ curl -L -o /usr/bin/jq.exe https://github.com/jqlang/jq/releases/latest/download

Install a specific version of OpenTofu.

If no parameter is passed, the version to use is resolved automatically via [TOFUENV_TOFU_VERSION environment variable](#TOFUENV_TOFU_VERSION) or [.opentofu-version files](#opentofu-version-file), in that order of precedence, i.e. TOFUENV_TOFU_VERSION, then .opentofu-version. The default is `latest` if none are found.
If no parameter is passed, the version to use is resolved automatically via the following:

- [TOFUENV_TOFU_VERSION environment variable](#TOFUENV_TOFU_VERSION)
- [.opentofu-version files](#opentofu-version-file)
- [TOFUENV_CONFIG_DIR](#TOFUENV_CONFIG_DIR)`/version`
- [TOFUENV_TOFU_DEFAULT_VERSION](#TOFUENV_TOFU_DEFAULT_VERSION)
- The default of `latest`.

If a parameter is passed, available options:

Expand Down Expand Up @@ -213,6 +219,12 @@ terraform {

#### TOFUENV

##### `TOFUENV_TOFU_DEFAULT_VERSION`

String (Default: "latest")

Specify which version of tofu to install if no other version information can be found.

##### `TOFUENV_GITHUB_TOKEN`

String (Default: "")
Expand Down
12 changes: 8 additions & 4 deletions lib/tofuenv-version-name.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ function tofuenv-version-name() {
&& log 'debug' "TOFUENV_VERSION_FILE retrieved from tofuenv-version-file: ${TOFUENV_VERSION_FILE}" \
|| log 'error' 'Failed to retrieve TOFUENV_VERSION_FILE from tofuenv-version-file';

TOFUENV_VERSION="$(cat "${TOFUENV_VERSION_FILE}" || true)" \
&& log 'debug' "TOFUENV_VERSION specified in TOFUENV_VERSION_FILE: ${TOFUENV_VERSION}";

TOFUENV_VERSION_SOURCE="${TOFUENV_VERSION_FILE}";
if [[ -f "${TOFUENV_VERSION_FILE}" ]]; then
TOFUENV_VERSION="$(cat "${TOFUENV_VERSION_FILE}" || true)" \
&& log 'debug' "TOFUENV_VERSION specified in TOFUENV_VERSION_FILE: ${TOFUENV_VERSION}";
TOFUENV_VERSION_SOURCE="${TOFUENV_VERSION_FILE}";
else
TOFUENV_VERSION="${TOFUENV_TOFU_DEFAULT_VERSION:-"latest"}";
Copy link
Contributor

Choose a reason for hiding this comment

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

Continuing my comment in the discussion, I suggest setting the version to an empty string by default, ex.
TOFUENV_VERSION="${TOFUENV_TOFU_DEFAULT_VERSION:-""}";

Copy link
Author

Choose a reason for hiding this comment

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

@anastasiiakozlova245 Won't that mean that tofuenv exec will not have the correct/consistent behaviour?

Copy link
Contributor

Choose a reason for hiding this comment

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

@orbitz could you please clarify what you mean by not having a consistent behaviour? As stated in the docs, tofuenv install command will still install the latest version if no version is specified, since it doesn't use this particular script. However, tofuenv version-name should show the version of tofu currently installed/set in configs (.opentofu-version file, env variables). If tofuenv is just installed, and no version is set anywhere, why should it print the latest one?

Copy link
Author

Choose a reason for hiding this comment

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

Perhaps I'm confused bu but tofuenv-exec which is called from the tofu wrapper does use this function. I would expect it to have the semantics as tofuenv install. If I change line 18 to :-"" then if no version is found it will look for version "", which will never exist. For example, in main, if I have no version information set:

> ./bin/tofu version
cat: /usr/home/orbitz/projects/terrateam/tofuenv/version: No such file or directory
Version could not be resolved (set by /usr/home/orbitz/projects/terrateam/tofuenv/version or tofuenv use <version>)

However, with this change:

> ./bin/tofu version
version '1.6.1' is not installed (set by /usr/home/orbitz/projects/terrateam/tofuenv/version). Installing now as TOFUENV_AUTO_INSTALL==true
Installing OpenTofu v1.6.1
Downloading release tarball from https://github.com/opentofu/opentofu/releases/download/v1.6.1/tofu_1.6.1_freebsd_amd64.zip
##################################################################################################################################################################################################### 100.0%
Downloading SHA hash file from https://github.com/opentofu/opentofu/releases/download/v1.6.1/tofu_1.6.1_SHA256SUMS
Not instructed to use Local GPG (/usr/home/orbitz/projects/terrateam/tofuenv/use-{gpgv,gnupg}), skipping GnuPG signature verification

Installation of tofu v1.6.1 successful. To make this your default version, run 'tofuenv use 1.6.1'
OpenTofu v1.6.1
on freebsd_amd64

This is the behaviour I would expect, is this not the behaviour you would expect? If so, what is?

Copy link
Contributor

@anastasiiakozlova245 anastasiiakozlova245 Feb 5, 2024

Choose a reason for hiding this comment

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

@orbitz I believe when you call ./bin/tofu version, it shouldn't trigger an automatic installation of a latest package. version command is not supposed to install anything, it should just provide an information about the version installed or set anywhere in configs. And if it's not - then returning an error is expected.
@Nmishin what do you think the correct behaviour would be?

Copy link
Author

@orbitz orbitz Feb 5, 2024

Choose a reason for hiding this comment

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

I think there are a few things to consider:

  1. Even if you don't think an install should happen, I believe that calling tofu foo should get the same version as when you called tofuenv install. So if tofuenv install defaults to latest if no version information is set, then tofu foo should get that value as well.
  2. As it stands now, if you DO set any version information then tofu foo will install that version if it is not already installed. Line 18 just guarantees that it is matches the tofuenv install version discovery algorithm.
  3. As the code stands now, whether or not tofu foo installs something depends on the value of the auto install variable.

Copy link
Author

Choose a reason for hiding this comment

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

Just bumping this discussion.

TOFUENV_VERSION_SOURCE='TOFUENV_TOFU_DEFAULT_VERSION';
fi;

else
TOFUENV_VERSION="${TOFUENV_TOFU_VERSION}" \
Expand Down
36 changes: 12 additions & 24 deletions libexec/tofuenv-resolve-version
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,18 @@ declare version_requested version regex min_required version_file;

declare arg="${1:-""}";

if [ -z "${arg}" -a -z "${TOFUENV_TOFU_VERSION:-""}" ]; then
version_file="$(tofuenv-version-file)";
log 'debug' "Version File: ${version_file}";

if [ "${version_file}" != "${TOFUENV_CONFIG_DIR}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TOFUENV_CONFIG_DIR}/version (${TOFUENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";

elif [ -f "${version_file}" ]; then
log 'debug' "Version File is the default \${TOFUENV_CONFIG_DIR}/version (${TOFUENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";

# Absolute fallback
if [ -z "${version_requested}" ]; then
log 'debug' 'Version file had no content. Falling back to "latest"';
version_requested='latest';
fi;

else
log 'debug' "Version File is the default \${TOFUENV_CONFIG_DIR}/version (${TOFUENV_CONFIG_DIR}/version) but it doesn't exist";
log 'debug' 'No version requested on the command line or in the version file search path. Installing "latest"';
version_requested='latest';
if [ -z "${arg}" ] && [ -z "${TOFUENV_TOFU_VERSION:-""}" ]; then
log 'debug' 'We are not hardcoded by a TOFUENV_TOFU_VERSION environment variable';

version_file="$(tofuenv-version-file)" \
&& log 'debug' "Version file retrieved from tofuenv-version-file: ${version_file}" \
|| log 'error' 'Failed to retrieve version file from tofuenv-version-file';

if [[ -f "${version_file}" ]]; then
version_requested="$(cat "${version_file}" || true)" \
&& log 'debug' "Version specified in ${version_file}: ${version_requested}";
else
version_requested="${TOFUENV_TOFU_DEFAULT_VERSION:-"latest"}";
fi;
elif [ -n "${TOFUENV_TOFU_VERSION:-""}" ]; then
version_requested="${TOFUENV_TOFU_VERSION}";
Expand Down
16 changes: 8 additions & 8 deletions test/test_uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function test_uninstall() {
tofuenv install "${v}" || return 1;
tofuenv uninstall "${v}" || return 1;
log 'info' 'Confirming uninstall success; an error indicates success:';
check_active_version "${v}" && return 1 || return 0;
env TOFUENV_AUTO_INSTALL=false "${TOFUENV_ROOT}/bin/tofu" version && return 1 || return 0;
Copy link
Author

Choose a reason for hiding this comment

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

I bypass the helper function here, but this guarantees that tofu fails if the version is not installed.

};

log 'info' '### Test Suite: Uninstall Local Versions';
Expand Down Expand Up @@ -88,13 +88,13 @@ done;
echo "### Uninstall removes versions directory"
cleanup || error_and_die "Cleanup failed?!"
(
tofuenv install 1.6.0-beta5 || exit 1
tofuenv install 1.6.0-beta4 || exit 1
[ -d "./versions" ] || exit 1
tofuenv uninstall 1.6.0-beta5 || exit 1
[ -d "./versions" ] || exit 1
tofuenv uninstall 1.6.0-beta4 || exit 1
[ -d "./versions" ] && exit 1 || exit 0
tofuenv install 1.6.0-beta5 || error_and_die "Failed to install 1.6.0-beta5"
tofuenv install 1.6.0-beta4 || error_and_die "Failed to install 1.6.0-beta"
[ -d "./versions" ] || error_and_die "Versions directory does not exist"
tofuenv uninstall 1.6.0-beta5 || error_and_die "Failed to uninstall 1.6.0-beta5"
[ -d "./versions" ] || error_and_die "Versions directory does not exist - 2"
tofuenv uninstall 1.6.0-beta4 || error_and_die "Failed to uninstall 1.6.0-beta4"
[ -d "./versions" ] && error_and_die "Versions directory exists but shouldn't" || exit 0
) || error_and_proceed "Removing last version deletes versions directory"

if [ "${#errors[@]}" -gt 0 ]; then
Expand Down
Loading