Skip to content

Commit

Permalink
feat: add SEMAPHORE_AGENT_TAG_NAME parameter (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
cchristous authored Oct 31, 2022
1 parent 8d434e0 commit 8fb0d33
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
8 changes: 6 additions & 2 deletions bin/aws-semaphore-agent.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node

const { App } = require('aws-cdk-lib');
const { App, Tags } = require('aws-cdk-lib');
const { AwsSemaphoreAgentStack } = require('../lib/aws-semaphore-agent-stack');
const { ArgumentStore } = require('../lib/argument-store');
const { getKeys } = require('../lib/github-keys');

const app = new App();
const argumentStore = buildArgumentStore();

new AwsSemaphoreAgentStack(app, 'AwsSemaphoreAgentStack', {
const awsSemaphoreAgentStack = new AwsSemaphoreAgentStack(app, 'AwsSemaphoreAgentStack', {
stackName: argumentStore.get("SEMAPHORE_AGENT_STACK_NAME"),
description: "Semaphore agent autoscaling stack",
argumentStore: argumentStore,
Expand All @@ -20,6 +20,10 @@ new AwsSemaphoreAgentStack(app, 'AwsSemaphoreAgentStack', {
},
});

argumentStore.getTags().forEach(tag => {
Tags.of(awsSemaphoreAgentStack).add(tag.key, tag.value);
});

function buildArgumentStore() {
try {
return ArgumentStore.fromEnv();
Expand Down
6 changes: 4 additions & 2 deletions execution-policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"lambda:DeleteFunction",
"lambda:InvokeFunction",
"lambda:UpdateFunctionCode",
"lambda:UpdateFunctionConfiguration"
"lambda:UpdateFunctionConfiguration",
"lambda:TagResource"
],
"Resource": "arn:aws:lambda:*:*:function:*"
},
Expand All @@ -101,7 +102,8 @@
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:PutParameter",
"ssm:DeleteParameter"
"ssm:DeleteParameter",
"ssm:AddTagsToResource"
],
"Resource": "*"
},
Expand Down
18 changes: 17 additions & 1 deletion lib/argument-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ArgumentStore {
"SEMAPHORE_AGENT_MANAGED_POLICY_NAMES": "",
"SEMAPHORE_AGENT_VOLUME_NAME": "",
"SEMAPHORE_AGENT_VOLUME_TYPE": "gp2",
"SEMAPHORE_AGENT_VOLUME_SIZE": "64"
"SEMAPHORE_AGENT_VOLUME_SIZE": "64",
"SEMAPHORE_AGENT_TAGS": ""
}

constructor() {
Expand Down Expand Up @@ -125,6 +126,21 @@ class ArgumentStore {
return this.get("SEMAPHORE_ENDPOINT");
}

getTags() {
var tags = [];
// example tag value = 'Name:Something,Category:SomethingElse'
if (!this.isEmpty("SEMAPHORE_AGENT_TAGS")) {
tags = this.get("SEMAPHORE_AGENT_TAGS").split(",").map(tagPair => {
const [key, value] = tagPair.trim().split(":");
return {
key: key.trim(),
value: value.trim(),
};
});
}
return tags;
}

// We only allow SSH ingress if a EC2 key has been specified.
// Access to Windows instances happen through AWS Systems Manager,
// so no need to allow SSH ingress for those as well.
Expand Down
33 changes: 33 additions & 0 deletions test/argument-store.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { ArgumentStore } = require("../lib/argument-store");

describe("get tags", () => {
test("empty tags", () => {
const argumentStore = ArgumentStore.fromMap({
SEMAPHORE_AGENT_STACK_NAME: "test-stack",
SEMAPHORE_ORGANIZATION: "test",
SEMAPHORE_AGENT_TOKEN_PARAMETER_NAME: "test-token"
});
const tags = argumentStore.getTags();
expect(tags).toEqual([]);
});

test("multiple tags", () => {
const argumentStore = ArgumentStore.fromMap({
SEMAPHORE_AGENT_STACK_NAME: "test-stack",
SEMAPHORE_ORGANIZATION: "test",
SEMAPHORE_AGENT_TOKEN_PARAMETER_NAME: "test-token",
SEMAPHORE_AGENT_TAGS: " Name : Something ,Category:SomethingElse"
});
const tags = argumentStore.getTags();
expect(tags).toEqual([
{
key: "Name",
value: "Something",
},
{
key: "Category",
value: "SomethingElse",
},
]);
});
})

0 comments on commit 8fb0d33

Please sign in to comment.