Exemplo n.º 1
0
Arquivo: Tool.py Projeto: irpr0/jok3r
    def checkInstall(self, output, referencing_tool=None):
        """
		Check if the tool is correctly installed.
		Basically, it runs the installed tool without any option

		@Args		output: 	CLIOutput instance
		@Returns	Boolean indicating status
		"""

        output.printInfo('Trying to run the tool {0} with no option...'.format(
            self.name))
        if referencing_tool:
            cmd, cmd_short = referencing_tool.command.getParsedCmdline(
                remove_args=True)
        elif self.command:
            cmd, cmd_short = self.command.getParsedCmdline(remove_args=True)
        else:
            raise Exception

        output.printBeginCmd(cmd_short)
        process = ProcessLauncher(cmd, output, None)
        process.start()
        output.printEndCmd()

        output.printPrompt(
            'Does the tool {0} seem to be running correctly ? [Y/n]'.format(
                self.name))
        return CLIUtils.promptYesNo(output, default='Y')
Exemplo n.º 2
0
Arquivo: Tool.py Projeto: u53r55/jok3r
    def checkInstall(self, output):
        """
		Check if the tool is correctly installed.
		Basically, it runs the installed tool without any option
		@Args		output: 	CLIOutput instance
		@Returns	Boolean indicating status
		"""

        # Directory where the tool should be installed
        if not FileUtils.is_dir(self.tool_dir):
            output.printFail(
                'Directory where the tool should be installed (\'{0}\') does not exist !'
                .self.tool_dir)
            return False

        # Try to run the tool
        output.printInfo('Trying to run the tool {0}...'.format(self.name))
        splitted = self.command.strip().split(' ')

        cmd = ''
        if splitted[0].lower() == 'sudo' and len(splitted) > 1:
            cmd = 'sudo '
            splitted = splitted[1:]
        cmd += splitted[0]

        if splitted[0].lower() in ('python', 'python3', 'perl',
                                   'ruby') and len(splitted) > 1:
            if splitted[1] != '-m':
                cmd += ' {0}'.format(splitted[1])
            elif len(splitted) > 2:
                cmd += ' -m {0}'.format(splitted[2])

        elif splitted[0].lower() == 'java' and len(splitted) > 2:
            if splitted[1].lower() == '-jar':
                cmd += ' -jar {0}'.format(splitted[2])

        c = Command(self.tool_dir, cmd, None, self.toolbox_dir, None, None,
                    None, None)
        cmd_check = c.getStandardCommandLine()
        cmd_check_print = cmd_check[cmd_check.index(';') + 1:].strip()

        output.printBeginCmd(cmd_check_print)
        process = ProcessLauncher(cmd_check, output, None)
        process.start()
        output.printEndCmd()

        # Prompt
        output.printPrompt(
            'Does the tool {0} seem to be running correctly ? [Y/n]'.format(
                self.name))
        return CLIUtils.promptYesNo(output, default='Y')
Exemplo n.º 3
0
    def __run_install_update(self, fast_mode, update=False):
        """
        Run install or update command.

        :param fast_mode: Set to true to disable prompts
        :param update: Mode selector, True for update | False for install (default)
        :return: Install/Update status
        :rtype: bool
        """
        if update : cmd = self.update_command.get_cmdline(self.tool_dir)
        else      : cmd = self.install_command.get_cmdline(self.tool_dir)

        mode = 'update' if update else 'install'

        logger.info('Description: {descr}'.format(descr=self.description))
        #Output.print('{mode} command : {cmd}'.format(
        #   mode=mode.capitalize(), cmd=cmd_short))

        if fast_mode \
           or Output.prompt_confirm('Confirm {mode} ?'.format(mode=mode), default=True):

            Output.begin_cmd(cmd)
            ProcessLauncher(cmd).start()
            Output.delimiter()
            logger.success('Tool {mode} has finished'.format(mode=mode))
            return True
        else:
            logger.warning('Tool {mode} aborted'.format(mode=mode))
            return False
Exemplo n.º 4
0
    def remove(self, settings):
        """
        Remove the tool:
            - Remove tool directory into toolbox
            - Change install status to false.

        :param Settings settings: Settings from config files
        :return: Removal status
        :rtype: bool
        """

        # Delete tool directory if tool was installed inside toolbox directory
        if self.install_command:
            if not FileUtils.is_dir(self.tool_dir):
                logger.warning('Directory "{dir}" does not exist'.format(
                    dir=self.tool_dir))
                #return False
            elif not FileUtils.remove_directory(self.tool_dir):
                logger.error('Unable to delete directory "{dir}". ' \
                    'Check permissions and/or re-run with sudo'.format(
                        dir=self.tool_dir))
                return False
            else:
                logger.success(
                    'Tool directory "{dir}" deleted'.format(dir=self.tool_dir))

        # Remove virtualenv files if necessary
        virtualenv_dir = '{}/{}'.format(VIRTUALENVS_DIR, self.name)
        if FileUtils.is_dir(virtualenv_dir):
            if FileUtils.remove_directory(virtualenv_dir):
                logger.success('Virtualenv directory deleted')
            else:
                logger.warning('Unable to delete Virtualenv directory')

        if self.virtualenv.startswith('ruby'):
            logger.info('Delete RVM environment ({ruby}@{name})...'.format(
                ruby=self.virtualenv, name=self.name))
            cmd = 'source /usr/local/rvm/scripts/rvm; rvm use {ruby} && ' \
                'rvm gemset delete {name} --force'.format(
                    ruby=self.virtualenv,
                    name=self.name)
            returncode, _ = ProcessLauncher(cmd).start()

            if returncode == 0:
                logger.success('RVM environment deleted with success')
            else:
                logger.warning('Unable to delete RVM environment')

        # Make sure "installed" option in config file is set to False
        if settings.change_installed_status(self.target_service,
                                            self.name,
                                            install_status=False):
            logger.success('Tool marked as uninstalled')
        else:
            logger.error('An unexpected error occured when trying to mark the tool ' \
                'as uninstalled !')
            return False

        self.installed = False
        return True
Exemplo n.º 5
0
    def __run_check_command(self):
        """
        Run the check command. The goal is to quickly check if the tool is not buggy or
        missing some dependencies
        :return: Boolean indicating if tool is correctly installed
        """
        logger.info('Running the check command for the tool {tool}...'.format(
            tool=self.name_display))
        cmd = self.check_command.get_cmdline(self.tool_dir)

        Output.begin_cmd(cmd)
        ProcessLauncher(cmd).start()
        Output.delimiter()

        return Output.prompt_confirm(
            'Does the tool {tool} seem to be running correctly ?'.format(
                tool=self.name_display),
            default=True)
Exemplo n.º 6
0
    def __run_check_command(self):
        """
        Run the check command.
        The goal is to quickly check if the tool is not buggy or missing some 
        dependencies. The user must analyze the output and gives an answer.

        :return: Response from user
        :rtype: bool
        """
        logger.info('Running the check command for the tool {tool}...'.format(
            tool=self.name))

        cmd = self.check_command.get_cmdline(self.tool_dir)

        Output.begin_cmd(cmd)
        ProcessLauncher(cmd).start()
        Output.delimiter()

        return Output.prompt_confirm('Does the tool {tool} seem to be running ' \
            'correctly ?'.format(tool=self.name), default=True) 
Exemplo n.º 7
0
    def run_check_command(self, fast_mode=False):
        """
        Run the check command.
        The goal is to quickly check if the tool is not buggy or missing some 
        dependencies. The user must analyze the output and gives an answer.

        :param bool fast_mode: Set to true to disable prompts

        :return: Response from user in interactive mode, otherwise status
            based on exit code (True if exit code is 0)
        :rtype: bool
        """
        if not self.check_command:
            logger.info('No check_command defined in settings for the tool ' \
                '{tool}'.format(tool=self.name))
            return True

        logger.info('Running the check command for the tool {tool}...'.format(
            tool=self.name))

        cmd = self.check_command.get_cmdline(self)

        Output.begin_cmd(cmd)
        returncode, _ = ProcessLauncher(cmd).start()
        Output.delimiter()

        if returncode != 0:
            logger.warning('Check command has finished with an error ' \
                'exit code: {code}'.format(code=returncode))
        else:
            logger.success('Check command has finished with success exit code')

        if fast_mode:
            return (returncode == 0)
        else:
            return Output.prompt_confirm('Does the tool {tool} seem to be running ' \
                'correctly ?'.format(tool=self.name), default=True)
Exemplo n.º 8
0
    def run(self, target, arguments, sqlsession, fast_mode=False):
        """
        Run the security check.
        It consists in running commands with context requirements matching with the
        target's context.

        :param Target target: Target
        :param ArgumentsParser arguments: Arguments from command-line
        :param Session sqlsession: SQLAlchemy session
        :param SmartModulesLoader smartmodules_loader: Loader of SmartModules
        :param bool fast_mode: Set to true to disable prompts
        :return: Status
        :rtype: bool
        """
        if not self.tool.installed:
            return False

        i = 1
        command_outputs = list()
        for command in self.commands:
            if command.context_requirements.check_target_compliance(target):
                if not command.context_requirements.is_empty:
                    logger.info('Command #{num:02} matches requirements: ' \
                        '{context}'.format(num=i, context=command.context_requirements))

                cmdline = command.get_cmdline(self.tool.tool_dir, target,
                                              arguments)

                if fast_mode:
                    logger.info('Run command #{num:02}'.format(num=i))
                    mode = 'y'
                else:
                    mode = Output.prompt_choice(
                        'Run command {num}? [Y/n/f/q] '.format(
                            num='' if len(self.commands) == 1 else \
                                '#{num:02} '.format(num=i)),
                        choices={
                            'y': 'Yes',
                            'n': 'No',
                            #'t': 'New tab',
                            #'w': 'New window',
                            'f': 'Switch to fast mode (do not prompt anymore)',
                            'q': 'Quit the program',
                        },
                        default='y')

                if mode == 'q':
                    logger.warning('Exit !')
                    sys.exit(0)
                elif mode == 'n':
                    logger.info('Skipping this command')
                    continue
                else:
                    if mode == 'f':
                        logger.info('Switch to fast mode')
                        arguments.args.fast_mode = True

                    Output.begin_cmd(cmdline)
                    process = ProcessLauncher(cmdline)
                    if mode == 'y' or mode == 'f':
                        output = process.start()
                    # elif mode == 't':
                    #     output = process.start_in_new_tab()
                    #     logger.info('Command started in new tab')
                    # else:
                    #     output = process.start_in_new_window(self.name)
                    #     logger.info('Command started in new window')
                    Output.delimiter()
                    print()

                    output = StringUtils.interpret_ansi_escape_clear_lines(
                        output)
                    outputraw = StringUtils.remove_ansi_escape(output)
                    command_outputs.append(
                        CommandOutput(cmdline=cmdline,
                                      output=output,
                                      outputraw=outputraw))

                    # Run smartmodule method on output
                    postcheck = SmartPostcheck(
                        target.service, sqlsession, self.tool.name,
                        '{0}\n{1}'.format(cmdline, outputraw))
                    postcheck.run()

            else:
                logger.info('Command #{num:02} does not match requirements: ' \
                    '{context}'.format(num=i, context=command.context_requirements))
                logger.debug('Context string: {rawstr}'.format(
                    rawstr=command.context_requirements))

            i += 1

        # Add outputs in database
        if command_outputs:
            results_requester = ResultsRequester(sqlsession)
            results_requester.add_result(target.service.id, self.name,
                                         self.category, command_outputs)

        return True
Exemplo n.º 9
0
    def run(self,
            target,
            smartmodules_loader,
            results_requester,
            fast_mode=False):
        """
        Run the check, i.e. run the commands for which Target's specific options and authentication
        level are matching the required context.
        :param target  : Target object
        :param smartmodules_loader: 
        :param results_requester: ResultsRequester object
        :param fast_mode: Boolean indicating whether prompts must be displayed or not
        :return:
        """
        if not self.tool.installed:
            return False

        i = 1
        command_outputs = list()
        for command in self.commands:
            if target.is_matching_context(command.context):
                if command.context:
                    logger.info(
                        'Command #{num:02} is matching current target\'s context: {context}'
                        .format(num=i, context=command.context))

                cmdline = command.get_cmdline(self.tool.tool_dir, target)

                #if i == 1:  logger.info('Check: {descr}'.format(descr=self.description))
                #logger.info('Command #{num:02}: {cmd}'.format(num=i, cmd=cmd_short))
                if fast_mode:
                    logger.info('Run command #{num:02}'.format(num=i))
                    mode = 'y'
                else:
                    mode = Output.prompt_choice(
                        'Run command #{num:02} ? [Y/n/t/w/q] '.format(num=i),
                        choices={
                            'y': 'Yes',
                            'n': 'No',
                            't': 'New tab',
                            'w': 'New window',
                            'q': 'Quit the program'
                        },
                        default='y')

                if mode == 'q':
                    logger.warning('Exit !')
                    sys.exit(0)
                elif mode == 'n':
                    logger.info('Skipping this command')
                    continue
                else:
                    Output.begin_cmd(cmdline)
                    process = ProcessLauncher(cmdline)
                    if mode == 'y':
                        output = process.start()
                    elif mode == 't':
                        output = process.start_in_new_tab()
                        logger.info('Command started in new tab')
                    else:
                        output = process.start_in_new_window(self.name)
                        logger.info('Command started in new window')
                    Output.delimiter()
                    print()

                    command_outputs.append(
                        CommandOutput(cmdline=cmdline, output=output))

                    if self.postrun:
                        smartmodules_loader.call_postcheck_method(
                            self.postrun, target.service, output)

            else:
                logger.info(
                    'Command #{num:02} is not matching current target\'s context: {context}'
                    .format(num=i, context=command.context))

            i += 1

        if i == 1:
            logger.warning('This check is skipped')
        else:
            # Add output(s) in db
            results_requester.add_result(target.service.id, self.name,
                                         self.category, command_outputs)
Exemplo n.º 10
0
Arquivo: Tool.py Projeto: u53r55/jok3r
    def runTool(self,
                settings,
                output,
                output_file,
                output_dir,
                target,
                specific_args,
                ignore_specific=False,
                auto_yes=False):
        """
		Run the tool 
		@Args		settings: 		instance of Settings
					output: 		instance of CLIOutput
					output_file:	file where tool execution output will be saved
					output_dir:		some tools (e.g. skipfish) require an output dir where results are saved
					target:			instance of Target
					specific_args:	specific arguments
					always_run: 	boolean indicating if tool should be always run (ignoring context specific options)
					auto_yes: 		boolean indicating if prompt should be displayed or not before running
		@Returns	Boolean indicating status
		"""
        # Tool not installed yet
        if not self.installed:
            output.printInfo(
                '{0} is not installed yet (according to config), skipped.'.
                format(self.name))
            return False

        # If context specific
        if self.specific_options:
            for opt in self.specific_options.keys():
                # Boolean option
                if self.specific_options[opt][0] == bool:
                    if self.specific_options[opt][1] == True and (
                            opt not in specific_args.keys()
                            or specific_args[opt] == False):
                        output.printInfo(
                            'Tool skipped. Specific to: {0} = True'.format(
                                opt))
                        return False
                    #elif self.specific_options[opt][1] == False and opt in specific_args.keys() and specific_args[opt] == True:
                    #	output.printInfo('Tool skipped. Specific to: {0} = False'.format(opt))
                    #	return False

                # List option
                elif self.specific_options[opt][
                        0] == list and self.specific_options[opt][1]:
                    if opt not in specific_args.keys() or \
                       specific_args[opt] != 'all' and specific_args[opt] not in self.specific_options[opt][1]:
                        output.printInfo(
                            'Tool skipped. Specific to: {0} = {1}'.format(
                                opt, ', '.join(self.specific_options[opt][1])))
                        return False

        # Run command parsing
        cmd_run = self.getRunCmd(target, output_file, output_dir,
                                 specific_args)
        cmd_run_print = self.getSimplifiedRunCmd(cmd_run)
        output.printInfo('Command:')
        output.printInfo(cmd_run_print)
        if not auto_yes:
            output.printPrompt('Run tool ? [Y/n/t/w/q]'.format(
                self.category, self.name))
            # Prompt
            to_run = CLIUtils.promptRunMode(output, default='Y')

        # Run update command if wanted
        if auto_yes or to_run == 'Yes':
            output.printBeginCmd(cmd_run_print)
            process = ProcessLauncher(cmd_run, output, None)
            process.start()
            output.printEndCmd()
        elif to_run == 'Tab':
            #TODO
            pass
        elif to_run == 'Window':
            output.printBeginCmd(cmd_run_print)
            process = ProcessLauncher(cmd_run, output, None)
            process.startInNewWindow()
            print
            output.printInfo('Started in another window.')
            output.printEndCmd()
        elif to_run == 'Quit':
            print
            output.printWarning('Exit !')
            sys.exit(0)

        print
Exemplo n.º 11
0
Arquivo: Tool.py Projeto: u53r55/jok3r
    def runUpdate(self, settings, output):
        """
		Run the update for the tool 
		@Args		settings: 	Settings instance
					output: 	CLIOutput instance
		@Returns	Boolean indicating status
		"""
        # Tool not installed yet
        if not self.installed:
            output.printInfo(
                '{0} is not installed yet (according to config), skipped.'.
                format(self.name))
            print
            return False

        # Not installed, but no update command specified
        elif not self.update:
            output.printWarning(
                'No tool update command specified in config file, skipped.')
            print
            return False

        # Create directory for the tool if necessary (should not be necessary because only update)
        if not FileUtils.is_dir(self.tool_dir):
            output.printFail(
                'Tool directory does not exist but tool marked as installed. Trying to re-install it...'
            )
            return self.runInstall(settings, output)

        # Update command parsing
        cmd_update = self.getUpdateCmd()
        cmd_update_print = cmd_update[cmd_update.index(';') + 1:].strip()
        output.printInfo('Update Command:')
        output.printInfo(cmd_update_print)
        output.printPrompt('{0} > {1} - Update ? [Y/n]'.format(
            self.category, self.name))
        # Prompt
        to_update = CLIUtils.promptYesNo(output, default='Y')

        # Run update command if wanted
        if to_update:
            output.printBeginCmd(cmd_update_print)
            process = ProcessLauncher(cmd_update, output, None)
            process.start()
            output.printEndCmd()

            output.printSuccess('Tool update has finished')

            output.printInfo('Now, checking if {0} has been updated correctly. '.format(self.name) + \
             'Hit any key to run test...')
            CLIUtils.getch()

            # Check update, update config options
            if self.checkInstall(output):
                if not settings.changeLastUpdateOption(self.service_name,
                                                       self.section_name):
                    output.printWarning(
                        'An unexpected error occured when trying to last update date.'
                    )
                else:
                    output.printSuccess(
                        'Tool {0} has been marked as successfully updated.'.
                        format(self.name))
            else:
                # If test fails, ask user if re-install ?
                output.printFail(
                    'Tool {0} has not been marked as updated.'.format(
                        self.name))
                output.printPrompt(
                    'Do you want to try to re-install {0} ? [Y/n]'.format(
                        self.name))

                # Prompt
                to_reinstall = CLIUtils.promptYesNo(output, default='Y')

                # Re-Install
                if to_reinstall:
                    self.reinstallTool(settings, output)
        else:
            output.printFail('Tool has not been updated')

        print
Exemplo n.º 12
0
Arquivo: Tool.py Projeto: u53r55/jok3r
    def runInstall(self, settings, output):
        """
		Install the tool
		@Args		settings: 	Settings instance
					output: 	CLIOutput instance
		@Returns	Boolean indicating status
		"""

        # Tool already marked as installed
        if self.installed:
            #if not FileUtils.is_dir(self.tool_dir):
            #	output.printInfo('{0} marked as installed in config, but directory does not exist. Will try to install it'.format(self.name))
            # elif FileUtils.is_directory_empty(self.tool_dir):
            # 	output.printInfo('{0} marked as installed in config, but directory empty. Will try to install it'.format(self.name))
            #else:
            output.printInfo(
                '{0} is already installed (according to config), skipped.'.
                format(self.name))
            print
            return False

        # Not installed, but no install command specified
        elif not self.install:
            output.printWarning(
                'No tool install command specified in config file, skipped.')
            output.printPrompt(
                'Do you want to mark this tool as installed ? [y/N]')
            to_mark = CLIUtils.promptYesNo(output, default='N')
            if to_mark:
                if not settings.changeInstalledOption(
                        self.service_name, self.section_name, 'True'):
                    output.printError(
                        'An unexpected error occured when trying to mark the tool as installed !'
                    )
                else:
                    output.printSuccess(
                        'Tool {0} has been marked as installed. '.format(
                            self.name))
            else:
                output.printInfo('Tool is still not marked as installed')
            print
            return False

        # Create directory for the tool if necessary
        if not self.createToolDirectory(output):
            output.printFail('Tool install skipped.')
            print
            return False

        # Install command parsing
        cmd_install = self.getInstallCmd()
        cmd_install_print = cmd_install[cmd_install.index(';') + 1:].strip()
        output.printInfo('Install Command:')
        output.printInfo(cmd_install_print)
        output.printPrompt('{0} > {1} - Install ? [Y/n]'.format(
            self.category, self.name))

        # Prompt
        to_install = CLIUtils.promptYesNo(output, default='Y')

        # Run install command if wanted
        if to_install:
            output.printBeginCmd(cmd_install)
            process = ProcessLauncher(cmd_install, output, None)
            process.start()
            output.printEndCmd()

            output.printSuccess('Tool installation has finished')
            output.printInfo('Now, checking if {0} has been installed correctly. '.format(self.name) + \
             'Hit any key to run test...')
            CLIUtils.getch()

            # Check install, update config options
            if self.checkInstall(output):
                if not settings.changeInstalledOption(
                        self.service_name, self.section_name, 'True'):
                    output.printError(
                        'An unexpected error occured when trying to mark the tool as installed !'
                    )
                else:
                    output.printSuccess(
                        'Tool {0} has been marked as installed. '.format(
                            self.name))
                    if not settings.changeLastUpdateOption(
                            self.service_name, self.section_name):
                        output.printWarning(
                            'An unexpected error occured when trying to change last update date'
                        )
            else:
                output.printFail(
                    'Tool {0} is still not marked as installed.'.format(
                        self.name))
                self.removeTool(settings, output)
        else:
            output.printFail('Tool has not been installed')

        print
Exemplo n.º 13
0
	def runTool(self, 
				settings, 
				output, 
				output_dir, 
				target, 
				specific_args, 
				ignore_specific=False,
				auto_yes=False):
		"""
		Run the tool

		@Args		settings: 		instance of Settings
					output: 		instance of CLIOutput
					output_dir:		directory where tool execution output will be saved
					target:			instance of Target
					specific_args:	specific arguments
					always_run: 	boolean indicating if tool should be always run (ignoring context specific options)
					auto_yes: 		boolean indicating if prompt should be displayed or not before running
		@Returns	Boolean indicating status
		"""
		# Tool not installed yet
		if not self.installed:
			output.printInfo('{0} is not installed yet (according to config), skipped.'.format(self.name))
			return False

		# If context specific
		if self.specific_options and not ignore_specific:
			for opt in self.specific_options.keys():
				# Boolean option
				if self.specific_options[opt][0] == bool:
					if self.specific_options[opt][1] == True and (opt not in specific_args.keys() or specific_args[opt] == False):
						output.printInfo('Tool skipped. Specific to: {0} = True'.format(opt))
						return False

				# List option
				elif self.specific_options[opt][0] == list and self.specific_options[opt][1]: 
					if opt not in specific_args.keys() or \
					   specific_args[opt] != 'all' and specific_args[opt] not in self.specific_options[opt][1]:
						output.printInfo('Tool skipped. Specific to: {0} = {1}'.format(opt, ', '.join(self.specific_options[opt][1])))
						return False

		# Print basic info and prompt confirmation
		cmd, cmd_short = self.command.getParsedCmdline(output_dir=output_dir, 
													   output_filename=self.name+'.txt',
													   target=target,
													   specific_args=specific_args)
		output.printInfo('Description : {0}'.format(self.description))
		output.printInfo('Run command : {0}'.format(cmd_short))
		if not auto_yes: 
			output.printPrompt('Run tool ? [Y/n/t/w/q]'.format(self.category, self.name))
			to_run = CLIUtils.promptRunMode(output, default='Y')
		else:
			to_run = 'Yes'

		# Run command if wanted
		if to_run == 'Quit':
			print
			output.printWarning('Exit !')
			sys.exit(0)
		elif to_run != 'No':
			output.printBeginCmd(cmd_short)
			process = ProcessLauncher(cmd, output, None)			
			# Normal running
			if auto_yes or to_run == 'Yes':
				process.start()

			# Start in new tab
			elif to_run == 'Tab':
				# TODO
				output.printInfo('Not yet implemented')

			# Start in new window
			elif to_run == 'Window':
				process.startInNewWindow()
				print
				output.printInfo('Started in another window')
			output.printEndCmd()
		print	
Exemplo n.º 14
0
	def runUpdate(self, settings, output, fast_mode=False, referencing_tool=None):
		"""
		Run the update for the tool 

		@Args		settings: 	Settings instance
					output: 	CLIOutput instance
		@Returns	Boolean indicating status
		"""

		# Check for cases where no update will be run
		if not self.installed:
			output.printInfo('{0} is not installed yet (according to config), skipped.'.format(self.name))
			print
			return False

		elif self.tooltype == ToolType.USE_MULTI:
			output.printInfo('This is a reference to the tool "{0}", which is not specific to the service {1}'.format(self.tool_ref_name, self.service_name))
			ref_tool = settings.toolbox.searchInToolboxForService(self.tool_ref_name, Constants.MULTI_SERVICES_CONF_FILE)
			if ref_tool:
				return ref_tool.runUpdate(settings, output, fast_mode=fast_mode, referencing_tool=self)
			else:
				output.printFail('The tool "{0}" has not been found inside the conf file "{1}{2}"'.format(self.tool_ref_name, \
					Constants.MULTI_SERVICES_CONF_FILE, Constants.CONF_EXT))
				return False

		elif not self.update:
			output.printWarning('No tool update command specified in config file, skipped.')
			print
			return False

		# Create directory for the tool if necessary (should not be necessary because only update)
		if not FileUtils.is_dir(self.tool_dir):
			output.printFail('Tool directory does not exist but tool marked as installed. Trying to re-install it...')
			return self.runInstall(settings, output, fast_mode)

		# Print basic info and prompt confirmation
		cmd, cmd_short = self.update.getParsedCmdline()
		output.printInfo('Description     : {0}'.format(self.description))
		output.printInfo('Install command : {0}'.format(cmd_short))
		if not fast_mode: output.printPrompt('Update ? [Y/n]')

		# Run update command if wanted
		if fast_mode or CLIUtils.promptYesNo(output, default='Y'):
			output.printBeginCmd(cmd_short)
			process = ProcessLauncher(cmd, output, None)
			process.start()
			output.printEndCmd()
			output.printSuccess('Tool update has finished')

			# Check install ?
			update_ok = True
			if not (self.tooltype == ToolType.MULTI_SERVICES and not referencing_tool) and not fast_mode:
				output.printInfo('Now, checking if {0} has been updateed correctly. Hit any key to run test...'.format(self.name))
				CLIUtils.getch()
				try:
					update_ok = self.checkInstall(output, referencing_tool=referencing_tool)
				except Exception as e:
					update_ok = False
					output.printError('An unexpected error occured when checking install: {0}'.format(e))

			# Change install status in configuration file
			if update_ok:
				try:
					if settings.changeInstalledOption(self.service_name, self.name, True):
						output.printSuccess('Tool {0} has been marked as successfully updated'.format(self.name))
					else:
						output.printError('Error when saving "{0}{1}" configuration file'.format(Constants.INSTALL_STATUS_CONF_FILE, Constants.CONF_EXT))
				except Exception as e:
					output.printError('An unexpected error occured when trying to change the last update date: {0}'.format(e))
					#self.removeTool(settings, output)
			else:
				output.printFail('Tool {0} has not been marked as updated'.format(self.name))
				#self.removeTool(settings, output)
				output.printPrompt('Do you want to try to re-install {0} ? [Y/n]'.format(self.name))
				if CLIUtils.promptYesNo(output, default='Y'):
					self.reinstallTool(settings, output, referencing_tool=referencing_tool)
		else:
			output.printFail('Tool has not been updated')
		print
Exemplo n.º 15
0
	def runInstall(self, settings, output, fast_mode=False, referencing_tool=None):
		"""
		Install the tool

		@Args		settings: 	Settings instance
					output: 	CLIOutput instance
					fast_mode:	Boolean. If True, do not prompt confirm before install and do not check install after
		@Returns	Boolean indicating status
		"""

		# Check for cases where no install will be run
		if self.installed:
			if self.tooltype == ToolType.USE_MULTI:
				output.printInfo('This is a reference to the tool "{0}" which is already installed, skipped.'.format(self.tool_ref_name))
			else:
				output.printInfo('{0} is already installed (according to config), skipped.'.format(self.name))
			print
			return False

		elif self.tooltype == ToolType.USE_MULTI:
			output.printInfo('This is a reference to the tool "{0}", which is not specific to the service {1}'.format(self.tool_ref_name, self.service_name))
			ref_tool = settings.toolbox.searchInToolboxForService(self.tool_ref_name, Constants.MULTI_SERVICES_CONF_FILE)
			if ref_tool:
				return ref_tool.runInstall(settings, output, fast_mode=fast_mode, referencing_tool=self)
			else:
				output.printFail('The tool "{0}" has not been found inside the conf file "{1}{2}"'.format(self.tool_ref_name, \
					Constants.MULTI_SERVICES_CONF_FILE, Constants.CONF_EXT))
				return False

		elif not self.install:
			output.printWarning('No tool install command specified in config file, skipped')
			if not fast_mode: output.printPrompt('Do you want to mark this tool as installed ? [Y/n]')
			if fast_mode or CLIUtils.promptYesNo(output, default='Y'):
				try:
					if settings.changeInstalledOption(self.service_name, self.name, True):
						output.printSuccess('Tool {0} has been marked as installed. '.format(self.name))
					else:
						output.printError('Error when saving "{0}{1}" configuration file'.format(Constants.INSTALL_STATUS_CONF_FILE, Constants.CONF_EXT))
				except Exception as e:
					output.printError('An unexpected error occured when trying to mark the tool as installed')
					self.removeTool(settings, output)
			else:
				output.printInfo('Tool is still not marked as installed')
			print
			return False

		# Create directory for the tool if necessary
		if not self.createToolDirectory(output):
			output.printFail('Tool install skipped.')
			print
			return False

		# Print basic info and prompt confirmation
		cmd, cmd_short = self.install.getParsedCmdline()
		output.printInfo('Description     : {0}'.format(self.description))
		output.printInfo('Install command : {0}'.format(cmd_short))
		if not fast_mode: output.printPrompt('Install ? [Y/n]')

		# Run install command if wanted
		if fast_mode or CLIUtils.promptYesNo(output, default='Y'):
			output.printBeginCmd(cmd_short)
			process = ProcessLauncher(cmd, output, None)
			process.start()
			output.printEndCmd()
			output.printSuccess('Tool installation has finished')

			# Check install ?
			install_ok = True
			if not (self.tooltype == ToolType.MULTI_SERVICES and not referencing_tool) and not fast_mode:
				output.printInfo('Now, checking if {0} has been installed correctly. Hit any key to run test...'.format(self.name))
				CLIUtils.getch()
				try:
					install_ok = self.checkInstall(output, referencing_tool=referencing_tool)
				except Exception as e:
					install_ok = False
					output.printError('An unexpected error occured when checking install: {0}'.format(e))

			# Change install status in configuration file
			if install_ok:
				try:
					if settings.changeInstalledOption(self.service_name, self.name, True):
						output.printSuccess('Tool {0} has been marked as installed. '.format(self.name))
					else:
						output.printError('Error when saving "{0}{1}" configuration file'.format(Constants.INSTALL_STATUS_CONF_FILE, Constants.CONF_EXT))
				except Exception as e:
					output.printError('An unexpected error occured when trying to mark the tool as installed: {0}'.format(e))
					self.removeTool(settings, output)
			else:
				output.printFail('Tool {0} has not been marked as installed'.format(self.name))
				self.removeTool(settings, output)
		else:
			output.printFail('Tool has not been installed')
		print