Beispiel #1
0
def finalize_project(sender, instance, created, raw, update_fields, **kwargs):
    # Ensure a project has at least one recipe on creation.
    if created and not instance.analysis_set.exists():
        # Add starter hello world recipe to project.
        try:
            json_text = open(join(DATA_DIR, 'starter.hjson'), 'r').read()
            template = open(join(DATA_DIR, 'starter.sh'), 'r').read()
            image = os.path.join(DATA_DIR, 'starter.png')
            image_stream = open(image, 'rb')
        except Exception as exc:
            logger.error(f'{exc}')
            json_text = '{}'
            template = "echo 'Hello World'"
            image_stream = None

        name = 'First recipe'
        text = "This recipe was created automatically."

        # Create starter recipe.
        auth.create_analysis(project=instance,
                             json_text=json_text,
                             template=template,
                             name=name,
                             text=text,
                             stream=image_stream)
def upload_recipe(obj, project, user=None):

    recipe = Analysis.objects.filter(uid=obj.uid).first()
    json_text = toml.dumps(obj.json)

    if not recipe:
        recipe = auth.create_analysis(project=project,
                                      user=user,
                                      uid=obj.uid,
                                      name=obj.name,
                                      text=obj.text,
                                      json_text=json_text,
                                      template=obj.code)

    recipe.uid = obj.uid
    recipe.name = obj.name
    recipe.text = obj.text
    recipe.date = obj.date

    recipe.json_text = json_text or recipe.json_text
    recipe.template = obj.code
    update_image(recipe, obj.image)

    recipe.save()

    return
Beispiel #3
0
def initial_recipe(project):
    # Add starter hello world recipe to project.
    try:
        json_text = open(join(DATA_DIR, 'starter.hjson'), 'r').read()
        template = open(join(DATA_DIR, 'starter.sh'), 'r').read()
        image = os.path.join(DATA_DIR, 'starter.png')
        image_stream = open(image, 'rb')
    except Exception as exc:
        logger.error(f'{exc}')
        json_text = ''
        template = "echo 'Hello World'"
        image_stream = None

    name = 'First recipe'
    text = "This recipe was created automatically."

    # Create starter recipe.
    recipe = auth.create_analysis(project=project,
                                  json_text=json_text,
                                  template=template,
                                  name=name,
                                  text=text,
                                  stream=image_stream,
                                  security=Analysis.AUTHORIZED)
    return recipe
Beispiel #4
0
def recipe_create(request, uid):
    # Get the project

    project = Project.objects.filter(uid=uid).first()

    # Prepare the form
    name = "Recipe Name"
    initial = dict(name=name, uid=f'recipe-{util.get_uuid(5)}')
    form = forms.RecipeForm(user=request.user, initial=initial, project=project)

    if request.method == "POST":
        form = forms.RecipeForm(data=request.POST, creating=True, project=project, files=request.FILES,
                                user=request.user)
        if form.is_valid():
            image = form.cleaned_data['image']
            recipe_uid = form.cleaned_data['uid']
            name = form.cleaned_data['name']
            json_text = form.cleaned_data['json_text']
            template = form.cleaned_data['template']
            recipe = auth.create_analysis(uid=recipe_uid, stream=image, name=name,
                                          json_text=json_text, template=template,
                                          project=project, user=request.user)

            return redirect(reverse("recipe_view", kwargs=dict(uid=recipe.uid)))

    action_url = reverse('recipe_create', kwargs=dict(uid=uid))
    context = dict(project=project, form=form, action_url=action_url,
                   activate='Create Recipe', name=name)
    counts = get_counts(project)
    context.update(counts)
    return render(request, 'recipe_edit.html', context)
Beispiel #5
0
def upload_recipe(obj, project, user=None, create=False):

    recipe = Analysis.objects.filter(uid=obj.uid).first()

    if not recipe and create:
        recipe = auth.create_analysis(project=project,
                                      user=user,
                                      uid=obj.uid,
                                      json_text=obj.json,
                                      template=obj.code)

    elif not recipe:
        return

    recipe.uid = obj.uid
    recipe.name = obj.name
    recipe.text = obj.text
    recipe.date = obj.date
    recipe.image = obj.image
    recipe.json_text = toml.dumps(obj.json)
    recipe.template = obj.code

    recipe.save()

    return
Beispiel #6
0
def create_recipe(json_file, root_dir, job=False):
    """Create a recipe from a json file."""

    source = os.path.abspath(os.path.join(root_dir, json_file))
    json_data = hjson.loads(open(source, "r").read())
    rid, url, image, template = parse_recipe(json_data)
    recipe = Analysis.objects.get_all(uid=rid).first()

    if not recipe:
        # Get an existing project uid
        pid = json_data.get("settings", {}).get("project_uid", )
        name = json_data.get("settings", {}).get("name", "New Recipe")
        text = json_data.get("settings", {}).get("help", "New Text")
        project = Project.objects.filter(uid=pid).first()
        image_stream = open(os.path.abspath(os.path.join(root_dir, image)),
                            "rb")
        template = open(os.path.abspath(os.path.join(root_dir, template)),
                        "r").read()
        recipe = auth.create_analysis(name=name,
                                      project=project,
                                      text=text,
                                      uid=rid,
                                      json_text=hjson.dumps(json_data),
                                      template=template,
                                      stream=image_stream)
    if job:
        create_job(recipe=recipe)

    return recipe
Beispiel #7
0
    def setUp(self):
        logger.setLevel(logging.WARNING)
        self.username = f"tested{get_uuid(10)}"

        self.owner = models.User.objects.create(username=self.username,
                                                is_staff=True,
                                                email="*****@*****.**")
        self.owner.set_password("tested")
        self.owner.save()

        self.project = auth.create_project(user=self.owner,
                                           name="Test project",
                                           privacy=models.Project.PUBLIC,
                                           uid="tested")
        data = auth.create_data(project=self.project, path=__file__)
        analysis = auth.create_analysis(project=self.project,
                                        json_text='',
                                        template="# Add code here.")
        self.job = auth.create_job(analysis=analysis)

        self.proj_params = dict(uid=self.project.uid)
        self.analysis_params = dict(uid=analysis.uid)
        self.recipes_id_param = dict(id=analysis.id)
        self.data_params = dict(uid=data.uid)
        self.job_params = dict(uid=self.job.uid)

        self.data_file_params = dict(uid=data.uid, path="foo.txt")
        self.job_file_params = dict(uid=self.job.uid, path="foo.txt")
Beispiel #8
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        # Set up generic owner
        self.owner = models.User.objects.create_user(
            username=f"tested{get_uuid(10)}", email="*****@*****.**")
        self.owner.set_password("tested")

        self.project = auth.create_project(user=self.owner,
                                           name="tested",
                                           text="Text",
                                           summary="summary",
                                           uid="tested")

        self.recipe = auth.create_analysis(project=self.project,
                                           json_text="{}",
                                           template="",
                                           security=models.Analysis.AUTHORIZED)

        self.snippet_type = models.SnippetType.objects.create(
            name='Snippet type', owner=self.owner)

        self.snippet = models.Snippet.objects.create(
            command='ls -l',
            type=self.snippet_type,
            help_text='List files in directory',
            owner=self.owner)

        self.job = auth.create_job(analysis=self.recipe, user=self.owner)
        self.job.save()
Beispiel #9
0
def recipe_create(request, uid):
    # Get the project
    project = Project.objects.filter(uid=uid).first()
    recipe = auth.create_analysis(project=project,
                                  name="My New Recipe",
                                  template="echo 'Hello World!'")
    url = reverse("recipe_view", kwargs=dict(uid=recipe.uid))
    return redirect(url)
    def test_recipe_update(self):
        "Test updating recipe through auth"

        changed = auth.create_analysis(project=self.project,
                                       json_text=self.recipe.json_text,
                                       template=self.recipe.template,
                                       uid=self.recipe.uid, update=True)

        self.assertEqual(changed.uid, self.recipe.uid)
Beispiel #11
0
 def copy(instance):
     # Cascade the root if the recipe is being cloned
     if paste_as_clone:
         root = instance.root if instance.is_cloned else instance
     else:
         root = None
     recipe = auth.create_analysis(project=project, user=user, root=root,
                                   json_text=instance.json_text, security=instance.security,
                                   template=instance.template,
                                   name=instance.name, text=instance.text, stream=instance.image)
     return recipe
Beispiel #12
0
def recipe_create(request, uid):
    # Get the project
    project = Project.objects.filter(uid=uid).first()
    recipe = auth.create_analysis(project=project, name="Recipe", template="echo 'Hello World!'")

    # Ensure recipe names are distinguishable from one another.
    recipe.name = f"{recipe.name} {recipe.id}"
    recipe.save()

    url = reverse("recipe_view", kwargs=dict(uid=recipe.uid))
    messages.success(request, "A new recipe has been created.")
    return redirect(url)
    def handle(self, *args, **options):

        # Collect the parameters.
        pid = options['pid']
        rid = options["rid"]
        json = options['json']
        template = options['template'] or replace_ext(json, "sh")
        image = options["image"] or replace_ext(json, "png")
        info = options['info']
        name = options['name']
        update = options["update"]

        # Select project
        project = Project.objects.filter(uid=pid).first() if pid else None

        # Project must exist if the recipe has not been created yet.
        if not project:
            logger.error(f"Project pid={pid} not found!")
            return

        # Get the project.
        recipe = Analysis.objects.filter(uid=rid).first() if rid else None

        # Check if recipe exists
        if recipe and not update:
            logger.error(
                f"Recipe already exists rid={rid}. Add the --update flag to update."
            )
            return

        # Create the recipe
        json = open(json, "r").read() if json else ""
        template = open(template, "r").read() if template else ""
        project = recipe.project if recipe else project

        # Get the image stream
        stream = open(image, "rb") if image else None

        rec = auth.create_analysis(project=project,
                                   json_text=json,
                                   template=template,
                                   name=name,
                                   text=info,
                                   uid=rid,
                                   update=update,
                                   stream=stream)

        if update:
            print(f"*** Updated recipe uid={rec.uid} name={rec.name}")
        else:
            print(f"*** Created recipe uid={rec.uid} name={rec.name}")
    def setUp(self):
        logger.setLevel(logging.WARNING)

        # Set up generic owner
        self.owner = models.User.objects.create_user(username=f"tested{get_uuid(10)}", email="*****@*****.**",
                                                     is_staff=True, is_superuser=True)
        self.owner.set_password("tested")
        self.factory = RequestFactory()

        self.project = auth.create_project(user=self.owner, name="tested", text="Text", summary="summary",
                                           uid="tested")
        # Test data
        self.recipe = auth.create_analysis(project=self.project, json_text="", template="#test template")
        self.recipe.save()
Beispiel #15
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        # Set up generic owner
        self.owner = models.User.objects.create_user(username=f"tested{util.get_uuid(10)}", email="*****@*****.**")
        self.owner.set_password("tested")

        self.project = auth.create_project(user=self.owner, name="tested", text="Text", summary="summary",
                                           uid="tested")

        self.recipe = auth.create_analysis(project=self.project, json_text="{}", template="",
                                           security=models.Analysis.AUTHORIZED)

        self.job = auth.create_job(analysis=self.recipe, user=self.owner)
        self.job.save()
Beispiel #16
0
    def handle(self, *args, **options):

        json = options['json']
        pid = options['id']
        uid = options["uid"]
        template_fname = options['template']
        jobs = options['jobs']
        update = options["update"]

        verbosity = int(options['verbosity'])

        if verbosity > 1:
            logger.setLevel(logging.DEBUG)
            logger.info(f"level={verbosity}")

        # Require JSON and templates to exist.
        if not (json and template_fname):
            logger.error(
                "This command requires --json and a --template to be set")
            return

        # Get the target project.
        if pid:
            project = Project.objects.filter(id=pid).first()
        else:
            project = Project.objects.filter(uid=uid).first()

        # Invalid project specified.
        if not project:
            logger.error(f'No project with id={pid} , uid={uid}')
            return

        # JSON file does not exist.
        if not os.path.isfile(json):
            logger.error(f'No file found for --json={json}')
            return

        # Template file does not exist.
        if not os.path.isfile(template_fname):
            logger.error(f'No file found for --template={template_fname}')
            return

        try:
            # Parse the json_text into json_data
            json_text = open(json).read()
            json_path = os.path.dirname(json)
            json_data = hjson.loads(json_text)
        except Exception as exc:
            logger.exception(f"JSON exception in file: {json}\n{exc}")
            return
        try:
            # Read the specification
            template = open(template_fname).read()
        except Exception as exc:
            logger.exception(f"Template exception: {exc}")
            return

        try:
            name = json_data.get("settings", {}).get("name", "")
            text = json_data.get("settings", {}).get("help", "")
            uid = json_data.get("settings", {}).get("uid", "")
            image = json_data.get("settings", {}).get("image", "")
            text = textwrap.dedent(text)
            summary = json_data.get("settings", {}).get("summary", "")

            analysis = auth.create_analysis(project=project,
                                            uid=uid,
                                            json_text=json_text,
                                            summary=summary,
                                            template=template,
                                            name=name,
                                            text=text,
                                            security=Analysis.AUTHORIZED,
                                            update=update)

            # Load the image if specified.
            if image:
                image_path = os.path.join(json_path, image)
                if os.path.isfile(image_path):
                    stream = open(image_path, 'rb')
                    analysis.image.save(image, stream, save=True)
                    logger.info(f"Image path: {image_path}")
                else:
                    logger.error(f"Skipping invalid image path: {image_path}")

            if jobs:
                # When creating a job automatically for data in projects
                # it will try to match the value of the parameter to the data name.
                missing_name = ''
                for key, obj in json_data.items():
                    if obj.get("source") != "PROJECT":
                        continue

                    name = obj.get('value', '')
                    data = Data.objects.filter(project=project,
                                               name=name).first()

                    if not data:
                        missing_name = name
                        break

                    data.fill_dict(obj)

                if missing_name:
                    logger.error(
                        f"Job not created! Missing data:{missing_name} in analysis:{analysis.name}"
                    )
                else:
                    auth.create_job(analysis=analysis, json_data=json_data)

        except Exception as exc:
            logger.exception(f"Error: {exc}")
            return