Exemple #1
0
def watch(name=None, tag=None, api=None, elnk=None, ctsk=None):
    registrations = core.read_reg('default')
    investigations = find_by(regs=registrations, name=name, tag=tag)
    # # print str(registrations[investigations[0]])
    if len(investigations) > 0 and registrations[investigations[0]]['status']['value'] != 'unregistered':
        if registrations[investigations[0]]['status']['value'] == 'watching':
            ## print "Already watching this entry."
            return [True, None]
        else:
            registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
            stamp = core.formated_stamp()
            registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
            registrations[investigations[0]]['status'] = {'value':'watching', 'stamp':stamp}
            task_process = watcher_launch(
                name=registrations[investigations[0]]['name'],
                tag=tag,
                api=api, elnk=elnk, ctsk=ctsk)
            if task_process:
                ## print "Watching on task-[{0}]...".format(int(task_process.pid))
                registrations[investigations[0]]['watcher'] = int(task_process.pid) + 4
                core.write_reg('default', registrations)
                ## print "Watching the investigation..."
                return [True, task_process]
            else:
                ## print "Error: Could not start this watcher."
                return [False, None]
    else:
        ## print "Could not found this investigation to watch."
        return [False, None]
Exemple #2
0
def tag(name=None, api=None, elnk=None, ctsk=None):
    ## print ""
    ## print "Taging the investigation..."
    config = core.read_config('default')
    registrations = core.read_reg('default')
    investigations = find_by(regs=registrations, name=name)
    if len(investigations) > 0 and registrations[investigations[0]]['status']['value'] != 'unregistered':
        ## print "An investigation with this name/tag has already been registered."
        ## print "Its name and tag are: [{0}|{1}]".format(registrations[investigations[0]]['name'], registrations[investigations[0]]['tags'])
        stamp = core.formated_stamp()
        tag = "{0}-tag-{1}".format(name, stamp)
        registrations[investigations[0]]['tags'].append(tag)
        core.ensure_repo(registrations[investigations[0]]['name'])
        core.write_reg('default', registrations)
        api_module = core.extend_load(api)
        api_response = api_module.project_update(
            config=config,
            project=registrations[investigations[0]]['project'],
            description=None,
            goals=None,
            tags=registrations[investigations[0]]['tags'])
        # # print api_response[1]
        return [tag, api_response[1]]
    else:
       ## print "No investigation with this name/tag has been registered."
       return None
Exemple #3
0
def unregister(name=None, tag=None, api=None, elnk=None, ctsk=None):
    ## print "Unregistering the investigation..."
    registrations = core.read_reg('default')
    investigations = find_by(regs=registrations, name=name, tag=tag)
    if len(investigations) > 0 and registrations[investigations[0]]['status']['value'] != 'unregistered':
        registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
        stamp = core.formated_stamp()
        registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
        registrations[investigations[0]]['status'] = {'value':'unregistered', 'stamp':stamp}
        core.write_reg('default', registrations)
        # Delete the .source
        # Delete investigation repo and section from registrations.
        ## print "investigation unregistered."
        return True
    else:
        ## print "Could not found a investigation with\
        # this name/tag to unregister."
        return False
Exemple #4
0
def align(api=None, elnk=None, ctsk=None):
    config = core.read_config('default')
    registrations = core.read_reg('default')
    # # print core.read_reg()
    # # print registrations
    api_module = core.extend_load(api)
    api_response = api_module.project_all(config=config)
    if api_response[0] == True:
        projects_json = api_response[1]
        # # print "--> Backend has {0} projects".format(
            # projects_json['total_projects'])
        projects = projects_json['projects']
        for project in projects:
            name = project['name']
            tags = project['tags']
            investigations = find_by(regs=registrations, name=name)
            if len(investigations) == 0:
                # # print "--> Aligning with remote project [{0}]...".format(name)
                investigation = {}
                stamp = core.formated_stamp()
                investigation['name'] = name
                investigation['tags'] = tags
                investigation['status'] = {'value':'registered', 'stamp':stamp}
                investigation['consistency'] = True
                investigation['project'] = project['id']
                investigation['history'] = []
                investigation['watcher'] = None
                registrations.append(investigation)
                core.ensure_repo(investigation['name'])
                # # print registrations
                core.write_reg('default', registrations)
                # # print "--> Done."
        # # print core.read_reg()
        return True 
    else:
        ## print "Registrations alignment failed."
        # print api_response[1]
        return False
Exemple #5
0
def unwatch(name=None, tag=None, api=None, elnk=None, ctsk=None):
    registrations = core.read_reg('default')
    investigations = find_by(regs=registrations, name=name, tag=tag)
    if len(investigations) > 0 and registrations[investigations[0]]['status']['value'] != 'unregistered':
        if registrations[investigations[0]]['status']['value'] == 'watching':
            registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
            stamp = core.formated_stamp()
            registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
            registrations[investigations[0]]['status'] = {'value':'unwatched', 'stamp':stamp}
            stop_process = watcher_stop(reg=registrations[investigations[0]], api=api, elnk=elnk, ctsk=ctsk)
            if stop_process:
                registrations[investigations[0]]['watcher'] = None
                core.write_reg('default', registrations)
                ## print "investigation unwatched"
                return [True, stop_process]
            else:
                ## print "Error: Could not stop this watcher."
                return [False, None]
        elif registrations[investigations[0]]['status']['value'] == 'unwatched':
            ## print "Already unwatched this entry."
            return [True, None]
    else:
        ## print "Could not found this investigation to unwatch."
        return [False, None]
Exemple #6
0
def sync(name=None, tag=None, force=False, api=None, elnk=None, ctsk=None):
    config = core.read_config()
    registrations = core.read_reg('default')
    if name is None and tag is None:
        ## print "Syncing all inconsistent registrations..."
        for idx, reg in enumerate(registrations):
            if not reg['consistency'] or force:
                ## print "Syncing the registration [{0}]...".format(reg['name'])
                api_module = core.extend_load(api)
                api_response = api_module.project_create(
                    config=config,
                    name=reg['name'],
                    description='no description provided.',
                    goals='no goals set.',
                    tags=reg['tags'])
                if api_response[0] == True:
                    reg['consistency'] = True
                    reg['project'] = api_response[1]['id']
                    core.ensure_repo(reg['name'])
                    registrations[idx] = reg
                    core.write_reg('default', registrations)
                    ## print "--> This associated project metadata is now consistent."
                    # return api_response[1]
                # else:
                    ## print "--> Consistency alignment between registration\
                    #and project metada failed. Please check connectivity\
                    #and try to sync the investigation again later."
                    ## print api_response[1]
                    # return api_response[1]
            # else:
                ## print "Registration [{0}] is already consistent.".format(reg['name'])
        return registrations
    else:
        ## print "Syncing the registration..."
        investigations = find_by(regs=registrations, name=name, tag=tag)
        if len(investigations) > 0 and (not registrations[investigations[0]]['consistency'] or force):
            api_module = core.extend_load(api)
            api_response = api_module.project_create(
                config=config,
                name=registrations[investigations[0]]['name'],
                description='no description provided.',
                goals='no goals set.',
                tags=[registrations[investigations[0]]['tags']])
            if api_response[0] == True:
                registrations[investigations[0]]['consistency'] = True
                registrations[investigations[0]]['project'] = api_response[1]['id']
                core.ensure_repo(registrations[investigations[0]]['name'])
                core.write_reg('default', registrations)
                ## print "The associated project metadata is now consistent."
                return api_response[1]
            else:
                ## print "--> Consistency alignment between registration\
                #    and project metada failed. Please check connectivity\
                #    and try to sync the investigation again later."
                ## print api_response[1]
                return None
        else:
            if len(investigations) > 0:
                ## print "The associated project metadata is already consistent."
                return registrations[investigations[0]]
            else:
                ## print "Error: No registration with this name."
                return None
Exemple #7
0
def register(name=None, api=None, elnk=None, ctsk=None):
    ## print ""
    ## print "Registering the investigation..."
    config = core.read_config('default')
    registrations = core.read_reg('default')
    investigations = find_by(regs=registrations, name=name)
    if len(investigations) > 0 and registrations[investigations[0]]['status']['value'] != 'unregistered':
        ## print "An investigation with this name/tag has already been registered."
        ## print "Its name and tag are: [{0}|{1}]".format(registrations[investigations[0]]['name'], registrations[investigations[0]]['tags'])
        return registrations[investigations[0]]
    else:
        if len(investigations) > 0 and registrations[investigations[0]]['status']['value'] == 'unregistered':
            stamp = core.formated_stamp()
            registrations[investigations[0]]['history'].append(registrations[investigations[0]]['status'])
            registrations[investigations[0]]['status'] = {'value':'registered', 'stamp':stamp}
            registrations[investigations[0]]['consistency'] = False
            core.ensure_repo(registrations[investigations[0]]['name'])
            core.write_reg('default', registrations)
            ## print "An investigation with this name/tag was\
            # unregistered and is now registered back again."
            if not registrations[investigations[0]]['consistency']:
                api_module = core.extend_load(api)
                api_response = api_module.project_create(config=config,
                                                  name=registrations[investigations[0]]['name'],
                                                  description='no description provided.',
                                                  goals='no goals set.',
                                                  tags=registrations[investigations[0]]['tags'])
                if api_response[0] == True:
                    registrations[investigations[0]]['consistency'] = True
                    registrations[investigations[0]]['project'] = api_response[1]['id']
                    core.ensure_repo(registrations[investigations[0]]['name'])
                    core.write_reg('default', registrations)
                    ## print "The associated project metadata is now consistent."
                    return registrations[investigations[0]]
                else:
                    ## print "Consistency alignment between registration and project metadat failed. \
                    #Please check connectivity and try to sync the investigation again later."
                    # print api_response[1]
                    return registrations[investigations[0]]
            else:
                return None
        else:
            investigation = {}
            stamp = core.formated_stamp()
            investigation['tags'] = ["%s-tag-%s"%(name, stamp)]
            if name != None:
                investigation['name'] = name
            else:
                investigation['name'] = "name_%s"%stamp
            investigation['status'] = {'value':'registered', 'stamp':stamp}
            investigation['consistency'] = False
            investigation['history'] = []
            investigation['watcher'] = None
            exists = False
            if not investigation['name'].isalnum():
                ## print "Registration failed. name has to be alphanumberial."
                return None
            else:
                for reg in registrations:
                    if reg['name'] == investigation['name']:
                        exists = True
                        break
                if not exists:
                    registrations.append(investigation)
                    core.ensure_repo(investigation['name'])
                    core.write_reg('default', registrations)
                    ## print "Registration produced tag: {0}".format(investigation['tags'])
                    api_module = core.extend_load(api)
                    api_response = api_module.project_create(
                        config=config,
                        name=investigation['name'],
                        description='no description provided.',
                        goals='no goals set.',
                        tags=investigation['tags'])
                    if api_response[0] == True:
                        investigation['consistency'] = True
                        investigation['project'] = api_response[1]['id']
                        core.ensure_repo(investigation['name'])
                        core.write_reg('default', registrations)
                        ## print "The associated project metadata is now\
                        #consistent with the registration."
                        return "Consistent"
                    else:
                        ## print "Consistency alignment between registration\
                        #and project metadat failed. Please check connectivity\
                        #and try to sync the investigation later."
                        # print api_response[1]
                        return investigation
                else:
                    ## print "Registration failed. A registration already exists\
                    #with the name: {0}".format(investigation['name'])
                    return investigation