-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github: Refactor Lightsail GH Actions
- Loading branch information
Showing
2 changed files
with
244 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
name: "Lightsail service management" | ||
run-name: "For subdomain ${{inputs.subdomain}}: ${{inputs.command}}" | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
subdomain: | ||
description: 'Subdomain of navalabs.co on which to run command' | ||
required: true | ||
type: choice | ||
default: '' | ||
options: | ||
- '' | ||
- 'chat' | ||
- 'chatbot' | ||
- 'chatbdt' | ||
- 'chat-bdt' | ||
- 'bdtbot' | ||
- 'bdt-bot' | ||
- 'bdt-chat' | ||
- 'bdt-chatbot' | ||
- 'chatbot-prototype' | ||
- 'chat.zone' | ||
command: | ||
description: "Command to perform on Lightsail service" | ||
required: true | ||
type: choice | ||
default: 'status' | ||
options: | ||
- 'status' | ||
- 'list_images' | ||
- 'delete_old_images' | ||
- 'enable' | ||
- 'disable' | ||
- 'update_power' | ||
- 'create_new' | ||
- 'delete_service' | ||
power: | ||
description: "Only for update_power command: power of service (useful for deployment failures)" | ||
required: true | ||
type: choice | ||
default: 'micro' | ||
options: | ||
- nano | ||
- micro | ||
- small | ||
- medium | ||
- large | ||
- xlarge | ||
|
||
env: | ||
AWS_REGION: us-east-1 | ||
SERVICE_NAME: ${{ inputs.subdomain }}-svc | ||
DOMAIN_NAME: navalabs.co | ||
FULL_DOMAIN: ${{ inputs.subdomain }}.navalabs.co | ||
|
||
jobs: | ||
lightsail: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Configure AWS credentials" | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
aws-region: ${{ env.AWS_REGION }} | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
mask-aws-account-id: true | ||
# TODO: secure credentials: https://github.com/aws-actions/amazon-ecr-login?tab=readme-ov-file#ecr-private | ||
# https://github.com/docker/login-action?tab=readme-ov-file#aws-elastic-container-registry-ecr | ||
# https://medium.com/@lukhee/automating-aws-lightsail-deployments-with-github-actions-53c73c9a1c1f | ||
|
||
- name: "Setup AWS lightsail command" | ||
run: | | ||
# Uncomment the following lines if you need to upgrade the AWS CLI version | ||
# aws --version | ||
# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | ||
# unzip awscliv2.zip | ||
# sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update | ||
# which aws | ||
aws --version | ||
sudo curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "/usr/local/bin/lightsailctl" | ||
sudo chmod +x /usr/local/bin/lightsailctl | ||
- name: "Print status" | ||
if: inputs.command == 'status' | ||
run: | | ||
aws lightsail get-container-services | jq '.containerServices[] | { containerServiceName, createdAt, state, isDisabled, | ||
"deployment_state": .currentDeployment.state, | ||
"container_image": .currentDeployment.containers.chatbot.image, | ||
"container_BUILD_DATE": .currentDeployment.containers.chatbot.environment.BUILD_DATE, | ||
"publicDomainNames": .publicDomainNames["navalabs-cert"] }' | ||
- name: "Enable service" | ||
if: inputs.command == 'enable' | ||
run: | | ||
aws lightsail update-container-service --service-name "$SERVICE_NAME" --no-is-disabled | ||
echo "Waiting for service to be ready" | ||
while true; do | ||
sleep 5 | ||
SVC_STATE=$(aws lightsail get-container-services --service-name "$SERVICE_NAME" | jq -r '.containerServices[0].state') | ||
echo "service state: $SVC_STATE" | ||
if [ "$SVC_STATE" == "READY" ]; then | ||
break | ||
fi | ||
done | ||
- name: "Disable service" | ||
if: inputs.command == 'disable' | ||
run: | | ||
aws lightsail update-container-service --service-name "$SERVICE_NAME" --is-disabled | ||
|
||
- name: "Update the power of the service to ${{inputs.power}}" | ||
if: inputs.command == 'update_power' | ||
run: | | ||
aws lightsail update-container-service --service-name "$SERVICE_NAME" --power ${{inputs.power}} | ||
echo "Waiting for service to be ready" | ||
while true; do | ||
sleep 5 | ||
SVC_STATE=$(aws lightsail get-container-services --service-name "$SERVICE_NAME" | jq -r '.containerServices[0].state') | ||
echo "service state: $SVC_STATE" | ||
if [ "$SVC_STATE" == "READY" ]; then | ||
break | ||
fi | ||
done | ||
- name: "List images associated with service" | ||
if: inputs.command == 'list_images' | ||
run: | | ||
aws lightsail get-container-images --service-name "$SERVICE_NAME" | ||
- name: "Delete previous images" | ||
if: inputs.command == 'delete_old_images' | ||
run: | | ||
IMAGE_NAMES=$(aws lightsail get-container-images --service-name "$SERVICE_NAME" | jq -r '.containerImages[].image') | ||
# Skip the first image, which is the current image | ||
OLD_IMAGE_NAMES=$(echo $IMAGE_NAMES | tail -n +2) | ||
echo "$OLD_IMAGE_NAMES" | while read IMG_NAME; do | ||
if [ "$IMG_NAME" == "" ]; then | ||
continue | ||
fi | ||
echo "Deleting image $IMG_NAME" | ||
echo aws lightsail delete-container-image --service-name "$SERVICE_NAME" --image $IMG_NAME; | ||
done | ||
echo "Images:" | ||
aws lightsail get-container-images --service-name "$SERVICE_NAME" | ||
- name: "Create new container service" | ||
if: inputs.command == 'create_new' | ||
run: | | ||
# check if service already exists | ||
if aws lightsail get-container-services --service-name "$SERVICE_NAME" > /dev/null; then | ||
echo "Already exists: $SERVICE_NAME" | ||
exit 0 | ||
fi | ||
# `micro` power is needed for it's memory capacity; 60%+ memory is needed for the vector DB | ||
aws lightsail create-container-service --service-name $SERVICE_NAME --power micro --scale 1 --public-domain-names navalabs-cert=$FULL_DOMAIN | ||
echo "Waiting for service to be ready to get URL for next step" | ||
while true; do | ||
sleep 15 | ||
SVC_STATE=$(aws lightsail get-container-services --service-name "$SERVICE_NAME" | jq -r '.containerServices[0].state') | ||
echo "service state: $SVC_STATE" | ||
if [ "$SVC_STATE" == "READY" ]; then | ||
break | ||
fi | ||
done | ||
SVC_URL=$(aws lightsail get-container-services --service-name "$SERVICE_NAME" | jq -r '.containerServices[0].url') | ||
# Remove 'https://' prefix | ||
URL_DOMAIN=${SVC_URL#https://} | ||
# Remove '/' suffix | ||
TARGET_DOMAIN=${URL_DOMAIN%/} | ||
# If domain entry exists, delete it | ||
OLD_TARGET=$(aws lightsail get-domain --domain-name $DOMAIN_NAME | jq -r ".domain.domainEntries[] | select( .name == \"$FULL_DOMAIN\" ) | .target") | ||
if [ "$OLD_TARGET" ] ; then | ||
echo "Deleting existing '$FULL_DOMAIN' entry with target '$OLD_TARGET'" | ||
aws lightsail delete-domain-entry --domain-name $DOMAIN_NAME --domain-entry "type=A,isAlias=true,name=$FULL_DOMAIN,target=$OLD_TARGET" | ||
fi | ||
echo "Creating DNS assignment by adding a domain entry $FULL_DOMAIN to target $TARGET_DOMAIN" | ||
aws lightsail create-domain-entry --domain-name $DOMAIN_NAME --domain-entry "type=A,isAlias=true,name=$FULL_DOMAIN,target=$TARGET_DOMAIN" | ||
- name: "Delete container service" | ||
if: inputs.command == 'delete_service' | ||
run: | | ||
# check if service exists | ||
if ! aws lightsail get-container-services --service-name "$SERVICE_NAME"; then | ||
echo "Service does not exist: $SERVICE_NAME" | ||
exit 0 | ||
fi | ||
aws lightsail delete-container-service --service-name "$SERVICE_NAME" |
Oops, something went wrong.