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
resource "random_uuid" "name" {} | |
resource "aws_s3_bucket" "bucket" { | |
bucket = "${var.application}-${random_uuid.name.result}" | |
acl = "public-read" | |
policy = <<POLICY | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ |
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
#!/bin/bash | |
if [ ! -f "/var/lib/jenkins/api_key.txt" ]; then | |
CRUMB=$(curl -s http://localhost:8080/crumbIssuer/api/json --user admin:${admin_password} | jq -r .crumb) | |
API_KEY=$(curl -s -X POST http://localhost:8080/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken --user admin:${admin_password} --data newTokenName=agent-token -H "Jenkins-Crumb: $CRUMB" | jq -r .data.tokenValue) | |
echo $API_KEY > /var/lib/jenkins/api_key.txt | |
fi | |
aws ssm put-parameter --name "${api_ssm_parameter}" --value "$(cat /var/lib/jenkins/api_key.txt)" --type "SecureString" --overwrite --region ${aws_region} |
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
#cloud-config | |
runcmd: | |
- MASTER_ID=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names ${master_asg} --query 'AutoScalingGroups[0].Instances[0].InstanceId' --output text --region ${aws_region}) && echo $MASTER_ID > /opt/master_id.txt | |
- AGENT_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) && echo $AGENT_ID > /opt/agent_id.txt | |
- MASTER_IP=$(aws ec2 describe-instances --instance-ids $(cat /opt/master_id.txt) --query 'Reservations[0].Instances[0].PrivateIpAddress' --output text --region ${aws_region}) && echo $MASTER_IP > /opt/master_ip.txt | |
- API_KEY=$(aws ssm get-parameter --name "${api_ssm_parameter}" --with-decryption --region ${aws_region} --output text --query 'Parameter.Value') && echo $API_KEY > /opt/api_key.txt |
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
#!/bin/bash | |
runuser -l jenkins -c "nohup java -jar /var/lib/jenkins/swarm-client-${swarm_version}.jar -master http://$(cat /opt/master_ip.txt):8080 -description $(cat /opt/agent_id.txt) -username admin -password $(cat /opt/api_key.txt) -name $(cat /opt/agent_id.txt) -labels $(cat /opt/agent_id.txt) -executors ${executors} -fsroot /tmp -retry 0 -disableClientsUniqueId" || aws ec2 terminate-instances --instance-ids $(cat /opt/agent_id.txt) --region ${aws_region} |
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
#!/bin/bash | |
for i in {1..3}; | |
do | |
AGENT_ASG="$(aws autoscaling describe-auto-scaling-groups --region ${aws_region} | jq -r '.AutoScalingGroups[] | select(.AutoScalingGroupName | startswith("${application}-agent-")) | .AutoScalingGroupName')" | |
BUILD_ACTIVE_QUEUE_VALUE1="$(curl -s -m 60 -u 'admin:APIKEY' 'http://localhost:8080/computer/api/json' | jq -r '.busyExecutors')" | |
BUILD_ACTIVE_QUEUE_VALUE2="$(curl -s -m 60 -u 'admin:APIKEY' 'http://localhost:8080/jqs-monitoring/api/json' | jq -r '.buildQueue.numberOfJobs')" | |
BUILD_ACTIVE_QUEUE_VALUE=$(( $BUILD_ACTIVE_QUEUE_VALUE1 + $BUILD_ACTIVE_QUEUE_VALUE2 )) | |
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
#!/bin/bash | |
for i in {1..3}; | |
do | |
AGENT_ASG="$(aws autoscaling describe-auto-scaling-groups --region ${aws_region} | jq -r '.AutoScalingGroups[] | select(.AutoScalingGroupName | startswith("${application}-agent-")) | .AutoScalingGroupName')" | |
BUILD_ACTIVE_QUEUE_VALUE1="$(curl -s -m 60 -u 'admin:APIKEY' 'http://localhost:8080/computer/api/json' | jq -r '.busyExecutors')" | |
BUILD_ACTIVE_QUEUE_VALUE2="$(curl -s -m 60 -u 'admin:APIKEY' 'http://localhost:8080/jqs-monitoring/api/json' | jq -r '.buildQueue.numberOfJobs')" | |
BUILD_ACTIVE_QUEUE_VALUE=$(( $BUILD_ACTIVE_QUEUE_VALUE1 + $BUILD_ACTIVE_QUEUE_VALUE2 )) | |
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
variable "jenkins_version" { | |
description = "The version number of Jenkins to use on the master. Change this value when a new version comes out, and it will update the launch configuration and the autoscaling group." | |
default = "2.164.3" | |
} | |
variable "swarm_version" { | |
description = "The version of swarm plugin to install on the agents. Update by updating this value." | |
default = "3.15" | |
} |
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
data "template_file" "agent_write_files" { | |
template = "${file("${path.module}/init/agent-write-files.cfg")}" | |
vars { | |
agent_logs = "${aws_cloudwatch_log_group.agent_logs.name}" | |
aws_region = "${var.region}" | |
executors = "${var.executors}" | |
swarm_version = "${var.swarm_version}" | |
} | |
} |
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
data "template_cloudinit_config" "agent_init" { | |
gzip = false | |
base64_encode = false | |
part { | |
filename = "agent.cfg" | |
content_type = "text/cloud-config" | |
content = "${data.template_file.agent_write_files.rendered}" | |
} |
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
module "jenkins_ha_agents" { | |
source = "neiman-marcus/jenkins-ha-agents/aws" | |
snipped for brevity… | |
extra_agent_userdata = "${data.template_file.extra_agent_userdata.rendered}" | |
extra_agent_userdata_merge = "list(append)+dict(recurse_array)+str()" | |
extra_master_userdata = "${data.template_file.extra_master_userdata.rendered}" | |
extra_master_userdata_merge = "list(append)+dict(recurse_array)+str()" | |
} |
OlderNewer