Exemple #1
0
def search(request):
    if request.method == "POST":
        val_search = request.POST["search"]
        val_type = request.POST["type"]
        script = Script()
        scripts = []
        if val_type == "title":
            scripts = script.getTitles(val_search)
        else:
            scripts = script.getAuthors(val_search)
        return render_to_response("scripts.html", {"scripts": scripts}, context_instance=RequestContext(request))
def processReplicaScripts(scriptsFile, replica):
    try:
        reader = csv.DictReader(scriptsFile, delimiter='\t')
    except (KeyboardInterrupt, SystemExit):
        raise
    except Exception as e:
        if hasattr(e, 'message'):
            return True, f'{e.message}', 0, 0
        else:
            return True, f'{e}', 0, 0
    # expected CSV structure:
    # Script    Step    Slot    Task Type   Oracle  Value
    last_script = None
    last_step = None
    last_script_num = 0
    last_step_num = 0
    _candidates = 0
    try:
        with transaction.atomic():
            for row in reader:
                # print(row['Script'], row['Step'], row['Slot'], row['Task Type'], row['Oracle'], row['Value'])
                _script, _step, _slot, _tasktype, _oracle, _value = row['Script'], row['Step'], row['Slot'], row['Task Type'], row['Oracle'], row['Value']
                if last_script_num != _script:
                    last_script = Script(replica=replica, script_num=_script, status=Script.STATUS_FREE, contactme=False, withdraw=False)
                    last_script.save()
                    last_script_num = _script
                    last_step = None
                    last_step_num = 0
                    _candidates = _candidates + 1
                if last_step_num != _step:
                    last_step = Step(script=last_script, step_num=_step, tasktype=_tasktype, oracle=_oracle)
                    last_step.save()
                    last_step_num = _step
                component = Component(step=last_step, slot_num=_slot, tasktype=_tasktype, value=_value)
                component.save()
    except KeyError:
        # rollback changes
        replica.scripts.all().delete()
        return True, 'Badly formed CSV file \n(expected: Script\tStep\tSlot\tTask Type\tOracle\tValue)', 0, 0
    except (KeyboardInterrupt, SystemExit):
        # rollback changes
        replica.scripts.all().delete()
        raise
    except Exception as e:
        # rollback changes
        replica.scripts.all().delete()
        if hasattr(e, 'message'):
            return True, f'{e.message}', 0, 0
        else:
            return True, f'{e}', 0, 0
    return False, '', _candidates, last_step_num
Exemple #3
0
def workflow_manager(workflow):
    form = AddScriptForm(request.form)
    form.scripts.choices = Script.choices()
    workflow = get_obj(Workflow, name=workflow)
    return render_template('workflow_manager.html',
                           form=form,
                           workflow=workflow)
Exemple #4
0
def shorturl(request, script_shorturl):
	#print short_url.encode_url(1)
	#print script_shorturl
	script_id = Script.get_id_from_url(script_shorturl)
	script = get_object_or_404(Script, pk=script_id)
	return render_to_response('scripts/showsource.html', 
		{'script' : script},
		context_instance=RequestContext(request))
Exemple #5
0
def task_management():
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    tasks = Task.query.all()
    return render_template('task_management.html',
                           tasks=tasks,
                           compare_form=CompareForm(request.form),
                           scheduling_form=scheduling_form)
Exemple #6
0
def view(view_type):
    view_options_form = ViewOptionsForm(request.form)
    google_earth_form = GoogleEarthForm(request.form)
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    labels = {'node': 'name', 'link': 'name'}
    if 'script' in request.form:
        data = dict(request.form)
        selection = map(int, session['selection'])
        scripts = request.form.getlist('scripts')
        workflows = request.form.getlist('workflows')
        data['scripts'] = [get_obj(Script, name=name) for name in scripts]
        data['workflows'] = [
            get_obj(Workflow, name=name) for name in workflows
        ]
        data['nodes'] = [get_obj(Node, id=id) for id in selection]
        data['user'] = current_user
        task = Task(**data)
        db.session.add(task)
        db.session.commit()
        return redirect(url_for('tasks_blueprint.task_management'))
    elif 'view_options' in request.form:
        # update labels
        labels = {
            'node': request.form['node_label'],
            'link': request.form['link_label']
        }
    # for the sake of better performances, the view defaults to markercluster
    # if there are more than 2000 nodes
    view = 'leaflet' if len(Node.query.all()) < 2000 else 'markercluster'
    if 'view' in request.form:
        view = request.form['view']
    # we clean the session's selected nodes
    session['selection'] = []
    return render_template(
        '{}_view.html'.format(view_type),
        filters=Filter.query.all(),
        view=view,
        scheduling_form=scheduling_form,
        view_options_form=view_options_form,
        google_earth_form=google_earth_form,
        labels=labels,
        names=pretty_names,
        subtypes=node_subtypes,
        node_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in Node.query.all()
        },
        link_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in Link.query.all()
        })
    def handle(self, *args, **kwargs):
        # 10-16-2020 changed the type of the agent's 'disks' model field
        # from a dict of dicts, to a list of disks in the golang agent
        # the following will convert dicts to lists for agent's still on the python agent
        agents = Agent.objects.only("pk", "disks")
        for agent in agents:
            if agent.disks is not None and isinstance(agent.disks, dict):
                new = []
                for k, v in agent.disks.items():
                    new.append(v)

                agent.disks = new
                agent.save(update_fields=["disks"])
                self.stdout.write(
                    self.style.SUCCESS(f"Migrated disks on {agent.hostname}"))

        # load community scripts into the db
        Script.load_community_scripts()
Exemple #8
0
def view(view_type):
    add_node_form = AddNode(request.form)
    add_link_form = AddLink(request.form)
    all_nodes = Node.choices()
    add_link_form.source.choices = add_link_form.destination.choices = all_nodes
    view_options_form = ViewOptionsForm(request.form)
    google_earth_form = GoogleEarthForm(request.form)
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    labels = {'node': 'name', 'link': 'name'}
    if 'view_options' in request.form:
        # update labels
        labels = {
            'node': request.form['node_label'],
            'link': request.form['link_label']
        }
    # for the sake of better performances, the view defaults to markercluster
    # if there are more than 2000 nodes
    view = 'leaflet' if len(Node.query.all()) < 2000 else 'markercluster'
    if 'view' in request.form:
        view = request.form['view']
    # we clean the session's selected nodes
    session['selection'] = []
    # name to id
    name_to_id = {node.name: id for id, node in enumerate(Node.query.all())}
    return render_template(
        '{}_view.html'.format(view_type),
        filters=Filter.query.all(),
        view=view,
        scheduling_form=scheduling_form,
        view_options_form=view_options_form,
        google_earth_form=google_earth_form,
        add_node_form=add_node_form,
        add_link_form=add_link_form,
        labels=labels,
        names=pretty_names,
        subtypes=node_subtypes,
        name_to_id=name_to_id,
        node_table={
            obj: OrderedDict([
                (property, getattr(obj, property))
                for property in type_to_public_properties[obj.type]
            ])
            for obj in Node.query.all()
        },
        link_table={
            obj: OrderedDict([
                (property, getattr(obj, property))
                for property in type_to_public_properties[obj.type]
            ])
            for obj in Link.query.all()
        })
Exemple #9
0
def newscript_view(request, *args, **kwargs):
    if request.method == 'POST':
        form = NewScriptForm(request.POST)
        print(f'received data: {request.POST}')
        print(f'new form: {form}')
        if form.is_valid():
            print('form is valid')
            save_script = Script(name=request.POST['name'],
                                 description=request.POST['description'],
                                 content=request.POST['content'],
                                 user=request.user)
            print(f'script: {save_script}')
            save_script.save()
            print('script saved')
            return redirect(scriptdash_view)
        else:
            return render(request, 'newscript.html', {'form': form})
    elif request.method == 'GET':
        form = NewScriptForm()
    else:
        return HttpResponse(status=404)
    return render(request, 'newscript.html', {'form': form})
    def handle(self, *args, **kwargs):
        # 10-16-2020 changed the type of the agent's 'disks' model field
        # from a dict of dicts, to a list of disks in the golang agent
        # the following will convert dicts to lists for agent's still on the python agent
        agents = Agent.objects.all()
        for agent in agents:
            if agent.disks is not None and isinstance(agent.disks, dict):
                new = []
                for k, v in agent.disks.items():
                    new.append(v)

                agent.disks = new
                agent.save(update_fields=["disks"])
                self.stdout.write(
                    self.style.SUCCESS(f"Migrated disks on {agent.hostname}"))

        # install go
        if not os.path.exists("/usr/local/rmmgo/"):
            self.stdout.write(self.style.SUCCESS("Installing golang"))
            subprocess.run("sudo mkdir -p /usr/local/rmmgo", shell=True)
            tmpdir = tempfile.mkdtemp()
            r = subprocess.run(
                f"wget https://golang.org/dl/go1.15.5.linux-amd64.tar.gz -P {tmpdir}",
                shell=True,
            )

            gotar = os.path.join(tmpdir, "go1.15.5.linux-amd64.tar.gz")

            subprocess.run(f"tar -xzf {gotar} -C {tmpdir}", shell=True)

            gofolder = os.path.join(tmpdir, "go")
            subprocess.run(f"sudo mv {gofolder} /usr/local/rmmgo/", shell=True)
            shutil.rmtree(tmpdir)

        # load community scripts into the db
        Script.load_community_scripts()
Exemple #11
0
def calendar():
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    tasks = {}
    for task in Task.query.all():
        # javascript dates range from 0 to 11, we must account for that by
        # substracting 1 to the month for the date to be properly displayed in
        # the calendar
        python_month = search(r'.*-(\d{2})-.*', task.start_date).group(1)
        month = '{:02}'.format((int(python_month) - 1) % 12)
        tasks[task] = sub(r"(\d+)-(\d+)-(\d+) (\d+):(\d+).*",
                          r"\1, " + month + r", \3, \4, \5", task.start_date)
    return render_template('calendar.html',
                           tasks=tasks,
                           scheduling_form=scheduling_form)
Exemple #12
0
    def handle(self, *args, **kwargs):

        if not os.path.exists("/usr/local/bin/goversioninfo"):
            self.stdout.write(self.style.ERROR("*" * 100))
            self.stdout.write("\n")
            self.stdout.write(
                self.style.ERROR(
                    "ERROR: New update script available. Delete this one and re-download."
                ))
            self.stdout.write("\n")
            sys.exit(1)

        # 10-16-2020 changed the type of the agent's 'disks' model field
        # from a dict of dicts, to a list of disks in the golang agent
        # the following will convert dicts to lists for agent's still on the python agent
        agents = Agent.objects.all()
        for agent in agents:
            if agent.disks is not None and isinstance(agent.disks, dict):
                new = []
                for k, v in agent.disks.items():
                    new.append(v)

                agent.disks = new
                agent.save(update_fields=["disks"])
                self.stdout.write(
                    self.style.SUCCESS(f"Migrated disks on {agent.hostname}"))

        # sync modules. split into chunks of 60 agents to not overload the salt master
        agents = Agent.objects.all()
        online = [i.salt_id for i in agents if i.status == "online"]

        chunks = (online[i:i + 60] for i in range(0, len(online), 60))

        self.stdout.write(self.style.SUCCESS("Syncing agent modules..."))
        for chunk in chunks:
            r = Agent.salt_batch_async(minions=chunk,
                                       func="saltutil.sync_modules")
            sleep(5)

        has_old_config = True
        rmm_conf = "/etc/nginx/sites-available/rmm.conf"
        if os.path.exists(rmm_conf):
            with open(rmm_conf) as f:
                for line in f:
                    if "location" and "builtin" in line:
                        has_old_config = False
                        break

        if has_old_config:
            new_conf = """
            location /builtin/ {
                internal;
                add_header "Access-Control-Allow-Origin" "https://rmm.yourwebsite.com";
                alias /srv/salt/scripts/;
            }
            """

            after_this = """
            location /saltscripts/ {
                internal;
                add_header "Access-Control-Allow-Origin" "https://rmm.yourwebsite.com";
                alias /srv/salt/scripts/userdefined/;
            }
            """

            self.stdout.write(self.style.ERROR("*" * 100))
            self.stdout.write("\n")
            self.stdout.write(
                self.style.ERROR(
                    "WARNING: A recent update requires you to manually edit your nginx config"
                ))
            self.stdout.write("\n")
            self.stdout.write(
                self.style.ERROR("Please add the following location block to ")
                + self.style.WARNING(rmm_conf))
            self.stdout.write(self.style.SUCCESS(new_conf))
            self.stdout.write("\n")
            self.stdout.write(
                self.style.ERROR(
                    "You can paste the above right after the following block that's already in your nginx config:"
                ))
            self.stdout.write(after_this)
            self.stdout.write("\n")
            self.stdout.write(
                self.style.ERROR(
                    "Make sure to replace rmm.yourwebsite.com with your domain"
                ))
            self.stdout.write(
                self.style.ERROR(
                    "After editing, restart nginx with the command ") +
                self.style.WARNING("sudo systemctl restart nginx"))
            self.stdout.write("\n")
            self.stdout.write(self.style.ERROR("*" * 100))
            input("Press Enter to continue...")

        # install go
        if not os.path.exists("/usr/local/rmmgo/"):
            self.stdout.write(self.style.SUCCESS("Installing golang"))
            subprocess.run("sudo mkdir -p /usr/local/rmmgo", shell=True)
            tmpdir = tempfile.mkdtemp()
            r = subprocess.run(
                f"wget https://golang.org/dl/go1.15.linux-amd64.tar.gz -P {tmpdir}",
                shell=True,
            )

            gotar = os.path.join(tmpdir, "go1.15.linux-amd64.tar.gz")

            subprocess.run(f"tar -xzf {gotar} -C {tmpdir}", shell=True)

            gofolder = os.path.join(tmpdir, "go")
            subprocess.run(f"sudo mv {gofolder} /usr/local/rmmgo/", shell=True)
            shutil.rmtree(tmpdir)

        # load community scripts into the db
        Script.load_community_scripts()
Exemple #13
0
    def handle(self, *args, **kwargs):
        # remove task pending actions. deprecated 4/20/2021
        PendingAction.objects.filter(action_type="taskaction").delete()

        # load community scripts into the db
        Script.load_community_scripts()
Exemple #14
0
def details(request, script_id):
    script = Script()
    script = script.getScriptByid(script_id)
    script.content.replace("\n", "<b>")
    return render_to_response("details.html", {"script": script}, context_instance=RequestContext(request))
Exemple #15
0
def view(view_type):
    add_node_form = AddNode(request.form)
    add_link_form = AddLink(request.form)
    view_options_form = ViewOptionsForm(request.form)
    google_earth_form = GoogleEarthForm(request.form)
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    labels = {'node': 'name', 'link': 'name'}
    if 'script' in request.form:
        data = dict(request.form)
        selection = map(int, session['selection'])
        scripts = request.form.getlist('scripts')
        workflows = request.form.getlist('workflows')
        data['scripts'] = [get_obj(Script, name=name) for name in scripts]
        data['workflows'] = [
            get_obj(Workflow, name=name) for name in workflows
        ]
        data['nodes'] = [get_obj(Node, id=id) for id in selection]
        data['user'] = current_user
        task = Task(**data)
        db.session.add(task)
        db.session.commit()
        return redirect(url_for('tasks_blueprint.task_management'))
    elif 'view_options' in request.form:
        # update labels
        labels = {
            'node': request.form['node_label'],
            'link': request.form['link_label']
        }
    elif 'google earth' in request.form:
        kml_file = Kml()
        for node in Node.query.all():
            point = kml_file.newpoint(name=node.name)
            point.coords = [(node.longitude, node.latitude)]
            point.style = styles[node.subtype]
            point.style.labelstyle.scale = request.form['label_size']

        for link in Link.query.all():
            line = kml_file.newlinestring(name=link.name)
            line.coords = [(link.source.longitude, link.source.latitude),
                           (link.destination.longitude,
                            link.destination.latitude)]
            line.style = styles[link.type]
            line.style.linestyle.width = request.form['line_width']
        filepath = join(current_app.ge_path, request.form['name'] + '.kmz')
        kml_file.save(filepath)
    # for the sake of better performances, the view defaults to markercluster
    # if there are more than 2000 nodes
    view = 'leaflet' if len(Node.query.all()) < 2000 else 'markercluster'
    if 'view' in request.form:
        view = request.form['view']
    # we clean the session's selected nodes
    session['selection'] = []
    # name to id
    name_to_id = {node.name: id for id, node in enumerate(Node.query.all())}
    return render_template(
        '{}_view.html'.format(view_type),
        filters=Filter.query.all(),
        view=view,
        scheduling_form=scheduling_form,
        view_options_form=view_options_form,
        google_earth_form=google_earth_form,
        add_node_form=add_node_form,
        add_link_form=add_link_form,
        labels=labels,
        names=pretty_names,
        subtypes=node_subtypes,
        name_to_id=name_to_id,
        node_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in Node.query.all()
        },
        link_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in Link.query.all()
        })
 def handle(self, *args, **kwargs):
     Script.load_community_scripts()
Exemple #17
0
def full_search(request):
    val_fulltext = request.POST["fulltext"]
    script = Script()
    return HttpResponse(script.getDensidadeTermos(val_fulltext))