blob: 00b43e82592c9fdd62d2957fd3d8f2b2045d5a3b [file] [log] [blame]
SHELL := /bin/bash
ROOT_DIR=$(dir $(realpath $(filter %Makefile.common,$(MAKEFILE_LIST))))
AWS_FC_COMMAND=export AWS_PAGER=;aws cloudformation
include $(ROOT_DIR)/operations/export-logs/Makefile
include $(ROOT_DIR)common.env
cluster-keys:
aws ec2 describe-key-pairs --region $(AWS_REGION) --key-names $(CLUSTER_KEYS) > /dev/null 2>&1 || \
aws ec2 create-key-pair --region $(AWS_REGION) --key-name $(CLUSTER_KEYS) --query 'KeyMaterial' --output text > $(CLUSTER_KEYS).pem
upload-common-templates:
$(eval CREATE_BUCKET_PARAMS := --bucket $(TEMPLATE_BUCKET_NAME))
ifneq ("$(AWS_REGION)", "us-east-1")
$(eval CREATE_BUCKET_PARAMS := $(CREATE_BUCKET_PARAMS) --create-bucket-configuration LocationConstraint=$(AWS_REGION))
endif
export AWS_PAGER=; aws s3api head-bucket --bucket $(TEMPLATE_BUCKET_NAME) 2>/dev/null || \
aws s3api create-bucket $(CREATE_BUCKET_PARAMS)
aws s3 cp ../common-templates/cf-gerrit-task-execution-role.yml s3://$(TEMPLATE_BUCKET_NAME)/
aws s3 cp ../common-templates/cf-gerrit-network-stack.yml s3://$(TEMPLATE_BUCKET_NAME)/
aws s3 cp ../common-templates/cf-gerrit-volume.yml s3://$(TEMPLATE_BUCKET_NAME)/
aws s3 cp ../common-templates/cf-primary-asg.yml s3://$(TEMPLATE_BUCKET_NAME)/
aws s3 cp ../common-templates/cf-efs-stack.yml s3://$(TEMPLATE_BUCKET_NAME)/
aws s3 cp ../common-templates/cf-ecs-service-cpu-autoscaling.yml s3://$(TEMPLATE_BUCKET_NAME)/
aws s3 cp ../common-templates/cf-dynamodb-stack.yml s3://$(TEMPLATE_BUCKET_NAME)/
set-optional-params-metrics-cloudwatch:
ifdef METRICS_CLOUDWATCH_ENABLED
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchEnabled,ParameterValue=$(METRICS_CLOUDWATCH_ENABLED))
endif
ifdef METRICS_CLOUDWATCH_NAMESPACE
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchNamespace,ParameterValue=$(METRICS_CLOUDWATCH_NAMESPACE))
endif
ifdef METRICS_CLOUDWATCH_RATE
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchRate,ParameterValue=$(METRICS_CLOUDWATCH_RATE))
endif
ifdef METRICS_CLOUDWATCH_INITIAL_DELAY
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchInitialDelay,ParameterValue=$(METRICS_CLOUDWATCH_INITIAL_DELAY))
endif
ifdef METRICS_CLOUDWATCH_JVM_ENABLED
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchJVMEnabled,ParameterValue=$(METRICS_CLOUDWATCH_JVM_ENABLED))
endif
ifdef METRICS_CLOUDWATCH_DRY_RUN
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchDryRun,ParameterValue=$(METRICS_CLOUDWATCH_DRY_RUN))
endif
ifdef METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST
$(eval METRICS_CW_OPTIONAL_PARAMS := $(METRICS_CW_OPTIONAL_PARAMS) ParameterKey=MetricsCloudwatchExcludeMetrics,ParameterValue=\"$(METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST)\")
endif
set-optional-params-smtp:
ifdef SMTP_SERVER_PORT
$(eval SMTP_OPTIONAL_PARAMS := $(SMTP_OPTIONAL_PARAMS) ParameterKey=SMTPServerPort,ParameterValue=$(SMTP_SERVER_PORT))
endif
ifdef SMTP_ENCRYPTION
$(eval SMTP_OPTIONAL_PARAMS := $(SMTP_OPTIONAL_PARAMS) ParameterKey=SMTPEncryption,ParameterValue=$(SMTP_ENCRYPTION))
endif
ifdef SMTP_SSL_VERIFY
$(eval SMTP_OPTIONAL_PARAMS := $(SMTP_OPTIONAL_PARAMS) ParameterKey=SMTPSslVerify,ParameterValue=$(SMTP_SSL_VERIFY))
endif
set-optional-params-multisite:
ifdef MULTISITE_ENABLED
$(eval MULTISITE_OPTIONAL_PARAMS := ParameterKey=MultiSiteEnabled,ParameterValue=$(MULTISITE_ENABLED))
$(eval MULTISITE_OPTIONAL_PARAMS := $(MULTISITE_OPTIONAL_PARAMS) ParameterKey=MultiSiteKafkaBrokers,ParameterValue=\"$(MULTISITE_KAFKA_BROKERS)\")
endif
set-ldap-account-pattern:
ifdef LDAP_ACCOUNT_PATTERN
$(eval LDAP_ACCOUNT_PATTERN_PARAM := ParameterKey=LDAPAccountPattern,ParameterValue=\"$(LDAP_ACCOUNT_PATTERN)\")
endif
set-optional-gerrit-ulimits:
ifdef GERRIT_CONTAINER_FDS_SOFT_LIMIT
$(eval GERRIT_ULIMITS := ParameterKey=FileDescriptorsSoftLimit,ParameterValue=$(GERRIT_CONTAINER_FDS_SOFT_LIMIT))
endif
ifdef GERRIT_CONTAINER_FDS_HARD_LIMIT
$(eval GERRIT_ULIMITS := $(GERRIT_ULIMITS) ParameterKey=FileDescriptorsHardLimit,ParameterValue=$(GERRIT_CONTAINER_FDS_HARD_LIMIT))
endif
set-optional-jgit-conf:
ifdef JGIT_OPEN_FILES
$(eval JGIT_OPTIONAL_PARAMS := ParameterKey=JgitOpenFiles,ParameterValue=$(JGIT_OPEN_FILES))
endif
set-optional-gerrit-primary-volume:
$(eval GERRIT_OPTIONAL_PRIMARY_VOLUME=)
ifdef GERRIT_VOLUME_ID
$(eval GERRIT_OPTIONAL_PRIMARY_VOLUME := $(GERRIT_OPTIONAL_PRIMARY_VOLUME) ParameterKey=GerritVolumeId,ParameterValue=$(GERRIT_VOLUME_ID))
endif
ifdef GERRIT_VOLUME_SNAPSHOT_ID
$(eval GERRIT_OPTIONAL_PRIMARY_VOLUME := $(GERRIT_OPTIONAL_PRIMARY_VOLUME) ParameterKey=GerritVolumeSnapshotId,ParameterValue=$(GERRIT_VOLUME_SNAPSHOT_ID))
endif
ifdef GERRIT_VOLUME_SIZE_IN_GIB
$(eval GERRIT_OPTIONAL_PRIMARY_VOLUME := $(GERRIT_OPTIONAL_PRIMARY_VOLUME) ParameterKey=GerritVolumeSizeInGiB,ParameterValue=$(GERRIT_VOLUME_SIZE_IN_GIB))
endif
set-optional-params-for-replica-filesystem:
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM=)
ifdef REPLICA_FILESYSTEM_ID
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM := $(GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM) ParameterKey=ReplicaFileSystemID,ParameterValue=$(REPLICA_FILESYSTEM_ID))
endif
ifdef REPLICA_FILESYSTEM_THROUGHPUT_MODE
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM := $(GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM) ParameterKey=ReplicaFileSystemThroughputMode,ParameterValue=$(REPLICA_FILESYSTEM_THROUGHPUT_MODE))
endif
ifdef REPLICA_FILESYSTEM_PROVISIONED_THROUGHPUT_IN_MIBPS
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM := $(GERRIT_OPTIONAL_PARAMS_REPLICA_FILESYSTEM) ParameterKey=ReplicaProvisionedThroughputInMibps,ParameterValue=$(REPLICA_FILESYSTEM_PROVISIONED_THROUGHPUT_IN_MIBPS))
endif
set-optional-params-for-replica-auto-scaling-capacity:
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY=)
ifdef REPLICA_AUTOSCALING_MIN_CAPACITY
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY := $(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY) ParameterKey=ReplicaAutoScalingMinCapacity,ParameterValue=$(REPLICA_AUTOSCALING_MIN_CAPACITY))
endif
ifdef REPLICA_AUTOSCALING_DESIRED_CAPACITY
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY := $(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY) ParameterKey=ReplicaAutoScalingDesiredCapacity,ParameterValue=$(REPLICA_AUTOSCALING_DESIRED_CAPACITY))
endif
ifdef REPLICA_AUTOSCALING_MAX_CAPACITY
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY := $(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY) ParameterKey=ReplicaAutoScalingMaxCapacity,ParameterValue=$(REPLICA_AUTOSCALING_MAX_CAPACITY))
endif
set-optional-params-for-replica-auto-scaling-policy:
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY=)
ifdef REPLICA_AUTOSCALING_SCALE_IN_COOLDOWN
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY := $(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY) ParameterKey=ReplicaAutoScalingScaleInCooldown,ParameterValue=$(REPLICA_AUTOSCALING_SCALE_IN_COOLDOWN))
endif
ifdef REPLICA_AUTOSCALING_SCALE_OUT_COOLDOWN
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY := $(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY) ParameterKey=ReplicaAutoScalingScaleOutCooldown,ParameterValue=$(REPLICA_AUTOSCALING_SCALE_OUT_COOLDOWN))
endif
ifdef REPLICA_AUTOSCALING_TARGET_CPU_PERCENTAGE
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY := $(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY) ParameterKey=ReplicaAutoScalingTargetCPUPercentage,ParameterValue=$(REPLICA_AUTOSCALING_TARGET_CPU_PERCENTAGE))
endif
set-optional-params-for-replica-capacity-provider:
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER=)
ifdef REPLICA_CAPACITY_PROVIDER_TARGET
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER := $(GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER) ParameterKey=ReplicaCapacityProviderTarget,ParameterValue=$(REPLICA_CAPACITY_PROVIDER_TARGET))
endif
ifdef REPLICA_CAPACITY_PROVIDER_MIN_STEP_SIZE
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER := $(GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER) ParameterKey=ReplicaCapacityProviderMinStepSize,ParameterValue=$(REPLICA_CAPACITY_PROVIDER_MIN_STEP_SIZE))
endif
ifdef REPLICA_CAPACITY_PROVIDER_MAX_STEP_SIZE
$(eval GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER := $(GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER) ParameterKey=ReplicaCapacityProviderMaxStepSize,ParameterValue=$(REPLICA_CAPACITY_PROVIDER_MAX_STEP_SIZE))
endif
set-optional-network-params:
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK=)
ifdef INTERNET_GATEWAY_ID
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID))
endif
ifdef VPC_ID
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=VPCIdProp,ParameterValue=$(VPC_ID))
endif
ifdef VPC_CIDR
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=VPCCIDR,ParameterValue=$(VPC_CIDR))
endif
ifdef SUBNET1_CIDR
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=Subnet1CIDR,ParameterValue=$(SUBNET1_CIDR))
endif
ifdef SUBNET1_ID
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=Subnet1IdProp,ParameterValue=$(SUBNET1_ID))
endif
ifdef SUBNET1_AZ
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=Subnet1AZProp,ParameterValue=$(SUBNET1_AZ))
endif
ifdef SUBNET2_CIDR
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=Subnet2CIDR,ParameterValue=$(SUBNET2_CIDR))
endif
ifdef SUBNET2_ID
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=Subnet2IdProp,ParameterValue=$(SUBNET2_ID))
endif
ifdef SUBNET2_AZ
$(eval GERRIT_OPTIONAL_PARAMS_NETWORK := $(GERRIT_OPTIONAL_PARAMS_NETWORK) ParameterKey=Subnet2AZProp,ParameterValue=$(SUBNET2_AZ))
endif
set-optional-x-ray:
$(eval XRAY_OPTIONAL_PARAMS=)
ifdef XRAY_ENABLED
$(eval XRAY_OPTIONAL_PARAMS := $(XRAY_OPTIONAL_PARAMS) ParameterKey=EnableXray,ParameterValue=$(XRAY_ENABLED))
endif
confirm-persistent-stack-deletion:
@echo ""
@echo "* * * * WARNING * * * * this is going to completely destroy the stack, including git data."
@echo ""
@echo -n "Are you sure you want to continue? [y/N] " && read ans && [ $${ans:-N} = y ]
delete-network-persistent-stack:
$(eval NETWORK_STACK_NAME=$(shell $(AWS_FC_COMMAND) list-stacks --stack-status-filter CREATE_COMPLETE --query "StackSummaries[*].StackName" | jq -r '.[]| select(startswith("$(CLUSTER_STACK_NAME)-ECSTaskNetworkStack"))'))
$(if $(NETWORK_STACK_NAME), \
$(AWS_FC_COMMAND) delete-stack \
--stack-name $(NETWORK_STACK_NAME) \
--region $(AWS_REGION) && \
echo "*** Wait for Network stack '$(NETWORK_STACK_NAME)' deletion" && \
$(AWS_FC_COMMAND) wait stack-delete-complete \
--stack-name $(NETWORK_STACK_NAME) \
--region $(AWS_REGION) && \
echo "*** Network stack '$(NETWORK_STACK_NAME)' deleted" \
, \
echo "No network stack found. Nothing to do." \
)
delete-git-replica-persistent-stack:
$(eval REPLICA_EFS_STACK_NAME := $(shell $(AWS_FC_COMMAND) list-stacks --stack-status-filter CREATE_COMPLETE --query "StackSummaries[*].StackName" | jq -r '.[]| select(startswith("$(CLUSTER_STACK_NAME)-ReplicaGitFileSystemPermanentStack"))'))
$(if $(REPLICA_EFS_STACK_NAME), \
$(AWS_FC_COMMAND) delete-stack \
--stack-name $(REPLICA_EFS_STACK_NAME) \
--region $(AWS_REGION) && \
echo "*** Wait for Git persistent stack of replicas '$(REPLICA_EFS_STACK_NAME)' deletion" && \
$(AWS_FC_COMMAND) wait stack-delete-complete \
--stack-name $(REPLICA_EFS_STACK_NAME) \
--region $(AWS_REGION) && \
echo "*** Git persistent stack '$(EFSREPLICA_EFS_STACK_NAME_STACK_NAME)' deleted" \
, \
echo "No Git persistent stack for replicas found. Nothing to do." \
)
find-asg:
$(eval ASGS := $(shell $(AWS) autoscaling describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='aws:cloudformation:stack-name') && Value=='$(CLUSTER_STACK_NAME)']]".AutoScalingGroupName \
| jq -r '.[]'))
@echo "FOUND AUTOSCALING GROUPS in '$(CLUSTER_STACK_NAME)': '$(ASGS)'"
delete-asg: find-asg
for asg in $(ASGS); \
do \
echo "Deleting $$asg Autoscaling group"; \
$(AWS) autoscaling delete-auto-scaling-group --force-delete --auto-scaling-group-name "$$asg"; \
done
wait_for_asg_deletion: find-asg
$(if $(ASGS), \
while [[ $$($(AWS) autoscaling describe-auto-scaling-groups --auto-scaling-group-names $(ASGS) | jq '.AutoScalingGroups[]' | grep '[A-Z]') ]]; do \
echo "Wait for ASGs $(ASGS) to be deleted"; \
sleep 5; \
done; \
echo "Autoscaling groups $(ASGS) have been terminated.", \
echo "No Autoscaling groups found. Nothing to do." \
)