From a898b3fceeed597e6a079fb439c6051d9c001c92 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 14 Aug 2023 17:00:29 +0300 Subject: [PATCH] infracost triggers fixes when there is no token --- .github/workflows/aws_caching_action.yml | 22 ------ .../aws_caching_action_with_infracost.yml | 64 ++++++++++++++++ .github/workflows/az_caching_action.yml | 22 ------ .../az_caching_action_with_infracost.yml | 73 +++++++++++++++++++ web/cluster_operations.py | 12 ++- 5 files changed, 147 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/aws_caching_action_with_infracost.yml create mode 100644 .github/workflows/az_caching_action_with_infracost.yml diff --git a/.github/workflows/aws_caching_action.yml b/.github/workflows/aws_caching_action.yml index 58c2f30..51ce8b2 100644 --- a/.github/workflows/aws_caching_action.yml +++ b/.github/workflows/aws_caching_action.yml @@ -23,42 +23,20 @@ jobs: with: python-version: 3.9 - - - name: Checkout infracost pricing repo - uses: actions/checkout@v3 - with: - repository: infracost/cloud-pricing-api - path: cloud-pricing-api - - name: Echo the values run: | echo "project_name is: ${{ github.event.client_payload.project_name }}" - echo "infracost_token is: ${{ github.event.client_payload.infracost_token }}" - name: Caching Tasks if: always() run: | - cd cloud-pricing-api - export SELF_HOSTED_INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" - export INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" - docker-compose run init_job - docker-compose ps - docker-compose up -d api - docker-compose ps - export PGPASSWORD=postgres - export POSTGRES_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cloud-pricing-api_postgres_1) - curl --retry 20 --retry-all-errors http://localhost:4000/health export MONGO_URL=${{ github.event.client_payload.mongo_url }} export MONGO_USER=${{ github.event.client_payload.mongo_user }} export MONGO_PASSWORD=${{ github.event.client_payload.mongo_password }} export PROJECT_NAME=${{ github.event.client_payload.project_name }} - export INFRACOST_TOKEN=${{ github.event.client_payload.infracost_token }} - export INFRACOST_URL=http://localhost:4000 - export POSTGRES_PORT=5432 export SECRET_KEY=${{ secrets.SECRET_KEY }} export GITHUB_ACTIONS_ENV=Yes export PYTHONPATH=$PYTHONPATH:. - cd .. pip3 install -r web/requirements.txt python3 web/scripts/aws_caching_script.py --aws-access-key-id ${{ github.event.client_payload.aws_access_key_id }} --aws-secret-access-key ${{ github.event.client_payload.aws_secret_access_key }} \ No newline at end of file diff --git a/.github/workflows/aws_caching_action_with_infracost.yml b/.github/workflows/aws_caching_action_with_infracost.yml new file mode 100644 index 0000000..790580b --- /dev/null +++ b/.github/workflows/aws_caching_action_with_infracost.yml @@ -0,0 +1,64 @@ +name: AWS Caching with Infracost +on: + repository_dispatch: + types: aws-caching-action-infracost-trigger + +jobs: + aws-caching-trigger: + runs-on: ubuntu-22.04 + steps: + - uses: 'actions/checkout@v3' + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1-node16 + with: + aws-access-key-id: ${{ github.event.client_payload.aws_access_key_id }} + aws-secret-access-key: ${{ github.event.client_payload.aws_secret_access_key }} + aws-region: us-east-1 + + - name: Checkout Code + uses: actions/checkout@v2 + - name: Set up Python 3.9 + uses: actions/setup-python@v4 + with: + python-version: 3.9 + + + - name: Checkout infracost pricing repo + uses: actions/checkout@v3 + with: + repository: infracost/cloud-pricing-api + path: cloud-pricing-api + + - name: Echo the values + run: | + echo "project_name is: ${{ github.event.client_payload.project_name }}" + echo "infracost_token is: ${{ github.event.client_payload.infracost_token }}" + + + - name: Caching Tasks + if: always() + run: | + cd cloud-pricing-api + export SELF_HOSTED_INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" + export INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" + docker-compose run init_job + docker-compose ps + docker-compose up -d api + docker-compose ps + export PGPASSWORD=postgres + export POSTGRES_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cloud-pricing-api_postgres_1) + curl --retry 20 --retry-all-errors http://localhost:4000/health + export MONGO_URL=${{ github.event.client_payload.mongo_url }} + export MONGO_USER=${{ github.event.client_payload.mongo_user }} + export MONGO_PASSWORD=${{ github.event.client_payload.mongo_password }} + export PROJECT_NAME=${{ github.event.client_payload.project_name }} + export INFRACOST_TOKEN=${{ github.event.client_payload.infracost_token }} + export INFRACOST_URL=http://localhost:4000 + export POSTGRES_PORT=5432 + export SECRET_KEY=${{ secrets.SECRET_KEY }} + export GITHUB_ACTIONS_ENV=Yes + export PYTHONPATH=$PYTHONPATH:. + cd .. + pip3 install -r web/requirements.txt + python3 web/scripts/aws_caching_script.py --aws-access-key-id ${{ github.event.client_payload.aws_access_key_id }} --aws-secret-access-key ${{ github.event.client_payload.aws_secret_access_key }} \ No newline at end of file diff --git a/.github/workflows/az_caching_action.yml b/.github/workflows/az_caching_action.yml index d6d6432..fc941df 100644 --- a/.github/workflows/az_caching_action.yml +++ b/.github/workflows/az_caching_action.yml @@ -28,7 +28,6 @@ jobs: echo "client_id is: ${{ github.event.client_payload.client_id }}" echo "client_secret is: ${{ github.event.client_payload.client_secret }}" echo "tenant_id is: ${{ github.event.client_payload.tenant_id }}" - echo "infracost_token is: ${{ github.event.client_payload.infracost_token }}" - name: Az CLI login uses: Azure/login@v1 @@ -38,36 +37,15 @@ jobs: creds: ${{ env.CREDENTIALS }} - - name: Checkout infracost pricing repo - uses: actions/checkout@v3 - with: - repository: infracost/cloud-pricing-api - path: cloud-pricing-api - - - name: Caching Tasks if: always() run: | - cd cloud-pricing-api - export SELF_HOSTED_INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" - export INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" - docker-compose run init_job - docker-compose ps - docker-compose up -d api - docker-compose ps - export PGPASSWORD=postgres - export POSTGRES_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cloud-pricing-api_postgres_1) - curl --retry 20 --retry-all-errors http://localhost:4000/health export MONGO_URL=${{ github.event.client_payload.mongo_url }} export MONGO_USER=${{ github.event.client_payload.mongo_user }} export MONGO_PASSWORD=${{ github.event.client_payload.mongo_password }} export PROJECT_NAME=${{ github.event.client_payload.project_name }} - export INFRACOST_TOKEN=${{ github.event.client_payload.infracost_token }} - export INFRACOST_URL=http://localhost:4000 - export POSTGRES_PORT=5432 export SECRET_KEY=${{ secrets.SECRET_KEY }} export GITHUB_ACTIONS_ENV=Yes export PYTHONPATH=$PYTHONPATH:. - cd .. pip3 install -r web/requirements.txt python3 web/scripts/az_caching_script.py diff --git a/.github/workflows/az_caching_action_with_infracost.yml b/.github/workflows/az_caching_action_with_infracost.yml new file mode 100644 index 0000000..5bb3d0d --- /dev/null +++ b/.github/workflows/az_caching_action_with_infracost.yml @@ -0,0 +1,73 @@ +name: AZ Caching with Infracost +on: + repository_dispatch: + types: az-caching-action-infracost-trigger + +jobs: + az_caching: + runs-on: ubuntu-22.04 + permissions: + contents: read + pages: write + id-token: write + steps: + - uses: 'actions/checkout@v3' + + - name: Checkout Code + uses: actions/checkout@v2 + - name: Set up Python 3.9 + uses: actions/setup-python@v4 + with: + python-version: 3.9 + + - name: Echo the values + run: | + echo "client_payload is: ${{ github.event.client_payload.client_payload }}" + echo "azure_credentials is: ${{ github.event.client_payload.azure_credentials }}" + echo "project_name is: ${{ github.event.client_payload.project_name }}" + echo "client_id is: ${{ github.event.client_payload.client_id }}" + echo "client_secret is: ${{ github.event.client_payload.client_secret }}" + echo "tenant_id is: ${{ github.event.client_payload.tenant_id }}" + echo "infracost_token is: ${{ github.event.client_payload.infracost_token }}" + + - name: Az CLI login + uses: Azure/login@v1 + env: + CREDENTIALS: ${{ github.event.client_payload.azure_credentials }} + with: + creds: ${{ env.CREDENTIALS }} + + + - name: Checkout infracost pricing repo + uses: actions/checkout@v3 + with: + repository: infracost/cloud-pricing-api + path: cloud-pricing-api + + + - name: Caching Tasks + if: always() + run: | + cd cloud-pricing-api + export SELF_HOSTED_INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" + export INFRACOST_API_KEY="${{ github.event.client_payload.infracost_token }}" + docker-compose run init_job + docker-compose ps + docker-compose up -d api + docker-compose ps + export PGPASSWORD=postgres + export POSTGRES_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cloud-pricing-api_postgres_1) + curl --retry 20 --retry-all-errors http://localhost:4000/health + export MONGO_URL=${{ github.event.client_payload.mongo_url }} + export MONGO_USER=${{ github.event.client_payload.mongo_user }} + export MONGO_PASSWORD=${{ github.event.client_payload.mongo_password }} + export PROJECT_NAME=${{ github.event.client_payload.project_name }} + export INFRACOST_TOKEN=${{ github.event.client_payload.infracost_token }} + export INFRACOST_URL=http://localhost:4000 + export POSTGRES_PORT=5432 + export SECRET_KEY=${{ secrets.SECRET_KEY }} + export GITHUB_ACTIONS_ENV=Yes + export PYTHONPATH=$PYTHONPATH:. + cd .. + pip3 install -r web/requirements.txt + python3 web/scripts/az_caching_script.py diff --git a/web/cluster_operations.py b/web/cluster_operations.py index 018d017..82bc219 100644 --- a/web/cluster_operations.py +++ b/web/cluster_operations.py @@ -313,8 +313,12 @@ def trigger_gcp_caching(self): return False def trigger_aws_caching(self): + if self.infracost_token: + event_type = "aws-caching-action-infracost-trigger" + else: + event_type = "aws-caching-action-trigger" json_data = { - "event_type": "aws-caching-action-trigger", + "event_type": event_type, "client_payload": {"project_name": self.project_name, "aws_access_key_id": self.aws_access_key_id, "aws_secret_access_key": self.aws_secret_access_key, @@ -336,8 +340,12 @@ def trigger_az_caching(self): client_id = json.loads(self.azure_credentials)['clientId'] client_secret = json.loads(self.azure_credentials)['clientSecret'] tenant_id = json.loads(self.azure_credentials)['tenantId'] + if self.infracost_token: + event_type = "az-caching-action-infracost-trigger" + else: + event_type = "az-caching-action-trigger" json_data = { - "event_type": "az-caching-action-trigger", + "event_type": event_type, "client_payload": {"azure_credentials": self.azure_credentials, "infracost_token": self.infracost_token, "client_id": client_id,