From c8ba3e1c2f603bf8a76fc7720ce1d5eb1fd1dab2 Mon Sep 17 00:00:00 2001 From: Josh Padnick Date: Sun, 9 Apr 2017 23:09:50 -0700 Subject: [PATCH] Add --download-dir option. 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. --- .gitignore | 1 + gruntwork-install | 68 ++++++++++++++++++++++++---------------- test/integration-test.sh | 3 ++ 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 797b705..29b636a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.idea *.iml \ No newline at end of file diff --git a/gruntwork-install b/gruntwork-install index db53c51..b28a9c8 100755 --- a/gruntwork-install +++ b/gruntwork-install @@ -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 { @@ -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 @@ -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" @@ -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 @@ -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 { @@ -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 @@ -219,6 +229,10 @@ function install_script_module { module_params+=("$param") shift ;; + --download-dir) + download_dir="$2" + shift + ;; --help) print_usage exit @@ -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!" diff --git a/test/integration-test.sh b/test/integration-test.sh index f884364..331373c 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -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