Skip to content

Commit

Permalink
Add --download-dir option.
Browse files Browse the repository at this point in the history
While building an AMI on top of a hardened OS that prevented any files
from being executed from /tmp, I hit an error where the Gruntwork
installer only downloads to /tmp with no other options. This changes
that.
  • Loading branch information
josh-padnick committed Apr 10, 2017
1 parent 4410017 commit c8ba3e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
*.iml
68 changes: 41 additions & 27 deletions gruntwork-install
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -e
readonly MODULES_DIR="modules"
readonly MODULE_INSTALL_FILE_NAME="install.sh"

readonly MODULES_DOWNLOAD_DIR="/tmp/gruntwork-script-modules"
readonly DEFAULT_MODULES_DOWNLOAD_DIR="/tmp/gruntwork-script-modules"
readonly BIN_DIR="/usr/local/bin"

function print_usage {
Expand All @@ -20,15 +20,23 @@ function print_usage {
echo
echo "Download a Gruntwork Script Module and install it."
echo
echo "Options:"
echo "Required Arguments:"
echo
echo -e " --repo\t\Required. The repo to install from."
echo -e " --tag\t\t\tRequired. The version of --repo to install. Follows the syntax described at https://github.com/gruntwork-io/fetch#tag-constraint-expressions."
echo -e " --module-name\t\tOptional. The name of a module to install. Can be any folder within the $MODULES_DIR directory of --repo. You must specify exactly one of --module-name or --binary-name."
echo -e " --binary-name\t\tOptional. The name of a binary to install. Can be any file uploaded as a release asset in --repo. You must specify exactly one of --module-name or --binary-name."
echo -e " --branch\t\tOptional. Download the latest commit from this branch in --repo. This is an alternative to --tag, used only for testing."
echo -e " --module-param\tOptional. A key-value pair of the format key=value you wish to pass to the module as a parameter. May be used multiple times."
echo -e " --help\t\Show this help text and exit."
echo -e " --repo\t\tThe repo to install from."
echo -e " --tag\t\t\tThe version of --repo to install. Follows the syntax described at https://github.com/gruntwork-io/fetch#tag-constraint-expressions."
echo
echo "Specify exactly one of:"
echo
echo -e " --module-name\t\The name of a module to install. Can be any folder within the $MODULES_DIR directory of --repo."
echo -e " --binary-name\t\tThe name of a binary to install. Can be any file uploaded as a release asset in --repo."
echo
echo "Optional Arguments:"
echo
echo -e " --download-dir\t\The directory where the module will be downloaded to and from which it will be installed. Default: $DEFAULT_MODULES_DOWNLOAD_DIR"
echo -e " --module-param\tA key-value pair of the format key=value you wish to pass to the module as a parameter. May be used multiple times."
echo -e " --branch\t\tDownload the latest commit from this branch in --repo. This is an alternative to --tag, used only for testing."
echo -e " --help\t\tShow this help text and exit."

echo
echo "Example:"
echo
Expand Down Expand Up @@ -72,37 +80,37 @@ function fetch_script_module {
local readonly module_name="$1"
local readonly tag="$2"
local readonly branch="$3"
local readonly download_path="$4"
local readonly download_dir="$4"
local readonly repo="$5"

# We want to make sure that all folders down to $download_path/$module_name exists, but that $download_path/$module_name itself is empty.
mkdir -p "$download_path/$module_name/"
rm -Rf "$download_path/$module_name/"
mkdir -p "$download_dir/$module_name/"
rm -Rf "$download_dir/$module_name/"

# Note that fetch can safely handle blank arguments for --tag or --branch
# If both --tag and --branch are specified, --branch will be used
echo "Downloading module $module_name from $repo"
fetch --repo="$repo" --tag="$tag" --branch="$branch" --source-path="/modules/$module_name" "$download_path/$module_name"
fetch --repo="$repo" --tag="$tag" --branch="$branch" --source-path="/modules/$module_name" "$download_dir/$module_name"
}

# Download a binary asset from a GitHub release using fetch (https://github.com/gruntwork-io/fetch)
function fetch_binary {
local readonly binary_name="$1"
local readonly tag="$2"
local readonly download_path="$3"
local readonly download_dir="$3"
local readonly repo="$4"

local binary_name_full=""
binary_name_full=$(determine_binary_name "$binary_name")

local readonly full_download_path="$download_path/$binary_name_full"
local readonly full_download_path="$download_dir/$binary_name_full"
local readonly full_dest_path="$BIN_DIR/$binary_name"

# We want to make sure that all folders down to $download_path exist, but that $download_path/$binary_name_full does not
mkdir -p "$download_path"
rm -f "$download_path/$binary_name_full"
mkdir -p "$download_dir"
rm -f "$download_dir/$binary_name_full"

fetch --repo="$repo" --tag="$tag" --release-asset="$binary_name_full" "$download_path"
fetch --repo="$repo" --tag="$tag" --release-asset="$binary_name_full" "$download_dir"

echo "Moving $full_download_path to $full_dest_path and setting execute permissions"
sudo mv "$full_download_path" "$full_dest_path"
Expand All @@ -112,12 +120,12 @@ function fetch_binary {
# Validate that at least one file was downloaded from the module; otherwise throw an error.
function validate_module {
local readonly module_name="$1"
local readonly download_path="$2"
local readonly download_dir="$2"
local readonly tag="$3"
local readonly branch="$4"
local reaodnly repo="$5"

if [[ ! -e "$download_path/$module_name" ]]; then
if [[ ! -e "$download_dir/$module_name" ]]; then
echo "ERROR: No files were downloaded. Are you sure \"$module_name\" is a valid Script Module in $repo (tag = $tag, branch = $branch)?"
exit 1
fi
Expand Down Expand Up @@ -175,11 +183,12 @@ function convert_module_params_format {

function run_module {
local readonly module_name="$1"
shift
local readonly download_dir="$2"
shift 2
local readonly module_params="$@"

chmod -R u+x "${MODULES_DOWNLOAD_DIR}/${module_name}"
${MODULES_DOWNLOAD_DIR}/${module_name}/${MODULE_INSTALL_FILE_NAME} $module_params
chmod -R u+x "${download_dir}/${module_name}"
${download_dir}/${module_name}/${MODULE_INSTALL_FILE_NAME} $module_params
}

function install_script_module {
Expand All @@ -188,6 +197,7 @@ function install_script_module {
local module_name=""
local binary_name=""
local repo=""
local download_dir="$DEFAULT_MODULES_DOWNLOAD_DIR"
local module_params=()

while [[ $# > 0 ]]; do
Expand Down Expand Up @@ -219,6 +229,10 @@ function install_script_module {
module_params+=("$param")
shift
;;
--download-dir)
download_dir="$2"
shift
;;
--help)
print_usage
exit
Expand Down Expand Up @@ -249,12 +263,12 @@ function install_script_module {

if [[ ! -z "$module_name" ]]; then
echo "Installing from $module_name..."
fetch_script_module "$module_name" "$tag" "$branch" "$MODULES_DOWNLOAD_DIR" "$repo"
validate_module "$module_name" "$MODULES_DOWNLOAD_DIR" "$tag" "$branch" "$repo"
run_module "$module_name" "${module_params[@]}"
fetch_script_module "$module_name" "$tag" "$branch" "$download_dir" "$repo"
validate_module "$module_name" "$download_dir" "$tag" "$branch" "$repo"
run_module "$module_name" "$download_dir" "${module_params[@]}"
else
echo "Installing $binary_name..."
fetch_binary "$binary_name" "$tag" "$MODULES_DOWNLOAD_DIR" "$repo"
fetch_binary "$binary_name" "$tag" "$download_dir" "$repo"
fi

echo "Success!"
Expand Down
3 changes: 3 additions & 0 deletions test/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ echo "Checking that the vault-ssh-helper installed correctly"
echo "Using gruntwork-install to install a module from the module-ecs repo"
gruntwork-install --module-name "ecs-scripts" --repo "https://github.com/gruntwork-io/module-ecs" --branch "v0.0.1"

echo "Using gruntwork-install to install a module from the module-ecs repo with --download-dir option"
gruntwork-install --module-name "ecs-scripts" --repo "https://github.com/gruntwork-io/module-ecs" --branch "v0.0.1" --download-dir "~/tmp"

echo "Checking that the ecs-scripts installed correctly"
configure-ecs-instance --help

Expand Down

0 comments on commit c8ba3e1

Please sign in to comment.