Example #1
0
    def handle_noargs(self, **options):
        self.run = True
        signal.signal(signal.SIGINT, lambda sig, frame: self.stop())
        signal.signal(signal.SIGTERM, lambda sig, frame: self.stop())

        last_call = time.time()
        while self.run:
            # Process all open tasks
            for task in RunTaskQueue.objects.all():
                try:
                    self._handle_task(task)
                except ScenarioError:
                    # This can happen if someone manages the vm manually.
                    # We can just ignore it, it does no harm
                    pass
                task.delete()
           
            # Delete expired scenarios
            expired_runs = ScenarioRun.objects.filter(last_activity__lt=
                    datetime.today() - settings.SCENARIO_EXPIRE_TIME)
            for scenario_run in expired_runs:
                try:
                    scenario_run.vm.destroy()
                except VirtualMachineError:
                    # We have an inconsistent state. See comment above.
                    pass

            current_time = time.time()
            time_passed = current_time - last_call
            if time_passed < MIN_SLEEP:
                time.sleep(MIN_SLEEP - time_passed)
            last_call = current_time
        connections.close()
Example #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))
Example #3
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))
Example #4
0
    def handle_noargs(self, **options):
        self.run = True
        signal.signal(signal.SIGINT, lambda sig, frame: self.stop())
        signal.signal(signal.SIGTERM, lambda sig, frame: self.stop())

        last_call = time.time()
        while self.run:
            for task in RunTaskQueue.objects.all():
                try:
                    self._handle_task(task)
                except ScenarioError:
                    # This can happen if someone manages the vm manually.
                    # We can just ignore it, it does no harm
                    pass
                task.delete()
            current_time = time.time()
            time_passed = current_time - last_call
            if time_passed < MIN_SLEEP:
                time.sleep(MIN_SLEEP - time_passed)
            last_call = current_time
        connections.close()
Example #5
0
    def handle_noargs(self, **options):
        self.run = True
        signal.signal(signal.SIGINT, lambda sig, frame: self.stop())
        signal.signal(signal.SIGTERM, lambda sig, frame: self.stop())

        last_call = time.time()
        while self.run:
            for task in RunTaskQueue.objects.all():
                try:
                    self._handle_task(task)
                except ScenarioError:
                    # This can happen if someone manages the vm manually.
                    # We can just ignore it, it does no harm
                    pass
                task.delete()
            current_time = time.time()
            time_passed = current_time - last_call
            if time_passed < MIN_SLEEP:
                time.sleep(MIN_SLEEP - time_passed)
            last_call = current_time
        connections.close()
Example #6
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))
Example #7
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))