From 85c6036f83f9c26bbe46c7238bda836f7346c601 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:05:01 +0300 Subject: [PATCH 01/24] run ci ansible job --- .circleci/config.yml | 20 ++++++++++++-------- ansible-ec2/ansible.cfg | 0 ansible-ec2/inventory | 2 -- ansible-ec2/inventory.txt | 2 ++ 4 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 ansible-ec2/ansible.cfg delete mode 100644 ansible-ec2/inventory create mode 100644 ansible-ec2/inventory.txt diff --git a/.circleci/config.yml b/.circleci/config.yml index db5aa16..7e18cf7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,18 +4,22 @@ orbs: python: circleci/python@1.5.0 jobs: - create_infrastructure: + configure_infrastructure: docker: - - image: amazon/aws-cli + - image: python:3.7-alpine3.11 steps: - checkout + - add_ssh_keys: + fingerprints: [ "2a:2e:cc:14:0b:d6:f2:bd:33:0c:cf:34:29:7e:09:6e" ] - run: - name: Create Cloudformation Stack + name: Install Ansible command: | - aws cloudformation deploy \ - --template-file ansible-ec2/template.yml \ - --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} \ - --region us-east-1 + apk add --update ansible + - run: + name: Run Playbook and Configure server + command: | + ansible-playbook -i ansible-ec2/inventory.txt ansible-ec2/main-remote.yml + build-and-test: docker: - image: cimg/python:3.10.2 @@ -30,5 +34,5 @@ jobs: workflows: sample: jobs: - - create_infrastructure + - configure_infrastructure - build-and-test diff --git a/ansible-ec2/ansible.cfg b/ansible-ec2/ansible.cfg new file mode 100644 index 0000000..e69de29 diff --git a/ansible-ec2/inventory b/ansible-ec2/inventory deleted file mode 100644 index 7ce733e..0000000 --- a/ansible-ec2/inventory +++ /dev/null @@ -1,2 +0,0 @@ -[all] -3.89.252.120 diff --git a/ansible-ec2/inventory.txt b/ansible-ec2/inventory.txt new file mode 100644 index 0000000..9779d6d --- /dev/null +++ b/ansible-ec2/inventory.txt @@ -0,0 +1,2 @@ +[all] +172.31.95.228 From 9d67d6d242fe16339aebd18b44b485b8182881bd Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:17:58 +0300 Subject: [PATCH 02/24] update ignore --- .gitignore | 2 ++ ansible-ec2/Project-udacity.pem | 27 --------------------------- ansible-ec2/inventory.txt | 2 +- ansible-ec2/udacity.pem | 27 +++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 28 deletions(-) delete mode 100644 ansible-ec2/Project-udacity.pem create mode 100644 ansible-ec2/udacity.pem diff --git a/.gitignore b/.gitignore index 99debc8..9d60a55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ venv .idea/ *.pyc +*.DS_Store +.DS_Store \ No newline at end of file diff --git a/ansible-ec2/Project-udacity.pem b/ansible-ec2/Project-udacity.pem deleted file mode 100644 index b3c79f1..0000000 --- a/ansible-ec2/Project-udacity.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpAIBAAKCAQEAoiPDum+RIPGxAnrTSGI1W04wg8Q+Td8IdnyJEn7ElINnANXN -FgF799pr0OjyMoK3idfU770544SHTXwI+/C4PZOtv46kKLrPuA8BfUi2tTJWFQQD -s1f9E9UHOlMs1NBHwUjAvtAkGpl00ruuhRXBmtSgkgwhXYfjejYVDrIoKLg56BI7 -PCqQHoje3M+IJxhtx4qZNZEc+pzOb6N6cg07MaXAqwStKQOcUQ9QbBzMqpchnVcE -YKc2210J9E8LY59bWD/63wjUQS//2WvGA05vW/J39tOR9955yLh/fuTqy7rMpfKj -rMICQRLyqWtVJELMwVjgxNKq0Ii4t3T2q0xptwIDAQABAoIBAFAJdZHvhuWLSUSf -GvLtP8d9lmdFFThdc5EBsqxFlYZ2Qv9fiuhk/SwBTx2GECCs9CsANuXNM+lCv4OZ -SdoMHvaOksMvh4tvhCAokCSnI0HYwjOHksgSFCSZEQNSkZvP4MvhvPu0RU44GDn6 -WZDqh3KFqWnOwR2peboXPlfNzBMk6nlKAppJ0vRbVlE4PNGqtXEgZ9iLt/m0bRz8 -telUfkOIjNWNI7yy3tKGYsBMTQYg9WyzOUFEqrYHOIdq8o+6ERiHWPUoOl5d3mPr -Ll0GlHCzDPr5N5UEIbv758JeVk68u73b0rfFgdiCy0beWfsUjRRDUhbeWEyC6xkE -8nh0LmECgYEA6222o7KtYgWS0B5u9Kkwez6XcRuJOyAKEZrLG39NSiPWepTt8toU -huUD+AKuCJ0WQYOuRCClqjiAY0SwvRIz1w0jDvRq7gZUtsciHcavJGPYtIfHmSQd -22t9wMzGXytWMF0GPFfkD7LAi20c3ypGynSegasHV6aUqi4QBbNOJ/ECgYEAsE6o -QYKh/YhieJYULokzsRQIkerbEM6xOoeEzutzWlrOfMoZN9g7KDZ4ZR95uRr60D6w -Fj0obVV+vaEWAlSEWYyrm8qTJCFTo6XLDz3HK/SBEJxtay3Sn4TCPqRlpenh8aPP -XZPm2lVfmKPuzKFsbjq24wtDHRQKO5A1C/UdlCcCgYEAjbhGuHYHrzkBrqyTSE5M -s/PywBC+ZEweI4jV+SCByz6Vjsb2Ejm7H5Te9b3PO7osOpXlInewoi5PAvLHnZlG -gY8T6p2xfMVs23Y2EEyZA8PjIJGX0pUOMBad5aCfTYzN3mExLsQNsnF9PLEbLy/I -23imLhJhYh8R4PvHg1bzllECgYEArkNAyWeeR6NreTcWAZ05S5i6k8en+afiwzA5 -0/AjVzaUDAISBQGkDy4vt/vxa0XAjN65W3HdqJutpy82Ot1g58ZDxLvozfhm3ES+ -PXVZjReiDlIfUKW9HlCxUgtaMTDIU/gOuba7oihqUcwO95HCQ/2i0S1Li81IA/GY -jRYcw1cCgYBJVhdq0hGFiPhlXRuWMltR5hs6AopOq/5QC9RzSU2alaC9tkWkiKD9 -+ncnCBRhAvRhcmTcuGA+a5F1oV4yH5/xlIQxMikb0GGWvlCC9Kp9qveNkzT3whIm -AG0gIOuCZXvu4CEllNw9mAReC5EMbw1y73QtnJgtR9Olp4XTo/0UHQ== ------END RSA PRIVATE KEY----- \ No newline at end of file diff --git a/ansible-ec2/inventory.txt b/ansible-ec2/inventory.txt index 9779d6d..5dc11c1 100644 --- a/ansible-ec2/inventory.txt +++ b/ansible-ec2/inventory.txt @@ -1,2 +1,2 @@ [all] -172.31.95.228 +ec2-54-161-67-165.compute-1.amazonaws.com diff --git a/ansible-ec2/udacity.pem b/ansible-ec2/udacity.pem new file mode 100644 index 0000000..fd46709 --- /dev/null +++ b/ansible-ec2/udacity.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAlqAxJfddGLQMaSd4Z1JSaZ3KWB6hD1kl+2L1XXeHljHFOQcM +XQN91OYFf35oNbF9tpyl5rXQLqFOmpF4A304dbEn55KYyJCTm54cASAxXbeH03Iy +Zrv39eUrIi4KbsCQ32j1YImV1d5znMXhM11KbKORiaxRdZPuL+NcPJba3OHrqxIH +TfQ9sazU6YjWiXuBvN1gU2Z/lQWA79E7aDylaNTL9JxXBliAfELc73t76S+Dwal0 +jPkfQdzAmEWUOeJBMqEyuS7EBQeavKH6nJfIRj/OYztCWfw/LE31b3Y8cue1dT7j +euwhfPofHc7Gpn8NJojkmhfAhzfO7BRTtmePNwIDAQABAoIBAHYK1HmMgcj6UT+X +TySpvyxGHG0yk8YTd1CrHN9C7V8nYghvHaFHOzW9jlF8Hu71Si/yU5Gaqqvgfllj +50nUuIqDa2Jv4IxNwWATaJ75O7bWPfvXYuhgJtLt5f/DVSm2f9KVzrb5HBOfYbta +2EIIumqpRUU/Zhde9ytQYtiY4Kx4qX2MpDhWSevP5raKfljLWn6CKpB//3q5/xFu +L8NJuIDlZSNbP1r5D4z6FlDq3OJ/d8YNFzbNCnRTD5cML35wzKrNbbQqyDBJeo4D +dnM4ZWbqP6nFhl2kslUJtblxiY5iG+CRieiEHnZJz0lpBuJKAtM5vaG2KsP5qcaa +o1oefAECgYEA5KWpXiI9eETcFYBDSeLo34Mb0A7O13HI3chBqhoLEyEFaYRu0gES +L+Tt7c2bHlpMSGvP4Eq5TQlr8A6ya0nrI6j/WeQcYVI8QM8mvS+MwyyY5RgU0d3c +ujbE5x3UtQSnUoWsEr3GckjxI45mPSPD7QanJ0CAg8iopmWq+glEP8cCgYEAqKUe +e9j1w1dQQbEfAWOxexk8A/kYdFNwWMUVEIAmbWH6lFGNNP/jsL2QaWpIxSrl+Muy +g8+L5axATn5FczZN1DBXPNTzJDLJOUWyFhNURJXTYSORYm7UUIOrxgyH1ZY6bwtr +XcSS6H4AqSldnB2uqKvw98ANNMxp7QeV9gs8FRECgYARUKZR1Z/4EqbkDfGKj994 +xbH+gJCTai9+9CJc4loCWdbSFdBaoPAipmCKSK69jrMGLPvyDVaZCeSdqk9Cprdi +3PJxeT/Y8uRTOBzD6Ozs/tyZRMiW2AlWJL2mjvDDwJV3P143kR2NHA7+1VLSwHme +4okYBN92m2SCLWLUSo1bkwKBgCIoX4ZCKXUJSE4l69vCL355W/iKCXP2OnkgYeXX +rvDUuYY1Bl7HvXxcyNUxG9AjojTEPUIJVZKNUvETDkoJo7XonKLLlvur9anN/gYO +oDDMD+ZK5/WB8SrloHMasqCGLwuyympRmFfSvZuFr1ohgPDU8pebpJEfHg1OTgdp +ux+hAoGAH5jD4kI4BwMFgHTCvhstNjctWbPkr3bjdTnl2jDrsBUQpPhRAnCyXcBM +U8MQI272GWTR4pRjPemQYbf4mbRoxb1toh1+BIsoHdX87o5Xk7qWd3tYNdAXSZPK +38q3ZZIf/pMjhNYVnrEcJNgeKXYXA9vRYmoE76SwRAm8fguPafo= +-----END RSA PRIVATE KEY----- \ No newline at end of file From 158cb374dda537a24d864bf26cbcc9e5869a2ed8 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:21:21 +0300 Subject: [PATCH 03/24] disable host checking in ansible --- ansible-ec2/ansible.cfg | 2 ++ ansible.cfg | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 ansible.cfg diff --git a/ansible-ec2/ansible.cfg b/ansible-ec2/ansible.cfg index e69de29..d313d1b 100644 --- a/ansible-ec2/ansible.cfg +++ b/ansible-ec2/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +host_key_checking = false \ No newline at end of file diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..d313d1b --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +host_key_checking = false \ No newline at end of file From 8594d6fad67fe84ea7e7da1288025e95c16a7e55 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:26:16 +0300 Subject: [PATCH 04/24] host unreachable error fix --- ansible-ec2/inventory.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible-ec2/inventory.txt b/ansible-ec2/inventory.txt index 5dc11c1..9779d6d 100644 --- a/ansible-ec2/inventory.txt +++ b/ansible-ec2/inventory.txt @@ -1,2 +1,2 @@ [all] -ec2-54-161-67-165.compute-1.amazonaws.com +172.31.95.228 From ecf100a204ede62a41608a6253fc5001c40832ce Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:35:55 +0300 Subject: [PATCH 05/24] remove ansible cfg --- ansible-ec2/ansible.cfg | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 ansible-ec2/ansible.cfg diff --git a/ansible-ec2/ansible.cfg b/ansible-ec2/ansible.cfg deleted file mode 100644 index d313d1b..0000000 --- a/ansible-ec2/ansible.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[defaults] -host_key_checking = false \ No newline at end of file From a00a0302567b6100528543e1d5d9aa4cc62d7c2f Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:43:20 +0300 Subject: [PATCH 06/24] update python image due to openssh issues --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7e18cf7..0eaf6e7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,7 @@ orbs: jobs: configure_infrastructure: docker: - - image: python:3.7-alpine3.11 + - image: python:3.10.5-alpine3.16 steps: - checkout - add_ssh_keys: From 4bddb4bac19f92792df10f5e09f2a06f455b62a4 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:44:20 +0300 Subject: [PATCH 07/24] manually install openssh --- .circleci/config.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0eaf6e7..9e74393 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,11 +6,15 @@ orbs: jobs: configure_infrastructure: docker: - - image: python:3.10.5-alpine3.16 + - image: python:3.7-alpine3.11 steps: - checkout - add_ssh_keys: fingerprints: [ "2a:2e:cc:14:0b:d6:f2:bd:33:0c:cf:34:29:7e:09:6e" ] + - run: + name: Install dependencies (openssh) + command: | + apk add --update openssh # Install openssh - run: name: Install Ansible command: | From 2b7610f7caa9a7a0a51d78a104cbfbbca22615e2 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Tue, 12 Jul 2022 00:46:04 +0300 Subject: [PATCH 08/24] update hostname --- ansible-ec2/inventory.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible-ec2/inventory.txt b/ansible-ec2/inventory.txt index 9779d6d..5dc11c1 100644 --- a/ansible-ec2/inventory.txt +++ b/ansible-ec2/inventory.txt @@ -1,2 +1,2 @@ [all] -172.31.95.228 +ec2-54-161-67-165.compute-1.amazonaws.com From 04f8f6ea8560501deca7c750993cf1b59612d715 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sat, 16 Jul 2022 23:32:16 +0300 Subject: [PATCH 09/24] simulate failed job --- .circleci/config.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9e74393..ce189db 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,6 +3,16 @@ version: 2.1 orbs: python: circleci/python@1.5.0 +commands: + destroy_environment: + steps: + - run: + name: Destroy environment + # ${CIRCLE_WORKFLOW_ID} is a Built-in environment variable + # ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID + command: | + aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} + jobs: configure_infrastructure: docker: @@ -35,8 +45,30 @@ jobs: name: Run tests command: pytest + smoke_test: + docker: + - image: alpine:latest + steps: + - checkout + - run: + name: simulate error + command: | + return 1 + - destroy_environment + when: on fail +# URL="https://blog.udacity.com/" +# # Test if website exists +# if curl -s --head ${URL} +# then +# return 0 +# else +# return 1 +# fi + + workflows: sample: jobs: + - smoke_test - configure_infrastructure - build-and-test From 529239487d37041d1a6ef44c335a03634b6586ff Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sat, 16 Jul 2022 23:33:51 +0300 Subject: [PATCH 10/24] run smoke tests --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ce189db..facc169 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -71,4 +71,4 @@ workflows: jobs: - smoke_test - configure_infrastructure - - build-and-test + - smoke_test From 4eecf9c70a107eddcb6c2ce591ed30730b50a1d2 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sat, 16 Jul 2022 23:48:17 +0300 Subject: [PATCH 11/24] fix --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index facc169..bcbb630 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,6 +10,7 @@ commands: name: Destroy environment # ${CIRCLE_WORKFLOW_ID} is a Built-in environment variable # ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID + when: on_fail command: | aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} @@ -55,7 +56,6 @@ jobs: command: | return 1 - destroy_environment - when: on fail # URL="https://blog.udacity.com/" # # Test if website exists # if curl -s --head ${URL} From 8cba756d86e67d2c503c5468962927d6643a838d Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sat, 16 Jul 2022 23:54:14 +0300 Subject: [PATCH 12/24] create and destroy infra --- .circleci/config.yml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bcbb630..36fd1ec 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -46,6 +46,20 @@ jobs: name: Run tests command: pytest + # Exercise - Rollback + create_infrastructure: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Create Cloudformation Stack + command: | + aws cloudformation deploy \ + --template-file template.yml \ + --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:7} \ + --region us-east-1 + smoke_test: docker: - image: alpine:latest @@ -69,6 +83,6 @@ jobs: workflows: sample: jobs: - - smoke_test - configure_infrastructure + - create_infrastructure - smoke_test From a99b3d75d7001340efa349c3cd31c4f92967d69d Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:00:30 +0300 Subject: [PATCH 13/24] correct template path --- .circleci/config.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 36fd1ec..862a4ed 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -56,7 +56,7 @@ jobs: name: Create Cloudformation Stack command: | aws cloudformation deploy \ - --template-file template.yml \ + --template-file ansible-ec2/template.yml \ --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:7} \ --region us-east-1 @@ -83,6 +83,5 @@ jobs: workflows: sample: jobs: - - configure_infrastructure - create_infrastructure - smoke_test From 2193ac74f5c7f742d9d96eac3cffc2aab41073a4 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:04:58 +0300 Subject: [PATCH 14/24] use aws iage --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 862a4ed..1dfe304 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -62,7 +62,7 @@ jobs: smoke_test: docker: - - image: alpine:latest + - image: amazon/aws-cli steps: - checkout - run: From 32fccf4f3fb883546bfb808c58a20473c56eb29a Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:10:12 +0300 Subject: [PATCH 15/24] remove on_fail from the command --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1dfe304..47fde9e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,7 +10,6 @@ commands: name: Destroy environment # ${CIRCLE_WORKFLOW_ID} is a Built-in environment variable # ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID - when: on_fail command: | aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} @@ -70,6 +69,7 @@ jobs: command: | return 1 - destroy_environment + when: on_fail # URL="https://blog.udacity.com/" # # Test if website exists # if curl -s --head ${URL} From c2353da230cc24131d443ae4140e60bde69ab8ac Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:15:36 +0300 Subject: [PATCH 16/24] fix identantion --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 47fde9e..e7b2794 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -69,7 +69,7 @@ jobs: command: | return 1 - destroy_environment - when: on_fail + when: on_fail # URL="https://blog.udacity.com/" # # Test if website exists # if curl -s --head ${URL} From 85182ab1f03db82ee45ab57fcc1ba2795daf5ae7 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:20:24 +0300 Subject: [PATCH 17/24] fix --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e7b2794..2b47821 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -68,7 +68,7 @@ jobs: name: simulate error command: | return 1 - - destroy_environment + - destroy_environment: when: on_fail # URL="https://blog.udacity.com/" # # Test if website exists From 99324b6aa6c1fc8418b329328413e9a03ca45707 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:23:18 +0300 Subject: [PATCH 18/24] revert positioning of when --- .circleci/config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2b47821..4cd8ca8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -9,7 +9,8 @@ commands: - run: name: Destroy environment # ${CIRCLE_WORKFLOW_ID} is a Built-in environment variable - # ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID + # ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID: + when: on_fail command: | aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} @@ -68,8 +69,7 @@ jobs: name: simulate error command: | return 1 - - destroy_environment: - when: on_fail + - destroy_environment # URL="https://blog.udacity.com/" # # Test if website exists # if curl -s --head ${URL} From 040917fee34feec654dddc360f5676b14c4262d8 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 00:25:47 +0300 Subject: [PATCH 19/24] specify aws cfn stack id --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4cd8ca8..0ae2400 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,7 +12,7 @@ commands: # ${CIRCLE_WORKFLOW_ID:0:5} takes the first 5 chars of the variable CIRCLE_CI_WORKFLOW_ID: when: on_fail command: | - aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:5} + aws cloudformation delete-stack --stack-name myStack-${CIRCLE_WORKFLOW_ID:0:7} jobs: configure_infrastructure: From 593bfed32494b6e6d9b3aeee740b7ee12e078600 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 22:51:41 +0300 Subject: [PATCH 20/24] promote to production exercise --- .circleci/config.yml | 14 ++++++++++++++ ansible-ec2/aws cmds | 6 ++++++ ansible-ec2/bucket.yml | 26 ++++++++++++++++++++++++++ ansible-ec2/cloudfront.yml | 35 +++++++++++++++++++++++++++++++++++ index.html | 10 ++++++++++ 5 files changed, 91 insertions(+) create mode 100644 ansible-ec2/aws cmds create mode 100644 ansible-ec2/bucket.yml create mode 100644 ansible-ec2/cloudfront.yml create mode 100644 index.html diff --git a/.circleci/config.yml b/.circleci/config.yml index 0ae2400..8e21255 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -79,6 +79,20 @@ jobs: # return 1 # fi + create_and_deploy_front_end: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Execute bucket.yml - Create Cloudformation Stack + command: | + aws cloudformation deploy \ + --template-file ansible-ec2/bucket.yml \ + --stack-name stack-create-bucket-${CIRCLE_WORKFLOW_ID:0:7} \ + --parameter-overrides MyBucketName="mybucket-${CIRCLE_WORKFLOW_ID:0:7}" + # Uncomment the step below if yoou wish to upload all contents of the current directory to the S3 bucket + - run: aws s3 sync . s3://mybucket-${CIRCLE_WORKFLOW_ID:0:7} --delete workflows: sample: diff --git a/ansible-ec2/aws cmds b/ansible-ec2/aws cmds new file mode 100644 index 0000000..f2f2151 --- /dev/null +++ b/ansible-ec2/aws cmds @@ -0,0 +1,6 @@ +aws cloudformation deploy \ +--template-file cloudfront.yml \ +--stack-name production-distro \ +--parameter-overrides PipelineID="roboticsbucket" \ # Name of the S3 bucket you created manually. +--tags project=udapeople + diff --git a/ansible-ec2/bucket.yml b/ansible-ec2/bucket.yml new file mode 100644 index 0000000..9660c01 --- /dev/null +++ b/ansible-ec2/bucket.yml @@ -0,0 +1,26 @@ +Parameters: + # New Bucket name + MyBucketName: + Description: Existing Bucket name + Type: String + +Resources: + WebsiteBucket: + Type: AWS::S3::Bucket + Properties: + BucketName: !Sub "${MyBucketName}" + AccessControl: PublicRead + WebsiteConfiguration: + IndexDocument: index.html + ErrorDocument: error.html + WebsiteBucketPolicy: + Type: AWS::S3::BucketPolicy + Properties: + Bucket: !Ref 'WebsiteBucket' + PolicyDocument: + Statement: + - Sid: PublicReadForGetBucketObjects + Effect: Allow + Principal: '*' + Action: s3:GetObject + Resource: !Join ['', ['arn:aws:s3:::', !Ref 'WebsiteBucket', /*]] \ No newline at end of file diff --git a/ansible-ec2/cloudfront.yml b/ansible-ec2/cloudfront.yml new file mode 100644 index 0000000..fca5445 --- /dev/null +++ b/ansible-ec2/cloudfront.yml @@ -0,0 +1,35 @@ +Parameters: + # Existing Bucket name + PipelineID: + Description: Existing Bucket name + Type: String + +Resources: + CloudFrontOriginAccessIdentity: + Type: "AWS::CloudFront::CloudFrontOriginAccessIdentity" + Properties: + CloudFrontOriginAccessIdentityConfig: + Comment: Origin Access Identity for Serverless Static Website + + WebpageCDN: + Type: AWS::CloudFront::Distribution + Properties: + DistributionConfig: + Origins: + - DomainName: !Sub "${PipelineID}.s3.amazonaws.com" + Id: webpage + S3OriginConfig: + OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}" + Enabled: True + DefaultRootObject: index.html + DefaultCacheBehavior: + ForwardedValues: + QueryString: False + TargetOriginId: webpage + ViewerProtocolPolicy: allow-all + +Outputs: + PipelineID: + Value: !Sub ${PipelineID} + Export: + Name: PipelineID \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..1314601 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + Version 1 + + + +

Hello World - version 1

+ + From d92ea2d3ad061d1488cce526937a2f1f0b5caeb9 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 22:54:50 +0300 Subject: [PATCH 21/24] update git workflows --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8e21255..e44b9b0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -97,5 +97,5 @@ jobs: workflows: sample: jobs: - - create_infrastructure - - smoke_test + - build-and-test + - create_and_deploy_front_end From 332169118850628f88b17255eed9449c9c37a2e0 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 23:15:31 +0300 Subject: [PATCH 22/24] cleanup resources and order jobs --- .circleci/config.yml | 58 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e44b9b0..ade695b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -94,8 +94,64 @@ jobs: # Uncomment the step below if yoou wish to upload all contents of the current directory to the S3 bucket - run: aws s3 sync . s3://mybucket-${CIRCLE_WORKFLOW_ID:0:7} --delete +# Executes the cloudfront.yml template that will modify the existing CloudFront Distribution, change its target from the old bucket to the new bucket - `mybucket-${CIRCLE_WORKFLOW_ID:0:7}`. +promote_to_production: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Execute cloudfront.yml + command: | + aws cloudformation deploy \ + --template-file cloudfront.yml \ + --stack-name production-distro \ + --parameter-overrides PipelineID="mybucket-${CIRCLE_WORKFLOW_ID:0:7}" + + # Fetch and save the pipeline ID (bucket ID) responsible for the last release. + get_last_deployment_id: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: yum install -y tar gzip + - run: + name: Fetch and save the old pipeline ID (bucket name) responsible for the last release. + command: | + aws cloudformation \ + list-exports --query "Exports[?Name==\`PipelineID\`].Value" \ + --no-paginate --output text > ~/textfile.txt + - persist_to_workspace: + root: ~/ + paths: + - textfile.txt + + # Destroy the previous production version's S3 bucket and CloudFormation stack. + clean_up_old_front_end: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: yum install -y tar gzip + - attach_workspace: + at: ~/ + - run: + name: Destroy the previous S3 bucket and CloudFormation stack. + # Use $OldBucketID environment variable or mybucket644752792305 below. + # Similarly, you can create and use $OldStackID environment variable in place of production-distro + command: | + export OldBucketID=$(cat ~/textfile.txt) + aws s3 rm "s3://${OldBucketID}" --recursive + workflows: sample: jobs: - - build-and-test - create_and_deploy_front_end + - promote_to_production: + requires: + - create_and_deploy_front_end + - get_last_deployment_id + - clean_up_old_front_end: + requires: + - get_last_deployment_id + - promote_to_production From 38f3b8ecf5d04819d48e2f2c043a253ea018b890 Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 23:18:45 +0300 Subject: [PATCH 23/24] fix identation --- .circleci/config.yml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ade695b..4e6ce90 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -92,21 +92,21 @@ jobs: --stack-name stack-create-bucket-${CIRCLE_WORKFLOW_ID:0:7} \ --parameter-overrides MyBucketName="mybucket-${CIRCLE_WORKFLOW_ID:0:7}" # Uncomment the step below if yoou wish to upload all contents of the current directory to the S3 bucket - - run: aws s3 sync . s3://mybucket-${CIRCLE_WORKFLOW_ID:0:7} --delete + - run: aws s3 sync ansible-ec2/ s3://mybucket-${CIRCLE_WORKFLOW_ID:0:7} --delete -# Executes the cloudfront.yml template that will modify the existing CloudFront Distribution, change its target from the old bucket to the new bucket - `mybucket-${CIRCLE_WORKFLOW_ID:0:7}`. -promote_to_production: - docker: - - image: amazon/aws-cli - steps: - - checkout - - run: - name: Execute cloudfront.yml - command: | - aws cloudformation deploy \ - --template-file cloudfront.yml \ - --stack-name production-distro \ - --parameter-overrides PipelineID="mybucket-${CIRCLE_WORKFLOW_ID:0:7}" + # Executes the cloudfront.yml template that will modify the existing CloudFront Distribution, change its target from the old bucket to the new bucket - `mybucket-${CIRCLE_WORKFLOW_ID:0:7}`. + promote_to_production: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Execute cloudfront.yml + command: | + aws cloudformation deploy \ + --template-file cloudfront.yml \ + --stack-name production-distro \ + --parameter-overrides PipelineID="mybucket-${CIRCLE_WORKFLOW_ID:0:7}" # Fetch and save the pipeline ID (bucket ID) responsible for the last release. get_last_deployment_id: From cbab38445737a6642a1bc718cf8c2ad1751e551f Mon Sep 17 00:00:00 2001 From: patrickRobotics Date: Sun, 17 Jul 2022 23:20:46 +0300 Subject: [PATCH 24/24] fix file path --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4e6ce90..c345f9c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -104,7 +104,7 @@ jobs: name: Execute cloudfront.yml command: | aws cloudformation deploy \ - --template-file cloudfront.yml \ + --template-file ansible-ec2/cloudfront.yml \ --stack-name production-distro \ --parameter-overrides PipelineID="mybucket-${CIRCLE_WORKFLOW_ID:0:7}"