Skip to content

Commit

Permalink
cd fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Apr 9, 2024
1 parent 68faed8 commit 5d05154
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ POSTGRES_DB=
POSTGRES_HOST=
POSTGRES_PORT=
AUTH_TOKEN=
TIF_PATH=
TIFF_PATH=
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ POSTGRES_DB=amazonia360
POSTGRES_HOST=postgis
POSTGRES_PORT=5432
AUTH_TOKEN=super_secret_token
TIF_PATH=/opt/api/test_data
TIFF_PATH=/opt/api/test_data
14 changes: 8 additions & 6 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 --abort-on-container-exit --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
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
*.venv
*data/
.env
.env.BCK
/env/
/infrastructure/vars/local.tfvars
__pycache__/
2 changes: 1 addition & 1 deletion api/app/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)}


Expand Down
4 changes: 2 additions & 2 deletions api/app/routers/zonal_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions api/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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],
Expand All @@ -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():
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/modules/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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")
}

0 comments on commit 5d05154

Please sign in to comment.