Skip to content

Commit

Permalink
Update and support for Darwin (MacOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
joglomedia committed Aug 17, 2024
1 parent ea9b56d commit 836edf6
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 37 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=terraform-install
VERSION=0.0.3
VERSION=0.1.0

DIRS=etc lib bin sbin share
INSTALL_DIRS=`find $(DIRS) -type d 2>/dev/null`
Expand All @@ -19,6 +19,7 @@ install:
for file in $(INSTALL_FILES); do cp $$file $(PREFIX)/$$file; done
mkdir -p $(DOC_DIR)
cp -r $(DOC_FILES) $(DOC_DIR)/
mv $(PREFIX)/bin/terraform-install $(PREFIX)/bin/tfm

uninstall:
for file in $(INSTALL_FILES); do rm -f $(PREFIX)/$$file; done
Expand Down
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,96 @@
# terraform-install

Bash command line to automate install and update Terraform from official executable binary file.
Bash command line to automate install and manage Terraform update from official executable binary file.

## How to Install

```
git clone https://github.com/joglomedia/terraform-install.git
cd terraform-install
make install
```

## Usage

### Lists all available Terraform versions from official download page

```
tfm list-remote
```

### Install specific Terraform version

```
tfm install 1.9.4
```

### Use specific installed version

```
tfm use 1.9.4
```

### More help

```
tfm --help
```

## Security Vulnerabilities and Bugs

If you discover any security vulnerabilities or any bugs within _Terrafom Install_, please open an [issue](https://github.com/joglomedia/terraform-install/issues/new).

## Contributing

* Fork it ([https://github.com/joglomedia/terraform-install/fork](https://github.com/joglomedia/terraform-install/fork))
* Create your feature branch (git checkout -b my-new-feature) or fix issue (git checkout -b fix-some-issue)
* Commit your changes (git commit -am 'Add some feature') or (git commit -am 'Fix some issue')
* Push to the branch (git push origin my-new-feature) or (git push origin fix-some-issue)
* Create a new Pull Request
* GitHub Workflows will be run to make sure that your changes does not have errors or warning

## Awesome People

**Terraform Install** is an open-source project licensed under the GNU GPLv3 license with its ongoing development made possible entirely by the support of all these smart and generous people, from code contributors to financial contributors. :purple_heart:

Thank you for considering contributing to this project!

### Project Maintainers

<table>
<tbody>
<tr>
<td align="center" valign="top">
<img width="125" height="125" src="https://github.com/joglomedia.png?s=150">
<br>
<strong>Edi Septriyanto</strong>
<br>
<a href="https://github.com/joglomedia">@joglomedia</a>
</td>
</tr>
</tbody>
</table>

### Code Contributors

<a href="https://github.com/joglomedia/terraform-install/graphs/contributors">
<img src="https://contrib.rocks/image?repo=joglomedia/terraform-install" />
</a>

Made with [contributors-img](https://contrib.rocks).

### Financial Contributors

You can support development by using any of the methods below:

**[Buy Me a Bottle of Milk or a Cup of Coffee](https://paypal.me/masedi) !!**

## Licence

Terraform Install is open-source project licensed under the GNU GPLv3 license.

## Copyright

(c) 2021-2024 | [MasEDI.Net](https://masedi.net/)

### Enjoy Terraform Install ;)
153 changes: 118 additions & 35 deletions bin/terraform-install
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# | Min requirement : GNU/Linux Debian 8, Ubuntu 16.04 or Linux Mint 17 |
# | Last Update : 13/08/2021 |
# | Author : MasEDI.Net (me@masedi.net) |
# | Version : 0.0.1 |
# | Version : 0.1.0 |
# +-------------------------------------------------------------------------+
# | Copyright (c) 2021 MasEDI.Net (https://masedi.net/terraform-install) |
# +-------------------------------------------------------------------------+
Expand Down Expand Up @@ -204,55 +204,57 @@ function determine_tf_sys_arch() {
##
function install_terraform() {
# If version not supplied, get the latest stable.
tf_version=${1:-$(determine_tf_latest_version)}
tf_install_path=${2:-"/usr/local/tfm/versions"}
tf_sys_arch=${TFM_ARCH:-$(determine_tf_sys_arch)}
tf_dist_mirror=${TFM_TF_DIST_MIRROR:-"https://releases.hashicorp.com/terraform"}
tf_zip_file_name="terraform_${tf_version}_${tf_sys_arch}.zip"
tf_zip_file_url="${tf_dist_mirror}/${tf_version}/${tf_zip_file_name}"
TF_VERSION=${1:-$(determine_tf_latest_version)}
TF_INSTALL_PATH=${2:-"${HOME}/.tfm"}
TF_SYS_ARCH=${TFM_ARCH:-$(determine_tf_sys_arch)}
TF_DIST_MIRROR=${TFM_TF_DIST_MIRROR:-"https://releases.hashicorp.com/terraform"}
TF_ZIP_FILENAME="terraform_${TF_VERSION}_${TF_SYS_ARCH}.zip"
TF_ZIP_FILE_URL="${TF_DIST_MIRROR}/${TF_VERSION}/${TF_ZIP_FILENAME}"
TF_TMP_DIR="${HOME}/.tfm/tmp"

echo "Installing Terraform ${tf_version} from ${tf_dist_mirror}"
echo "Installing Terraform ${TF_VERSION} from ${TF_DIST_MIRROR}"

# Install path directory.
[ ! -d "${tf_install_path}" ] && mkdir -p "${tf_install_path}"
[ ! -d "${TF_INSTALL_PATH}" ] && mkdir -p "${TF_INSTALL_PATH}"

if curl -sLI "${tf_zip_file_url}" | grep -q "HTTP/[.12]* [2].."; then
if curl -sLI "${TF_ZIP_FILE_URL}" | grep -q "HTTP/[.12]* [2].."; then
# Download Terraform zip file from releases page.
echo -n "Downloading binary file..."
echo -n "Downloading binary file, please wait..."

curl -sL -o "/tmp/${tf_zip_file_name}" "${tf_zip_file_url}" && \
unzip -q "/tmp/${tf_zip_file_name}" -d "/tmp/" && \
rm -f "/tmp/${tf_zip_file_name}"
#curl -sL -o "${TF_TMP_DIR}/${TF_ZIP_FILENAME}" "${TF_ZIP_FILE_URL}" && \
curl -# -o "${TF_TMP_DIR}/${TF_ZIP_FILENAME}" "${TF_ZIP_FILE_URL}" && \
unzip -q "${TF_TMP_DIR}/${TF_ZIP_FILENAME}" -d "${TF_TMP_DIR}/" && \
rm -f "${TF_TMP_DIR}/${TF_ZIP_FILENAME}"

if [ -f "/tmp/terraform" ]; then
echo "OK"
if [ -f "${TF_TMP_DIR}/terraform" ]; then
echo "Download binary file...OK"
else
echo "ERROR"
echo "Download binary file...ERROR"
fi

# Backup existing Terraform binary file.
#if [ -f "${tf_install_path}/terraform" ]; then
#if [ -f "${TF_INSTALL_PATH}/terraform" ]; then
# Check existing Terraform version.
# tf_cur_version=$(terraform --version | grep 'Terraform v' | cut -d'v' -f2)
#echo "Found existing Terraform binary, backed up to ${tf_install_path}/terraform${tf_cur_version}."
# mv -f "${tf_install_path}/terraform" "${tf_install_path}/terraform.old"
#echo "Found existing Terraform binary, backed up to ${TF_INSTALL_PATH}/terraform${tf_cur_version}."
# mv -f "${TF_INSTALL_PATH}/terraform" "${TF_INSTALL_PATH}/terraform.old"
#fi

echo -n "Installing binary file..."

mv /tmp/terraform "${tf_install_path}/terraform-${tf_version}" && \
chmod +x "${tf_install_path}/terraform-${tf_version}" && \
chwon -hR "${USER}:${USER}" "${tf_install_path}/terraform-${tf_version}"
mv "${TF_TMP_DIR}/terraform" "${TF_INSTALL_PATH}/terraform-${TF_VERSION}" && \
chmod +x "${TF_INSTALL_PATH}/terraform-${TF_VERSION}" && \
chown -hR "${USER}:" "${TF_INSTALL_PATH}/terraform-${TF_VERSION}"

if [ -x "${tf_install_path}/terraform-${tf_version}" ]; then
if [ -x "${TF_INSTALL_PATH}/terraform-${TF_VERSION}" ]; then
echo "OK"
else
echo "ERROR"
fi
#echo "Terraform ${tf_version} succesfully installed."
#echo "Terraform ${TF_VERSION} succesfully installed."
else
echo "Failed to download Terraform ${tf_version},"
echo "remote file '${tf_zip_file_url}' not found."
echo "Failed to download Terraform ${TF_VERSION},"
echo "remote file '${TF_ZIP_FILE_URL}' not found."
exit 1
fi
}
Expand All @@ -265,7 +267,6 @@ function tfm__install() {
# Check command line arguments.
if [[ -n "${1}" ]]; then
TF_VERSION="${1}"
TF_DIR="${HOME}/.tfm/versions"
TF_FORCE_INSTALL=false
shift # Pass the remaining arguments as command parameters.

Expand Down Expand Up @@ -325,20 +326,28 @@ function tfm__install() {
# Installing Terraform.
DO_INSTALL_TF=""

if [[ -x "${TF_DIR}/terraform-${TF_VERSION}" && "${TF_FORCE_INSTALL}" != true ]]; then
if [[ -x "${TF_VERSIONS_DIR}/terraform-${TF_VERSION}" && "${TF_FORCE_INSTALL}" != true ]]; then
echo "Terraform ${TF_VERSION} already exists."

while [[ "${DO_INSTALL_TF}" != "y" && "${DO_INSTALL_TF}" != "n" ]]; do
read -rp "Do you want to force install? [y/n]: " \
-i y -e DO_INSTALL_TF
read -rp "Do you want to re-install? [y/n]: " -e DO_INSTALL_TF
done
else
DO_INSTALL_TF="y"
fi

if [[ ${DO_INSTALL_TF} == y* || ${DO_INSTALL_TF} == Y* ]]; then
[ -n "${TFM_DIR}" ] && TF_DIR="${TFM_DIR}/versions"
install_terraform "${TF_VERSION}" "${TF_DIR}"
[ -n "${TFM_DIR}" ] && TF_VERSIONS_DIR="${TFM_DIR}/versions"
install_terraform "${TF_VERSION}" "${TF_VERSIONS_DIR}"

# Set as default binary executable.
if [[ ! -x "${TF_BIN_DIR}/terraform" ]]; then
echo "Currently no terraform binary, set ${TF_VERSION} as default executable!"
mkdir -p "${TF_BIN_DIR}"
ln -sf "${TF_INSTALL_PATH}/terraform-${TF_VERSION}" "${TF_BIN_DIR}/terraform"
fi
else
echo "Terraform ${TF_VERSION} installation aborted."
echo "Installation aborted."
fi
else
echo "${PROG_NAME} install: missing required arguments."
Expand All @@ -347,10 +356,74 @@ function tfm__install() {
fi
}

##
# tfm uninstall [FLAGS] [OPTIONS]
##
function tfm__use() {
if [[ -n "${1}" ]]; then
TF_VERSION="${1}"

if [[ -f "${TF_VERSIONS_DIR}/terraform-${TF_VERSION}" ]]; then
echo "Set Terraform ${TF_VERSION} as default executable binary."
ln -sf "${TF_VERSIONS_DIR}/terraform-${TF_VERSION}" "${TF_BIN_DIR}/terraform"
else
echo "Terraform ${TF_VERSION} executable binary not found."
echo "Hint: ${PROG_NAME} install ${TF_VERSION}"
fi

fi
}

##
# tfm uninstall [FLAGS] [OPTIONS]
##
function tfm__uninstall() {
if [[ -n "${1}" ]]; then
TF_VERSION="${1}"
DO_REMOVE_TF=""

if [[ -f "${TF_VERSIONS_DIR}/terraform-${TF_VERSION}" ]]; then
while [[ "${DO_REMOVE_TF}" != "y" && "${DO_REMOVE_TF}" != "n" ]]; do
read -rp "Do you want to uninstall Terraform ${TF_VERSION}? [y/n]: " \
-e DO_REMOVE_TF
done
else
echo "Terraform ${TF_VERSION} executable binary not found."
fi

if [[ ${DO_REMOVE_TF} == y* || ${DO_REMOVE_TF} == Y* ]]; then
rm -f "${TF_VERSIONS_DIR}/terraform-${TF_VERSION}" && \
echo "Terraform ${TF_VERSION} successfully uninstalled."
else
echo "Uninstall aborted."
fi
else
echo "${PROG_NAME} uninstall: missing required arguments."
echo "Hint: ${PROG_NAME} uninstall <version>"
exit 1
fi
}

##
# Aliases of tfm uninstall
##
function tfm__remove() {
tfm__uninstall "$@"
}

##
# tfm list installed version
##
function tfm__list() {
echo "Installed Terraform versions:"
find "${TF_VERSIONS_DIR}" | awk '/terraform-/ { print $1 }' | awk -F"-" '{print $NF}'
}

##
# tfm list-remote [FLAGS] [OPTIONS]
##
function tfm__list-remote() {
echo "Available Terraform versions:"
tf_versions_remote=$(get_tf_versions_available)
echo "${tf_versions_remote}" | tr ' ' '\n' | sort -V
}
Expand Down Expand Up @@ -382,7 +455,17 @@ function tfm__version() {
##
function init_tfm() {
# Check root user.
requires_root "$@"
#requires_root "$@"

# Default TFM directories.
TF_VERSIONS_DIR="${HOME}/.tfm/versions"
TF_BIN_DIR="${HOME}/.tfm/bin"

if [[ ! -d "${HOME}/.tfm/bin" ]]; then
mkdir -p "${HOME}/.tfm/bin"
mkdir -p "${HOME}/.tfm/versions"
mkdir -p "${HOME}/.tfm/tmp"
fi

# Check command line arguments.
if [[ -n "${1}" ]]; then
Expand Down

0 comments on commit 836edf6

Please sign in to comment.