Skip to content

Commit

Permalink
skjbgf
Browse files Browse the repository at this point in the history
  • Loading branch information
xXkoshmarikXx committed Aug 26, 2024
1 parent f25d9de commit 6f32aba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 38 deletions.
37 changes: 19 additions & 18 deletions parameters_accepts.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env bash

set -euxo pipefail
# Constants
LOCAL_IDENTIFY_OS_SCRIPT="identify_os.sh"
REMOTE_IDENTIFY_OS_SCRIPT="https://raw.githubusercontent.com/inqwise/ansible-automation-toolkit/default/identify_os.sh"
VAULT_PASSWORD_FILE="vault_password"
PLAYBOOK_VERSION="latest"

ACCOUNT_ID=""
TOPIC_NAME=""
REGION=""
Expand All @@ -14,14 +15,15 @@ EXTRA=""
OFFLINE=false
TEST_MODE=false
PIP_COMMAND="pip"

TOKEN=""
GET_PIP_URL=""
PLAYBOOK_NAME=""
PLAYBOOK_BASE_URL=""

VAULT_PASSWORD=""
METADATA_TOKEN=""

usage() {
echo "Usage: $0 [-e <extra>] [--skip-tags <skip-tags>] [--tags <tags>] [--offline] [--test] [--token <token>] [--get_pip_url <url>] [--playbook_name <name>] [--playbook_base_url <url>] [-r <name>] [--account_id <name>] [--topic_name <name>]"
echo "Usage: $0 [-e <extra>] [--skip-tags <skip-tags>] [--tags <tags>] [--offline] [--test] [--token <token>] [--get_pip_url <url>] [--playbook_name <name>] [--playbook_base_url <url>] [-r <name>] [--account_id <name>] [--topic_name <name>] [--vault_password <name>]"
exit 1
}

Expand All @@ -34,10 +36,11 @@ while getopts ":e:r:-:" option; do
tags) TAGS="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
account_id) ACCOUNT_ID="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
topic_name) TOPIC_NAME="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
token) TOKEN="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
get_pip_url) GET_PIP_URL="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
playbook_name) PLAYBOOK_NAME="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
playbook_base_url) PLAYBOOK_BASE_URL="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
vault_password) VAULT_PASSWORD="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
metadata_token) METADATA_TOKEN="${!OPTIND}"; OPTIND=$((OPTIND + 1));;
offline) OFFLINE=true;;
test) TEST_MODE=true;;
*) echo "Invalid option --${OPTARG}"; usage;;
Expand Down Expand Up @@ -76,8 +79,11 @@ identify_os() {
fi
}

get_metadata_token() {
curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"
cleanup() {
if [ -f "$VAULT_PASSWORD_FILE" ]; then
rm -f "$VAULT_PASSWORD_FILE"
echo "vault_password file removed."
fi
}

catch_error() {
Expand Down Expand Up @@ -157,18 +163,8 @@ main() {

identify_os

METADATA_TOKEN=$(get_metadata_token)
PLAYBOOK_NAME=$(get_instance_tags "$METADATA_TOKEN" playbook_name)
echo "playbook_name: $PLAYBOOK_NAME"

GET_PIP_URL=$(echo "$PARAMETER" | grep 'get_pip_url' | awk '{print $2}')
echo "Get Pip URL: $GET_PIP_URL"

PLAYBOOK_BASE_URL=$(echo "$PARAMETER" | grep 'playbook_base_url' | awk '{print $2}')
echo "Playbook Base URL: $PLAYBOOK_BASE_URL"

VAULT_PASSWORD=$(aws secretsmanager get-secret-value --secret-id "$SECRET_NAME" --region "$REGION" --query 'SecretString' --output text)

assert_var "PLAYBOOK_NAME" "$PLAYBOOK_NAME"
assert_var "PLAYBOOK_BASE_URL" "$PLAYBOOK_BASE_URL"
assert_var "VAULT_PASSWORD" "$VAULT_PASSWORD"
Expand All @@ -186,4 +182,9 @@ main() {
trap 'catch_error "$ERROR"' ERR

# Execute the main function and capture errors
{ ERROR=$(main 2>&1 1>&$out); } {out}>&1
ERROR=$(main 2>&1)

# If you want to ensure that the script exits after an error, you can use:
if [ $? -ne 0 ]; then
exit 1
fi
24 changes: 24 additions & 0 deletions parameters_exist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euxo pipefail

REGION="il-central-1"
echo "region: $REGION"

ACCOUNT_ID="992382682634"
echo "account: $ACCOUNT_ID"

TOPIC_NAME="errors"
echo "topic: $TOPIC_NAME"

GET_PIP_URL="s3://resource-opinion-stg/get-pip.py"
echo "Get Pip URL: $GET_PIP_URL"

PLAYBOOK_BASE_URL="s3://bootstrap-opinion-stg/playbooks"
echo "Playbook Base URL: $PLAYBOOK_BASE_URL"

PLAYBOOK_NAME="ansible-openvpn"
echo "playbook_name: $PLAYBOOK_NAME"

VAULT_PASSWORD="123123"

bash parameters_accepts.sh --get_pip_url "$GET_PIP_URL" --playbook_name "$PLAYBOOK_NAME" --playbook_base_url "$PLAYBOOK_BASE_URL" -r "$REGION" --account_id "$ACCOUNT_ID" --topic_name "$TOPIC_NAME" --vault_password $VAULT_PASSWORD
43 changes: 23 additions & 20 deletions parameters_find.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
set -euxo pipefail

SECRET_NAME="vault_secret"

get_region() {
Expand All @@ -14,6 +16,21 @@ get_parameter() {
aws ssm get-parameter --name "$name" --query "Parameter.Value" --output text --region "$REGION"
}

REGION=$(get_region)
echo "region: $REGION"

ACCOUNT_ID=$(get_account_id)
echo "account: $ACCOUNT_ID"

PARAMETER=$(get_parameter "UserDataYAMLConfig")

TOPIC_NAME=$(echo "$PARAMETER" | grep 'topic_name' | awk '{print $2}')
echo "topic: $TOPIC_NAME"

get_metadata_token() {
curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"
}

get_instance_tags() {
local token=$1
local tag=$2
Expand All @@ -32,33 +49,19 @@ get_instance_tags() {
fi
}

cleanup() {
if [ -f "$VAULT_PASSWORD_FILE" ]; then
rm -f "$VAULT_PASSWORD_FILE"
echo "vault_password file removed."
fi
}
METADATA_TOKEN=$(get_metadata_token)

PARAMETER=$(get_parameter "UserDataYAMLConfig")

REGION=$(get_region)
echo "region: $REGION"

ACCOUNT_ID=$(get_account_id)
echo "account: $ACCOUNT_ID"

TOPIC_NAME=$(echo "$PARAMETER" | grep 'topic_name' | awk '{print $2}')
echo "topic: $TOPIC_NAME"
PLAYBOOK_NAME=$(get_instance_tags "$METADATA_TOKEN" playbook_name)
echo "playbook_name: $PLAYBOOK_NAME"

GET_PIP_URL=$(echo "$PARAMETER" | grep 'get_pip_url' | awk '{print $2}')
echo "Get Pip URL: $GET_PIP_URL"

PLAYBOOK_BASE_URL=$(echo "$PARAMETER" | grep 'playbook_base_url' | awk '{print $2}')
echo "Playbook Base URL: $PLAYBOOK_BASE_URL"

PLAYBOOK_NAME=$(get_instance_tags "$METADATA_TOKEN" playbook_name)
echo "playbook_name: $PLAYBOOK_NAME"

VAULT_PASSWORD=$(aws secretsmanager get-secret-value --secret-id "$SECRET_NAME" --region "$REGION" --query 'SecretString' --output text)

bash parameters_accepts.sh --token "$TOKEN" --get_pip_url "$GET_PIP_URL" --playbook_name "$PLAYBOOK_NAME" --playbook_base_url "$PLAYBOOK_BASE_URL" -r "$REGION" --account_id "$ACCOUNT_ID" --topic_name "$TOPIC_NAME"
curl -o /tmp/parameters_accepts.sh https://raw.githubusercontent.com/xXkoshmarikXx/test-commit/master/parameters_accepts.sh
bash /tmp/parameters_accepts.sh --tags installation --get_pip_url "$GET_PIP_URL" --playbook_name "$PLAYBOOK_NAME" --playbook_base_url "$PLAYBOOK_BASE_URL" -r "$REGION" --account_id "$ACCOUNT_ID" --topic_name "$TOPIC_NAME" --vault_password $VAULT_PASSWORD --metadata_token $METADATA_TOKEN
#bash parameters_accepts.sh --get_pip_url "$GET_PIP_URL" --playbook_name "$PLAYBOOK_NAME" --playbook_base_url "$PLAYBOOK_BASE_URL" -r "$REGION" --account_id "$ACCOUNT_ID" --topic_name "$TOPIC_NAME"

0 comments on commit 6f32aba

Please sign in to comment.