Skip to content

Commit 33e9f7f

Browse files
committed
Rework redis-geo to use leaflet and OSM.
Due to osm reverse lookup data differences, introduce mapping of state codes.
1 parent a87104a commit 33e9f7f

14 files changed

+524
-96
lines changed

.github/workflows/README.md

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# ServiceStack mix GitHub Actions
2+
`release.yml` generated from `x mix release-ecr-aws`, this template in designed to help with automating CI deployments to AWS ECS and dedicated AWS ECS cluster.
3+
This is a cheap way to start without an AWS Application Load Balancer (ALB) and also be in a situation that will easier to add one once the web service needs additional scale or high availability.
4+
5+
## Overview
6+
`release.yml` is designed to work with a ServiceStack app templates deploying directly to a single server in a dedicated ECS cluster via templated GitHub Actions.
7+
8+
## Setup
9+
### Create unique ECS cluster
10+
For this setup, it is best to create a separate cluster as cluster will only have the single instance in it running.
11+
This pattern is to start from a good base with AWS ECS and automated CI deployment while avoiding the higher costs of needing to run an application load balancer (ALB).
12+
13+
If/when you can justify the cost of an ALB for easier scaling and zero downtime deployment, the GitHub Action `release.yml` can be slightly modified to be used with a re-created or different ECS Service that is configured to be used with an Application Load Balancer and Target Group.
14+
15+
### Elastic IP (optional)
16+
The reason you might want to register this first is because we are only running one EC2 instance and hosting our own `nginx-proxy` on the same instance as the applications.
17+
Since an `A` record will be pointing there, one advantage of not using an auto-assigned IP is that we can reassign the elastic IP if for what ever reason the instance goes down or is lost.
18+
19+
## Launch to EC2 Instance
20+
When launching the EC2 instance, you'll need to select an 'ECS optimized' AMI as the image used for your instance.
21+
### Choose AMI
22+
The easiest way to find the latest Amazon Linux 2 image for this is to go to the [AWS documentation for ECS-optimized AMIs and look up your region here](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html#ecs-optimized-ami-linux).
23+
24+
Using the AMI ID (starts with `ami-`) at the bottom, search in the 'Community AMIs' tab on the first step of the `Launch EC2 Instance` wizard.
25+
26+
### Choose Instance Type
27+
A t2.micro or larger will work fine, this pattern can be used to host multiple applications on the 1 server so if the number of applications gets larger, you might need a larger instance type.
28+
> Note this pattern is suitable for testing prototypes or low traffic applications as it is cost effective and makes it easy to bundle multiple apps onto 1 EC2 instance.
29+
30+
### Configure Instance
31+
Under `IAM role`, use the `ecsInstanceRole`, if this is not available, see [AWS documentation for the process of checking if it exists and creating it if needed](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html).
32+
33+
You will also want to add the following Userdata script (in the `Configure` step of the launch wizard) with your own `ECS_CLUSTER` value. This tells the ecs-agent running on the instance which ECS cluster the instance should join.
34+
35+
```bash
36+
#!/bin/bash
37+
cat <<EOS >/etc/ecs/ecs.config
38+
ECS_CLUSTER=redis-geos
39+
ECS_AVAILABLE_LOGGING_DRIVERS=["awslogs", "syslog"]
40+
ECS_ENABLE_CONTAINER_METADATA=true
41+
EOS
42+
```
43+
44+
Note down your cluster name as it will need to be used to create the cluster in ECS before it is visible.
45+
See [`ECS Container Agent Configuration`](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html) for more information.
46+
47+
### Add Storage
48+
The default of 30gb is fine but take into account how large/how many applications you'll have running.
49+
50+
### Configure Security Groups
51+
You'll want to expose at least ports 80 and 443.
52+
53+
### Setup Docker-compose and nginx-proxy
54+
To let your server handle multiple ServiceStack applications and automate the generation and management of TLS certificates, an additional docker-compose file is provided via the `x mix` template, `nginx-proxy-compose.yml`. This docker-compose file is ready to run and can be copied to the deployment server.
55+
> This is done via docker-compose rather than via ECS for simplicity.
56+
57+
For example, once copied to remote `~/nginx-proxy-compose.yml`, the following command can be run on the remote server.
58+
59+
```
60+
docker-compose -f ~/nginx-proxy-compose.yml up -d
61+
```
62+
63+
This will run an nginx reverse proxy along with a companion container that will watch for additional containers in the same docker network and attempt to initialize them with valid TLS certificates.
64+
65+
## GitHub Repository setup
66+
The `release.yml` assumes 6 secrets have been setup.
67+
68+
- AWS_ACCESS_KEY_ID - AWS access key for programmatic access to AWS APIs.
69+
- AWS_SECRET_ACCESS_KEY - AWS access secrets for programmatic access to AWS APIs.
70+
- AWS_REGION - default region for AWS API calls.
71+
- AWS_ECS_CLUSTER - Cluster name in ECS, this should match the value in your Userdata.
72+
- HOST_DOMAIN - Domain/submain of your application, eg `redis-geo.example.com` .
73+
- LETSENCRYPT_EMAIL - Email address, required for Let's Encrypt automated TLS certificates.
74+
75+
These secrets are used to populate variables within GitHub Actions and other configuration files.
76+
77+
For the AWS access, a separate user specifically for deploying via GitHub Actions should be used.
78+
79+
The policies required for the complete initial setup will be:
80+
- `AmazonEC2ContainerRegistryFullAccess`
81+
- `AmazonECS_FullAccess`
82+
83+
Once the application is successfully deployed the first time, reduced access for both ECR and ECS can be used instead. For application updates, the GitHub Action can use the following policy.
84+
85+
```json
86+
{
87+
"Version": "2012-10-17",
88+
"Statement": [
89+
{
90+
"Sid": "VisualEditor0",
91+
"Effect": "Allow",
92+
"Action": [
93+
"ecr:GetRegistryPolicy",
94+
"ecr:PutImageTagMutability",
95+
"ecr:GetDownloadUrlForLayer",
96+
"ecr:DescribeRegistry",
97+
"ecr:GetAuthorizationToken",
98+
"ecr:ListTagsForResource",
99+
"ecr:UploadLayerPart",
100+
"ecr:ListImages",
101+
"ecr:PutImage",
102+
"ecr:UntagResource",
103+
"ecr:BatchGetImage",
104+
"ecr:CompleteLayerUpload",
105+
"ecr:DescribeImages",
106+
"ecr:TagResource",
107+
"ecr:DescribeRepositories",
108+
"ecr:InitiateLayerUpload",
109+
"ecr:BatchCheckLayerAvailability",
110+
"ecr:ReplicateImage",
111+
"ecr:GetRepositoryPolicy",
112+
"ecs:SubmitTaskStateChange",
113+
"ecs:UpdateContainerInstancesState",
114+
"ecs:RegisterContainerInstance",
115+
"ecs:DescribeTaskDefinition",
116+
"ecs:DescribeClusters",
117+
"ecs:ListServices",
118+
"ecs:UpdateService",
119+
"ecs:ListTasks",
120+
"ecs:ListTaskDefinitionFamilies",
121+
"ecs:RegisterTaskDefinition",
122+
"ecs:SubmitContainerStateChange",
123+
"ecs:StopTask",
124+
"ecs:DescribeServices",
125+
"ecs:ListContainerInstances",
126+
"ecs:DescribeContainerInstances",
127+
"ecs:DeregisterContainerInstance",
128+
"ecs:TagResource",
129+
"ecs:DescribeTasks",
130+
"ecs:UntagResource",
131+
"ecs:ListTaskDefinitions",
132+
"ecs:ListClusters"
133+
],
134+
"Resource": "*"
135+
}
136+
]
137+
}
138+
```
139+
> Further permission reduction can be done by reducing what resources can be accessed.
140+
> Application permissions can be controlled via `taskRoleArn`, see [AWS docs for details](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html).
141+
142+
## What's the process of the `release.yml`?
143+
144+
![](https://raw.githubusercontent.com/ServiceStack/docs/master/docs/images/mix/release-ecr-aws-diagram.png)

.github/workflows/build.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Build
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
branches:
7+
- '**' # matches every branch
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-20.04
12+
steps:
13+
- name: checkout
14+
uses: actions/[email protected]
15+
16+
- name: setup .net core
17+
uses: actions/[email protected]
18+
with:
19+
dotnet-version: 5.0.100
20+
21+
- name: build
22+
run: dotnet build
23+
working-directory: .
24+
25+
- name: test
26+
run: |
27+
dotnet test
28+
if [ $? -eq 0 ]; then
29+
echo TESTS PASSED
30+
else
31+
echo TESTS FAILED
32+
exit 1
33+
fi
34+
working-directory: ./RedisGeo.Tests
35+

.github/workflows/release.yml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: Release
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
push_to_ecr:
7+
runs-on: ubuntu-20.04
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v2
11+
12+
- name: repository name fix
13+
run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
14+
15+
- name: Configure AWS credentials
16+
uses: aws-actions/configure-aws-credentials@v1
17+
with:
18+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
19+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
20+
aws-region: ${{ secrets.AWS_REGION }}
21+
22+
- name: Login to Amazon ECR
23+
id: login_ecr
24+
uses: aws-actions/amazon-ecr-login@v1
25+
26+
- name: Create ECR repo if not exists.
27+
env:
28+
ECR_REPOSITORY: ${{ env.image_repository_name }}
29+
run: aws ecr describe-repositories --repository-names ${ECR_REPOSITORY} || aws ecr create-repository --repository-name ${ECR_REPOSITORY}
30+
31+
- name: Build and push to ECR
32+
id: push_image_to_ecr
33+
uses: docker/[email protected]
34+
with:
35+
file: Dockerfile
36+
context: .
37+
push: true
38+
tags: ${{ steps.login_ecr.outputs.registry }}/${{ env.image_repository_name }}:${{ github.event.release.tag_name }}
39+
40+
deploy_ecs:
41+
needs: push_to_ecr
42+
runs-on: ubuntu-20.04
43+
steps:
44+
- name: checkout
45+
uses: actions/checkout@v2
46+
47+
- name: Configure AWS credentials
48+
uses: aws-actions/configure-aws-credentials@v1
49+
with:
50+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
51+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
52+
aws-region: ${{ secrets.AWS_REGION }}
53+
54+
- name: Login to Amazon ECR
55+
id: login_ecr
56+
uses: aws-actions/amazon-ecr-login@v1
57+
58+
- name: Repository name fix and env values setup
59+
run: |
60+
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
61+
echo "domain=${{ secrets.HOST_DOMAIN }}" >> $GITHUB_ENV
62+
echo "letsencrypt_email=${{ secrets.LETSENCRYPT_EMAIL }}" >> $GITHUB_ENV
63+
echo "app_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | cut -d'/' -f2)" >> $GITHUB_ENV
64+
echo "cluster_name=${{ secrets.AWS_ECS_CLUSTER }}" >> $GITHUB_ENV
65+
echo "image_url=${{ steps.login_ecr.outputs.registry }}/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]'):${{ github.event.release.tag_name }}" >> $GITHUB_ENV
66+
echo "aws_region=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
67+
68+
- name: Populate task definition template
69+
uses: danielr1996/[email protected]
70+
env:
71+
RELEASE_VERSION: ${{ github.event.release.tag_name }}
72+
APP_NAME: ${{ env.app_name }}
73+
IMAGE_URL: ${{ env.image_url }}
74+
HOST_DOMAIN: ${{ env.domain }}
75+
LETSENCRYPT_EMAIL: ${{ env.letsencrypt_email }}
76+
AWS_REGION: ${{ env.aws_region }}
77+
CLUSTER_NAME: ${{ env.cluster_name }}
78+
with:
79+
input: deploy/task-definition-template.json
80+
output: deploy/task-definition.json
81+
82+
- name: Create task definition if doesn't exist
83+
run: aws ecs describe-task-definition --task-definition ${{ env.app_name }} || aws ecs register-task-definition --cli-input-json file://deploy/task-definition.json
84+
85+
- name: Create ECS Service if not exists.
86+
run: aws ecs describe-services --cluster ${{ env.cluster_name }} --services ${{ env.app_name }} | jq '.services[0]' -e || aws ecs create-service --cluster ${{ env.cluster_name }} --service-name ${{ env.app_name }} --task-definition ${{ env.app_name }} --desired-count 1
87+
88+
- name: Deploy new revision of the task definition
89+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
90+
with:
91+
task-definition: deploy/task-definition.json
92+
service: ${{ env.app_name }}
93+
cluster: ${{ env.cluster_name }}
94+
force-new-deployment: true

.travis.yml

-19
This file was deleted.

Dockerfile

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
2-
COPY src /app
3-
WORKDIR /app
2+
WORKDIR /source
3+
4+
COPY . .
5+
RUN dotnet restore ./src/RedisGeo.sln
46

5-
RUN dotnet restore
6-
RUN dotnet publish -c Release -o out
7+
WORKDIR /source/src/RedisGeo
8+
RUN dotnet publish -c release -o /app --no-restore
79

8-
# Build runtime image
910
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
1011
WORKDIR /app
11-
COPY --from=build /app/RedisGeo/out .
12-
ENV ASPNETCORE_URLS http://*:5000
12+
COPY --from=build /app ./
1313
ENTRYPOINT ["dotnet", "RedisGeo.dll"]

deploy-envs.sh

-13
This file was deleted.

deploy/nginx-proxy-compose.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
version: '2'
2+
3+
services:
4+
nginx-proxy:
5+
image: jwilder/nginx-proxy
6+
container_name: nginx-proxy
7+
restart: always
8+
ports:
9+
- "80:80"
10+
- "443:443"
11+
volumes:
12+
- conf:/etc/nginx/conf.d
13+
- vhost:/etc/nginx/vhost.d
14+
- html:/usr/share/nginx/html
15+
- dhparam:/etc/nginx/dhparam
16+
- certs:/etc/nginx/certs:ro
17+
- /var/run/docker.sock:/tmp/docker.sock:ro
18+
network_mode: bridge
19+
20+
letsencrypt:
21+
image: jrcs/letsencrypt-nginx-proxy-companion
22+
container_name: nginx-proxy-le
23+
restart: always
24+
environment:
25+
26+
volumes_from:
27+
- nginx-proxy
28+
volumes:
29+
- certs:/etc/nginx/certs:rw
30+
- acme:/etc/acme.sh
31+
- /var/run/docker.sock:/var/run/docker.sock:ro
32+
network_mode: bridge
33+
34+
volumes:
35+
conf:
36+
vhost:
37+
html:
38+
dhparam:
39+
certs:
40+
acme:
41+
42+
networks:
43+
default:
44+
external:
45+
name: webproxy

0 commit comments

Comments
 (0)