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

Add port redirection #23

Closed
wants to merge 1 commit into from
Closed
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
84 changes: 56 additions & 28 deletions remote
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,51 @@ WHITE="\e[0m"

function remote {

if [[ "$2" == '-v' ]] || [[ "$2" == '--verbose' ]]; then

VERBOSE=1

fi
while [[ $# -gt 1 ]]
do
echo $1
key="$1"

case $key in
-v|--verbose)
VERBOSE=1
shift # past argument
;;
# unknown option
*)
;;
esac
shift # past argument or value
done

#
# Start a stopped instance
#
case "$1" in

if [[ "$1" == 'start' ]]; then
"start")

INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
"Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped" | \
jq -r ".Reservations[] | .Instances[] | .InstanceId")
INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
"Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped" | \
jq -r ".Reservations[] | .Instances[] | .InstanceId")

echo -e "$ZAP Sending start signal to instance $BLUE$INSTANCE_NAME$WHITE"
echo -e "$ZAP Sending start signal to instance $BLUE$INSTANCE_NAME$WHITE"

INSTANCE_IP=$(aws ec2 describe-instances --filters \
"Name=tag:Name,Values=$INSTANCE_NAME" \
"Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped" | \
jq -r '.Reservations[] | .Instances[] | .NetworkInterfaces[] | .Association.PublicDnsName')
INSTANCE_IP=$(aws ec2 describe-instances --filters \
"Name=tag:Name,Values=$INSTANCE_NAME" \
"Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped" | \
jq -r '.Reservations[] | .Instances[] | .NetworkInterfaces[] | .Association.PublicDnsName')

aws ec2 start-instances --instance-ids $INSTANCE_ID && \
echo -e "$STRONG $GREEN Instance $BLUE$INSTANCE_NAME$GREEN started$WHITE"
aws ec2 start-instances --instance-ids $INSTANCE_ID && \
echo -e "$STRONG $GREEN Instance $BLUE$INSTANCE_NAME$GREEN started$WHITE"
;;

#
# Create an ec2 remote in your local repo
#

elif [[ "$1" == 'git' ]]; then
"down")

: "${REMOTE_PATH:?"You must set the path to the remote working repo."}"
: "${LOCAL_PATH:?"You must set the path to the local working repo."}"
Expand Down Expand Up @@ -80,13 +93,14 @@ function remote {
echo ""; \
echo " You may now push to the remote datalabs repo with:"; \
echo " git push ec2"
;;


#
# Connect to started instance
#

elif [[ "$1" == 'connect' ]]; then
"connect")

INSTANCE_IP=$(aws ec2 describe-instances --filters \
"Name=tag:Name,Values=$INSTANCE_NAME" \
Expand All @@ -112,12 +126,13 @@ function remote {
fi
sleep 10
done
;;

#
# Return the instance ID
#

elif [[ "$1" == 'id' ]]; then
"id")

INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
Expand All @@ -129,12 +144,13 @@ function remote {
else
echo -e "$INSTANCE_ID"
fi
;;

#
# Return the instance IP
#

elif [[ "$1" == 'ip' ]]; then
"ip")

INSTANCE_IP=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
Expand All @@ -147,11 +163,13 @@ function remote {
echo -e "$INSTANCE_IP"
fi

;;

#
# Get the instance status
#

elif [[ "$1" == 'status' ]]; then
"status")

echo -e "$WAIT Getting status of instance $BLUE$INSTANCE_NAME$WHITE"

Expand All @@ -163,12 +181,16 @@ function remote {
aws ec2 describe-instance-status --instance-ids $INSTANCE_ID \
--output table \
--query 'InstanceStatuses[].{InstanceId:InstanceId, AvailabilityZone:AvailabilityZone,InstanceStatus:InstanceStatus.Status,SystemStatus:SystemStatus.Status}'
;;

#
# List all datalabs instances
#

elif [[ "$1" =~ 'list' ]] || [[ "$1" =~ 'ls' ]]; then
#"list|ls")
"list")

#elif [[ "$1" =~ 'list' ]] || [[ "$1" =~ 'ls' ]]; then

: "${FILTER_PREFIX:?"You must set a prefix by which to filter instances."}"

Expand All @@ -178,12 +200,14 @@ function remote {
--filters "Name=tag:Name,Values=*$FILTER_PREFIX*" \
"Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped" \
--query 'Reservations[].Instances[].{Name:Tags[?Key==`Name`]|[0].Value, Instance:InstanceId,State:State.Name,InstanceType:InstanceType}'
#;;&
;;

#
# Change the instance type
#

elif [[ "$1" == 'type' ]]; then
"type")


if [[ -z "$2" ]]; then
Expand Down Expand Up @@ -261,12 +285,13 @@ function remote {
fi

fi
;;

#
# Shutdown the instance
#

elif [[ "$1" == 'stop' ]]; then
"stop")

echo -e "$ZAP Sending shutdown command to instance $BLUE$INSTANCE_NAME$WHITE"

Expand All @@ -276,12 +301,14 @@ function remote {
jq -r ".Reservations[] | .Instances[] | .InstanceId")

aws ec2 stop-instances --instance-ids $INSTANCE_ID
;;

#
# List instance types
#

elif [[ "$1" == 'instances' ]]; then
"instances")

if [[ "$2" ]]; then
FILTER_INSTANCES="$2"
else
Expand All @@ -291,8 +318,9 @@ function remote {
--output table \
--query "InstanceTypes[].{Instance:InstanceType,Memory:MemoryInfo.SizeInMiB,vCPUs:VCpuInfo.DefaultVCpus,GPUs:GpuInfo.Gpus[0].Count}" \
--filters "Name=instance-type,Values=$FILTER_INSTANCES"
;;

else
*)

echo -e "$BLUE $ZAP AWS EC2 instance remote control $ZAP"
echo ""
Expand Down Expand Up @@ -341,7 +369,7 @@ function remote {
echo " -v / --verbose - Commands return verbose output"
echo ""

fi
esac
}

remote $1 $2