From 0a7a1f1f1ea4e25a2ca6707962fb31593e1483f9 Mon Sep 17 00:00:00 2001 From: alexeh Date: Tue, 9 Apr 2024 06:17:19 +0300 Subject: [PATCH] cd fixes --- .env.default | 2 +- .env.test | 2 +- .github/workflows/cicd.yml | 14 ++++++++------ .gitignore | 1 - api/app/config/config.py | 2 +- api/app/main.py | 8 ++++---- api/app/routers/zonal_stats.py | 4 ++-- api/tests/test_main.py | 14 +++++++------- docker-compose.yml | 5 +++++ infrastructure/modules/ec2/main.tf | 4 ++-- 10 files changed, 31 insertions(+), 25 deletions(-) diff --git a/.env.default b/.env.default index e8848bce..4c6593e3 100644 --- a/.env.default +++ b/.env.default @@ -4,4 +4,4 @@ POSTGRES_DB= POSTGRES_HOST= POSTGRES_PORT= AUTH_TOKEN= -TIF_PATH= \ No newline at end of file +TIFF_PATH= \ No newline at end of file diff --git a/.env.test b/.env.test index 17a89b59..6c3f0d98 100644 --- a/.env.test +++ b/.env.test @@ -4,4 +4,4 @@ POSTGRES_DB=amazonia360 POSTGRES_HOST=postgis POSTGRES_PORT=5432 AUTH_TOKEN=super_secret_token -TIF_PATH=/opt/api/test_data \ No newline at end of file +TIFF_PATH=/opt/api/test_data \ No newline at end of file diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 3fbe0e12..098035f6 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -20,15 +20,16 @@ jobs: uses: docker/setup-buildx-action@v1 - name: Build and run tests - run: docker-compose up --build test + run: docker-compose up --build --exit-code-from test test - name: Clean up run: docker-compose down deploy: + name: Deploy runs-on: ubuntu-latest needs: build-and-test - if: github.ref == 'refs/heads/develop' + #if: github.ref == 'refs/heads/develop' steps: - name: Deploy to EC2 @@ -38,15 +39,16 @@ jobs: username: ${{ secrets.USERNAME }} key: ${{ secrets.SSH_KEY }} script: | - cd amazonia360/amazonia-360 + cd amazonia-360 git pull - docker-compose down - docker-compose up -d api --build + sudo docker-compose down + sudo docker-compose up api --exit-code-from api health-check: + name: Health Check runs-on: ubuntu-latest needs: deploy - if: github.ref == 'refs/heads/develop' + #if: github.ref == 'refs/heads/develop' steps: - name: URL Health Check uses: Jtalk/url-health-check-action@v4 diff --git a/.gitignore b/.gitignore index 8218d9e1..1978ce3f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ *.venv *data/ .env -.env.BCK /env/ /infrastructure/vars/local.tfvars __pycache__/ diff --git a/api/app/config/config.py b/api/app/config/config.py index c5fd5668..acbfeb38 100644 --- a/api/app/config/config.py +++ b/api/app/config/config.py @@ -13,7 +13,7 @@ class Settings(BaseSettings): postgres_host: str postgres_port: str auth_token: str - tif_path: str + tiff_path: str @lru_cache def get_settings() -> Settings: diff --git a/api/app/main.py b/api/app/main.py index 509fdd61..52d8f271 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -14,8 +14,8 @@ def path_params(raster_filename: Annotated[str, Query(description="Raster filename.")]): """Dependency to get the path of the raster file.""" - tif_path = get_settings().tif_path - raster = os.path.join(tif_path, raster_filename) + tiff_path = get_settings().tiff_path + raster = os.path.join(tiff_path, raster_filename) if not os.path.exists(raster): raise HTTPException(status_code=404, detail=f"Raster file {raster} does not exist.") return raster @@ -35,8 +35,8 @@ def path_params(raster_filename: Annotated[str, Query(description="Raster filena @app.get("/tifs") async def list_files(): """List all available tif files.""" - tif_path = get_settings().tif_path - files = os.listdir(tif_path) + tiff_path = get_settings().tiff_path + files = os.listdir(tiff_path) return {"files": sorted(files)} diff --git a/api/app/routers/zonal_stats.py b/api/app/routers/zonal_stats.py index 530da88f..0f9911c5 100644 --- a/api/app/routers/zonal_stats.py +++ b/api/app/routers/zonal_stats.py @@ -65,8 +65,8 @@ def exact_zonal_stats( features = [geojson.model_dump()] with rasterio.Env(**env): - tif_path = get_settings().tif_path - src_path = os.path.join(tif_path, src_path) + tiff_path = get_settings().tiff_path + src_path = os.path.join(tiff_path, src_path) with rasterio.open(src_path, **reader_params) as src_dst: statistics = [op.value for op in statistics] # extract the values from the Enum stats = exact_extract(src_dst, features, ops=statistics) diff --git a/api/tests/test_main.py b/api/tests/test_main.py index cbdbf40b..41d0988b 100644 --- a/api/tests/test_main.py +++ b/api/tests/test_main.py @@ -36,11 +36,11 @@ @pytest.fixture() def setup_data_folder(): - os.mkdir(get_settings().tif_path) + os.mkdir(get_settings().tiff_path) yield - os.rmdir(get_settings().tif_path) + os.rmdir(get_settings().tiff_path) @pytest.fixture() @@ -57,7 +57,7 @@ def tif_file(setup_data_folder): data = np.array([[0, 1, 0], [1, 9, 1], [0, 1, 0]]) transform = rasterio.transform.from_origin(0, 10, 1, 1) with rasterio.open( - f"{get_settings().tif_path}/raster.tif", + f"{get_settings().tiff_path}/raster.tif", "w", driver="GTiff", width=data.shape[1], @@ -71,22 +71,22 @@ def tif_file(setup_data_folder): yield - os.remove(f"{get_settings().tif_path}/raster.tif") + os.remove(f"{get_settings().tiff_path}/raster.tif") @pytest.fixture() def setup_empty_files(setup_data_folder): - test_tif_path = get_settings().tif_path + test_tiff_path = get_settings().tiff_path for file in FILES: # Create empty files writing nothing - with open(f"{test_tif_path}/{file}", "w") as f: + with open(f"{test_tiff_path}/{file}", "w") as f: f.write("") yield for file in FILES: - os.remove(f"{test_tif_path}/{file}") + os.remove(f"{test_tiff_path}/{file}") def test_no_token(): diff --git a/docker-compose.yml b/docker-compose.yml index 52abd9ba..ec64011b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,11 @@ services: environment: - AUTH_TOKEN=${AUTH_TOKEN} - TIFF_PATH=${TIFF_PATH} + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_HOST=${POSTGRES_HOST} + - POSTGRES_PORT=${POSTGRES_PORT} networks: - amazonia360-network restart: diff --git a/infrastructure/modules/ec2/main.tf b/infrastructure/modules/ec2/main.tf index 994e955f..4c592d0a 100644 --- a/infrastructure/modules/ec2/main.tf +++ b/infrastructure/modules/ec2/main.tf @@ -8,7 +8,7 @@ resource "aws_instance" "amazonia360-ec2" { user_data = <<-EOF #!/bin/bash - echo "${var.authorized_key}" >> /home/ec2-user/.ssh/authorized_keys + echo "${var.authorized_key}" >> /home/ubuntu/.ssh/authorized_keys EOF tags = { @@ -19,5 +19,5 @@ resource "aws_instance" "amazonia360-ec2" { resource "aws_key_pair" "local_public_key" { key_name = "amazonia360-key-pair" - public_key = file("~/.ssh/id_rsa.pub") + public_key = file("~/.ssh/key_amazonia360_ec2.pub") }