Skip to content

Commit 97d9735

Browse files
committed
+
1 parent eea3eda commit 97d9735

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

Misc/Cloud/reboot_instance_aws.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
3+
##Requirements: aws-wire-lengths bash coreutils curl findutils jq moreutils
4+
5+
##Usage:
6+
# bash <(curl -qfsSL "https://raw.githubusercontent.com/pkgforge/devscripts/refs/heads/main/Misc/Cloud/eboot_instance_aws.sh")
7+
8+
#----------------------------------------------------------------------------#
9+
##Sanity Checks
10+
SYSTMP="$(dirname $(mktemp -u))" && export SYSTMP="${SYSTMP}"
11+
USER_AGENT="$(curl -qfsSL 'https://raw.githubusercontent.com/pkgforge/devscripts/refs/heads/main/Misc/User-Agents/ua_firefox_macos_latest.txt')" && export USER_AGENT="${USER_AGENT}"
12+
#Curl
13+
if ! command -v curl &> /dev/null; then
14+
echo -e "\n[-] FATAL: Install Curl (https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/curl)\n"
15+
exit 1
16+
fi
17+
#Get Bin
18+
if command -v sudo &> /dev/null && sudo -n true 2>/dev/null; then
19+
sudo curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/aws-wire-lengths" -o "/usr/local/bin/aws-wire-lengths" && sudo chmod +x "/usr/local/bin/aws-wire-lengths"
20+
sudo curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/ipinfo" -o "/usr/local/bin/ipinfo" && sudo chmod +x "/usr/local/bin/ipinfo"
21+
sudo curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/jq" -o "/usr/local/bin/jq" && sudo chmod +x "/usr/local/bin/jq"
22+
else
23+
curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/aws-wire-lengths" -o "${SYSTMP}/aws-wire-lengths" && chmod +x "${SYSTMP}/aws-wire-lengths"
24+
curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/ipinfo" -o "${SYSTMP}/ipinfo" && chmod +x "${SYSTMP}/ipinfo"
25+
curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/jq" -o "${SYSTMP}/jq" && chmod +x "${SYSTMP}/jq"
26+
export PATH="${PATH}:${SYSTMP}"
27+
fi
28+
#AWS_ACCESS_KEY_ID
29+
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "${AWS_ACCESS_KEY_ID+x}" ]; then
30+
echo -e "\n[-] AWS_ACCESS_KEY_ID isn't Exported\n"
31+
exit 1
32+
fi
33+
#AWS_ACCESS_KEY_ID
34+
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "${AWS_SECRET_ACCESS_KEY+x}" ]; then
35+
echo -e "\n[-] AWS_SECRET_ACCESS_KEY isn't Exported\n"
36+
exit 1
37+
fi
38+
#AWS_REGION
39+
if [ -z "$AWS_REGION" ] || [ -z "${AWS_REGION+x}" ]; then
40+
echo -e "\n[-] AWS_REGION isn't Exported\n"
41+
exit 1
42+
fi
43+
#EC2_INSTANCE_NAME
44+
if [ -z "$EC2_INSTANCE_NAME" ] || [ -z "${EC2_INSTANCE_NAME+x}" ]; then
45+
echo -e "\n[-] EC2_INSTANCE_NAME isn't Exported\n"
46+
exit 1
47+
fi
48+
#EC2_INSTANCE_ID
49+
if [ -z "$EC2_INSTANCE_ID" ] || [ -z "${EC2_INSTANCE_ID+x}" ]; then
50+
echo -e "\n[-] EC2_INSTANCE_ID isn't Exported\n"
51+
exit 1
52+
fi
53+
#----------------------------------------------------------------------------#
54+
55+
#----------------------------------------------------------------------------#
56+
#List
57+
aws-wire-lengths --region-ec2 "${AWS_REGION}" instance list
58+
#Get IP
59+
EC2_IP_OLD="$(aws-wire-lengths --region-ec2 "${AWS_REGION}" instance ip "${EC2_INSTANCE_NAME}" | tr -d '[:space:]')" && export EC2_IP_OLD="${EC2_IP_OLD}"
60+
echo -e "\n[+] IPv4 Address: ${EC2_IP_OLD}\n"
61+
#curl -A "${USER_AGENT}" -H "Accept: application/json" -qfsSL "https://ipinfo.io/${EC2_IP_OLD}/json" | jq . ; echo
62+
ipinfo "${EC2_IP_OLD}" --pretty 2>/dev/null
63+
#Start/Stop: https://web.archive.org/web/2/https://devopslearning.medium.com/differences-between-aws-ec2-instance-start-stop-and-restart-operation-35f98f9e1065
64+
echo -e "\n[+] Stopping... ${EC2_INSTANCE_NAME}"
65+
aws-wire-lengths --region-ec2 "${AWS_REGION}" instance stop "${EC2_INSTANCE_NAME}" >/dev/null 2>&1
66+
echo -e "[+] Starting... ${EC2_INSTANCE_NAME}\n"
67+
aws-wire-lengths --region-ec2 "${AWS_REGION}" instance start "${EC2_INSTANCE_NAME}" >/dev/null 2>&1
68+
#List again
69+
aws-wire-lengths --region-ec2 "${AWS_REGION}" instance list
70+
#Recheck IP
71+
EC2_IP_NEW="$(aws-wire-lengths --region-ec2 "${AWS_REGION}" instance ip "${EC2_INSTANCE_NAME}" | tr -d '[:space:]')" && export EC2_IP_NEW="${EC2_IP_NEW}"
72+
echo -e "\n[+] IPv4 Address: ${EC2_IP_NEW}\n"
73+
#curl -A "${USER_AGENT}" -H "Accept: application/json" -qfsSL "https://ipinfo.io/${EC2_IP_NEW}/json" | jq . ; echo
74+
ipinfo "${EC2_IP_NEW}" --pretty 2>/dev/null
75+
#END
76+
unset EC2_IP_OLD EC2_IP_NEW
77+
#EOF
78+
#----------------------------------------------------------------------------#

Misc/Cloud/reboot_instance_cntb.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
3+
##Requirements: cntb bash coreutils curl findutils jq moreutils
4+
5+
##Config: https://my.contabo.com/api/details
6+
# export CONTABO_CLIENT_ID='id'
7+
# export CONTABO_CLIENT_SECRET='secret'
8+
# export CONTABO_API_USERNAME='email'
9+
# export CONTABO_API_PASSWORD='api_password_NOT_account_password'
10+
# export INSTANCE_HOST_NUMBER="Host Number from: https://my.contabo.com/vps/0#"
11+
12+
##Usage:
13+
# bash <(curl -qfsSL "https://raw.githubusercontent.com/pkgforge/devscripts/refs/heads/main/Misc/Cloud/reboot_instance_cntb.sh")
14+
15+
#----------------------------------------------------------------------------#
16+
##Sanity Checks
17+
SYSTMP="$(dirname $(mktemp -u))" && export SYSTMP="${SYSTMP}"
18+
USER_AGENT="$(curl -qfsSL 'https://raw.githubusercontent.com/pkgforge/devscripts/refs/heads/main/Misc/User-Agents/ua_firefox_macos_latest.txt')" && export USER_AGENT="${USER_AGENT}"
19+
#Curl
20+
if ! command -v curl &> /dev/null; then
21+
echo -e "\n[-] FATAL: Install Curl (https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/curl)\n"
22+
exit 1
23+
fi
24+
#Get Bin
25+
if command -v sudo &> /dev/null && sudo -n true 2>/dev/null; then
26+
sudo curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/cntb" -o "/usr/local/bin/cntb" && sudo chmod +x "/usr/local/bin/cntb"
27+
sudo curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/ipinfo" -o "/usr/local/bin/ipinfo" && sudo chmod +x "/usr/local/bin/ipinfo"
28+
sudo curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/jq" -o "/usr/local/bin/jq" && sudo chmod +x "/usr/local/bin/jq"
29+
else
30+
curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/cntb" -o "${SYSTMP}/cntb" && chmod +x "${SYSTMP}/cntb"
31+
curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/ipinfo" -o "${SYSTMP}/ipinfo" && chmod +x "${SYSTMP}/ipinfo"
32+
curl -A "${USER_AGENT}" -qfsSL "https://bin.pkgforge.dev/$(uname -m)-$(uname -s)/jq" -o "${SYSTMP}/jq" && chmod +x "${SYSTMP}/jq"
33+
export PATH="${PATH}:${SYSTMP}"
34+
fi
35+
#CONTABO_CLIENT_ID
36+
if [ -z "$CONTABO_CLIENT_ID" ] || [ -z "${CONTABO_CLIENT_ID+x}" ]; then
37+
echo -e "\n[-] CONTABO_CLIENT_ID isn't Exported: https://my.contabo.com/api/details\n"
38+
exit 1
39+
fi
40+
#CONTABO_CLIENT_SECRET
41+
if [ -z "$CONTABO_CLIENT_SECRET" ] || [ -z "${CONTABO_CLIENT_SECRET+x}" ]; then
42+
echo -e "\n[-] CONTABO_CLIENT_SECRET isn't Exported: https://my.contabo.com/api/details\n"
43+
exit 1
44+
fi
45+
#CONTABO_API_USERNAME
46+
if [ -z "$CONTABO_API_USERNAME" ] || [ -z "${CONTABO_API_USERNAME+x}" ]; then
47+
echo -e "\n[-] CONTABO_API_USERNAME isn't Exported: https://my.contabo.com/api/details\n"
48+
exit 1
49+
fi
50+
#CONTABO_API_PASSWORD
51+
if [ -z "$CONTABO_API_PASSWORD" ] || [ -z "${CONTABO_API_PASSWORD+x}" ]; then
52+
echo -e "\n[-] CONTABO_API_PASSWORD isn't Exported: https://my.contabo.com/api/details\n"
53+
exit 1
54+
fi
55+
#INSTANCE_HOST_NUMBER
56+
if [ -z "$INSTANCE_HOST_NUMBER" ] || [ -z "${INSTANCE_HOST_NUMBER+x}" ]; then
57+
echo -e "\n[-] INSTANCE_HOST_NUMBER isn't Exported: https://my.contabo.com/api/details\n"
58+
exit 1
59+
fi
60+
#----------------------------------------------------------------------------#
61+
62+
#----------------------------------------------------------------------------#
63+
#Set config [ "$HOME/.cntb.yaml" || "/etc/cntb/.cntb.yaml" ]
64+
#cntb config set-credentials --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info"
65+
rm -rf "$HOME/.cntb.yaml" 2>/dev/null ; sudo rm -rf "/etc/cntb/.cntb.yaml" 2>/dev/null
66+
#List
67+
cntb get instances --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info"
68+
#Get Instance_ID
69+
CONTABO_INSTANCE_ID="$(cntb get instances --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info" --output="json" | jq --arg HN "${INSTANCE_HOST_NUMBER}" '.[] | select(.vHostNumber == ($HN | tonumber))' | jq -r '.instanceId')" && export CONTABO_INSTANCE_ID="${CONTABO_INSTANCE_ID}"
70+
CONTABO_INSTANCE_IP="$(cntb get instances --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info" --output="json" | jq --arg HN "${INSTANCE_HOST_NUMBER}" '.[] | select(.vHostNumber == ($HN | tonumber))' | jq -r '.ipv4')" && export CONTABO_INSTANCE_IP="${CONTABO_INSTANCE_IP}"
71+
#Reboot
72+
echo -e "\n[+] Stopping... ${CONTABO_INSTANCE_ID} (${INSTANCE_HOST_NUMBER})"
73+
cntb stop instance "${CONTABO_INSTANCE_ID}" --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info" && sleep 10
74+
cntb get instance "${CONTABO_INSTANCE_ID}" --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info"
75+
echo -e "\n[+] Starting... ${CONTABO_INSTANCE_ID} (${INSTANCE_HOST_NUMBER})"
76+
cntb start instance "${CONTABO_INSTANCE_ID}" --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info" && sleep 10
77+
cntb get instance "${CONTABO_INSTANCE_ID}" --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info"
78+
cntb get instance "${CONTABO_INSTANCE_ID}" --oauth2-clientid="$(echo "${CONTABO_CLIENT_ID}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-client-secret="$(echo "${CONTABO_CLIENT_SECRET}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-user="$(echo "${CONTABO_API_USERNAME}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --oauth2-password="$(echo "${CONTABO_API_PASSWORD}" | sed -e 's/"//g' -e "s/'//g" | tr -d '[:space:]')" --debug="info" --output="yaml"
79+
#Get IP Info
80+
#curl -A "${USER_AGENT}" -H "Accept: application/json" -qfsSL "https://ipinfo.io/${CONTABO_INSTANCE_IP}/json" | jq . ; echo
81+
ipinfo "${CONTABO_INSTANCE_IP}" --pretty 2>/dev/null
82+
#END
83+
unset CONTABO_INSTANCE_ID
84+
rm -rf "$HOME/.cntb.yaml" 2>/dev/null ; sudo rm -rf "/etc/cntb/.cntb.yaml" 2>/dev/null
85+
#EOF
86+
#----------------------------------------------------------------------------#

0 commit comments

Comments
 (0)