Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awsify #16

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*
!_targets.R
!in/
!src/
!docker/
!aws/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
_targets/
*.Rproj
*.svg
# vim swap files
.*.swp
.swp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repository uses the [`targets` package](https://docs.ropensci.org/targets/)

Install all packages needed to run:
```
install.packages(c('targets', 'tidyverse', 'lubridate', 'geofacet', 'cowplot','ggfx', 'showtext', 'magick', 'maps', 'svglite', 'xml2'))
install.packages(c('targets', 'tidyverse', 'lubridate', 'geofacet', 'cowplot','ggfx', 'showtext', 'magick', 'maps', 'svglite', 'xml2', 'dataRetrieval', 'rsvg'))
```

Running `tar_make()` will produce:
Expand Down
43 changes: 33 additions & 10 deletions _targets.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
library(targets)

options(tidyverse.quiet = TRUE)
tar_option_set(packages = c('tidyverse', 'lubridate', 'geofacet', 'cowplot','ggfx', 'showtext', 'xml2'))
tar_option_set(
packages = c(
'cowplot',
'geofacet',
'ggfx',
'lubridate',
'showtext',
'tidyverse',
'xml2'
)
)

source("src/prep_data.R")
source("src/plot_cartogram.R")
Expand All @@ -18,7 +28,8 @@ list(
# Read in data from gage-flow-conditions pipeline output
tar_target(
dv,
read_csv("https://labs.waterdata.usgs.gov/visualizations/data/flow_conditions_202211.csv", col_types = "cTnnnn")
#read_csv("https://labs.waterdata.usgs.gov/visualizations/data/flow_conditions_202209_full.csv", col_types = "cTnnnn")
readRDS("/ephemeral/dv_stats.rds")
),
tar_target(
date_start,
Expand Down Expand Up @@ -84,22 +95,34 @@ list(
plot_nat,
plot_national_area(national_data = flow_national, pal = pal_wetdry, date_start, date_end, color_bknd)
),
# Build a string for the prior month, e.g. 2022-11
tar_target(
prior_month,
strftime(Sys.Date() - lubridate::days(lubridate::day(Sys.Date())),
format = "%Y-%m")
),
# Combine charts and assemble final plot
tar_target(
flow_cartogram_svg,
combine_plots(file_svg = "flow_cartogram.svg",
plot_left = plot_nat,
plot_right = plot_cart,
date_start,
width = 16, height = 9, color_bknd),
combine_plots(
file_svg = paste("flow_cartogram_", prior_month, ".svg", sep = ""),
plot_left = plot_nat,
plot_right = plot_cart,
date_start,
width = 16,
height = 9,
color_bknd
),
format = "file"
),
# Remove facet clipping and save as png
tar_target(
flow_cartogram_png,
rm_facet_clip(svg_in = flow_cartogram_svg,
file_out = "flow_cartogram.png",
width = 16),
rm_facet_clip(
svg_in = flow_cartogram_svg,
file_out = paste("flow_cartogram_", prior_month, ".png", sep = ""),
width = 16
),
format = "file"
)
)
22 changes: 22 additions & 0 deletions aws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Docker Image
### Building the Image
```bash
cd docker
docker-compose build
```

This builds an image with the name `807615458658.dkr.ecr.us-west-2.amazonaws.com/vizlab/flow-tiles:latest`. It also will tag this with the name `flow-tiles`, allowing for convenient local runs a la `docker run -it flow-tiles bash`.

### Pushing the Image to ECR
```bash
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 807615458658.dkr.ecr.us-west-2.amazonaws.com
cd docker
docker-compose push
# line above will throw an error when it tries to push the short name
```

Image will now be available as `807615458658.dkr.ecr.us-west-2.amazonaws.com/vizlab/flow-tiles`.

## ECS Setup
* Created a "network-only" cluster named `vizlab-cluster` on ECS.
* Copied the task definition for fetch-daily-obs-task
136 changes: 136 additions & 0 deletions aws/flow-tiles-task.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"executionRoleArn": "arn:aws:iam::807615458658:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/flow-tiles-task",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"portMappings": [],
"command": [
"s3",
"cp",
"s3://vizlab-flow-tiles/in/dv_stats.rds",
"/ephemeral/"
],
"cpu": 0,
"environment": [],
"mountPoints": [
{
"containerPath": "/ephemeral",
"sourceVolume": "bind-mount"
}
],
"volumesFrom": [],
"image": "amazon/aws-cli:2.4.6",
"essential": false,
"name": "get-gage-conditions"
},
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/flow-tiles-task",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"portMappings": [],
"command": [
"bash",
"aws/run-pipeline.sh"
],
"cpu": 0,
"environment": [],
"mountPoints": [
{
"containerPath": "/ephemeral",
"sourceVolume": "bind-mount"
}
],
"workingDirectory": "/flow-tiles",
"volumesFrom": [],
"image": "807615458658.dkr.ecr.us-west-2.amazonaws.com/vizlab/flow-tiles:latest",
"dependsOn": [
{
"containerName": "get-gage-conditions",
"condition": "SUCCESS"
}
],
"essential": false,
"name": "build-viz"
},
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/flow-tiles-task",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
},
"portMappings": [],
"command": [
"s3",
"cp",
"--recursive",
"/ephemeral/",
"s3://vizlab-flow-tiles/out/",
"--exclude='*'",
"--include='flow_cartogram*'"
],
"cpu": 0,
"environment": [],
"mountPoints": [
{
"containerPath": "/ephemeral",
"sourceVolume": "bind-mount"
}
],
"volumesFrom": [],
"image": "amazon/aws-cli:2.4.6",
"dependsOn": [
{
"containerName": "build-viz",
"condition": "SUCCESS"
}
],
"essential": true,
"name": "upload-result"
}
],
"memory": "2048",
"taskRoleArn": "arn:aws:iam::807615458658:role/ecsTaskExecutionRole",
"family": "vizlab-flow-tiles-task",
"requiresCompatibilities": [
"FARGATE"
],
"networkMode": "awsvpc",
"runtimePlatform": {
"operatingSystemFamily": "LINUX"
},
"cpu": "256",
"inferenceAccelerators": [],
"volumes": [
{
"name": "bind-mount",
"host": {
"sourcePath": null
}
}
],
"tags": [
{
"key": "wma:project_id",
"value": "vizlab"
},
{
"key": "wma:contact",
"value": "Jesse Ross [email protected]"
}
]
}
4 changes: 4 additions & 0 deletions aws/run-pipeline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

Rscript -e 'targets::tar_make()'
cp flow_cartogram* /ephemeral/
82 changes: 82 additions & 0 deletions aws/state_machine.asm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"Comment": "Run gage-conditions and create the flow-tiles PNG",
"StartAt": "Run gage-conditions",
"TimeoutSeconds": 7200,
"States": {
"Run gage-conditions": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:us-west-2:807615458658:cluster/vizlab-cluster",
"TaskDefinition": "arn:aws:ecs:us-west-2:807615458658:task-definition/vizlab-gage-conditions-task:12",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"subnet-0b250cbaebb683b8d"
],
"AssignPublicIp": "ENABLED"
}
}
},
"Next": "Create flow-tiles PNG",
"Catch": [
{
"ErrorEquals": [ "States.ALL" ],
"Next": "Notify gage-conditions Failure"
}
]
},
"Create flow-tiles PNG": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:us-west-2:807615458658:cluster/vizlab-cluster",
"TaskDefinition": "arn:aws:ecs:us-west-2:807615458658:task-definition/vizlab-flow-tiles-task:14",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"subnet-0b250cbaebb683b8d"
],
"AssignPublicIp": "ENABLED"
}
}
},
"Next": "Notify Success",
"Catch": [
{
"ErrorEquals": [ "States.ALL" ],
"Next": "Notify flow-tiles Failure"
}
]
},
"Notify Success": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"Message": "The pipeline to build the flow-tiles visualization has run successfully. See the result: https://s3.console.aws.amazon.com/s3/buckets/vizlab-flow-tiles?region=us-west-2&prefix=out/&showversions=false",
"TopicArn": "arn:aws:sns:us-west-2:807615458658:vizlab-flow-tiles-pipeline"
},
"End": true
},
"Notify gage-conditions Failure": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"Message": "The pipeline to build the flow-tiles visualization has FAILED in the gage-conditions task. See logs in the developer account: https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logsV2:log-groups/log-group/$252Fecs$252Fgage-conditions-task",
"TopicArn": "arn:aws:sns:us-west-2:807615458658:vizlab-flow-tiles-pipeline"
},
"End": true
},
"Notify flow-tiles Failure": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"Message": "The pipeline to build the flow-tiles visualization has FAILED in the flow-tiles task. See logs in the developer account: https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logsV2:log-groups/log-group/$252Fecs$252Fflow-tiles-task",
"TopicArn": "arn:aws:sns:us-west-2:807615458658:vizlab-flow-tiles-pipeline"
},
"End": true
}
}
}
56 changes: 56 additions & 0 deletions docker/DOIRootCA2.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-----BEGIN CERTIFICATE-----
MIIJ+jCCB+KgAwIBAgIQSeV7u0WVHrFBqkjcKiEnEzANBgkqhkiG9w0BAQsFADAV
MRMwEQYDVQQDEwpET0lSb290Q0EyMB4XDTE2MDQyNjE4MjE1MFoXDTM2MDQyNjE4
MjE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