Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first scripting azure experiments (fixes #42) #41

Merged
merged 5 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/vpn/conf/*
*.conf
!vpn/templates/*.conf
.treehouses
108 changes: 54 additions & 54 deletions init.sh → src/aws/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,73 +134,73 @@ function usage {
exit 1
}

while getopts 'n:pN:a:' OPTION; do
case "$OPTION" in
n)
keyname=$OPTARG
;;
p)
portConfigArray=$(getArrayValueAsStringByKey $instanceName tcpPortArray)
udpPortConfigArray=$(getArrayValueAsStringByKey $instanceName udpPortArray)
if [ -z "$portConfigArray" ]
then
echo "There is no stored port numbers. The default port numbers are used"
fi
if [ -z "$udpPortConfigArray" ]
then
echo "There is no stored udp port numbers. The default port numbers are used"
fi
;;
a)
groupName=$OPTARG-sg
instanceName=$OPTARG
keyname=$OPTARG
;;
?)
usage
;;
esac
done
shift "$(($OPTIND -1))"


if [ -z $keyname ]
then
keyname=luftballon
fi
function init {
while getopts 'n:pN:a:' OPTION; do
case "$OPTION" in
n)
keyname=$OPTARG
;;
p)
portConfigArray=$(getArrayValueAsStringByKey $instanceName tcpPortArray)
udpPortConfigArray=$(getArrayValueAsStringByKey $instanceName udpPortArray)
if [ -z "$portConfigArray" ]
then
echo "There is no stored port numbers. The default port numbers are used"
fi
if [ -z "$udpPortConfigArray" ]
then
echo "There is no stored udp port numbers. The default port numbers are used"
fi
;;
a)
groupName=$OPTARG-sg
instanceName=$OPTARG
keyname=$OPTARG
;;
?)
usage
;;
esac
done
shift "$(($OPTIND -1))"


keyName=$(importSshKey | getValueByKeyword KeyName )
if [ -z $keyname ]
then
keyname=luftballon
fi

if [ -z $keyName ]
then
exit 1
fi

echo "Success to add ssh key: $keyName"
keyName=$(importSshKey | getValueByKeyword KeyName )

createSecurityGroups
echo "Add security group"
if [ -z $keyName ]
then
exit 1
fi

instanceId=$(createEc2 | getValueByKeyword InstanceId )
echo "Create EC2 Instance"
echo "Instance id is $instanceId"
echo "Success to add ssh key: $keyName"

createSecurityGroups
echo "Add security group"

aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
instanceId=$(createEc2 | getValueByKeyword InstanceId )
echo "Create EC2 Instance"
echo "Instance id is $instanceId"


publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
echo "Public IP Address is $publicIp"
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses

echo "Will open ssh tunnel soon"
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
echo "Opened ssh tunnel"

openSSHTunnel $publicIp $portConfigArray
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $keyName $instanceId $publicIp $groupName
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
echo "Public IP Address is $publicIp"

echo "Will open ssh tunnel soon"
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
echo "Opened ssh tunnel"

openSSHTunnel $publicIp $portConfigArray
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $keyName $instanceId $publicIp $groupName
}


26 changes: 26 additions & 0 deletions src/azure/auth/driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
authUsage() {
echo "Usage: $0 credential [command]"
echo "Commands:"
echo " login - Login azure as service-principal"
exit 1
}

function auth(){

# Check if at least one argument is provided
if [ $# -eq 0 ]; then
authUsage
fi

# Execute the appropriate command
case "$1" in
login)
login
;;
*)
echo "Error: Invalid command."
authUsage
;;
esac

}
2 changes: 2 additions & 0 deletions src/azure/auth/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source auth/login.sh
source auth/driver.sh
8 changes: 8 additions & 0 deletions src/azure/auth/login.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function login(){

username=$(retrieveCred username)
password=$(retrieveCred password)
tenant=$(retrieveCred tenant_name)

az login --service-principal --username $username --password $password --tenant $tenant
}
7 changes: 7 additions & 0 deletions src/azure/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Directory and file names
DIR_NAME=".luftballon"
FILE_NAME="credentials.txt"

# Full path of the directory and the file
DIR_PATH="$HOME/$DIR_NAME"
FILE_PATH="$DIR_PATH/$FILE_NAME"
35 changes: 35 additions & 0 deletions src/azure/credential/driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

credentialUsage() {
echo "Usage: $0 credential [command]"
echo "Commands:"
echo " init - Initialize and store new credentials"
echo " update - Update existing credentials"
echo " show - Display current credentials"
exit 1
}

function credential(){

# Check if at least one argument is provided
if [ $# -eq 0 ]; then
credentialUsage
fi

# Execute the appropriate command
case "$1" in
init)
initCreds
;;
update)
updateCreds
;;
show)
showCreds
;;
*)
echo "Error: Invalid command."
credentialUsage
;;
esac

}
5 changes: 5 additions & 0 deletions src/azure/credential/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source credential/driver.sh
source credential/setup.sh
source credential/show.sh
source credential/update.sh
source credential/retrieve.sh
14 changes: 14 additions & 0 deletions src/azure/credential/retrieve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

retrieveCred() {
local key=$1
local file=$FILE_PATH

local value=$(grep "^$key=" "$file" | cut -d'=' -f2)

if [ -z "$value" ]; then
echo ""
else
echo $value
fi
}
36 changes: 36 additions & 0 deletions src/azure/credential/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

saveCreds() {
read -p "Enter your username: " username
read -sp "Enter your password: " password
echo
read -p "Enter your tenant name: " tenant_name

# Storing credentials in the file
echo "username=$username" > "$FILE_PATH"
echo "password=$password" >> "$FILE_PATH"
echo "tenant_name=$tenant_name" >> "$FILE_PATH"

echo "Credentials stored successfully in $FILE_PATH."
}

checkDirFile() {
if [ ! -d "$DIR_PATH" ]; then
echo "Directory $DIR_PATH does not exist. Creating now."
mkdir "$DIR_PATH"
else
echo "Directory $DIR_PATH already exists."
fi

if [ ! -f "$FILE_PATH" ]; then
echo "Creating credentials file at $FILE_PATH."
touch "$FILE_PATH"
else
echo "Credentials file already exists at $FILE_PATH."
fi
}

initCreds() {
checkDirFile
saveCreds
}
19 changes: 19 additions & 0 deletions src/azure/credential/show.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

showCreds() {
if [ ! -f "$FILE_PATH" ]; then
echo "Credentials file not found."
exit 1
fi

echo "Current credentials:"
while IFS= read -r line; do
if [[ $line == password=* ]]; then
password=${line#password=}
masked_password="${password:0:6}*****"
echo "password=$masked_password"
else
echo "$line"
fi
done < "$FILE_PATH"
}
31 changes: 31 additions & 0 deletions src/azure/credential/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

updateCreds() {
if [ ! -f "$FILE_PATH" ]; then
echo "Credentials file not found. Please run the setup script first."
exit 1
fi

echo "Updating credentials..."

# Update username
read -p "Enter your new username (leave blank to keep current): " new_username
if [ -n "$new_username" ]; then
sed -i "s/^username=.*/username=$new_username/" "$FILE_PATH"
fi

# Update password
read -sp "Enter your new password (leave blank to keep current): " new_password
echo
if [ -n "$new_password" ]; then
sed -i "s/^password=.*/password=$new_password/" "$FILE_PATH"
fi

# Update tenant name
read -p "Enter your new tenant name (leave blank to keep current): " new_tenant_name
if [ -n "$new_tenant_name" ]; then
sed -i "s/^tenant_name=.*/tenant_name=$new_tenant_name/" "$FILE_PATH"
fi

echo "Credentials updated successfully."
}
31 changes: 31 additions & 0 deletions src/azure/driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

source config.sh
source load.sh

rootUsage() {
echo "Usage: $0 [command group] [command]"
echo "Command groups:"
echo " credential - Manage credentials (init, update, show)"
echo " auth - Authentication management (login)"
exit 1
}

# Check if at least one argument is provided
if [ $# -eq 0 ]; then
rootUsage
fi

# Execute the appropriate command
case "$1" in
credential)
credential "$2"
;;
auth)
auth "$2"
;;
*)
echo "Error: Invalid command."
rootUsage
;;
esac
5 changes: 5 additions & 0 deletions src/azure/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
az group create --name luftballon --location eastus
az vm create --resource-group luftballon --name luftballon --image Ubuntu2204 --admin-username hiroyuki --generate-ssh-keys --public-ip-sku Standard
echo $(az vm show --show-details --resource-group luftballon --name luftballon --query publicIps --output tsv)
az vm run-command invoke --resource-group luftballon --name luftballon --command-id RunShellScript --scripts "sudo apt-get update && sudo apt-get install -y nginx"
az vm open-port --port 80 --resource-group luftballon --name luftballon
3 changes: 3 additions & 0 deletions src/azure/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source credential/load.sh
source auth/load.sh

Loading