[cs615asa] [git commit] CS615 EBS-BACKUP; backup a directory into Elastic Block Storage (EBS) branch main updated. 34fa2f390dd59e9525fe9fd1ba38f6b13678fd2a

Git Owner jschauma at stevens.edu
Sun May 2 13:19:03 EDT 2021


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "CS615 EBS-BACKUP; backup a directory into Elastic Block Storage (EBS)".

The branch, main has been updated
       via  34fa2f390dd59e9525fe9fd1ba38f6b13678fd2a (commit)
      from  c949801a4611ed877b37544e905acc607c8b0214 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 34fa2f390dd59e9525fe9fd1ba38f6b13678fd2a
Author: camatang <camatang at stevens.edu>
Date:   Sat May 1 22:11:34 2021 -0400

    EBS_BACKUP_FLAGS_AWS arg parser

diff --git a/src/ec2.py b/src/ec2.py
index a351225..011527e 100644
--- a/src/ec2.py
+++ b/src/ec2.py
@@ -1,12 +1,11 @@
+import json
 import os
+import yaml
+import re
 
 from botocore.exceptions import ClientError
 
 
-def parse_overrides(override_string):
-    pass
-
-
 class EC2(object):
     session = None
     ec2_client = None
@@ -27,19 +26,22 @@ class EC2(object):
             if self.instance is None:
                 raise Exception('Could not find an instance with ID: ' + config['instance_id'])
         else:
+            args = {
+                'ImageId': 'ami-0018b2d98332ba7e3',
+                'MinCount': 1,
+                'MaxCount': 1,
+                'InstanceType': 't2.micro',
+                'KeyName': 'ebs-backup',
+                'Placement': {"AvailabilityZone": config.get('zone_id', 'us-east-1a')}
+            }
+
             # TODO: need to parse 'EBS_BACKUP_FLAGS_AWS' in
             #  order to override defaults...
             if os.getenv('EBS_BACKUP_FLAGS_AWS') is not None:
-                parse_overrides(os.getenv('EBS_BACKUP_FLAGS_AWS'))
-
-            instance = self.ec2_client.run_instances(
-                ImageId='ami-0018b2d98332ba7e3',
-                MinCount=1,
-                MaxCount=1,
-                InstanceType='t2.micro',
-                KeyName='ebs-backup',
-                Placement={"AvailabilityZone": config.get('zone_id', 'us-east-1a')}
-            )
+                args = EC2.parse_overrides(os.getenv('EBS_BACKUP_FLAGS_AWS'), args)
+
+            instance = self.ec2_client.run_instances(**args)
+
             # TODO: make sure we are getting the correct instance
             #  (and not an existing instance on the account)
             self.instance_id = instance["Instances"][0]["InstanceId"]
@@ -47,6 +49,120 @@ class EC2(object):
                 if i.instance_id == self.instance_id:
                     self.instance = i
 
+    @staticmethod
+    def primitive_check(value):
+        if '[' not in value and '{' not in value and '=' not in value and ',' not in value:
+            return True
+        return False
+
+    @staticmethod
+    def parse_values(value, pl):
+        if EC2.primitive_check(value):
+            return { 'pl': value, 'n': len(value) }
+        cursor = 0
+        x = 0
+        n = 0
+        while n < len(value):
+
+            if value[n] == '[':
+                group = re.search('\[(.*)]', value[n:]).group(1)
+                res = EC2.parse_values(group, [])['pl']
+                if isinstance(pl, list):
+                    pl.append(res)
+                else:
+                    pl = res
+                n += len(group) + 1
+                cursor += n + 1
+                x = n
+            elif value[n] == '{':
+                group = re.search('\{(.*?)\}', value[n:]).group(1)
+                res = EC2.parse_values(group, {})['pl']
+                if isinstance(pl, list):
+                    pl.append(res)
+                else:
+                    pl = res
+                n += len(group) + 1
+                cursor += n + 1
+                x = n
+            elif value[n] == '=':
+                key = value[cursor:n]
+                res = EC2.parse_values(value[n + 1:], {})
+                pl[key] = res['pl']
+                n = n + res['n'] + 1
+                cursor += n + 1
+                x = n
+            elif value[n] == ',':
+                if cursor != n:
+                    x = n
+                    if len(value.split('=')[0].split(',')) > 1:
+                        res = value.split('=')[0].split(',')[:-1]
+                        pl = res
+                        break
+                    else:
+                        res = value[cursor-1:n]
+                    if isinstance(pl, list):
+                        pl.append(res)
+                    elif bool(pl):
+                        cursor = n+1
+                        pass
+                    else:
+                        pl = value[cursor-1:n]
+                        break
+            elif value.lower().startswith('true'):
+                res = True
+                if isinstance(pl, list):
+                    pl.append(res)
+                else:
+                    pl = res
+                    break
+            elif value.lower().startswith('false'):
+                res = False
+                if isinstance(pl, list):
+                    pl.append(res)
+                else:
+                    pl = res
+                break
+            else:
+                pass
+            n += 1
+        return { 'pl': pl, 'n': x }
+
+    @staticmethod
+    def parse_overrides(override_string, args):
+        if '--cli-input-json' in override_string:
+            file = override_string.split('--cli-input-json')[1].trim().split(' ')[0]
+            return json.loads(file)
+        if '--cli-input-yaml' in override_string:
+            file = override_string.split('--cli-input-yaml')[1].trim().split(' ')[0]
+            return yaml.load(file)
+
+        parts = override_string.split(" ")
+        i = 0
+        while i < len(parts):
+            temp = parts[i + 0].replace('--', '').split('-')
+            key = temp[0].capitalize() + ''.join(ele.capitalize() for ele in temp[1:])
+            val = parts[i + 1]
+            if EC2.primitive_check(val):
+                if val.lower().startswith('true') or val.lower().startswith('false'):
+                    args[key] = bool(val)
+                else:
+                    args[key] = val
+            else:
+                if key in ['BlockDeviceMappings',
+                           'Ipv6Addresses',
+                           'SecurityGroupIds',
+                           'SecurityGroups',
+                           'NetworkInterfaces',
+                           'ElasticGpuSpecification',
+                           'ElasticInferenceAccelerators',
+                           'TagSpecifications',
+                           'LicenseSpecifications']:
+                    args[key] = [ EC2.parse_values(val, {})['pl'] ]
+                else:
+                    args[key] = EC2.parse_values(val, {})['pl']
+            i += 2
+        return args
+
     def get_a_zone(self):
         try:
             self.ec2_client.describe_availability_zones(DryRun=True)

-----------------------------------------------------------------------

Summary of changes:
 src/ec2.py | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 130 insertions(+), 14 deletions(-)


hooks/post-receive
-- 
CS615 EBS-BACKUP; backup a directory into Elastic Block Storage (EBS)


More information about the cs615asa mailing list