From f18a102c839e97e126f2531b4d45250c79f999a5 Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra Date: Fri, 18 Oct 2024 23:04:47 +0530 Subject: [PATCH] chore(tools): remove AWS related resources --- tools/ssm-tool/commands/list-instances.sh | 82 ------------------- tools/ssm-tool/commands/port-forward.sh | 97 ----------------------- tools/ssm-tool/commands/ssh.sh | 79 ------------------ tools/ssm-tool/lib/common.sh | 93 ---------------------- tools/ssm-tool/ssm-tool.sh | 57 ------------- 5 files changed, 408 deletions(-) delete mode 100755 tools/ssm-tool/commands/list-instances.sh delete mode 100755 tools/ssm-tool/commands/port-forward.sh delete mode 100755 tools/ssm-tool/commands/ssh.sh delete mode 100755 tools/ssm-tool/lib/common.sh delete mode 100755 tools/ssm-tool/ssm-tool.sh diff --git a/tools/ssm-tool/commands/list-instances.sh b/tools/ssm-tool/commands/list-instances.sh deleted file mode 100755 index f4b71934..00000000 --- a/tools/ssm-tool/commands/list-instances.sh +++ /dev/null @@ -1,82 +0,0 @@ -# List instances command -list_instances_help() { - echo "Usage: $0 list [-t|--tag TAG_NAME:TAG_VALUE]..." - echo - echo "Options:" - echo " -t, --tag TAG_NAME:TAG_VALUE EC2 instance tag name and value for filtering (can be specified multiple times)" - echo " -h, --help Display this help menu" -} - -list_instances() { - local tag_filters=() - - while [[ $# -gt 0 ]]; do - case $1 in - -t | --tag) - shift - IFS=':' read -ra tag <<<"$1" - if [[ ${#tag[@]} -ne 2 ]]; then - log_error "Invalid tag format. Use -t TAG_NAME:TAG_VALUE or --tag TAG_NAME:TAG_VALUE" - list_instances_help - return 1 - fi - tag_filters+=("Name=tag:${tag[0]},Values=${tag[1]}") - ;; - -h | --help) - list_instances_help - return 0 - ;; - *) - log_error "Invalid option: $1" - list_instances_help - return 1 - ;; - esac - shift - done - - if [[ ${#tag_filters[@]} -eq 0 ]]; then - log_error "At least one tag filter is required." - list_instances_help - return 1 - fi - - check_dependencies "column" "sort" - check_aws_credentials - - local query="Reservations[].Instances[] | [?State.Name != 'terminated'] | [].[InstanceId, " - local tag_keys="Name" - for filter in "${tag_filters[@]}"; do - if [[ $filter =~ Name=tag:([^,]+) ]]; then - tag_keys+=" ${BASH_REMATCH[1]}" - fi - done - - local tag_query="" - for key in $tag_keys; do - [[ -n "$tag_query" ]] && tag_query+=", " - tag_query+="join(' ', Tags[?Key=='$key'].Value || ['-'])" - done - query+="$tag_query, State.Name, PrivateIpAddress]" - - local aws_command="aws ec2 describe-instances" - for filter in "${tag_filters[@]}"; do - aws_command+=" --filters '$filter'" - done - aws_command+=" --query \"$query\" --output text" - - local instances - instances=$( - ( - echo -e "Instance-Id\tName\t${tag_keys#Name }\tState\tPrivate-IP" - eval $aws_command - ) | column -t | sort -k2 - ) - - if [[ $(echo "$instances" | wc -l) -le 1 ]]; then - log_error "No instances found matching the specified tags." - return 1 - fi - - echo "$instances" -} diff --git a/tools/ssm-tool/commands/port-forward.sh b/tools/ssm-tool/commands/port-forward.sh deleted file mode 100755 index 5d658c35..00000000 --- a/tools/ssm-tool/commands/port-forward.sh +++ /dev/null @@ -1,97 +0,0 @@ -# Port forwarding command -port_forward_help() { - echo "Usage: $0 port-forward [-t|--tag TAG_NAME:TAG_VALUE]... [-p|--port PORT_NUMBER] [-l|--local-port LOCAL_PORT_NUMBER] [-y|--yes]" - echo - echo "Options:" - echo " -t, --tag TAG_NAME:TAG_VALUE EC2 instance tag name and value for filtering (can be specified multiple times)" - echo " -p, --port PORT_NUMBER Remote port number to forward" - echo " -l, --local-port LOCAL_PORT_NUMBER Local port number to use" - echo " -y, --yes Bypass confirmation prompt" - echo " -h, --help Display this help menu" -} - -port_forward() { - local tag_filters=() - local port_number="" - local local_port_number="" - local bypass_confirmation=false - - while [[ $# -gt 0 ]]; do - case $1 in - -t | --tag) - shift - IFS=':' read -ra tag <<<"$1" - if [[ ${#tag[@]} -ne 2 ]]; then - log_error "Invalid tag format. Use -t TAG_NAME:TAG_VALUE or --tag TAG_NAME:TAG_VALUE" - port_forward_help - return 1 - fi - tag_filters+=("Name=tag:${tag[0]},Values=${tag[1]}") - ;; - -p | --port) - shift - port_number="$1" - ;; - -l | --local-port) - shift - local_port_number="$1" - ;; - -y | --yes) - bypass_confirmation=true - ;; - -h | --help) - port_forward_help - return 0 - ;; - *) - log_error "Invalid option: $1" - port_forward_help - return 1 - ;; - esac - shift - done - - if [[ ${#tag_filters[@]} -eq 0 ]]; then - log_error "At least one tag filter is required. Use -t or --tag option to specify tags." - port_forward_help - return 1 - fi - - if [[ -z "$port_number" ]]; then - log_error "Remote port number is required. Use -p or --port option to specify the port number." - port_forward_help - return 1 - fi - - if [[ -z "$local_port_number" ]]; then - log_error "Local port number is required. Use -l or --local-port option to specify the local port number." - port_forward_help - return 1 - fi - - check_dependencies "fzf" "awk" - check_aws_credentials - - instance_id=$(find_instance_id "${tag_filters[@]}") - - if [[ -z $instance_id ]]; then - log_error "No running instances found matching the specified tags." - return 1 - fi - - port_forward_command="aws ssm start-session --target \"$instance_id\" --document-name AWS-StartPortForwardingSession --parameters \"{\\\"portNumber\\\":[\\\"$port_number\\\"],\\\"localPortNumber\\\":[\\\"$local_port_number\\\"]}\"" - - if ! $bypass_confirmation; then - echo "Port forwarding command:" - echo "$port_forward_command" - read -rp "Do you want to proceed? (y/N): " confirm - if [[ $confirm != [yY] ]]; then - echo "Aborted." - return 0 - fi - fi - - log_info "Forwarding remote port $port_number to local port $local_port_number on instance $instance_id" - eval "$port_forward_command" -} diff --git a/tools/ssm-tool/commands/ssh.sh b/tools/ssm-tool/commands/ssh.sh deleted file mode 100755 index 74d22e3d..00000000 --- a/tools/ssm-tool/commands/ssh.sh +++ /dev/null @@ -1,79 +0,0 @@ -# SSH command -ssh_connect_help() { - echo "Usage: $0 ssh [-u|--user USER_NAME] [-t|--tag TAG_NAME:TAG_VALUE]... [-y|--yes]" - echo - echo "Options:" - echo " -u, --user USER_NAME User name to use for SSH connection (default: freecodecamp)" - echo " -t, --tag TAG_NAME:TAG_VALUE EC2 instance tag name and value for filtering (can be specified multiple times)" - echo " -y, --yes Bypass confirmation prompt" - echo " -h, --help Display this help menu" -} - -ssh_connect() { - local tag_filters=() - local user="freecodecamp" - local bypass_confirmation=false - - while [[ $# -gt 0 ]]; do - case $1 in - -u | --user) - shift - user="$1" - ;; - -t | --tag) - shift - IFS=':' read -ra tag <<<"$1" - if [[ ${#tag[@]} -ne 2 ]]; then - log_error "Invalid tag format. Use -t TAG_NAME:TAG_VALUE or --tag TAG_NAME:TAG_VALUE" - ssh_connect_help - return 1 - fi - tag_filters+=("Name=tag:${tag[0]},Values=${tag[1]}") - ;; - -y | --yes) - bypass_confirmation=true - ;; - -h | --help) - ssh_connect_help - return 0 - ;; - *) - log_error "Invalid option: $1" - ssh_connect_help - return 1 - ;; - esac - shift - done - - if [[ ${#tag_filters[@]} -eq 0 ]]; then - log_error "At least one tag filter is required." - ssh_connect_help - return 1 - fi - - check_dependencies "fzf" "awk" "ssh" - check_aws_credentials - - instance_id=$(find_instance_id "${tag_filters[@]}") - - if [[ -z $instance_id ]]; then - log_error "No running instances found matching the specified tags." - return 1 - fi - - ssh_command="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ProxyCommand='aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters portNumber=%p' \"$user@$instance_id\"" - - if ! $bypass_confirmation; then - echo "SSH command:" - echo "$ssh_command" - read -rp "Do you want to proceed? (y/N): " confirm - if [[ $confirm != [yY] ]]; then - echo "Aborted." - return 0 - fi - fi - - log_info "Opening SSH session to instance $instance_id" - eval "$ssh_command" -} diff --git a/tools/ssm-tool/lib/common.sh b/tools/ssm-tool/lib/common.sh deleted file mode 100755 index be07bb47..00000000 --- a/tools/ssm-tool/lib/common.sh +++ /dev/null @@ -1,93 +0,0 @@ -#!/bin/bash - -# Common variables -LOG_DIR="/tmp/logs" -LOG_FILE="${LOG_DIR}/ssm.log" -ERROR_LOG_FILE="${LOG_DIR}/ssm-error.log" - -# Create the log directory if it doesn't exist -mkdir -p "$LOG_DIR" - -# Common functions -timestamp() { - date '+%Y-%m-%d %H:%M:%S' -} - -log_info() { - echo "$(timestamp) [INFO] $1" | tee -a "$LOG_FILE" -} - -log_error() { - echo "$(timestamp) [ERROR] $1" | tee -a "$ERROR_LOG_FILE" >&2 -} - -check_dependencies() { - local dependencies=("$@") - for dep in "${dependencies[@]}"; do - if ! command -v "$dep" >/dev/null 2>&1; then - log_error "Dependency '$dep' not found. Please install it and try again." - exit 1 - fi - done -} - -check_aws_credentials() { - if ! command -v aws &>/dev/null; then - log_error "AWS CLI is not installed. Please install it first." - exit 1 - fi - - if ! aws sts get-caller-identity &>/dev/null; then - log_error "AWS credentials are not configured properly. Please configure them using 'aws configure'." - exit 1 - fi -} - -find_instance_id() { - local filters=("$@") - local query="Reservations[].Instances[] | [?State.Name != 'terminated'] | [].[InstanceId, " - local tag_keys="Name" - for filter in "${filters[@]}"; do - if [[ $filter =~ Name=tag:([^,]+) ]]; then - tag_keys+=" ${BASH_REMATCH[1]}" - fi - done - - local tag_query="" - for key in $tag_keys; do - [[ -n "$tag_query" ]] && tag_query+=", " - tag_query+="join(' ', Tags[?Key=='$key'].Value || ['-'])" - done - query+="$tag_query, State.Name, PrivateIpAddress]" - - local temp_file=$(mktemp) - local aws_command="aws ec2 describe-instances" - for filter in "${filters[@]}"; do - aws_command+=" --filters '$filter'" - done - aws_command+=" --query \"$query\" --output text" - - if ! eval $aws_command >"$temp_file" 2>&1; then - log_error "Error querying AWS EC2 instances: $(cat "$temp_file")" - rm "$temp_file" - return 1 - fi - - if [[ $(wc -l <"$temp_file") -le 1 ]]; then - rm "$temp_file" - return 0 - fi - - instance_id=$( - ( - echo -e "Instance-Id\tName\t${tag_keys#Name }\tState\tPrivate-IP" - cat "$temp_file" - ) | - column -t | sort -k2 | - fzf --header-lines=1 | - awk '{print $1}' - ) - - rm "$temp_file" - echo "$instance_id" -} diff --git a/tools/ssm-tool/ssm-tool.sh b/tools/ssm-tool/ssm-tool.sh deleted file mode 100755 index e22b2602..00000000 --- a/tools/ssm-tool/ssm-tool.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash -set -o nounset -set -o errexit -set -o pipefail - -# Source common functions and variables -source "$(dirname "$0")/lib/common.sh" - -# Source individual command files -source "$(dirname "$0")/commands/port-forward.sh" -source "$(dirname "$0")/commands/list-instances.sh" -source "$(dirname "$0")/commands/ssh.sh" - -# Main help menu -main_help() { - echo "Usage: $0 {port-forward|list|ssh} [options]" - echo "Run '$0 {command} --help' for more information on a specific command." - echo - echo "Commands:" - echo " port-forward Forward a port from an EC2 instance" - echo " list List EC2 instances" - echo " ssh Connect to an EC2 instance via SSH" -} - -# Main function to handle command selection -main() { - if [[ $# -eq 0 ]]; then - main_help - exit 1 - fi - - local command="$1" - shift - - case "$command" in - port-forward) - port_forward "$@" - ;; - list) - list_instances "$@" - ;; - ssh) - ssh_connect "$@" - ;; - -h | --help) - main_help - ;; - *) - log_error "Unknown command: $command" - main_help - exit 1 - ;; - esac -} - -# Run the main function with all arguments -main "$@"