From f35c25c1992dc286949d8247f2233ee991c6ac5a Mon Sep 17 00:00:00 2001 From: Talgat Tairov Date: Tue, 1 Oct 2024 19:46:27 +0600 Subject: [PATCH 01/19] update --- .github/workflows/packer.yaml | 2 +- .gitignore | 4 +++- packer/apache-server.pkr.hcl | 26 +++++++++++++------------- packer/apache-server_variables.pkr.hcl | 2 +- packer/manifest-2.json | 14 -------------- packer/manifest.json | 14 ++++++++++++++ packer/sample_for_jq.json | 24 ------------------------ 7 files changed, 32 insertions(+), 54 deletions(-) delete mode 100644 packer/manifest-2.json delete mode 100644 packer/sample_for_jq.json diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index c36c6ea..cdeb5f0 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -63,7 +63,7 @@ jobs: echo "snapshot $snapshot_id deleted" fi fi - + # TODO: степ разделить тут cd ${{ github.event.repository.name }} cd packer packer init . diff --git a/.gitignore b/.gitignore index efe4764..22ce9a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .DS_Store -.terraform/ \ No newline at end of file +.terraform/ + +services/ diff --git a/packer/apache-server.pkr.hcl b/packer/apache-server.pkr.hcl index 00534d9..9867bfb 100644 --- a/packer/apache-server.pkr.hcl +++ b/packer/apache-server.pkr.hcl @@ -7,7 +7,7 @@ packer { } } -source "amazon-ebs" "default_webserver" { +source "amazon-ebs" "amazon-linux-sandbox" { region = "us-east-1" ami_name = var.ami_name instance_type = "t2.micro" @@ -29,9 +29,9 @@ source "amazon-ebs" "default_webserver" { } build { - name = "default_webserver" + name = "amazon-linux-sandbox" sources = [ - "source.amazon-ebs.default_webserver" + "source.amazon-ebs.amazon-linux-sandbox" ] provisioner "shell" { @@ -39,16 +39,16 @@ build { "GREETING=kek", ] inline = [ - # "sudo dnf group install -y 'Development Tools'", - # "sudo dnf install -y nc", - # "sudo dnf install -y telnet", - # "sudo dnf install -y jq", - # "sudo dnf install -y httpd", - # "sudo systemctl start httpd", - # "sudo systemctl enable httpd", - # "sudo systemctl status httpd", - # "echo '<Region> - <Node #>' | sudo tee /var/www/html/index.html > /dev/null",§ qA - "echo LOLKEK" + "sudo dnf group install -y 'Development Tools'", + "sudo dnf install -y nc", + "sudo dnf install -y telnet", + "sudo dnf install -y jq", + # TODO: yaml query yq + "sudo dnf install -y httpd", + "sudo systemctl start httpd", + "sudo systemctl enable httpd", + "sudo systemctl status httpd", + "echo '<Region> - <Node #>' | sudo tee /var/www/html/index.html > /dev/null" ] expect_disconnect = true } diff --git a/packer/apache-server_variables.pkr.hcl b/packer/apache-server_variables.pkr.hcl index 3f906cb..8176039 100644 --- a/packer/apache-server_variables.pkr.hcl +++ b/packer/apache-server_variables.pkr.hcl @@ -1,4 +1,4 @@ variable "ami_name" { type = string - default = "default" + default = "amazon-linux-sandbox-v0.1" } \ No newline at end of file diff --git a/packer/manifest-2.json b/packer/manifest-2.json deleted file mode 100644 index a56123b..0000000 --- a/packer/manifest-2.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "builds": [ - { - "name": "default_webserver", - "builder_type": "amazon-ebs", - "build_time": 1727274008, - "files": null, - "artifact_id": "us-east-1:ami-0b79f8b5153fb99a9", - "packer_run_uuid": "c9c99496-f3f1-44a2-4972-57bb3ecaab61", - "custom_data": null - } - ], - "last_run_uuid": "c9c99496-f3f1-44a2-4972-57bb3ecaab61" -} \ No newline at end of file diff --git a/packer/manifest.json b/packer/manifest.json index e69de29..39bdcd0 100644 --- a/packer/manifest.json +++ b/packer/manifest.json @@ -0,0 +1,14 @@ +{ + "builds": [ + { + "name": "default_webserver", + "builder_type": "amazon-ebs", + "build_time": 1727695190, + "files": null, + "artifact_id": "us-east-1:ami-0f199b688986c1a54", + "packer_run_uuid": "c43fc714-16a7-3a90-0616-92381fcf216e", + "custom_data": null + } + ], + "last_run_uuid": "c43fc714-16a7-3a90-0616-92381fcf216e" +} \ No newline at end of file diff --git a/packer/sample_for_jq.json b/packer/sample_for_jq.json deleted file mode 100644 index 364ab1a..0000000 --- a/packer/sample_for_jq.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "something": { - "Identifying Key": [ - { - "foo": "a.b.c", - "bar": "First Three" - }, - { - "foo": "a.b.d", - "bar": "Second Three" - } - ], - "Another Key": [ - { - "foo": "z.b.c", - "bar": "First Three, Take Two" - }, - { - "foo": "z.b.d", - "bar": "Second Three, Take Two" - } - ] - } -} From c0fec72c64847b7ecf68b0615f2e3b5e6ed84d92 Mon Sep 17 00:00:00 2001 From: slice312 Date: Tue, 1 Oct 2024 20:07:06 +0600 Subject: [PATCH 02/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index cdeb5f0..0d11e1b 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -4,7 +4,7 @@ on: push: branches: - main - - feature/packer-pipeline + - feature/* jobs: packer_build: From fcb43f0b9e201b65644b9df9238c864e8a071df3 Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 14:54:33 +0600 Subject: [PATCH 03/19] splitted packer's steps --- .github/workflows/packer.yaml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 0d11e1b..93d921c 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -1,4 +1,4 @@ -name: Build AMI with Packer +name: Deploy on: push: @@ -8,9 +8,14 @@ on: jobs: packer_build: - name: Packer Build + name: AMI build with Packer runs-on: ubuntu-latest environment: dev + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: us-east-1 + AMI_NAME: default_webserver outputs: ami_id: ${{ steps.build.outputs.ami_id }} @@ -37,14 +42,7 @@ jobs: #TODO: добавить возможность пропуска, хз вручную как то, апровы или что еще, придумать короче # wokrlfow dipstach с входными параметрами - - name: build - id: build - # if: steps.changes.outcome != 'success' - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-east-1 - AMI_NAME: default_webserver + - name: deregister previous AMI run: | image_info=$(aws ec2 describe-images --owners self \ --filters Name=name,Values=$AMI_NAME \ @@ -63,7 +61,10 @@ jobs: echo "snapshot $snapshot_id deleted" fi fi - # TODO: степ разделить тут + + - name: build + id: build + run: | cd ${{ github.event.repository.name }} cd packer packer init . @@ -99,4 +100,3 @@ jobs: terraform init terraform plan -var="ami_id=ami-0d02f4dc282064d6a" # terraform apply -var="ami_id=ami-0d02f4dc282064d6a" -auto-approve - From c733b50f3fbcd4a5453810d41ea4ade0e3d164ce Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 15:50:14 +0600 Subject: [PATCH 04/19] update --- packer/apache-server.pkr.hcl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packer/apache-server.pkr.hcl b/packer/apache-server.pkr.hcl index 9867bfb..3b7006f 100644 --- a/packer/apache-server.pkr.hcl +++ b/packer/apache-server.pkr.hcl @@ -44,6 +44,7 @@ build { "sudo dnf install -y telnet", "sudo dnf install -y jq", # TODO: yaml query yq + # TODO: tree "sudo dnf install -y httpd", "sudo systemctl start httpd", "sudo systemctl enable httpd", @@ -54,6 +55,6 @@ build { } post-processor "manifest" { - output = "manifest.json" + output = "manifest.json" } } From 443d6cdc52675f3a2def7309f5d669c099127b64 Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 15:54:03 +0600 Subject: [PATCH 05/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 93d921c..a808e20 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -15,7 +15,7 @@ jobs: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: us-east-1 - AMI_NAME: default_webserver + AMI_NAME: amazon-linux-sandbox outputs: ami_id: ${{ steps.build.outputs.ami_id }} From 2c6351eafc15e06e9c7df9017fa7f1757b04ae28 Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 16:05:00 +0600 Subject: [PATCH 06/19] fix checkout branch --- .github/workflows/packer.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index a808e20..5b46f87 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -23,7 +23,7 @@ jobs: steps: - name: сheckout run: | - git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git + git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git@${{gi}} pwd ls -lah cd ${{ github.event.repository.name }} @@ -48,6 +48,7 @@ jobs: --filters Name=name,Values=$AMI_NAME \ --query 'Images[0].{ImageId: ImageId, SnapshotId: BlockDeviceMappings[0].Ebs.SnapshotId}') + # TODO: else лог if [ "$image_info" != "null" ]; then image_id=$(echo $image_info | jq -r '.ImageId') snapshot_id=$(echo $image_info| jq -r '.SnapshotId') @@ -69,7 +70,7 @@ jobs: cd packer packer init . packer build -var "ami_name=$AMI_NAME" . - cat manifest.json + cat manifest.json | cat jq . # сolorful json ami_id_val=$(jq -r '(.last_run_uuid as $uid | .builds[] | select(.packer_run_uuid == $uid) | .artifact_id | split(":")[1])' manifest.json) echo "ami_id=${ami_id_val}" >> "$GITHUB_OUTPUT" @@ -82,7 +83,8 @@ jobs: steps: - name: сheckout run: | - git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git + git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ + --branch ${{ github.ref }} pwd ls -lah cd ${{ github.event.repository.name }} From 14db7431dffcba5db69f41120851290c12a812ad Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 16:05:59 +0600 Subject: [PATCH 07/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 5b46f87..6707cfa 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -23,7 +23,7 @@ jobs: steps: - name: сheckout run: | - git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git@${{gi}} + git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git pwd ls -lah cd ${{ github.event.repository.name }} From f96de9609553187fe4a85d3e46d43d47352ccfe3 Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 16:08:06 +0600 Subject: [PATCH 08/19] fix checkout --- .github/workflows/packer.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 6707cfa..4f80ae1 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -23,7 +23,8 @@ jobs: steps: - name: сheckout run: | - git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git + git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ + --branch ${{ github.ref }} pwd ls -lah cd ${{ github.event.repository.name }} From 4ceefe356941cd69a4ca811cdf75f10a2c42a731 Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 16:13:46 +0600 Subject: [PATCH 09/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 4f80ae1..7b97e41 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -24,7 +24,7 @@ jobs: - name: сheckout run: | git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ - --branch ${{ github.ref }} + --branch ${{ github.ref_name }} pwd ls -lah cd ${{ github.event.repository.name }} From 7b13e87f507541d3729345e725000fb64a716468 Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 16:44:28 +0600 Subject: [PATCH 10/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 7b97e41..67f53a2 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -71,7 +71,7 @@ jobs: cd packer packer init . packer build -var "ami_name=$AMI_NAME" . - cat manifest.json | cat jq . # сolorful json + cat manifest.json | jq . # сolorful json ami_id_val=$(jq -r '(.last_run_uuid as $uid | .builds[] | select(.packer_run_uuid == $uid) | .artifact_id | split(":")[1])' manifest.json) echo "ami_id=${ami_id_val}" >> "$GITHUB_OUTPUT" From fdaa40a13e8d3321b1034026d569eaa87f863f9d Mon Sep 17 00:00:00 2001 From: slice312 Date: Wed, 2 Oct 2024 17:57:11 +0600 Subject: [PATCH 11/19] update --- .github/workflows/packer.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 67f53a2..2a23105 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -15,7 +15,7 @@ jobs: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: us-east-1 - AMI_NAME: amazon-linux-sandbox + AMI_NAME: amazon-linux-sandbox-v2 outputs: ami_id: ${{ steps.build.outputs.ami_id }} @@ -71,7 +71,7 @@ jobs: cd packer packer init . packer build -var "ami_name=$AMI_NAME" . - cat manifest.json | jq . # сolorful json + cat manifest.json ami_id_val=$(jq -r '(.last_run_uuid as $uid | .builds[] | select(.packer_run_uuid == $uid) | .artifact_id | split(":")[1])' manifest.json) echo "ami_id=${ami_id_val}" >> "$GITHUB_OUTPUT" From 5010b6ec340209dcd25bfd898df46642f82bc85a Mon Sep 17 00:00:00 2001 From: slice312 Date: Thu, 3 Oct 2024 19:45:09 +0600 Subject: [PATCH 12/19] update --- .github/workflows/packer.yaml | 6 ++++-- manifest.json | 23 ----------------------- packer/apache-server_variables.pkr.hcl | 2 +- packer/manifest.json | 10 +++++----- sample.yaml | 16 ++++++++++++++++ 5 files changed, 26 insertions(+), 31 deletions(-) delete mode 100644 manifest.json create mode 100644 sample.yaml diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 2a23105..dbd2cbb 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -15,7 +15,7 @@ jobs: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: us-east-1 - AMI_NAME: amazon-linux-sandbox-v2 + AMI_NAME: amazon-linux-sandbox outputs: ami_id: ${{ steps.build.outputs.ami_id }} @@ -23,6 +23,8 @@ jobs: steps: - name: сheckout run: | + jq --verison + yq --verison git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ --branch ${{ github.ref_name }} pwd @@ -45,7 +47,7 @@ jobs: - name: deregister previous AMI run: | - image_info=$(aws ec2 describe-images --owners self \ + image_info=$(aws ec2 describe-images --owners self \ --filters Name=name,Values=$AMI_NAME \ --query 'Images[0].{ImageId: ImageId, SnapshotId: BlockDeviceMappings[0].Ebs.SnapshotId}') diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 60f8ff2..0000000 --- a/manifest.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "builds": [ - { - "name": "default_webserver", - "builder_type": "amazon-ebs", - "build_time": 1727359158, - "files": null, - "artifact_id": "ue_east-1:ami-03d0c1a6276eef46d", - "packer_run_uuid": "ad8ecf16-f117-deae-ca50-9f7d8478e766", - "custom_data": null - }, - { - "name": "default_webserver", - "builder_type": "amazon-ebs", - "build_time": 1727368318, - "files": null, - "artifact_id": "ue_east-1:ami-0fa60ef1c2084d33b", - "packer_run_uuid": "f033fcb5-36e5-1229-26fc-b726be250b7e", - "custom_data": null - } - ], - "last_run_uuid": "f033fcb5-36e5-1229-26fc-b726be250b7e" -} \ No newline at end of file diff --git a/packer/apache-server_variables.pkr.hcl b/packer/apache-server_variables.pkr.hcl index 8176039..88f1a30 100644 --- a/packer/apache-server_variables.pkr.hcl +++ b/packer/apache-server_variables.pkr.hcl @@ -1,4 +1,4 @@ variable "ami_name" { type = string - default = "amazon-linux-sandbox-v0.1" + default = "amazon-linux-sandbox-v1" } \ No newline at end of file diff --git a/packer/manifest.json b/packer/manifest.json index 39bdcd0..c8dd350 100644 --- a/packer/manifest.json +++ b/packer/manifest.json @@ -1,14 +1,14 @@ { "builds": [ { - "name": "default_webserver", + "name": "amazon-linux-sandbox", "builder_type": "amazon-ebs", - "build_time": 1727695190, + "build_time": 1727879628, "files": null, - "artifact_id": "us-east-1:ami-0f199b688986c1a54", - "packer_run_uuid": "c43fc714-16a7-3a90-0616-92381fcf216e", + "artifact_id": "us-east-1:ami-07fd38f876de9bc3d", + "packer_run_uuid": "79caed67-faec-6054-180d-baf8c344b5ef", "custom_data": null } ], - "last_run_uuid": "c43fc714-16a7-3a90-0616-92381fcf216e" + "last_run_uuid": "79caed67-faec-6054-180d-baf8c344b5ef" } \ No newline at end of file diff --git a/sample.yaml b/sample.yaml new file mode 100644 index 0000000..30f8013 --- /dev/null +++ b/sample.yaml @@ -0,0 +1,16 @@ +version: 1.0 +services: + web: + image: nginx:latest + ports: + - 80:80 + environment: + - NGINX_HOST=localhost + - NGINX_PORT=80 + db: + image: postgres:latest + ports: + - 5432:5432 + environment: + - POSTGRES_USER=admin + - POSTGRES_PASSWORD=secret From ede4e797bd39b73048382aa63ecb85c10fae931d Mon Sep 17 00:00:00 2001 From: slice312 Date: Thu, 3 Oct 2024 19:46:53 +0600 Subject: [PATCH 13/19] update --- .github/workflows/packer.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index dbd2cbb..e3b9302 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -23,8 +23,7 @@ jobs: steps: - name: сheckout run: | - jq --verison - yq --verison + yq git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ --branch ${{ github.ref_name }} pwd From effb47c7eaf25602f397e0d8e72d7a94775c1058 Mon Sep 17 00:00:00 2001 From: slice312 Date: Thu, 3 Oct 2024 19:49:08 +0600 Subject: [PATCH 14/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index e3b9302..a31218e 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -23,12 +23,12 @@ jobs: steps: - name: сheckout run: | - yq git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ --branch ${{ github.ref_name }} pwd ls -lah cd ${{ github.event.repository.name }} + cat sample.yaml | yq .git # - name: Check for Packer changes # id: changes From ff5096cb391f21793f229e90252ce81022f7537d Mon Sep 17 00:00:00 2001 From: slice312 Date: Thu, 3 Oct 2024 19:53:28 +0600 Subject: [PATCH 15/19] update --- .github/workflows/packer.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index a31218e..55de8e2 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -28,7 +28,8 @@ jobs: pwd ls -lah cd ${{ github.event.repository.name }} - cat sample.yaml | yq .git + cat samples.yaml + cat sample.yaml | yq . # - name: Check for Packer changes # id: changes From a7b764777ec3187690ed371df7033815bb1b6fa2 Mon Sep 17 00:00:00 2001 From: slice312 Date: Thu, 3 Oct 2024 19:53:40 +0600 Subject: [PATCH 16/19] update --- .github/workflows/packer.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index 55de8e2..fb25eae 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -28,7 +28,7 @@ jobs: pwd ls -lah cd ${{ github.event.repository.name }} - cat samples.yaml + cat sample.yaml cat sample.yaml | yq . # - name: Check for Packer changes From 633c7f23c64c21b99f887ddcea751a0d83b6cdcc Mon Sep 17 00:00:00 2001 From: slice312 Date: Sat, 5 Oct 2024 18:15:50 +0600 Subject: [PATCH 17/19] refactoring --- .github/workflows/packer.yaml | 14 ++++++-------- .gitignore | 2 ++ packer/apache-server.pkr.hcl | 16 ++++------------ packer/manifest.json | 8 ++++---- packer/scripts/configure.sh | 3 +++ packer/scripts/install.sh | 13 +++++++++++++ sample.yaml | 16 ---------------- terraform/main.tf | 26 +++++++++++++++++++++++--- terraform/scripts/init-index-html.sh | 8 ++++++++ terraform/terraform.tfstate | 9 --------- terraform/variables.tf | 2 +- test.sh | 18 ------------------ 12 files changed, 64 insertions(+), 71 deletions(-) create mode 100644 packer/scripts/configure.sh create mode 100644 packer/scripts/install.sh delete mode 100644 sample.yaml create mode 100644 terraform/scripts/init-index-html.sh delete mode 100644 terraform/terraform.tfstate delete mode 100755 test.sh diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index fb25eae..f539740 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -10,12 +10,12 @@ jobs: packer_build: name: AMI build with Packer runs-on: ubuntu-latest - environment: dev + environment: tmp env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: us-east-1 - AMI_NAME: amazon-linux-sandbox + AMI_NAME: default_webserver_snapshot # amazon-linux-sandbox outputs: ami_id: ${{ steps.build.outputs.ami_id }} @@ -28,8 +28,6 @@ jobs: pwd ls -lah cd ${{ github.event.repository.name }} - cat sample.yaml - cat sample.yaml | yq . # - name: Check for Packer changes # id: changes @@ -51,7 +49,7 @@ jobs: --filters Name=name,Values=$AMI_NAME \ --query 'Images[0].{ImageId: ImageId, SnapshotId: BlockDeviceMappings[0].Ebs.SnapshotId}') - # TODO: else лог + # TODO: else логирование if [ "$image_info" != "null" ]; then image_id=$(echo $image_info | jq -r '.ImageId') snapshot_id=$(echo $image_info| jq -r '.SnapshotId') @@ -80,7 +78,7 @@ jobs: terraform_deploy: name: Terraform Deploy runs-on: ubuntu-latest - environment: dev + environment: tmp needs: packer_build steps: @@ -103,5 +101,5 @@ jobs: aws sts get-caller-identity --region=us-east-1 terraform init - terraform plan -var="ami_id=ami-0d02f4dc282064d6a" - # terraform apply -var="ami_id=ami-0d02f4dc282064d6a" -auto-approve + terraform plan -var="ami_id=${{ needs.packer_build.outputs.ami_id }}" + terraform apply -var="ami_id=${{ needs.packer_build.outputs.ami_id }}" -auto-approve diff --git a/.gitignore b/.gitignore index 22ce9a3..d0899b9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ .terraform/ +terraform.tfstate + services/ diff --git a/packer/apache-server.pkr.hcl b/packer/apache-server.pkr.hcl index 3b7006f..806d7f6 100644 --- a/packer/apache-server.pkr.hcl +++ b/packer/apache-server.pkr.hcl @@ -38,18 +38,10 @@ build { environment_vars = [ "GREETING=kek", ] - inline = [ - "sudo dnf group install -y 'Development Tools'", - "sudo dnf install -y nc", - "sudo dnf install -y telnet", - "sudo dnf install -y jq", - # TODO: yaml query yq - # TODO: tree - "sudo dnf install -y httpd", - "sudo systemctl start httpd", - "sudo systemctl enable httpd", - "sudo systemctl status httpd", - "echo '<Region> - <Node #>' | sudo tee /var/www/html/index.html > /dev/null" + + scripts = [ + "./scripts/install.sh", + "./scripts/configure.sh" ] expect_disconnect = true } diff --git a/packer/manifest.json b/packer/manifest.json index c8dd350..5fe0349 100644 --- a/packer/manifest.json +++ b/packer/manifest.json @@ -3,12 +3,12 @@ { "name": "amazon-linux-sandbox", "builder_type": "amazon-ebs", - "build_time": 1727879628, + "build_time": 1728025626, "files": null, - "artifact_id": "us-east-1:ami-07fd38f876de9bc3d", - "packer_run_uuid": "79caed67-faec-6054-180d-baf8c344b5ef", + "artifact_id": "us-east-1:ami-03497412185c7e9f5", + "packer_run_uuid": "3ba4f35c-847d-0f25-f22f-53e994445300", "custom_data": null } ], - "last_run_uuid": "79caed67-faec-6054-180d-baf8c344b5ef" + "last_run_uuid": "3ba4f35c-847d-0f25-f22f-53e994445300" } \ No newline at end of file diff --git a/packer/scripts/configure.sh b/packer/scripts/configure.sh new file mode 100644 index 0000000..af84290 --- /dev/null +++ b/packer/scripts/configure.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo '<Region> - <Node #>' | sudo tee /var/www/html/index.html > /dev/null \ No newline at end of file diff --git a/packer/scripts/install.sh b/packer/scripts/install.sh new file mode 100644 index 0000000..3227aae --- /dev/null +++ b/packer/scripts/install.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +sudo dnf update +sudo dnf group install -y 'Development Tools' +sudo dnf install -y nc +sudo dnf install -y telnet +sudo dnf install -y jq + # TODO: yaml query yq + # TODO: tree +sudo dnf install -y httpd +sudo systemctl start httpd +sudo systemctl enable httpd +sudo systemctl status httpd \ No newline at end of file diff --git a/sample.yaml b/sample.yaml deleted file mode 100644 index 30f8013..0000000 --- a/sample.yaml +++ /dev/null @@ -1,16 +0,0 @@ -version: 1.0 -services: - web: - image: nginx:latest - ports: - - 80:80 - environment: - - NGINX_HOST=localhost - - NGINX_PORT=80 - db: - image: postgres:latest - ports: - - 5432:5432 - environment: - - POSTGRES_USER=admin - - POSTGRES_PASSWORD=secret diff --git a/terraform/main.tf b/terraform/main.tf index 26a7bc7..f6a1958 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -34,6 +34,7 @@ data "aws_vpc" "default_west" { } + module "sg_access_to_web_servers_east" { source = "./sg_access_to_web_servers" @@ -70,7 +71,6 @@ resource "aws_security_group" "access_to_jumpbox" { } - resource "aws_security_group" "ssh_access_to_web_servers" { provider = aws.us_east vpc_id = data.aws_vpc.default_east.id @@ -90,7 +90,7 @@ resource "aws_instance" "jump_box" { provider = aws.us_east ami = var.ami_id instance_type = "t2.micro" - associate_public_ip_address = true + associate_public_ip_address = false security_groups = [ aws_security_group.access_to_jumpbox.name, @@ -122,6 +122,8 @@ resource "aws_instance" "web_server_1" { http_tokens = "required" } + user_data = file("${path.module}/scripts/init-index-html.sh") + tags = { Name = "web_server_1" Target = "test-ec2-part1" @@ -129,11 +131,27 @@ resource "aws_instance" "web_server_1" { } +data "aws_ami" "ami_source" { + filter { + name = "image-id" + values = [var.ami_id] + } +} + +resource "aws_ami_copy" "ami_west" { + name = data.aws_ami.ami_source.name + source_ami_id = data.aws_ami.ami_source.id + source_ami_region = "us-east-1" + description = "AMI copied from us-east-1 to us-west-1" + + provider = aws.us_west +} + resource "aws_instance" "web_server_2" { provider = aws.us_west - ami = var.ami_id + ami = aws_ami_copy.ami_west.id instance_type = "t2.micro" associate_public_ip_address = true key_name = module.key_pairs.name_us_west @@ -144,6 +162,8 @@ resource "aws_instance" "web_server_2" { http_tokens = "required" } + user_data = file("${path.module}/scripts/init-index-html.sh") + tags = { Name = "web_server_2" } diff --git a/terraform/scripts/init-index-html.sh b/terraform/scripts/init-index-html.sh new file mode 100644 index 0000000..6e66221 --- /dev/null +++ b/terraform/scripts/init-index-html.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +token=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 300"` + +region=`curl -s -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/placement/region` +node=`curl -s -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/placement/availability-zone` + +echo "$region - $node" | sudo tee /var/www/html/index.html diff --git a/terraform/terraform.tfstate b/terraform/terraform.tfstate deleted file mode 100644 index 442b234..0000000 --- a/terraform/terraform.tfstate +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": 4, - "terraform_version": "1.9.6", - "serial": 1, - "lineage": "937b3183-0f44-9402-d8a7-6f8526b9d330", - "outputs": {}, - "resources": [], - "check_results": null -} diff --git a/terraform/variables.tf b/terraform/variables.tf index 327b6a7..f696bc7 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,4 +1,4 @@ variable "ami_id" { - description = "AMI id" + description = "AMI ID" type = string } \ No newline at end of file diff --git a/test.sh b/test.sh deleted file mode 100755 index a1458a3..0000000 --- a/test.sh +++ /dev/null @@ -1,18 +0,0 @@ -image_info=$(aws ec2 describe-images --filters Name=name,Values=default_webserver --owners self \ ---query 'Images[0].{ImageId: ImageId, SnapshotId: BlockDeviceMappings[0].Ebs.SnapshotId}') - -echo $image_info - -if [ "$image_info" != "null" ]; then - image_id=$(echo $image_info | jq -r '.ImageId') - snapshot_id=$(echo $image_info| jq -r '.SnapshotId') - - # aws ec2 deregister-image --image-id $image_id - if [ $? -eq 0 ]; then - echo "image $image_id deregistered" - fi - # aws ec2 delete-snapshot --snapshot-id $snapshot_id - if [ $? -eq 0 ]; then - echo "snapshot $snapshot_id deleted" - fi -fi \ No newline at end of file From 8f15eed958a45293e7ab835fc54daa10abb3158f Mon Sep 17 00:00:00 2001 From: slice312 Date: Sat, 5 Oct 2024 18:29:33 +0600 Subject: [PATCH 18/19] fix checkout --- .github/workflows/packer.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index f539740..b7d5808 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -75,6 +75,13 @@ jobs: ami_id_val=$(jq -r '(.last_run_uuid as $uid | .builds[] | select(.packer_run_uuid == $uid) | .artifact_id | split(":")[1])' manifest.json) echo "ami_id=${ami_id_val}" >> "$GITHUB_OUTPUT" + # - name: artifact + # uses: actions/upload-artifact@v4 + # with: + # name: manifest.json + # path: + + terraform_deploy: name: Terraform Deploy runs-on: ubuntu-latest @@ -85,7 +92,7 @@ jobs: - name: сheckout run: | git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \ - --branch ${{ github.ref }} + --branch ${{ github.ref_name }} pwd ls -lah cd ${{ github.event.repository.name }} From 413f1e408605363d5f05219a529d0ab7f41b81b4 Mon Sep 17 00:00:00 2001 From: slice312 Date: Sat, 5 Oct 2024 18:47:06 +0600 Subject: [PATCH 19/19] up --- .github/workflows/packer.yaml | 2 +- terraform/main.tf | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/packer.yaml b/.github/workflows/packer.yaml index b7d5808..70959c4 100644 --- a/.github/workflows/packer.yaml +++ b/.github/workflows/packer.yaml @@ -15,7 +15,7 @@ jobs: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: us-east-1 - AMI_NAME: default_webserver_snapshot # amazon-linux-sandbox + AMI_NAME: default_webserver_image # amazon-linux-sandbox outputs: ami_id: ${{ steps.build.outputs.ami_id }} diff --git a/terraform/main.tf b/terraform/main.tf index f6a1958..e387e5b 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -132,6 +132,8 @@ resource "aws_instance" "web_server_1" { data "aws_ami" "ami_source" { + provider = aws.us_east + filter { name = "image-id" values = [var.ami_id]