Skip to content

Commit

Permalink
feat: support execution as a command (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
portellaa authored Sep 22, 2022
1 parent 4eaacc2 commit 5d5097c
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 62 deletions.
62 changes: 40 additions & 22 deletions .github/workflows/prereleased.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ on:

env:
DOCKERHUB_REGISTRY: ydata
DOCKER_REPOSITORY: aws-asg-tags-lambda
DOCKER_REPOSITORY_COMMAND: aws-asg-tags-command
DOCKER_REPOSITORY_LAMBDA: aws-asg-tags-lambda



Expand Down Expand Up @@ -51,8 +52,8 @@ jobs:
run: echo ::set-output name=value::${GITHUB_REF#refs/*/}


build:
name: Build and push to Docker Hub
build-command:
name: Build and push the COMMAND version
runs-on: ubuntu-20.04

needs:
Expand All @@ -68,50 +69,67 @@ jobs:
restore-keys: |
${{ runner.os }}-spm-
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
- name: Login to Dockerhub Registry
uses: docker/login-action@v2
with:
role-to-assume: ${{ secrets.AWS_ECR_ROLE_ARN }}
aws-region: ${{ secrets.AWS_ECR_REGION }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Login to Amazon ECR
id: ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and push docker image
id: docker_build
uses: docker/build-push-action@v3
env:
DOCKER_IMAGE_TAG: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY_COMMAND }}:${{ needs.prepare.outputs.version }}
with:
file: command.Dockerfile
push: true
tags: ${{ env.DOCKER_IMAGE_TAG }}


build-lambda:
name: Build and push LAMBDA version
runs-on: ubuntu-20.04

needs:
- prepare

steps:
- uses: actions/checkout@v3

- uses: actions/cache@v3
with:
path: .build
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
${{ runner.os }}-spm-
- name: Login to Dockerhub Registry
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Build and push to AWS ECR
- name: Build and push docker image
id: docker_build
uses: docker/build-push-action@v3
env:
DOCKER_IMAGE_TAG: ${{ steps.ecr.outputs.registry }}/${{ env.DOCKER_REPOSITORY }}:${{ needs.prepare.outputs.version }}
DOCKER_IMAGE_TAG: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY_LAMBDA }}:${{ needs.prepare.outputs.version }}
with:
file: lambda.Dockerfile
push: true
tags: ${{ env.DOCKER_IMAGE_TAG }}

- name: Tag to docker hub and push
env:
SOURCE: ${{ steps.ecr.outputs.registry }}/${{ env.DOCKER_REPOSITORY }}:${{ needs.prepare.outputs.version }}
DESTINATION: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{ needs.prepare.outputs.version }}
run: |
docker tag $SOURCE $DESTINATION
docker push $DESTINATION

update-manifests:
name: Update AWS Marketplace
runs-on: ubuntu-20.04

needs:
- prepare
- build
- build-command

env:
COMPONENT: ASG_TAGS_LAMBDA_VERSION
COMPONENT: ASG_TAGS_VERSION

steps:
- name: Checkout AWS Marketplace repo
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ jobs:
- name: Package Resolve
run: swift package resolve

- name: Build for test
run: swift build
- name: Build CloudFormation for test
run: swift build --product CloudFormation

- name: Build Command for test
run: swift build --product Command
46 changes: 29 additions & 17 deletions .github/workflows/released.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ on:

env:
DOCKERHUB_REGISTRY: ydata
DOCKER_REPOSITORY: aws-asg-tags-lambda
DOCKER_REPOSITORY_COMMAND: aws-asg-tags-command
DOCKER_REPOSITORY_LAMBDA: aws-asg-tags-lambda



Expand Down Expand Up @@ -65,24 +66,38 @@ jobs:
run: echo ::set-output name=value::$(git tag | grep ${{ steps.short_sha.outputs.value }} | sed -r 's|([0-9].[0-9].[0-9]).*|\1|g')


docker-tag:
name: Docker Tag and Push to public and private Container Registries
docker-tag-command:
name: Docker Tag and Push COMMAND version
runs-on: ubuntu-20.04

needs:
- prepare

steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
- name: Login to Dockerhub Registry
uses: docker/login-action@v2
with:
role-to-assume: ${{ secrets.AWS_ECR_ROLE_ARN }}
aws-region: ${{ secrets.AWS_ECR_REGION }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Login to Amazon ECR
id: ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Docker tag and push
env:
SOURCE: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY_COMMAND }}:${{ needs.prepare.outputs.old_version }}.${{ needs.prepare.outputs.build_number }}
DESTINATION_DOCKERHUB: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY_COMMAND }}:${{ needs.prepare.outputs.new_version }}
run: |
docker pull $SOURCE
docker tag $SOURCE $DESTINATION_DOCKERHUB
docker push $DESTINATION_DOCKERHUB
docker-tag-lambda:
name: Docker Tag and Push LAMBDA version
runs-on: ubuntu-20.04

needs:
- prepare

steps:
- name: Login to Dockerhub Registry
uses: docker/login-action@v2
with:
Expand All @@ -91,13 +106,10 @@ jobs:

- name: Docker tag and push
env:
SOURCE: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{ needs.prepare.outputs.old_version }}.${{ needs.prepare.outputs.build_number }}
DESTINATION_ECR: ${{ steps.ecr.outputs.registry }}/${{ env.DOCKER_REPOSITORY }}:${{ needs.prepare.outputs.new_version }}
DESTINATION_DOCKERHUB: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{ needs.prepare.outputs.new_version }}
SOURCE: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY_LAMBDA }}:${{ needs.prepare.outputs.old_version }}.${{ needs.prepare.outputs.build_number }}
DESTINATION_DOCKERHUB: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKER_REPOSITORY_LAMBDA }}:${{ needs.prepare.outputs.new_version }}
run: |
docker pull $SOURCE
docker tag $SOURCE $DESTINATION_ECR
docker push $DESTINATION_ECR
docker tag $SOURCE $DESTINATION_DOCKERHUB
docker push $DESTINATION_DOCKERHUB
Expand All @@ -108,10 +120,10 @@ jobs:

needs:
- prepare
- docker-tag
- docker-tag-command

env:
COMPONENT: ASG_TAGS_LAMBDA_VERSION
COMPONENT: ASG_TAGS_VERSION

steps:
- name: Checkout AWS Marketplace repo
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
"version" : "6.0.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "9f39744e025c7d377987f30b03770805dcb0bcd1",
"version" : "1.1.4"
}
},
{
"identity" : "swift-aws-lambda-events",
"kind" : "remoteSourceControl",
Expand Down
12 changes: 11 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ let package = Package(
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
.package(url: "https://github.com/ydataai/swift-aws-lambda-events.git", branch: "main"),
.package(url: "https://github.com/soto-project/soto.git", from: "6.0.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.11.1")
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.11.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.1.4")
],
targets: [
.target(name: "Models"),
Expand All @@ -35,6 +36,15 @@ let package = Package(
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
],
swiftSettings: [ .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)) ]
),
.executableTarget(
name: "Command",
dependencies: [
.byName(name: "App"),
.byName(name: "Models"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
swiftSettings: [ .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)) ]
)
]
)
8 changes: 4 additions & 4 deletions Sources/App/ASG/ASGClient.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import Foundation
import SotoAutoScaling

protocol ASGClientRepresentable {
public protocol ASGClientRepresentable {
func updateTags(_ tags: [AutoScaling.Tag]) async throws
}

struct ASGClient<Provider: ASGProvider>: ASGClientRepresentable {
public struct ASGClient<Provider: ASGProvider>: ASGClientRepresentable {
let logger: Logger
let provider: Provider

init(logger: Logger, provider: Provider) {
public init(logger: Logger, provider: Provider) {
self.logger = logger
self.provider = provider
}

func updateTags(_ tags: [AutoScaling.Tag]) async throws {
public func updateTags(_ tags: [AutoScaling.Tag]) async throws {
let updatedTags = tags.map {
AutoScaling.Tag(
key: $0.key,
Expand Down
4 changes: 2 additions & 2 deletions Sources/App/ASG/ASGProvider.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Foundation
import SotoAutoScaling

protocol ASGProvider {
public protocol ASGProvider {
func createOrUpdateTags(_ input: AutoScaling.CreateOrUpdateTagsType, logger: Logger) async throws
}

extension AutoScaling: ASGProvider {
@inlinable
func createOrUpdateTags(_ input: CreateOrUpdateTagsType, logger: Logger) async throws {
public func createOrUpdateTags(_ input: CreateOrUpdateTagsType, logger: Logger) async throws {
try await createOrUpdateTags(input, logger: logger, on: nil)
}
}
10 changes: 5 additions & 5 deletions Sources/App/EKS/EKSClient.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import SotoEKS

protocol EKSClientRepresentable {
public protocol EKSClientRepresentable {
func describeNodeGroup(name: String, clusterName: String) async throws -> EKS.Nodegroup
}

struct EKSClient<Provider: EKSProvider>: EKSClientRepresentable {
public struct EKSClient<Provider: EKSProvider>: EKSClientRepresentable {
let logger: Logger
let provider: Provider

init(logger: Logger, provider: Provider) {
public init(logger: Logger, provider: Provider) {
self.logger = logger
self.provider = provider
}

func describeNodeGroup(name: String, clusterName: String) async throws -> EKS.Nodegroup {
public func describeNodeGroup(name: String, clusterName: String) async throws -> EKS.Nodegroup {
let request = EKS.DescribeNodegroupRequest(clusterName: clusterName, nodegroupName: name)

let response = try await provider.describeNodegroup(request, logger: logger)
Expand All @@ -26,7 +26,7 @@ struct EKSClient<Provider: EKSProvider>: EKSClientRepresentable {
}
}

extension EKSClient {
public extension EKSClient {
enum Error: Swift.Error {
case cannotFindNodeGroup(_ name: String, _ clusterName: String)
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/App/EKS/EKSProvider.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Foundation
import SotoEKS

protocol EKSProvider {
public protocol EKSProvider {
func describeNodegroup(_ input: EKS.DescribeNodegroupRequest, logger: Logger) async throws
-> EKS.DescribeNodegroupResponse
}

extension EKS: EKSProvider {
@inlinable
func describeNodegroup(_ input: DescribeNodegroupRequest, logger: Logger) async throws -> DescribeNodegroupResponse {
public func describeNodegroup(_ input: DescribeNodegroupRequest, logger: Logger) async throws
-> DescribeNodegroupResponse {
try await describeNodegroup(input, logger: logger, on: nil)
}
}
10 changes: 8 additions & 2 deletions Sources/App/Hulk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import Models
import SotoAutoScaling
import SotoEKS

struct Hulk {
public struct Hulk {
let asgClient: any ASGClientRepresentable
let eksClient: any EKSClientRepresentable
let logger: Logger

func smash(_ clusterInfo: ClusterNodesTags) async throws {
public init(asgClient: any ASGClientRepresentable, eksClient: any EKSClientRepresentable, logger: Logger) {
self.asgClient = asgClient
self.eksClient = eksClient
self.logger = logger
}

public func smash(_ clusterInfo: ClusterNodesTags) async throws {
logger.info("let's smash tags into ASGs with \(clusterInfo)")

let asgNames = try await withThrowingTaskGroup(
Expand Down
21 changes: 21 additions & 0 deletions Sources/Command/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

enum Environment {
static var decoder: JSONDecoder = JSONDecoder()

static func get<Key: CodingKey>(_ key: Key) -> String? {
ProcessInfo.processInfo.environment[key.stringValue]
}

static func get<Key: CodingKey, D: Decodable>(
_ key: Key,
_ type: D.Type = D.self,
decoder: JSONDecoder = Self.decoder
) throws -> D? {
guard let data = get(key)?.data(using: .utf8) else {
return nil
}

return try decoder.decode(D.self, from: data)
}
}
Loading

0 comments on commit 5d5097c

Please sign in to comment.