Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit cc0cae1

Browse files
committed
Merge branch 'master' into 'develop'
Master See merge request verbose-equals-true/django-postgres-vue-gitlab-ecs!11
2 parents 1a8a0fd + 8264984 commit cc0cae1

30 files changed

+558
-433
lines changed

.env.template

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
SECRET_KEY=secret
22
DEBUG=True
33

4-
RDS_DB_NAME=postgres
5-
RDS_USERNAME=postgres
6-
RDS_PASSWORD=postgres
7-
RDS_HOSTNAME=postgres
8-
RDS_PORT=5432
4+
POSTGRES_NAME=postgres
5+
POSTGRES_USERNAME=postgres
6+
POSTGRES_PASSWORD=postgres
7+
POSTGRES_SERVICE_HOST=postgres
8+
POSTGRES_SERVICE_PORT=5432
99

1010
GITHUB_KEY=yourkey
1111
GITHUB_SECRET=yoursecret

.gitlab-ci.yml

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ variables:
1111
REPOSITORY_URL: ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${AppUrl}/backend
1212
ECRBackendRepositoryName: ${AppUrl}/backend
1313
GitSHA: $CI_COMMIT_SHORT_SHA
14-
AWSAccessKeyId: $AWS_ACCESS_KEY_ID
15-
AWSSecretAccessKey: $AWS_SECRET_ACCESS_KEY
1614

1715
pages:
1816
image: node:latest
@@ -145,13 +143,11 @@ Build Quasar PWA Assets:
145143
only:
146144
- master
147145
variables:
148-
VUE_APP_CI_COMMIT_SHORT_SHA: "$CI_COMMIT_SHORT_SHA"
149-
VUE_APP_CI_JOB_URL: "$CI_JOB_URL"
150-
VUE_APP_API_BASE_URL: "https://api.${AppUrl}"
151-
VUE_APP_BASE_URL: "https://${AppUrl}"
152-
VUE_APP_WS_PROTOCOL: wss://
153-
VUE_APP_BASE_HOST: api.${AppUrl}
154-
VUE_APP_SITE_DOMAIN: ${AppUrl}
146+
DOMAIN_NAME: "api.${AppUrl}"
147+
GOOGLE_OAUTH2_KEY: google123
148+
GITHUB_KEY: github123
149+
WS_PROTOCOL: wss
150+
HTTP_PROTOCOL: https
155151
artifacts:
156152
paths:
157153
- quasar/dist/pwa
@@ -172,7 +168,6 @@ Release backend:
172168
services:
173169
- docker:dind
174170
before_script:
175-
- cd backend
176171
- apk add --no-cache curl jq python py-pip
177172
- pip install awscli
178173
script:
@@ -181,17 +176,17 @@ Release backend:
181176
-u $CI_REGISTRY_USER \
182177
-p $CI_REGISTRY_PASSWORD \
183178
$CI_REGISTRY
184-
- docker pull $CI_REGISTRY_IMAGE:latest || true
179+
- docker pull $CI_REGISTRY_IMAGE/backend:latest || true
185180
- |
186181
docker build \
187-
--cache-from $CI_REGISTRY_IMAGE:latest \
182+
--cache-from $CI_REGISTRY_IMAGE/backend:latest \
188183
--target production \
189184
--tag $REPOSITORY_URL:${GitSHA} \
190-
--file scripts/prod/Dockerfile .
185+
--file backend/scripts/prod/Dockerfile .
191186
- $(aws ecr get-login --no-include-email --region us-east-1)
192187
- docker push $REPOSITORY_URL:${GitSHA}
193188
after_script:
194-
- echo "The production image has been released from the GitLab to ECR"
189+
- echo "The production image has been released from GitLab to ECR"
195190

196191
Sync Quasar PWA Assets:
197192
image: python:3.7
@@ -213,7 +208,7 @@ Sync Quasar PWA Assets:
213208
./dist/pwa/ s3://${AppUrl}/
214209
- |
215210
aws s3 cp \
216-
--cache-control 'max-age=0'
211+
--cache-control 'max-age=0' \
217212
./dist/pwa/index.html s3://${AppUrl}/
218213
- |
219214
aws s3 cp \
@@ -224,9 +219,7 @@ Sync Quasar PWA Assets:
224219

225220
.Create stack:
226221
image: python:3.7
227-
stage: cloudformation
228-
only:
229-
- master
222+
stage: deploy
230223
variables:
231224
EnvironmentName: staging
232225
before_script:
@@ -246,7 +239,7 @@ Sync Quasar PWA Assets:
246239

247240
Update stack:
248241
image: python:3.7
249-
stage: cloudformation
242+
stage: deploy
250243
only:
251244
- master
252245
variables:
@@ -265,3 +258,29 @@ Update stack:
265258
--parameters file://./parameters.json
266259
after_script:
267260
- echo "Update stack complete"
261+
262+
collectstatic: &task
263+
image: python:3.7
264+
stage: deploy
265+
only:
266+
- master
267+
variables:
268+
EnvironmentName: staging
269+
before_script:
270+
- pip install awscli
271+
when: manual
272+
script:
273+
- |
274+
aws ecs run-task \
275+
--cluster ${EnvironmentName}-cluster \
276+
--task-definition collectstatic
277+
278+
migrate:
279+
<<: *task
280+
variables:
281+
EnvironmentName: staging
282+
script:
283+
- |
284+
aws ecs run-task \
285+
--cluster ${EnvironmentName}-cluster \
286+
--task-definition migrate

backend/backend/settings/base.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
CORS_ALLOW_HEADERS = default_headers + (
3737
'access-control-allow-headers',
3838
'access-control-allow-methods',
39+
'access-control-allow-origin'
3940
)
4041

4142
# Application definition
@@ -102,6 +103,7 @@
102103
'django.middleware.security.SecurityMiddleware',
103104
'django.contrib.sessions.middleware.SessionMiddleware',
104105
'social_django.middleware.SocialAuthExceptionMiddleware',
106+
'corsheaders.middleware.CorsMiddleware',
105107
'django.middleware.common.CommonMiddleware',
106108
'django.middleware.csrf.CsrfViewMiddleware',
107109
'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -136,33 +138,33 @@
136138
DATABASES = {
137139
'default': {
138140
'ENGINE': 'django.db.backends.postgresql_psycopg2',
139-
'NAME': os.environ.get('RDS_DB_NAME', 'postgres'),
140-
'USER': os.environ.get('RDS_USERNAME', 'postgres'),
141-
'PASSWORD': os.environ.get('RDS_PASSWORD', 'postgres'),
142-
'HOST': os.environ.get('RDS_HOSTNAME', 'postgres'),
143-
'PORT': os.environ.get('RDS_PORT', 5432),
141+
'NAME': os.environ.get('POSTGRES_NAME', 'postgres'),
142+
'USER': os.environ.get('POSTGRES_USERNAME', 'postgres'),
143+
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'postgres'),
144+
'HOST': os.environ.get('POSTGRES_SERVICE_HOST', 'postgres'),
145+
'PORT': os.environ.get('POSTGRES_SERVICE_PORT', 5432),
144146
}
145147
}
146148

147149
ASGI_APPLICATION = 'backend.routing.application'
148150

149-
ELASTICACHE_REDIS_HOST_NAME = \
151+
REDIS_SERVICE_HOST = \
150152
os.environ.get(
151-
'CELERY_BROKER_URL',
153+
'REDIS_SERVICE_HOST',
152154
'redis://redis:6379'
153-
)[8:].split(':')[0]
155+
)
154156

155157
CHANNEL_LAYERS = {
156158
'default': {
157159
'BACKEND': 'channels_redis.core.RedisChannelLayer',
158160
'CONFIG': {
159-
"hosts": [(ELASTICACHE_REDIS_HOST_NAME, 6379)],
161+
"hosts": [(REDIS_SERVICE_HOST, 6379)],
160162
},
161163
},
162164
}
163165

164166
REDIS = redis.Redis(
165-
host=ELASTICACHE_REDIS_HOST_NAME,
167+
host=REDIS_SERVICE_HOST,
166168
port=6379,
167169
db=3,
168170
charset="utf-8",
@@ -186,10 +188,6 @@
186188

187189
# Celery Configuration
188190

189-
CELERY_BROKER_URL = \
190-
os.environ.get('CELERY_BROKER_URL', 'redis://redis:6379')
191-
CELERY_RESULT_BACKEND = \
192-
os.environ.get('CELERY_RESULT_BACKEND', 'redis://redis:6379')
193191
CELERY_ACCEPT_CONTENT = ['application/json']
194192
CELERY_TASK_SERIALIZER = 'json'
195193
CELERY_RESULT_SERIALIZER = 'json'
@@ -234,8 +232,7 @@
234232
# https://docs.djangoproject.com/en/2.1/howto/static-files/
235233

236234
AWS_DEFAULT_ACL = None
237-
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', 'key_id')
238-
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', 'key')
235+
239236
AWS_STORAGE_BUCKET_NAME = os.environ.get(
240237
'AWS_STORAGE_BUCKET_NAME', 'bucketname')
241238
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"

backend/backend/settings/development.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
SECRET_KEY = "my-secret-key"
44

5-
DATABASES = {
6-
'default': {
7-
'ENGINE': 'django.db.backends.postgresql_psycopg2',
8-
'NAME': os.environ.get('RDS_DB_NAME', 'postgres'), # noqa
9-
'USER': os.environ.get('RDS_USERNAME', 'postgres'), # noqa
10-
'PASSWORD': os.environ.get('RDS_PASSWORD', 'postgres'), # noqa
11-
'HOST': os.environ.get('RDS_HOSTNAME', 'postgres'), # noqa
12-
'PORT': os.environ.get('RDS_PORT', 5432), # noqa
13-
}
14-
}
5+
# Celery
6+
7+
CELERY_BROKER_URL = \
8+
os.environ.get('CELERY_BROKER_URL', 'redis://redis:6379') # noqa
9+
CELERY_RESULT_BACKEND = \
10+
os.environ.get('CELERY_RESULT_BACKEND', 'redis://redis:6379') # noqa
1511

1612
DEBUG_APPS = [
1713
'django_extensions',
@@ -65,6 +61,9 @@ def show_toolbar(request):
6561
'--no-browser',
6662
]
6763

64+
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', 'key_id') # noqa
65+
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', 'key') # noqa
66+
6867
STATIC_URL = '/static/'
6968

7069
STATIC_ROOT = '/static/'

backend/backend/settings/production.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@
3636
},
3737
},
3838
}
39+
40+
# Celery
41+
42+
CELERY_BROKER_URL = f"redis://{REDIS_SERVICE_HOST}:6379/0" # noqa
43+
CELERY_RESULT_BACKEND = f"redis://{REDIS_SERVICE_HOST}:6379/0" # noqa

backend/core/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def hello_world(request):
4545
response = JsonResponse(
4646
{
4747
'message': 'Hello, World!',
48-
'git_sha': os.environ.get('GIT_SHA', '<git sha>')
48+
'git_sha': os.environ.get('GIT_SHA', '<git SHA>'),
49+
'debug': settings.DEBUG,
50+
'format': 'JSON'
4951
}
5052
)
5153
return response

backend/scripts/prod/start_prod.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/bin/bash
22

33
python3 manage.py collectstatic --no-input
4-
python3 manage.py makemigrations
54
python3 manage.py migrate --no-input
65
gunicorn -t 300 -b 0.0.0.0:8000 backend.wsgi

cloudformation/infrastructure/ecr-repository.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ Description: >
22
This template deploys an ECR Repository that we will use when pushing our backend container images.
33
44
Parameters:
5+
6+
StackName:
7+
Type: String
8+
Description: The name of the stack
9+
510
ECRBackendRepositoryName:
611
Type: "String"
712
Description: "The name of the ECR Repository for our backend conainer."
@@ -13,5 +18,6 @@ Resources:
1318
RepositoryName: !Ref ECRBackendRepositoryName
1419

1520
Outputs:
16-
ECRBackendRepositoryURL:
21+
EcrBackendRepo:
22+
Description: The ECR Repository for the main backend container
1723
Value: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${ECRBackendRepositoryName}"

cloudformation/infrastructure/ecs-cluster.yaml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Description: >
33
44
Parameters:
55

6+
StackName:
7+
Type: String
8+
Description: The name of the stack
9+
610
EnvironmentName:
711
Type: String
812
Description: An environemnt name to namespace cluster resources (e.g. qa, staging, production)
@@ -17,10 +21,6 @@ Parameters:
1721
r5.large, r5.xlarge, r5.2xlarge, r5.4xlarge, r5.12xlarge, r5.24xlarge ]
1822
ConstraintDescription: Please choose a valid instance type.
1923

20-
VPC:
21-
Description: Main VPC
22-
Type: AWS::EC2::VPC::Id
23-
2424
LoadBalancerSecurityGroup:
2525
Type: AWS::EC2::SecurityGroup::Id
2626
Description: Security group for the load balancer
@@ -57,12 +57,16 @@ Resources:
5757

5858
ECSCluster:
5959
Type: AWS::ECS::Cluster
60+
Properties:
61+
ClusterName: !Sub ${EnvironmentName}-cluster
6062

6163
ContainerInstanceSecurityGroup:
6264
Type: AWS::EC2::SecurityGroup
6365
Properties:
6466
GroupDescription: Access to the ECS hosts that run containers.
65-
VpcId: !Ref 'VPC'
67+
VpcId:
68+
Fn::ImportValue:
69+
!Sub ${StackName}:VpcId
6670
SecurityGroupIngress:
6771
- SourceSecurityGroupId: !Ref LoadBalancerSecurityGroup
6872
IpProtocol: -1
@@ -173,6 +177,29 @@ Resources:
173177
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
174178
- arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
175179
Policies:
180+
- PolicyName: s3-access
181+
PolicyDocument: !Sub |
182+
{
183+
"Version": "2012-10-17",
184+
"Statement": [
185+
{
186+
"Sid": "VisualEditor0",
187+
"Effect": "Allow",
188+
"Action": [
189+
"s3:PutObject",
190+
"s3:GetObjectAcl",
191+
"s3:GetObject",
192+
"s3:ListBucket",
193+
"s3:DeleteObject",
194+
"s3:PutObjectAcl"
195+
],
196+
"Resource": [
197+
"arn:aws:s3:::${StackName}-assets/*",
198+
"arn:aws:s3:::${StackName}-assets"
199+
]
200+
}
201+
]
202+
}
176203
- PolicyName: ecs-service
177204
PolicyDocument: |
178205
{
@@ -318,7 +345,7 @@ Outputs:
318345
Description: The name of the ECS cluster
319346
Value: !Ref 'ECSCluster'
320347
Export:
321-
Name: !Sub ${EnvironmentName}:ClusterName
348+
Name: !Sub ${StackName}:ECSCluster
322349

323350
AutoScalingRole:
324351
Description: The ARN of the role used for auto-scaling

0 commit comments

Comments
 (0)