Esempio n. 1
0
    def _create_scenario(self, metadata, description, scenario_img,
                         scenario_size, options):
        try:
            scenario = Scenario.objects.get(name=metadata['name'])
            was_enabled = scenario.enabled
            scenario.title = metadata['title']
            scenario.memory = metadata['memory']
            scenario.description = description
            scenario.enabled = False
            created = False
            print('Updating scenario ...')
        except Scenario.DoesNotExist:
            scenario = Scenario(name=metadata['name'],
                                title=metadata['title'],
                                memory=metadata['memory'],
                                description=description)
            created = True
            print('Creating scenario ...')

        scenario.save()

        print('Importing secrets for scenario ...')
        for scenario_secret in Secret.objects.filter(scenario=scenario):
            if scenario_secret.secret not in metadata['secrets']:
                scenario_secret.delete()

        for secret in metadata['secrets']:
            Secret.objects.get_or_create(scenario=scenario, secret=secret)

        print('Storing image on all nodes:')
        for node in scenario.get_nodes():
            volume = self._update_volume(node, scenario, scenario_size)

            if not options['skip_upload']:
                self._upload_image(node, scenario_img, scenario_size, volume)

            connections.close()

        if not created:
            scenario.enabled = was_enabled
            scenario.save()

        enable_str = 'is' if scenario.enabled else 'is NOT'
        print('Done! Scenario {0} enabled'.format(enable_str))
Esempio n. 2
0
    def _create_scenario(self, metadata, description, scenario_img,
                         scenario_size, options):
        try:
            scenario = Scenario.objects.get(name=metadata['name'])
            was_enabled = scenario.enabled
            scenario.title = metadata['title']
            scenario.memory = metadata['memory']
            scenario.description = description
            scenario.enabled = False
            created = False
            print('Updating scenario ...')
        except Scenario.DoesNotExist:
            scenario = Scenario(name=metadata['name'], title=
                    metadata['title'], memory=metadata['memory'],
                    description=description)
            created = True
            print('Creating scenario ...')
        
        scenario.save()

        print('Importing secrets for scenario ...')
        for scenario_secret in Secret.objects.filter(scenario=scenario):
            if scenario_secret.secret not in metadata['secrets']:
                scenario_secret.delete()

        for secret in metadata['secrets']:
            Secret.objects.get_or_create(scenario=scenario, secret=secret)

        print('Storing image on all nodes:')
        for node in scenario.get_nodes():
            volume = self._update_volume(node, scenario, scenario_size)

            if not options['skip_upload']:
                self._upload_image(node, scenario_img, scenario_size, volume)


            connections.close()

        if not created:
            scenario.enabled = was_enabled
            scenario.save()
        
        enable_str = 'is' if scenario.enabled else 'is NOT'
        print('Done! Scenario {0} enabled'.format(enable_str))
Esempio n. 3
0
    def _create_scenario(self, metadata, description, scenario_img,
                         scenario_size, media_dir, options):
        secrets = extract_secrets(description)
        num_secrets = len(secrets)
        
        image_name = 'si' + str(int(time.time() * 1000))
        image_hash = self._calculate_image_hash(scenario_img)

        try:
            scenario = Scenario.objects.get(name=metadata['name'])
            was_enabled = scenario.enabled
            scenario.title = metadata['title']
            scenario.memory = metadata['memory']
            scenario.description = description
            scenario.num_secrets = num_secrets 
            scenario.enabled = False
            created = False
            image = scenario.image
            if image_hash != image.hash:
                image = BaseImage.objects.create(name=image_name,
                                                 hash=image_hash)
                upload_image = True
            else:
                upload_image = False
            print('Updating scenario ...')
        except Scenario.DoesNotExist:
            image = BaseImage.objects.create(name=image_name, hash=image_hash)
            scenario = Scenario(name=metadata['name'], title=
                    metadata['title'], memory=metadata['memory'],
                    image=image, description=description,
                    num_secrets=num_secrets)
            created = True
            upload_image = True
            print('Creating scenario ...')
        
        scenario.save()

        print('Importing secrets for scenario ...')
        for scenario_secret in Secret.objects.filter(scenario=scenario):
            if scenario_secret.secret not in secrets:
                scenario_secret.delete()

        for secret in secrets:
            Secret.objects.get_or_create(scenario=scenario, secret=secret)

        print('Copying media files ...')
        media_target = os.path.join(settings.MEDIA_ROOT, metadata['name'])
        
        # Remove old media dir if it exists, otherwise just ignore errors
        shutil.rmtree(media_target, ignore_errors=True)

        if os.path.exists(media_dir):
            shutil.copytree(media_dir, media_target)

        if upload_image:
            print('Storing image on all nodes:')
            for node in scenario.get_nodes():
                volume = self._create_volume(node, image, scenario_size)
                self._upload_image(node, scenario_img, scenario_size, volume)
                connections.close()

        if not created:
            scenario.image = image
            scenario.enabled = was_enabled
            scenario.save()
        
        enable_str = 'is' if scenario.enabled else 'is NOT'
        print('Done! Scenario {0} enabled'.format(enable_str))
Esempio n. 4
0
    def _create_scenario(self, metadata, description, scenario_img,
                         scenario_size, media_dir, options):
        secrets = extract_secrets(description)
        num_secrets = len(secrets)

        image_name = 'si' + str(int(time.time() * 1000))
        image_hash = self._calculate_image_hash(scenario_img)

        try:
            scenario = Scenario.objects.get(name=metadata['name'])
            was_enabled = scenario.enabled
            scenario.title = metadata['title']
            scenario.memory = metadata['memory']
            scenario.description = description
            scenario.num_secrets = num_secrets
            scenario.enabled = False
            created = False
            image = scenario.image
            if image_hash != image.hash:
                image = BaseImage.objects.create(name=image_name,
                                                 hash=image_hash)
                upload_image = True
            else:
                upload_image = False
            print('Updating scenario ...')
        except Scenario.DoesNotExist:
            image = BaseImage.objects.create(name=image_name, hash=image_hash)
            scenario = Scenario(name=metadata['name'],
                                title=metadata['title'],
                                memory=metadata['memory'],
                                image=image,
                                description=description,
                                num_secrets=num_secrets)
            created = True
            upload_image = True
            print('Creating scenario ...')

        scenario.save()

        print('Importing secrets for scenario ...')
        for scenario_secret in Secret.objects.filter(scenario=scenario):
            if scenario_secret.secret not in secrets:
                scenario_secret.delete()

        for secret in secrets:
            Secret.objects.get_or_create(scenario=scenario, secret=secret)

        print('Copying media files ...')
        media_target = os.path.join(settings.MEDIA_ROOT, metadata['name'])

        # Remove old media dir if it exists, otherwise just ignore errors
        shutil.rmtree(media_target, ignore_errors=True)

        if os.path.exists(media_dir):
            shutil.copytree(media_dir, media_target)

        if upload_image:
            print('Storing image on all nodes:')
            for node in scenario.get_nodes():
                volume = self._create_volume(node, image, scenario_size)
                self._upload_image(node, scenario_img, scenario_size, volume)
                connections.close()

        if not created:
            scenario.image = image
            scenario.enabled = was_enabled
            scenario.save()

        enable_str = 'is' if scenario.enabled else 'is NOT'
        print('Done! Scenario {0} enabled'.format(enable_str))