mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-10 23:11:01 +00:00
Merge pull request #1786 from molecula/fixBugInCicdPipeline
remove duplicate code in multiple stages of gitlab yml for CICD
This commit is contained in:
commit
42f3b79640
2 changed files with 99 additions and 66 deletions
|
|
@ -196,54 +196,13 @@ build container fb:
|
|||
- docker push $tag
|
||||
- echo Created docker featurebase image with tag "$tag"
|
||||
|
||||
# deploy EC2 instance and save instance ID
|
||||
# deploy EC2 instance, configure and run featurebase
|
||||
deploy node for linux amd64:
|
||||
stage: integration
|
||||
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
|
||||
variables:
|
||||
AMI: ""
|
||||
INSTANCE: "t3a.large"
|
||||
SECURITY_GROUP: ""
|
||||
SUBNET_ID: ""
|
||||
PROFILE: "default"
|
||||
AWS_SSH_PRIVATE_KEY: $AWS_SSH_PRIVATE_KEY
|
||||
before_script:
|
||||
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
|
||||
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
|
||||
- aws configure set region "us-east-2"
|
||||
- aws configure set aws_profile $PROFILE
|
||||
- echo $AWS_SSH_PRIVATE_KEY > gitlab-featurebase-dev.pem
|
||||
- chmod 400 gitlab-featurebase-dev.pem
|
||||
script:
|
||||
- AMI=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-ebs --query 'Parameters[0].[Value]' --output text --profile $PROFILE)
|
||||
- SECURITY_GROUP=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values=vpc-03a4ba3d5b7c8f978 Name=group-name,Values=default --query 'SecurityGroups[*].[GroupId]' --output text --profile $PROFILE)
|
||||
- SUBNET_ID=$(aws ec2 describe-subnets --filters 'Name=vpc-id,Values=vpc-03a4ba3d5b7c8f978' 'Name=availability-zone,Values=us-east-2a' --query 'Subnets[0].SubnetId' --output text --profile $PROFILE)
|
||||
- aws ec2 run-instances --image-id $AMI --instance-type $INSTANCE --security-group-ids $SECURITY_GROUP --subnet-id $SUBNET_ID --key-name gitlab-featurebase-dev --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=linux-amd64-node}]' --profile $PROFILE --user-data file://./qa/scripts/cloud-init.sh --iam-instance-profile Name=featurebase-dev-ssm > config.json
|
||||
- INSTANCE_ID=$(jq '.Instances | .[] |.InstanceId' config.json | tr -d '"')
|
||||
- echo $INSTANCE_ID > linux-amd64-instance.txt
|
||||
- sleep 120 # Need to wait for the EC2 instance to launch and run the initialization commands passed through user-data
|
||||
- PUBLIC_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --filters 'Name=instance-state-name, Values=running' --query 'Reservations[*].Instances[*].PublicIpAddress' --output text --profile $PROFILE)
|
||||
- echo $PUBLIC_IP
|
||||
- scp -o StrictHostKeyChecking=no -i gitlab-featurebase-dev.pem featurebase_linux_amd64 ec2-user@$PUBLIC_IP:.
|
||||
- scp -o StrictHostKeyChecking=no -i gitlab-featurebase-dev.pem ./qa/scripts/featurebase.conf ./qa/scripts/featurebase.service ec2-user@$PUBLIC_IP:.
|
||||
- aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $INSTANCE_ID --cli-input-json file://./qa/scripts/configureFeatureBase.json --profile $PROFILE --region us-east-2
|
||||
needs:
|
||||
- job: build for linux amd64
|
||||
artifacts:
|
||||
paths:
|
||||
- linux-amd64-instance.txt
|
||||
needs:
|
||||
- job: build for linux amd64
|
||||
|
||||
# scp featurebase binary, config and service file, and configure featurebase
|
||||
initiate featurebase linux amd64 in node:
|
||||
stage: integration
|
||||
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
|
||||
variables:
|
||||
IP: ""
|
||||
PROFILE: "default"
|
||||
AWS_SSH_PRIVATE_KEY: $AWS_SSH_PRIVATE_KEY
|
||||
INSTANCE_ID: ""
|
||||
before_script:
|
||||
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
|
||||
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
|
||||
|
|
@ -253,33 +212,12 @@ initiate featurebase linux amd64 in node:
|
|||
- chmod 400 gitlab-featurebase-dev.pem
|
||||
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
|
||||
- eval `ssh-agent -s`
|
||||
- mkdir -p ~/.ssh
|
||||
- mkdir -p ~/.ssh
|
||||
- echo "$AWS_SSH_PRIVATE_KEY" | ssh-add -
|
||||
- chmod 700 /root/.ssh
|
||||
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
|
||||
- INSTANCE_ID=$(cat linux-amd64-instance.txt)
|
||||
- apt update && apt -y install jq
|
||||
script:
|
||||
- for i in {0..24}; do IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --filters 'Name=instance-state-name, Values=running' --query 'Reservations[*].Instances[*].PublicIpAddress' --output text --profile $PROFILE); if [ -n "$IP" ]; then break; fi; sleep 5; done
|
||||
- scp -o StrictHostKeyChecking=no -i gitlab-featurebase-dev.pem featurebase_linux_amd64 .gitlab/featurebase.conf .gitlab/featurebase.service ec2-user@$IP:.
|
||||
- aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $INSTANCE_ID --cli-input-json file://.gitlab/configureFeatureBase.json --profile $PROFILE --region us-east-2
|
||||
- ./qa/scripts/deployNode.sh $PROFILE
|
||||
needs:
|
||||
- job: build for linux amd64
|
||||
- job: deploy node for linux amd64
|
||||
|
||||
# terminate EC2 instance
|
||||
terminate node for linux amd64:
|
||||
stage: integration
|
||||
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
|
||||
variables:
|
||||
INSTANCE_ID: ""
|
||||
before_script:
|
||||
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
|
||||
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
|
||||
- aws configure set region "us-east-2"
|
||||
- INSTANCE_ID=$(cat linux-amd64-instance.txt)
|
||||
script:
|
||||
- aws ec2 terminate-instances --instance-ids $INSTANCE_ID
|
||||
needs:
|
||||
- job: deploy node for linux amd64
|
||||
- job: initiate featurebase linux amd64 in node
|
||||
when: always
|
||||
|
|
|
|||
95
qa/scripts/deployNode.sh
Executable file
95
qa/scripts/deployNode.sh
Executable file
|
|
@ -0,0 +1,95 @@
|
|||
#!/bin/bash
|
||||
|
||||
# To run script: ./deployNode.sh $PROFILE
|
||||
|
||||
function deploy_node() {
|
||||
# get AMI, security group and subnet ID
|
||||
AMI=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-ebs --query 'Parameters[0].[Value]' --output text --profile $PROFILE)
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "aws session manager failed to find AMI"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SECURITY_GROUP=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values=vpc-03a4ba3d5b7c8f978 Name=group-name,Values=default --query 'SecurityGroups[*].[GroupId]' --output text --profile $PROFILE)
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "aws session manager failed to find security group"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SUBNET_ID=$(aws ec2 describe-subnets --filters 'Name=vpc-id,Values=vpc-03a4ba3d5b7c8f978' 'Name=availability-zone,Values=us-east-2a' --query 'Subnets[0].SubnetId' --output text --profile $PROFILE)
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "aws session manager failed to find subnet ID"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# launch EC2 instance and get instance ID
|
||||
aws ec2 run-instances --image-id $AMI --instance-type $INSTANCE --security-group-ids $SECURITY_GROUP --subnet-id $SUBNET_ID --key-name gitlab-featurebase-dev --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=linux-amd64-node}]' --profile $PROFILE --user-data file://./qa/scripts/cloud-init.sh --iam-instance-profile Name=featurebase-dev-ssm > config.json
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "aws run-instances failed to launch a new EC2 instance"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
INSTANCE_ID=$(jq '.Instances | .[0] |.InstanceId' config.json | tr -d '"')
|
||||
echo "aws run-instances succeeded in launching a new EC2 instance with instance ID: " $INSTANCE_ID
|
||||
}
|
||||
|
||||
function initialize_featurebase() {
|
||||
# get IP for node
|
||||
for i in {0..24}
|
||||
do
|
||||
IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --filters 'Name=instance-state-name, Values=running' --query 'Reservations[*].Instances[*].PublicIpAddress' --output text --profile $PROFILE)
|
||||
if [ -n "$IP" ]; then
|
||||
echo "Public IP for EC2 instance found: " $IP
|
||||
break
|
||||
fi
|
||||
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "aws cli describe-instances command failed to find public IP"
|
||||
terminate_node
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sleep 5
|
||||
done
|
||||
|
||||
sleep 60 # to allow enough time for node to be ready for use
|
||||
|
||||
# copy featurebase binary and files to ec2 instance
|
||||
scp -o StrictHostKeyChecking=no -i gitlab-featurebase-dev.pem featurebase_linux_amd64 ./qa/scripts/featurebase.conf ./qa/scripts/featurebase.service ec2-user@$IP:.
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "scp of featurebase binary, service and config files to EC2 instance failed"
|
||||
terminate_node
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# execute script to configure featurebase on the EC2 node
|
||||
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $INSTANCE_ID --cli-input-json file://./qa/scripts/configureFeatureBase.json --profile $PROFILE --region $REGION
|
||||
if [[ $? > 0 ]]; then
|
||||
echo "aws cli session manager send-command failed"
|
||||
terminate_node
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
function terminate_node() {
|
||||
aws ec2 terminate-instances --instance-ids $INSTANCE_ID --profile $PROFILE
|
||||
}
|
||||
|
||||
# Pass variables to shell script
|
||||
PROFILE=$1
|
||||
shift
|
||||
|
||||
# set some variables
|
||||
INSTANCE="t3a.large"
|
||||
REGION="us-east-2"
|
||||
|
||||
# get AMI, security group and subnet for EC2 instance,
|
||||
# launch instance, save instance Id and run cloud-init to set up node env
|
||||
deploy_node
|
||||
|
||||
# Get IP for instance, scp featurebase binary, config and service files;
|
||||
# set up featurebase config in node
|
||||
initialize_featurebase
|
||||
|
||||
# terminate node
|
||||
terminate_node
|
||||
Loading…
Add table
Reference in a new issue