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

smoother command workflow (fixes #54) #55

Merged
merged 16 commits into from
Jul 25, 2024
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ If you have the properly configured AWS CLI, you can start Luftballon by a comma
Execute the below command

```
./init.sh
./up.sh
```

This command does several things
Expand Down Expand Up @@ -78,7 +78,7 @@ You can login to your Raspberry Pi via the EC2 instance.
### Start Luftballon with different name

```
./init.sh -a [name]
./up.sh -a [name]
```

This a flag changes three names
Expand All @@ -96,12 +96,12 @@ This a flag changes three names
### Restart Luftballon (currently unstable)

```
./restart.sh [name]
./start.sh [name]
```

### Add other ports to sshtunnel

The init.sh only makes the sshtunnel connecting the port 2222 of EC2 instance to the port 22 of the Raspberry Pi.
The up.sh only makes the sshtunnel connecting the port 2222 of EC2 instance to the port 22 of the Raspberry Pi.
You can make another sshtunnel by the below command.

`./addPort.sh [EC2 port number] [Raspberry Pi port number]`
Expand All @@ -112,7 +112,7 @@ If you host a web application on your Raspberry Pi on the port 80, people can ac

### Delete ports from the sshtunnel

You can delete the sshtunnel by the below command
You can down the sshtunnel by the below command

`./deletePort.sh [EC2 port number] [Raspberry Pi port number]`

Expand All @@ -121,11 +121,11 @@ You can delete the sshtunnel by the below command
It is nice to connect to your Raspberry Pi by sshtunnel, but you have to pay money to Amazon to have the sshtunnels.
If you do not need to use the sshtunnel, you do not want to pay money for nothing.

The below comand delete the Luftballon.
The below comand down the Luftballon.

`./delete.sh`
`./down.sh`

This command basically does the opposite of the `init.sh`
This command basically does the opposite of the `up.sh`

1. Close sshtunnels
2. Delete EC2 instance
Expand Down Expand Up @@ -173,13 +173,13 @@ These commands adjust the specified configurations, replacing old values with ne
#### Delete Command

**What it does:**
The `delete` command removes an entire SSH configuration block for a specified host from the SSH config file, effectively discontinuing the SSH management for that host through the configuration file.
The `down` command removes an entire SSH configuration block for a specified host from the SSH config file, effectively discontinuing the SSH management for that host through the configuration file.

**How to execute:**
To remove the configuration for a host:

```
delete myserver
down myserver
```

This command deletes all settings associated with the host `myserver`, cleaning up the SSH config file by removing unused or outdated entries.
4 changes: 2 additions & 2 deletions archive/storeConfigByJson.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function storeConfig(){
local publicIp=$4
local groupName=$5

local value=$(init $instanceName)
local value=$(up $instanceName)
value=$(addKeyValue "$value" $instanceName instanceName $instanceName )
value=$(addKeyValue "$value" $instanceName key $keyName )
value=$(addKeyValue "$value" $instanceName instanceId $instanceId )
Expand All @@ -27,7 +27,7 @@ function makeConfig(){
local publicIp=$4
local groupName=$5

local value=$(init $instanceName)
local value=$(up $instanceName)
value=$(addKeyValue "$value" $instanceName instanceName $instanceName )
value=$(addKeyValue "$value" $instanceName key $keyName )
value=$(addKeyValue "$value" $instanceName instanceId $instanceId )
Expand Down
22 changes: 11 additions & 11 deletions src/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ This script is designed to simplify the management of AWS EC2 instances. It prov

## Usage

This script supports two main commands: init and delete.
This script supports two main commands: up and delete.

### Command: init
### Command: up

The init command verifies the AWS CLI installation, checks for the existence of an SSH key, and defines functions for importing an SSH key and adding ports in AWS EC2.
The up command verifies the AWS CLI installation, checks for the existence of an SSH key, and defines functions for importing an SSH key and adding ports in AWS EC2.

```bash
./driver.sh init [additional options]
./driver.sh up [additional options]
```

Options for init:
Options for up:

- -n [ssh key name]: Specify a name for the SSH key on AWS.
- -a [balloon name]: Change the SSH key name, instance name, and group name, based on the provided balloon name.
- -p: Use stored port numbers instead of the default port number.

### Command: delete
### Command: down

The delete command deletes an AWS EC2 instance and its related resources, identified by a given "balloon name". It also handles cleanup tasks such as removing SSH tunnels and deleting security keys.
The down command deletes an AWS EC2 instance and its related resources, identified by a given "balloon name". It also handles cleanup tasks such as removing SSH tunnels and deleting security keys.

```bash
./driver.sh delete [balloon name]
./driver.sh down [balloon name]
```

### Command: stop
Expand All @@ -36,12 +36,12 @@ The stop command stops a specified AWS EC2 instance and removes its associated S
./driver.sh stop [balloon name]
```

### Command: restart
### Command: start

The restart command restarts a specified Amazon EC2 instance, updates its IP address, and opens a new SSH tunnel to it.
The start command restarts a specified Amazon EC2 instance, updates its IP address, and opens a new SSH tunnel to it.

```bash
./driver.sh restart [balloon name]
./driver.sh start [balloon name]
```

### Help
Expand Down
6 changes: 6 additions & 0 deletions src/aws/dependencies/utilitiyFunction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ function makePortArray {

echo "${portArray[@]}"
}

function getState(){
local instanceId=$1
local state=$(aws ec2 describe-instances --instance-ids $instanceId | jq '.Reservations[].Instances[].State.Name')
echo $state
}
6 changes: 3 additions & 3 deletions src/aws/delete.sh → src/aws/down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#BASE=$HOME
BASE=/home/pi

function delete(){
function down(){

balloonName=$(setBalloonName "$1")

Expand All @@ -15,8 +15,8 @@ function delete(){
instanceId=$(getValueByAttribute $balloonName instanceId)

if [ "$instanceId" = "null" ]; then
echo "$balloonName is already deleted"
exit 1
echo "$balloonName does not exist"
exit 0
fi

keyName=$(getValueByAttribute $balloonName key)
Expand Down
20 changes: 10 additions & 10 deletions src/aws/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
awsUsage() {
echo "Usage: aws [command]"
echo "Commands:"
echo " init - Verify AWS CLI installation, SSH key existence, and defines functions for SSH key import and port addition in AWS EC2"
echo " delete - deletes an AWS EC2 instance and its related resources, identified by a given "balloon name", and handles associated cleanup tasks such as removing SSH tunnels and deleting security keys"
echo " stop - stops a specified AWS EC2 instance and removes its associated SSH tunnel"
echo " restart- restarts a specified Amazon EC2 instance, updates its IP address, and opens a new SSH tunnel to it"
echo " up - Verify AWS CLI installation, SSH key existence, and defines functions for SSH key import and port addition in AWS EC2"
echo " down - deletes an AWS EC2 instance and its related resources, identified by a given "balloon name", and handles associated cleanup tasks such as removing SSH tunnels and deleting security keys"
echo " stop - stops a specified AWS EC2 instance and removes its associated SSH tunnel"
echo " start - starts a specified Amazon EC2 instance, updates its IP address, and opens a new SSH tunnel to it"
exit 1
}

Expand All @@ -18,17 +18,17 @@ function awsDriver() {

# Execute the appropriate command
case "$1" in
init)
init "${@:2}"
up)
up "${@:2}"
;;
delete)
delete "${@:2}"
down)
down "${@:2}"
;;
stop)
stop "${@:2}"
;;
restart)
restart "${@:2}"
start)
start "${@:2}"
;;
install)
installAwsCli "${@:2}"
Expand Down
6 changes: 3 additions & 3 deletions src/aws/load.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ source $manageConfigPath/src/utils/delete.sh
source $manageConfigPath/src/utils/callUpdate.sh
source $manageConfigPath/src/utils/driver.sh

source $manageConfigPath/src/aws/init.sh
source $manageConfigPath/src/aws/delete.sh
source $manageConfigPath/src/aws/up.sh
source $manageConfigPath/src/aws/down.sh
source $manageConfigPath/src/aws/stop.sh
source $manageConfigPath/src/aws/restart.sh
source $manageConfigPath/src/aws/start.sh
source $manageConfigPath/src/aws/installAwsCli.sh
source $manageConfigPath/src/aws/callUtils.sh
source $manageConfigPath/src/aws/driver.sh
16 changes: 14 additions & 2 deletions src/aws/restart.sh → src/aws/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#BASE=$HOME
BASE=/home/pi

function restart(){
function start(){
balloonName=$(setBalloonName "$1")

if ! isBalloonNameValid "$balloonName"; then
Expand All @@ -14,12 +14,24 @@ function restart(){
instanceId=$(getValueByAttribute $balloonName instanceId)

if [ "$instanceId" = "null" ]; then
echo "$balloonName is already deleted"
echo "$balloonName does not exist"
exit 1
fi

oldPublicIp=$(getValueByAttribute $balloonName publicIp)

state=$(getState $instanceId)

if [ "$state" == "\"running\"" ]; then
echo "The instance is already running"
exit 0
fi

if [ "$state" == "\"stopping\"" ]; then
echo "The instance is stopping"
exit 1
fi

aws ec2 start-instances --instance-ids $instanceId

echo "get the new ip address. The procedure might take time for a while"
Expand Down
8 changes: 7 additions & 1 deletion src/aws/stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ function stop(){
instanceId=$(getValueByAttribute $balloonName instanceId)

if [ "$instanceId" = "null" ]; then
echo "$balloonName is already deleted"
echo "$balloonName does not exist"
exit 1
fi

state=$(getState $instanceId)
if [[ "$state" == "\"stopped\"" || "$state" == "\"stopping\"" ]]; then
echo "The instance is already stopped"
exit 0
fi

groupName=$(getValueByAttribute $balloonName groupName)
storePortArrayString $groupName tcp $balloonName
storePortArrayString $groupName udp $balloonName
Expand Down
4 changes: 2 additions & 2 deletions src/aws/init.sh → src/aws/up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ function getValueByKeyword(){
}

function usage {
echo "script usage: $(basename \$0 aws init) [-n ssh key name] [-p] [-a change key name, instance name, and group name]" >&2
echo "script usage: $(basename \$0 aws up) [-n ssh key name] [-p] [-a change key name, instance name, and group name]" >&2
echo 'Start Luftballon.'
echo ' -n Change SSH key name on AWS'
echo ' -a Change SSH key name, instance name, and group name'
echo ' -p Use stored port Numbers instead of the default port number.'
exit 1
}

function init {
function up {
while getopts 'n:pN:a:' OPTION; do
case "$OPTION" in
n)
Expand Down
Loading