Skip to content

Commit 5c4d1ba

Browse files
Merge branch 'main' of https://github.com/code-dot-org/aiproxy into cat5inthecradle-patch-1
2 parents 24fa750 + f9a8002 commit 5c4d1ba

16 files changed

+335
-69
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
config.txt
22
__pycache__
3+
4+
# Ignore local Ruby config necessary for our custom AWS auth solution
5+
.ruby-version

Dockerfile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
FROM python:3.11-slim
22

3-
RUN pip install Flask
4-
53
WORKDIR /app
64
COPY requirements.txt .
75

8-
RUN pip install -r requirements.txt
6+
RUN pip install --no-cache-dir -r requirements.txt
97

108
COPY ./test /app/test
119
COPY ./lib /app/lib
1210
COPY ./src /app/src
1311

14-
EXPOSE 5000
15-
CMD ["waitress-serve", "--host=0.0.0.0", "--port=5000", "--call", "src:create_app"]
12+
EXPOSE 80
13+
CMD ["waitress-serve", "--host=0.0.0.0", "--port=80", "--call", "src:create_app"]

ci-build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
if [ -n "$CODEBUILD_BUILD_ID" ]; then
6+
# CodeBuild environment
7+
IMAGE_TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION:0:7}
8+
else
9+
# Local Git repository
10+
IMAGE_TAG=$(git rev-parse --short HEAD)
11+
fi
12+
13+
IMAGE_NAME=aiproxy
14+
15+
echo "Building Docker Image ${IMAGE_NAME}:${IMAGE_TAG}..."
16+
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .

ci-lint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "Validating Cloudformation Templates..."
6+
cfn-lint cicd/1-setup/*.template.yml
7+
cfn-lint cicd/2-cicd/*.template.yml
8+
cfn-lint cicd/3-app/aiproxy/template.yml
9+
10+
echo "Validating Dockerfile..."
11+
docker run --rm -i hadolint/hadolint < Dockerfile

cicd/1-setup/cicd-dependencies.template.yml

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Resources:
1414
# the common policy rules required. Since we may want to create multiple
1515
# codebuild projects for different branches, we can append policies when those
1616
# stack resources are created (in "2-cicd/cicd.template.yml")
17-
AiProxyCodeBuildRole:
17+
CodeBuildRole:
1818
Type: AWS::IAM::Role
1919
Properties:
2020
AssumeRolePolicyDocument:
@@ -68,6 +68,11 @@ Resources:
6868
- s3:GetObjectVersion
6969
Resource:
7070
- !Sub arn:aws:s3:::${ArtifactStore}/*
71+
# TODO: Scope to specific ECR Repos?
72+
- Effect: Allow
73+
Action:
74+
- ecr:GetAuthorizationToken
75+
Resource: '*'
7176
- Effect: Allow
7277
Action: codestar-connections:UseConnection
7378
Resource:
@@ -81,16 +86,59 @@ Resources:
8186
Resource:
8287
- !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:cicd/*
8388

89+
# This would ideally be defined in 3-app/aiproxy/template.yml, but we do
90+
# not allow the CloudFormationServiceRole permission to CreateRole, so we
91+
# have to define it here. TODO: Tweak permissions for the role used to create
92+
# the cloudformation stack.
93+
ECSTaskExecutionRole:
94+
Type: AWS::IAM::Role
95+
Properties:
96+
AssumeRolePolicyDocument:
97+
Version: "2012-10-17"
98+
Statement:
99+
- Effect: Allow
100+
Principal:
101+
Service:
102+
- ecs-tasks.amazonaws.com
103+
Action:
104+
- sts:AssumeRole
105+
Policies:
106+
- PolicyName: ECRPolicy
107+
PolicyDocument:
108+
Version: "2012-10-17"
109+
Statement:
110+
- Effect: Allow
111+
Action:
112+
- ecr:GetAuthorizationToken
113+
- ecr:BatchCheckLayerAvailability
114+
- ecr:GetDownloadUrlForLayer
115+
- ecr:BatchGetImage
116+
Resource: "*"
117+
- PolicyName: LogsPolicy
118+
PolicyDocument:
119+
Version: "2012-10-17"
120+
Statement:
121+
- Effect: Allow
122+
Action:
123+
- logs:CreateLogGroup
124+
- logs:CreateLogStream
125+
- logs:PutLogEvents
126+
Resource: "*"
127+
84128
Outputs:
85-
AiProxyCodeBuildArtifactBucket:
129+
CodeBuildArtifactBucket:
86130
Description: AiProxy CodeBuild Artifact Bucket Name
87131
Value: !Ref ArtifactStore
88132
Export: {Name: AiProxyCodeBuildArtifactBucket}
89-
AiProxyCodeBuildRoleArn:
133+
CodeBuildRoleArn:
90134
Description: AiProxy CodeBuild Role ARN
91-
Value: !GetAtt AiProxyCodeBuildRole.Arn
135+
Value: !GetAtt CodeBuildRole.Arn
92136
Export: {Name: AiProxyCodeBuildRoleArn}
93-
AiProxyCodeBuildRoleName:
137+
CodeBuildRoleName:
94138
Description: AiProxy CodeBuild Role Name
95-
Value: !Ref AiProxyCodeBuildRole
139+
Value: !Ref CodeBuildRole
96140
Export: {Name: AiProxyCodeBuildRoleName}
141+
ECSTaskExecutionRoleArn:
142+
Description: AiProxy ECS Task Execution Role ARN
143+
Value: !GetAtt ECSTaskExecutionRole.Arn
144+
Export: {Name: AiProxyECSTaskExecutionRoleArn}

cicd/2-cicd/cicd.template.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ Resources:
170170
Value: !ImportValue AiProxyCodeBuildArtifactBucket
171171
- Name: ECR_REPOSITORY
172172
Value: !GetAtt EcrRepository.RepositoryUri
173+
- Name: BRANCH
174+
Value: !Ref GitHubBranch
173175
Source:
174176
Type: CODEPIPELINE
175177
BuildSpec: cicd/3-app/aiproxy/buildspec.yml
@@ -306,10 +308,10 @@ Resources:
306308
TemplatePath: appBuildResults::packaged-app-template.yml
307309
TemplateConfiguration: appBuildResults::cicd/3-app/aiproxy/test.config.json
308310
ParameterOverrides: !Join
309-
- ''
310-
- - '{ "SubdomainName": "'
311-
- !If [ TargetsMainBranch, 'aiproxy-test', !Sub 'aiproxy-${GitHubBranch}-test' ]
312-
- '" }'
311+
- ''
312+
- - '{ "SubdomainName": "'
313+
- !If [ TargetsMainBranch, 'aiproxy-test', !Sub 'aiproxy-test-${GitHubBranch}' ]
314+
- '" }'
313315
Capabilities: CAPABILITY_AUTO_EXPAND,CAPABILITY_IAM
314316
RoleArn: !Sub arn:aws:iam::${AWS::AccountId}:role/admin/CloudFormationService
315317
- !Ref AWS::NoValue

cicd/3-app/aiproxy/buildspec.yml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,40 @@ phases:
1616
commands:
1717
- echo Logging in to Docker Hub...
1818
- echo $DOCKER_HUB_PAT | docker login -u $DOCKER_HUB_USERNAME --password-stdin
19+
20+
- echo "Logging in to AWS ECR..."
21+
- ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
22+
- AWS_REGION=us-east-1
23+
- ECR_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
24+
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
1925

2026
build:
2127
# This will build and push the docker image to ECR and package the
2228
# Cloudformation template to be passed as an Artifact to future pipeline
2329
# steps. It will also run unit tests and linting.
2430
commands:
2531
- set -e
26-
- BRANCH_NAME=${CODEBUILD_WEBHOOK_HEAD_REF#"refs/heads/"}
32+
33+
- echo ECR_REPOSITORY=$ECR_REPOSITORY
34+
- echo BRANCH=$BRANCH
2735

2836
- cd $CODEBUILD_SRC_DIR
2937

30-
- echo "Validating Cloudformation Templates..."
31-
- cfn-lint cicd/1-setup/*.template.yml
32-
- cfn-lint cicd/2-cicd/*.template.yml
33-
- cfn-lint cicd/3-app/aiproxy/template.yml
38+
- ./ci-lint.sh
39+
40+
- echo "Running Unit Tests..."
41+
- echo "This is where I would run my unit tests"
3442

3543
- echo "Building Docker Image..."
3644
- IMAGE_NAME=aiproxy
37-
- IMAGE_TAG=$(git rev-parse --short HEAD)
45+
- IMAGE_TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION:0:7} # short commit hash
3846
- docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
3947

40-
- echo "Running Unit Tests..."
41-
- echo "This is where I would run my unit tests"
42-
4348
- echo "Pushing Docker Image to ECR..."
44-
- ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
45-
- AWS_REGION=$(aws configure get region)
46-
- ECR_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
47-
- ECR_REPO_NAME="aiproxy-${BRANCH_NAME}"
48-
- FULL_IMAGE_NAME="${ECR_REGISTRY}/${ECR_REPO_NAME}:${IMAGE_TAG}"
49-
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
50-
- docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${FULL_IMAGE_NAME}
51-
- docker push ${FULL_IMAGE_NAME}
49+
- docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${ECR_REPOSITORY}:${IMAGE_TAG}
50+
- docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${ECR_REPOSITORY}:latest
51+
- docker push ${ECR_REPOSITORY}:${IMAGE_TAG}
52+
- docker push ${ECR_REPOSITORY}:latest
5253

5354
- echo "Linting cloudformation..."
5455
- cd $CODEBUILD_SRC_DIR
@@ -57,6 +58,7 @@ phases:
5758
- cat template.yml
5859

5960
- echo "Creating environment config..."
61+
- IMAGE_URI="${ECR_REPOSITORY}:${IMAGE_TAG}"
6062
- cicd/3-app/aiproxy/config/create-environment-config.sh
6163

6264
- echo "Packaging Cloudformation Template..."

cicd/3-app/aiproxy/config/create-environment-config.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
set -e
44

55
dir=cicd/3-app/aiproxy
66

7+
# AMAGE_URI should be set
8+
if [[ -z "${IMAGE_URI}" ]]; then
9+
echo "IMAGE_URI is not set. Please set it to the desired value."
10+
exit 1
11+
fi
12+
713
# loop over config files
814
for i in $(ls "${dir}/config" | egrep -i '.*\.config\.json' ); do
915
file="${dir}/config/${i}"
1016
echo "tranforming ${i}..."
1117
contents=$(cat $file)
1218

1319
# New value insertion
14-
# contents="$(jq '.NewValueDemo = "abcde"' <<< $contents)"
20+
contents="$(jq --arg uri "$IMAGE_URI" '.Parameters.AppImageUri = $uri' <<< "$contents")"
21+
1522

1623
# Edit existing value
1724
# if [ "$( jq 'has("TransformDemo")' $file )" == "true" ]; then

cicd/3-app/aiproxy/config/production.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"Parameters": {
3-
"BaseDomainName": "code.org"
3+
"BaseDomainName": "code.org",
4+
"BaseDomainNameHostedZonedID": "Z2LCOI49SCXUGU"
45
},
56
"Tags": {
67
"EnvType": "production"

cicd/3-app/aiproxy/config/test.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"Parameters": {
3-
"BaseDomainName": "code.org"
3+
"BaseDomainName": "code.org",
4+
"BaseDomainNameHostedZonedID": "Z2LCOI49SCXUGU"
45
},
56
"Tags": {
67
"EnvType": "test"

0 commit comments

Comments
 (0)