{"id":2165,"date":"2021-03-24T19:21:36","date_gmt":"2021-03-24T16:21:36","guid":{"rendered":"https:\/\/artem.services\/?p=2165"},"modified":"2022-06-08T21:43:48","modified_gmt":"2022-06-08T18:43:48","slug":"python-aws-ebs-%d1%81%d0%be%d0%b7%d0%b4%d0%b0%d0%bd%d0%b8%d0%b5-%d1%81%d0%bd%d0%b0%d0%bf%d1%88%d0%be%d1%82%d0%be%d0%b2-%d0%bf%d0%be-%d1%82%d0%b5%d0%b3%d0%b0%d0%bc-%d0%b8-%d1%85%d1%80%d0%b0%d0%bd","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=2165","title":{"rendered":"Python &#8212; AWS EBS \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u043e\u0432 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u0433\u0430 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"wp-image-2273 size-full aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo.png\" alt=\"\" width=\"1024\" height=\"500\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-300x146.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-768x375.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-954x466.png 954w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0438\u0449\u0435\u0442 <strong>EBS <\/strong>\u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0435 &quot;eu-west-1&quot; \u0441 \u0442\u0435\u0433\u043e\u043c, \u043a\u043b\u044e\u0447 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e &quot;<strong>Application<\/strong>&quot; \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043d\u0430\u043f\u0448\u043e\u0442 \u0434\u0430\u043d\u043d\u043e\u0433\u043e <strong>EBS<\/strong>. \u0422\u0430\u043a \u0436\u0435 \u043f\u043e \u0442\u043e\u043c\u0443 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u043e\u043d \u0438\u0449\u0435\u0442 \u0441\u043d\u0430\u043f\u0448\u043e\u0442 \u043f\u043e \u0442\u0435\u0433\u0443, \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0432\u0441\u0435, \u043a\u0440\u043e\u043c\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u0430:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 main.py create artem-test-app\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0440\u044b\u0445 \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u043e\u0432:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 main.py cleanup artem-test-app\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>main.py:<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom __future__ import print_function\r\nfrom datetime import datetime\r\nimport sys\r\nimport boto3\r\nimport botocore\r\nimport urllib.request\r\n\r\nec2 = boto3.client(&#039;ec2&#039;, region_name=&#039;eu-west-1&#039;)\r\n\r\ndef create_snapshot(app):\r\n    print(&quot;Creating snapshot for &quot; + str(app))\r\n    # Get Instance ID\r\n    instances_id = []\r\n    response = ec2.describe_instances(\r\n        Filters = [\r\n            {\r\n                &#039;Name&#039;: &#039;tag:Application&#039;,\r\n                &#039;Values&#039;: [\r\n                    app,\r\n                ]\r\n            },\r\n            {\r\n                &#039;Name&#039;: &#039;instance-state-name&#039;,\r\n                &#039;Values&#039;: [&#039;running&#039;]\r\n            }\r\n        ]\r\n    )\r\n\r\n    for reservation in response[&#039;Reservations&#039;]:\r\n        for instance in reservation[&#039;Instances&#039;]:\r\n            instances_id.append(instance[&#039;InstanceId&#039;])\r\n\r\n    # Get Volumes ID\r\n    volumes_id = []\r\n    for instance_id in instances_id:\r\n        response = ec2.describe_instance_attribute(InstanceId = instance_id, Attribute = &#039;blockDeviceMapping&#039;)\r\n        for BlockDeviceMappings in response[&#039;BlockDeviceMappings&#039;]:\r\n            volumes_id.append(BlockDeviceMappings[&#039;Ebs&#039;][&#039;VolumeId&#039;])\r\n\r\n    # Create Volume Snapshot\r\n    for volume_id in volumes_id:\r\n        date = datetime.now()\r\n        response = ec2.create_snapshot(\r\n            Description = app + &quot; snapshot created by Jenkins at &quot; + str(date),\r\n            VolumeId = volume_id,\r\n            TagSpecifications=[\r\n                {\r\n                    &#039;ResourceType&#039;: &#039;snapshot&#039;,\r\n                    &#039;Tags&#039;: [\r\n                        {\r\n                            &#039;Key&#039;: &#039;Name&#039;,\r\n                            &#039;Value&#039;: app + &quot;-snapshot&quot;\r\n                        },\r\n                    ]\r\n                },\r\n            ]\r\n        )\r\n\r\n\r\n        try:\r\n            print(&quot;Waiting until snapshot will be created...&quot;)\r\n            snapshot_id = response[&#039;SnapshotId&#039;]\r\n            snapshot_complete_waiter = ec2.get_waiter(&#039;snapshot_completed&#039;)\r\n            snapshot_complete_waiter.wait(SnapshotIds=[snapshot_id], WaiterConfig = { &#039;Delay&#039;: 30, &#039;MaxAttempts&#039;: 120})\r\n\r\n        except botocore.exceptions.WaiterError as e:\r\n                print(e)\r\n\r\ndef cleanup_snapshot(app):\r\n    print(&quot;Clean up step.&quot;)\r\n    print(&quot;Looking for all snapshots regarding &quot; + str(app) + &quot; application.&quot;)\r\n    # Get snapshots\r\n    response = ec2.describe_snapshots(\r\n        Filters = [\r\n            {\r\n                &#039;Name&#039;: &#039;tag:Name&#039;,\r\n                &#039;Values&#039;: [\r\n                    app + &quot;-snapshot&quot;,\r\n                ]\r\n            },\r\n        ],\r\n        OwnerIds = [\r\n            &#039;self&#039;,\r\n        ]\r\n    )\r\n\r\n    sorted_snapshots = sorted(response[&#039;Snapshots&#039;], key=lambda k: k[&#039;StartTime&#039;])\r\n\r\n    # Clean up snapshots and keep only the latest snapshot\r\n    for snapshot in sorted_snapshots[:-1]:\r\n        print(&quot;Deleting snapshot: &quot; + str(snapshot[&#039;SnapshotId&#039;]))\r\n        response = ec2.delete_snapshot(\r\n            SnapshotId = snapshot[&#039;SnapshotId&#039;]\r\n        )\r\n\r\ndef main():\r\n    if sys.argv[1:] and sys.argv[2:]:\r\n        action = sys.argv[1]\r\n        app = sys.argv[2]\r\n        if action == &#039;create&#039;:\r\n            create_snapshot(app)\r\n        elif action == &#039;cleanup&#039;:\r\n            cleanup_snapshot(app)\r\n        else:\r\n            print(&quot;Wrong action: &quot; + str(action))\r\n    else:\r\n        print(&quot;This script for create and cleanup snapshots&quot;)\r\n        print(&quot;Usage  : python3 &quot; + sys.argv[0] + &quot; {create|cleanup} &quot; + &quot;{application_tag}&quot;)\r\n        print(&quot;Example: python3 &quot; + sys.argv[0] + &quot; create &quot; + &quot;ebs-snapshot-check&quot;)\r\n\r\nif __name__ == &#039;__main__&#039;:\r\n    main()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0438\u0449\u0435\u0442 EBS \u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0435 &quot;eu-west-1&quot; \u0441 \u0442\u0435\u0433\u043e\u043c, \u043a\u043b\u044e\u0447 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e &quot;Application&quot; \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043d\u0430\u043f\u0448\u043e\u0442 \u0434\u0430\u043d\u043d\u043e\u0433\u043e EBS. \u0422\u0430\u043a \u0436\u0435 \u043f\u043e \u0442\u043e\u043c\u0443 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u043e\u043d \u0438\u0449\u0435\u0442 \u0441\u043d\u0430\u043f\u0448\u043e\u0442 \u043f\u043e \u0442\u0435\u0433\u0443, \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0432\u0441\u0435, \u043a\u0440\u043e\u043c\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e. \u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u0430: &nbsp; \u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0440\u044b\u0445 \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u043e\u0432: &nbsp; main.py: &nbsp; &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[25,1425,43,1159,1161,1767],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2165"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2165"}],"version-history":[{"count":6,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2165\/revisions"}],"predecessor-version":[{"id":2274,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2165\/revisions\/2274"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}