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

Lightsail instance #125

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
9f4d91d
moved from 030 to 016
Priyankg101 Sep 6, 2023
b33d32f
changes to support 016
Priyankg101 Sep 7, 2023
59a0783
docker aws cred update
Priyankg101 Sep 7, 2023
64ad98b
move lightsail.sh
Priyankg101 Sep 7, 2023
5fb3195
fix directory issue
Priyankg101 Sep 7, 2023
e094650
Shubham | Lightsail Instance
shubhamKcloud Sep 7, 2023
3370cab
Shubham | Lightsail Instance
shubhamKcloud Sep 7, 2023
9645b9e
Shubham | Lightsail Instance
shubhamKcloud Sep 7, 2023
830831e
Shubham | Lightsail Instance
shubhamKcloud Sep 7, 2023
e33cca1
Shubham | Lightsail Instance
shubhamKcloud Sep 7, 2023
df336c4
Update lightsail.sh
shubhamKcloud Sep 8, 2023
4e6f9eb
Shubham | Lightsail Instance
shubhamKcloud Sep 12, 2023
996a9a2
port verification
Priyankg101 Sep 13, 2023
65f3285
removed root url
Priyankg101 Sep 14, 2023
4c5993f
set rooturl to localhost
Priyankg101 Sep 14, 2023
c8f9b20
domain test.tvarit.com
Priyankg101 Sep 14, 2023
74671bc
admin user
Priyankg101 Sep 26, 2023
1d66a9a
temp change in login conf
Priyankg101 Sep 27, 2023
c57c507
Removed AWS creds
Priyankg101 Oct 10, 2023
a156074
multiple fixes
Priyankg101 Oct 10, 2023
becb9eb
key switch
Priyankg101 Oct 10, 2023
7657ed3
key switch
Priyankg101 Oct 10, 2023
22cd36d
revert changes
Priyankg101 Oct 10, 2023
362ff37
userdata.sh creds
Priyankg101 Oct 11, 2023
81bee08
removed profile
Priyankg101 Oct 11, 2023
a1fd9bd
skip role sync
Priyankg101 Oct 11, 2023
8cc55ca
docker tag replace
Priyankg101 Oct 11, 2023
7dec537
fix tag
Priyankg101 Oct 11, 2023
ab3cab7
switch admin
Priyankg101 Oct 12, 2023
bbac46f
change deployment agent
Priyankg101 Oct 12, 2023
7ed3b04
removed sleeps
Priyankg101 Oct 13, 2023
72bbe50
removed instance downtime
Priyankg101 Oct 16, 2023
ba5b6b1
ignore unhealthy instance
Priyankg101 Oct 16, 2023
2a5edaf
prevent instance-name update
Priyankg101 Oct 16, 2023
9abf6db
prevent instance name update
Priyankg101 Oct 16, 2023
e3bd673
instance name bug
Priyankg101 Oct 16, 2023
6bad513
Merge branch 'lightsailInstance' of github.com:tvarit-foggy/grafana i…
Priyankg101 Oct 16, 2023
3e4e690
fix lightsail.sh
Priyankg101 Oct 16, 2023
5392d96
execute userdata during deployment
Priyankg101 Oct 16, 2023
24fb840
name swap
Priyankg101 Oct 16, 2023
8f621ee
add timestamp to names
Priyankg101 Oct 16, 2023
4244b5f
Made save message mandatory
Priyankg101 Oct 16, 2023
b133dd7
database swap
Priyankg101 Oct 16, 2023
7f077f6
database reset
Priyankg101 Oct 17, 2023
abbe54a
changed s3 to test plugins
Priyankg101 Oct 17, 2023
5e1b01b
increase instance size
Priyankg101 Oct 19, 2023
65dbf41
Disable azuread roles sync
KamalGalrani Oct 25, 2023
7fd0736
single html template for all alerts
useEffects Dec 7, 2023
644fa1f
Update defaults.ini
KamalGalrani Jul 11, 2024
dc972a5
Update defaults.ini
KamalGalrani Jul 12, 2024
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
27 changes: 27 additions & 0 deletions .github/tvarit/conf/prod/lightsail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
echo "executing user data"
sudo su
apt update
apt install docker.io -y

cd /home/ubuntu
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
apt install unzip -y
unzip awscliv2.zip
./aws/install
sleep 300
rm -rf awscliv2.zip

#with only ECR pull access. TODO: update
AWS_ACCESS_KEY_ID=<AWS_ACCESS_KEY/>
AWS_SECRET_ACCESS_KEY=<AWS_SECRET_KEY/>
AWS_REGION="eu-central-1"

aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY

docker login -u AWS -p $(aws ecr get-login-password --region eu-central-1) 250373516626.dkr.ecr.eu-central-1.amazonaws.com
docker pull 250373516626.dkr.ecr.eu-central-1.amazonaws.com/lightsailinstance:latest
#docker run -d -p 3000:3000 grafana/grafana
docker run -d -p 80:3000 250373516626.dkr.ecr.eu-central-1.amazonaws.com/lightsailinstance:latest
docker images >> test.txt
echo "user data executed"
193 changes: 193 additions & 0 deletions .github/tvarit/deploy_to_dev_16.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#!/usr/bin/env bash
set -e

PREFIX=$1
if [ -z "${PREFIX}" ]; then
echo "Usage .github/tvarit/deploy_to_dev_sk.sh <PREFIX>"
exit 1
fi

validate_lightsail_instance() {
local instance_name="$1"
aws lightsail get-instance --instance-name "$instance_name" >/dev/null 2>&1
return $?
}

delete_lightsail_instance() {
local instance_name="$1"
aws lightsail delete-instance --instance-name $instance_name
}

add_instance_to_load_balancer() {
local instance_name="$1"
local load_balancer_name="$2"
aws lightsail attach-instances-to-load-balancer --load-balancer-name "$load_balancer_name" --instance-names "$instance_name"
}

check_load_balancer_existence() {
local load_balancer_name="$1"
aws lightsail get-load-balancer --load-balancer-name "$load_balancer_name" >/dev/null 2>&1
return $?
}

create_load_balancer() {
local load_balancer_name="$1"
local instance_port="$2"
aws lightsail create-load-balancer --load-balancer-name "$load_balancer_name" --instance-port "$instance_port"
}

wait_for_instance_ready() {
local instance_name="$1"
end=$((SECONDS+600))
while [ $SECONDS -lt $end ]; do
instance_state=$(aws lightsail get-instance --instance-name "$instance_name" --query 'instance.state.name' --output text)
if [[ "$instance_state" == "running" ]]; then
return 0
fi
sleep 10
done
echo "Instance did not become ready in 10 minutes."
exit 1
}

wait_for_lb_instance_healthy() {
local load_balancer_name="$1"
local instance_name="$2"
end=$((SECONDS+600))
while [ $SECONDS -lt $end ]; do
instance_health=$(aws lightsail get-load-balancer --load-balancer-name "$load_balancer_name" --query "loadBalancer.instanceHealth[?instanceName=='$instance_name'].status" --output text)
if [[ "$instance_health" == "healthy" ]]; then
return 0
fi
sleep 10
done
echo "Instance did not become healthy on the load balancer in 10 minutes."
exit 1
}

echo "Creating Test database..."
aws lightsail create-relational-database \
--relational-database-name ${PREFIX}-grafana-db \
--availability-zone ${AWS_DEFAULT_REGION}a \
--relational-database-blueprint-id mysql_8_0 \
--relational-database-bundle-id micro_1_0 \
--preferred-backup-window 00:00-00:30 \
--preferred-maintenance-window Sun:01:00-Sun:01:30 \
--master-database-name grafana \
--master-username grafana \
--no-publicly-accessible || :

echo "Waiting for database to be available..."
for run in {1..60}; do
state=$(aws lightsail get-relational-database --relational-database-name ${PREFIX}-grafana-db --output text --query 'relationalDatabase.state')
if [ "${state}" == "available" ]; then
break
fi
echo "Waiting for database to be available..."
sleep 5
done

if [ "${state}" != "available" ]; then
echo "Database not created in 5 mins"
exit 1
fi

DB_ENDPOINT=$(aws lightsail get-relational-database --relational-database-name ${PREFIX}-grafana-db --output text --query 'relationalDatabase.masterEndpoint.address')
DB_PASSWORD=$(aws lightsail get-relational-database-master-user-password --relational-database-name ${PREFIX}-grafana-db --output text --query masterUserPassword)
SIGNING_SECRET=$(aws secretsmanager get-secret-value --secret-id grafana-signing-secret --output text --query SecretString)

#AWS-016
AWS_ACCESS_KEY=$(aws secretsmanager get-secret-value --secret-id /credentials/grafana-user/access-key --output text --query SecretString)
AWS_SECRET_KEY=$(aws secretsmanager get-secret-value --secret-id /credentials/grafana-user/secret-key --output text --query SecretString)

echo "Building docker image..."
docker build --tag grafana/grafana:latest .

cd .github/tvarit/conf/prod/
echo "Downloading plugins..."
rm -rf plugins
aws s3 sync s3://com.tvarit.grafana.artifacts/grafana-test-plugins plugins
find plugins/ -type f -name *.tar.gz -exec bash -c 'cd $(dirname $1) && tar -xf $(basename $1) && rm $(basename $1); cd -' bash {} \;

echo "Finalising docker image..."
cp grafana.ini.template grafana.ini
sed -i "s#<DOMAIN/>#${PREFIX}.tvarit.com#g" grafana.ini
sed -i "s#<ROOT_URL/>#https://${PREFIX}.tvarit.com/#g" grafana.ini
sed -i "s#<SIGNING_SECRET/>#${SIGNING_SECRET}#g" grafana.ini
sed -i "s#<DB_ENDPOINT/>#${DB_ENDPOINT}#g" grafana.ini
sed -i "s#<DB_PASSWORD/>#$(echo ${DB_PASSWORD} | sed 's/#/\\#/g' | sed 's/&/\\&/g')#g" grafana.ini
sed -i "s#<OAUTH_CLIENT_ID/>#${OAUTH_CLIENT_ID}#g" grafana.ini
sed -i "s#<OAUTH_CLIENT_SECRET/>#${OAUTH_CLIENT_SECRET}#g" grafana.ini
sed -i "s#<SMTP_HOST/>#${SMTP_HOST}#g" grafana.ini
sed -i "s#<SMTP_USER/>#${SMTP_USER}#g" grafana.ini
sed -i "s#<SMTP_PASSWORD/>#${SMTP_PASSWORD}#g" grafana.ini
sed -i "s#<SMTP_FROM/>#[BETA] Tvarit AI Platform#g" grafana.ini

cp cloudwatch.json.template cloudwatch.json
sed -i "s#<DOMAIN/>#${PREFIX}.tvarit.com#g" cloudwatch.json

cp Dockerfile.template Dockerfile
sed -i "s#<BASE_IMAGE/>#grafana/grafana:latest#g" Dockerfile
sed -i "s#<AWS_ACCESS_KEY/>#${AWS_ACCESS_KEY}#g" Dockerfile
sed -i "s#<AWS_SECRET_KEY/>#${AWS_SECRET_KEY}#g" Dockerfile
sed -i "s#<AWS_REGION/>#${AWS_DEFAULT_REGION}#g" Dockerfile
docker build --tag grafana/grafana:latest .

#push Docker image to ECR
echo "push docker image to ECR........."
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin 250373516626.dkr.ecr.eu-central-1.amazonaws.com
docker tag grafana/grafana:latest 250373516626.dkr.ecr.eu-central-1.amazonaws.com/lightsailinstance:latest
docker push 250373516626.dkr.ecr.eu-central-1.amazonaws.com/lightsailinstance:latest


# Fetch the name of the only Lightsail instance
instance_name=$(aws lightsail get-instances --query "instances[0].name" --output text)

if [ -z "$instance_name" ]; then
echo "No Lightsail instances found."
exit 1
fi

timestamp=$(date +%Y%m%d%H%M%S)

new_instance_name="grafana-${PREFIX}-$timestamp"

# Check if previous instance exists
return_value_instance=$(validate_lightsail_instance $instance_name)
if [ $? -eq 0 ]; then
previous_instance_exists=true
else
previous_instance_exists=false
fi

echo "Creating lightsail instance!!!!!!"
cp lightsail.sh userdata.sh
sed -i "s#<AWS_ACCESS_KEY/>#${AWS_ACCESS_KEY_ID_016}#g" userdata.sh
sed -i "s#<AWS_SECRET_KEY/>#${AWS_SECRET_KEY_ID_016}#g" userdata.sh

# Create a new Lightsail instance with a different name
aws lightsail create-instances --instance-names $new_instance_name --availability-zone eu-central-1a --blueprint-id ubuntu_22_04 --bundle-id small_3_0 --user-data file://userdata.sh
sleep 600
# Wait for the new instance to be ready
wait_for_instance_ready $new_instance_name

# Open necessary ports on the instance
aws lightsail open-instance-public-ports --port-info fromPort=3000,toPort=3000,protocol=TCP --instance-name $new_instance_name

# Attach the new instance to the load balancer
add_instance_to_load_balancer $new_instance_name grafana-lb

# Wait for the new instance to be healthy on the load balancer
# wait_for_lb_instance_healthy grafana-lb $new_instance_name
sleep 90

# If a previous instance existed, detach it from the LB and then delete it
if [ "$previous_instance_exists" == "true" ]; then
aws lightsail detach-instances-from-load-balancer --load-balancer-name grafana-lb --instance-names $instance_name
delete_lightsail_instance $instance_name
fi

# Rename the new instance to the original instance name
# aws lightsail update-instance-name --instance-name $new_instance_name --new-instance-name $instance_name

echo "Deployment completed!"
Loading
Loading