Example #1
0
 def get(self, task_id):
     task = celery_runner.do_long_running_task.AsyncResult(task_id)
     if task.state == 'PENDING':
         result = "Task not found"
         resp = app.make_response((result, 404))
         return resp
     if task.state == "PROGRESS":
         result = task.info['output']
     else:
         result = task.info['output']
     #result_out = task.info.replace('\n', "<br>")
     #result = result.replace('\n', '<br>')
     #return result, 200, {'Content-Type': 'text/html; charset=utf-8'}
     resp = app.make_response((result, 200))
     resp.headers['content-type'] = 'text/plain'
     return resp
    def get(self, task_id):
        task = celery_runner.do_long_running_task.AsyncResult(task_id)
        if task.state == 'PENDING':
            result = "Task not found"
            resp = app.make_response((result, 404))
            return resp
        elif task.state == 'PROGRESS':
            result_obj = {
                'Status': "PROGRESS",
                'description': "Task is currently running",
                'returncode': None
            }
        else:
            try:
                return_code = task.info['returncode']
                description = task.info['description']
                if return_code is 0:
                    result_obj = {
                        'Status': "SUCCESS",
                        'description': description
                    }
                else:
                    result_obj = {
                        'Status': "FLANSIBLE_TASK_FAILURE",
                        'description': description,
                        'returncode': return_code
                    }
            except Exception as e:
                result_obj = {'Status': "CELERY_FAILURE: %s" % str(e)}

        return result_obj
    def get(self, task_id):
        task = celery_runner.do_long_running_task.AsyncResult(task_id)
        if task.state == 'PENDING':
            result = "Task not found"
            resp = app.make_response((result, 404))
            return resp
        elif task.state == 'PROGRESS':
            result_obj = {'Status': "PROGRESS",
                              'description': "Task is currently running",
                              'returncode': None}
        else:
            try:
                return_code = task.info['returncode']
                description = task.info['description']
                if return_code is 0:
                    result_obj = {'Status': "SUCCESS", 
                                  'description': description}
                else:
                    result_obj = {'Status': "FLANSIBLE_TASK_FAILURE",
                                  'description': description,
                                  'returncode': return_code}
            except Exception as e:
                result_obj = {'Status': "CELERY_FAILURE: %s" % str(e)}

        return result_obj
 def get(self, task_id):
     task = celery_runner.do_long_running_task.AsyncResult(task_id)
     if task.state == 'PENDING':
         result = "Task not found"
         resp = app.make_response((result, 404))
         return resp
     if task.state == "PROGRESS":
         result = task.info['output']
     else:
         result = task.info['output']
     #result_out = task.info.replace('\n', "<br>")
     #result = result.replace('\n', '<br>')
     #return result, 200, {'Content-Type': 'text/html; charset=utf-8'}
     resp = app.make_response((result, 200))
     resp.headers['content-type'] = 'text/plain'
     return resp
Example #5
0
    def get(self, task_id):
        title = "Playbook Results"
        task = celery_runner.do_long_running_task.AsyncResult(task_id)
        if task.state == 'PENDING':
            result = "Task not found"
            resp = app.make_response((result, 404))
            return resp
        if task.state == "PROGRESS":
            result = task.info['output']
        else:
            result = task.info['output']
        result = result.replace('\n', '<br>\n')

        refresh = 5

        if "RECAP" in result or "ERROR" in result:
            # disable refresh in template
            refresh = 1000

        response = make_response(render_template('status.j2', title=title, status=result, refresh=refresh))
        response.headers['Content-Type'] = 'text/html'
        return response
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('playbook_dir',
                            type=str, help='folder where playbook file resides', required=True)
        parser.add_argument('playbook', type=str, help='name of the playbook', required=True)
        parser.add_argument('inventory', type=str, help='path to inventory', required=False,)
        parser.add_argument('extra_vars', type=dict, help='extra vars', required=False)
        parser.add_argument('forks', type=int, help='forks', required=False)
        parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False)
        parser.add_argument('vault_file', type=str, help='vault file location', required=False)
        parser.add_argument('become', type=bool, help='run with become', required=False)
        parser.add_argument('update_git_repo', type=bool,
                            help='Set to true to update git repo prior to executing',
                            required=False)
        args = parser.parse_args()

        playbook_dir = args['playbook_dir']
        playbook = args['playbook']
        become = args['become']
        inventory = args['inventory']
        extra_vars = args['extra_vars']
        vault_file = args['vault_file']
        do_update_git_repo = args['update_git_repo']

        if do_update_git_repo is True:
            result = FlansibleGit.update_git_repo(playbook_dir)
            task = celery_runner.do_long_running_task.AsyncResult(result.id)
            while task.state == "PENDING" or task.state == "PROGRESS":
                #waiting for finish
                task = celery_runner.do_long_running_task.AsyncResult(result.id)
            if task.result['returncode'] != 0:
                #git update failed
                resp = app.make_response((str.format("Failed to update git repo: {0}",
                                                     playbook_dir), 404))
                return resp

        curr_user = auth.username()

        playbook_full_path = playbook_dir + "/" + playbook
        playbook_full_path = playbook_full_path.replace("//", "/")

        if not os.path.exists(playbook_dir):
            resp = app.make_response((str.format("Directory not found: {0}", playbook_dir), 404))
            return resp
        if not os.path.isdir(playbook_dir):
            resp = app.make_response((str.format("Not a directory: {0}", playbook_dir), 404))
            return resp
        if not os.path.exists(playbook_full_path):
            resp = app.make_response((str.format("Playbook not found in folder. Path does not exist: {0}", playbook_full_path), 404))
            return resp

        if not inventory:
            inventory = ansible_default_inventory
            has_inv_access = get_inventory_access(curr_user, inventory)
            if not has_inv_access:
                resp = app.make_response((str.format("User does not have access to inventory {0}", inventory), 403))
                return resp
        else:
            if not os.path.exists(inventory):
                resp = app.make_response((str.format("Inventory path not found: {0}", inventory), 404))
                return resp

        inventory = str.format(" -i {0}", inventory)

        if become:
            become_string = ' --become'
        else:
            become_string = ''

        extra_vars_string = ''
        if extra_vars:
            #extra_vars_string = str.format("  --extra-vars \'{0}\'", (json.dumps(extra_vars)))
            extra_vars_string = " --extra-vars '%s'" % (json.dumps(extra_vars).replace("'", "'\\''"))

        if vault_file:
            vault_file_string = " --vault-password-file '%s'" % (vault_file)

        command = str.format("cd {0};ansible-playbook {1}{2}{3}{4}{5}", playbook_dir, playbook, become_string, inventory, extra_vars_string, vault_file_string)
        task_result = celery_runner.do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout)
        result = {'task_id': task_result.id}
        return result
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('playbook_dir', type=str, help='folder where playbook file resides', required=True)
        parser.add_argument('playbook', type=str, help='name of the playbook', required=True)
        parser.add_argument('inventory', type=str, help='path to inventory', required=False,)
        parser.add_argument('extra_vars', type=dict, help='extra vars', required=False)
        parser.add_argument('forks', type=int, help='forks', required=False)
        parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False)
        parser.add_argument('become', type=bool, help='run with become', required=False)
        parser.add_argument('update_git_repo', type=bool, help='Set to true to update git repo prior to executing', required=False)
        parser.add_argument('limit', type=str, help='limit playbook target', required=False)
        args = parser.parse_args()

        playbook_dir = args['playbook_dir']
        playbook = args['playbook']
        become = args['become']
        inventory = args['inventory']
        extra_vars = args['extra_vars']
        do_update_git_repo = args['update_git_repo']
        limit = args['limit']

        if do_update_git_repo is True:
            result = FlansibleGit.update_git_repo(playbook_dir)
            task = celery_runner.do_long_running_task.AsyncResult(result.id)
            while task.state == "PENDING" or task.state == "PROGRESS":
                #waiting for finish
                task = celery_runner.do_long_running_task.AsyncResult(result.id)
            if task.result['returncode'] != 0:
                #git update failed
                resp = app.make_response((str.format("Failed to update git repo: {0}", playbook_dir), 404))
                return resp

        curr_user = auth.username()
        
        playbook_full_path = playbook_dir + "/" + playbook
        playbook_full_path = playbook_full_path.replace("//","/")

        if not os.path.exists(playbook_dir):
            resp = app.make_response((str.format("Directory not found: {0}", playbook_dir), 404))
            return resp
        if not os.path.isdir(playbook_dir):
            resp = app.make_response((str.format("Not a directory: {0}", playbook_dir), 404))
            return resp
        if not os.path.exists(playbook_full_path):
            resp = app.make_response((str.format("Playbook not found in folder. Path does not exist: {0}", playbook_full_path), 404))
            return resp

        if not inventory:
            inventory = ansible_default_inventory
            has_inv_access =  get_inventory_access(curr_user,  inventory)
            if not has_inv_access:
                resp = app.make_response((str.format("User does not have access to inventory {0}", inventory), 403))
                return resp

        inventory = str.format(" -i {0}", inventory)

        if become:
            become_string = ' --become'
        else:
            become_string = ''

        extra_vars_string = ''
        if extra_vars:
            counter = 1
            extra_vars_string += ' -e "'
            for key in extra_vars.keys():
                if counter < len(extra_vars):
                    spacer = " "
                else:
                    spacer = ""
                opt_string = str.format("{0}={1}{2}",key,extra_vars[key], spacer)
                extra_vars_string += opt_string
                counter += 1
            extra_vars_string += '"'

        if limit:
            limit_str = ' --limit ' + limit
        else:
            limit_str = ''

        command = str.format("cd {0}; ansible-playbook {1}{2}{3}{4}{5}", playbook_dir, playbook, become_string, inventory, extra_vars_string, limit_str)
        task_result = celery_runner.do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout)
        result = {'task_id': task_result.id}
        return result
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('host_pattern', type=str, help='need to specify host_pattern', required=True)
        parser.add_argument('module', type=str, help='module name', required=True)
        parser.add_argument('module_args', type=dict, help='module_args', required=False)
        parser.add_argument('extra_vars', type=dict, help='extra_vars', required=False)
        parser.add_argument('inventory', type=str, help='path to inventory', required=False,)
        parser.add_argument('forks', type=int, help='forks', required=False)
        parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False)
        parser.add_argument('become', type=bool, help='run with become', required=False)
        parser.add_argument('become_method', type=str, help='become method', required=False)
        parser.add_argument('become_user', type=str, help='become user', required=False)
        args = parser.parse_args()
        host_pattern = args['host_pattern']
        req_module = args['module']
        module_args = args['module_args']
        extra_vars = args['extra_vars']
        inventory = args['inventory']
        forks = args['forks']
        verbose_level = args['verbose_level']
        become = args['become']
        become_method = args['become_method']
        become_user = args['become_user']
        module_args_string = ''
        extra_vars_string = ''
        curr_user = auth.username()
        
        if module_args:
            counter = 1
            module_args_string += '-a"'
            for key in module_args.keys():
                if counter < len(module_args):
                    spacer = " "
                else:
                    spacer = ""
                opt_string = str.format("{0}={1}{2}", key, module_args[key], spacer)
                module_args_string += opt_string
                counter += 1
            module_args_string += '"'

        if extra_vars:
            counter = 1
            extra_vars_string += ' -e"'
            for key in extra_vars.keys():
                if counter < len(extra_vars):
                    spacer = " "
                else:
                    spacer = ""
                opt_string = str.format("{0}={1}{2}", key, extra_vars[key], spacer)
                extra_vars_string += opt_string
                counter += 1
            extra_vars_string += '"'
        
        if not inventory:
            inventory = ansible_default_inventory
            has_inv_access = get_inventory_access(curr_user, inventory)
            if not has_inv_access:
                resp = app.make_response((str.format("User does not have access to inventory {0}", inventory), 403))
                return resp

        inventory = str.format(" -i {0}", inventory)
        if forks:
            fork_string = str.format('-f {0}', str(forks))
        else:
            fork_string = ''

        if verbose_level and verbose_level != 0:
            verb_counter = 1
            verb_string = " -"
            while verb_counter <= verbose_level:
                verb_string += "v"
                verb_counter += 1
        else:
            verb_string = ''

        if become:
            become_string = ' --become'
        else:
            become_string = ''

        if become_method:
            become_method_string = str.format(' --become-method={0}', become_method)
        else:
            become_method_string = ''

        if become_user:
            become_user_string = str.format(' --become-user={0}', become_user)
        else:
            become_user_string = ''

        command = str.format("ansible {9} -m {0} {1} {2} {3}{4}{5}{6}{7}{8}",
                             req_module,
                             module_args_string,
                             fork_string,
                             verb_string,
                             become_string,
                             become_method_string,
                             become_user_string,
                             inventory,
                             extra_vars_string,
                             host_pattern
                             )
        task_result = celery_runner.do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout)
        result = {'task_id': task_result.id}
        return result
Example #9
0
def runPlaybook(web, username, playbook_dir, playbook, inventory, extra_vars,
                verbose_level, become):
    curr_user = username

    playbook_full_path = playbook_dir + "/" + playbook
    playbook_full_path = playbook_full_path.replace("//", "/")

    if not os.path.exists(playbook_dir):
        if web:
            resp = app.make_response((str.format("Directory not found: {0}",
                                                 playbook_dir), 404))
            return resp
        else:
            return str.format("Directory not found: {0}", playbook_dir)

    if not os.path.isdir(playbook_dir):
        if web:
            resp = app.make_response((str.format("Not a directory: {0}",
                                                 playbook_dir), 404))
            return resp
        else:
            return str.format("Not a directory: {0}", playbook_dir)

    if not os.path.exists(playbook_full_path):
        if web:
            resp = app.make_response((str.format(
                "Playbook not found in folder. Path does not exist: {0}",
                playbook_full_path), 404))
            return resp
        else:
            str.format(
                "Playbook not found in folder. Path does not exist: {0}",
                playbook_full_path)

    if not inventory:
        inventory = ansible_default_inventory
        has_inv_access = get_inventory_access(curr_user, inventory)
        if not has_inv_access:
            if web:
                resp = app.make_response(
                    (str.format("User does not have access to inventory {0}",
                                inventory), 403))
                return resp
            else:
                return str.format("User does not have access to inventory {0}",
                                  inventory)
    else:
        if not os.path.exists(inventory):
            if web:
                resp = app.make_response(
                    (str.format("Inventory path not found: {0}",
                                inventory), 404))
                return resp
            else:
                return str.format("Inventory path not found: {0}", inventory)

    inventory = str.format(" -i {0}", inventory)

    if become:
        become_string = ' --become'
    else:
        become_string = ''

    extra_vars_string = ''
    if extra_vars:
        #extra_vars_string = str.format("  --extra-vars \'{0}\'", (json.dumps(extra_vars)))
        extra_vars_string = " --extra-vars '%s'" % (
            json.dumps(extra_vars).replace("'", "'\\''"))

    command = str.format("cd {0};ansible-playbook {1}{2}{3}{4}", playbook_dir,
                         playbook, become_string, inventory, extra_vars_string)
    task_result = celery_runner.do_long_running_task.apply_async(
        [command], soft=task_timeout, hard=task_timeout)
    result = {'task_id': task_result.id}
    return result
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('host_pattern', type=str, help='need to specify host_pattern', required=True)
        parser.add_argument('module', type=str, help='module name', required=True)
        parser.add_argument('module_args', type=dict, help='module_args', required=False)
        parser.add_argument('extra_vars', type=dict, help='extra_vars', required=False)
        parser.add_argument('inventory', type=str, help='path to inventory', required=False,)
        parser.add_argument('forks', type=int, help='forks', required=False)
        parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False)
        parser.add_argument('become', type=bool, help='run with become', required=False)
        parser.add_argument('become_method', type=str, help='become method', required=False)
        parser.add_argument('become_user', type=str, help='become user', required=False)
        args = parser.parse_args()
        host_pattern = args['host_pattern']
        req_module = args['module']
        module_args = args['module_args']
        extra_vars = args['extra_vars']
        inventory = args['inventory']
        forks = args['forks']
        verbose_level = args['verbose_level']
        become = args['become']
        become_method = args['become_method']
        become_user = args['become_user']
        module_args_string = ''
        extra_vars_string = ''
        curr_user = auth.username()
        
        if module_args:
            counter = 1
            module_args_string += '-a"'
            for key in module_args.keys():
                if counter < len(module_args):
                    spacer = " "
                else:
                    spacer = ""
                opt_string = str.format("{0}={1}{2}",key,module_args[key], spacer)
                module_args_string += opt_string
                counter += 1
            module_args_string += '"'

        if extra_vars:
            counter = 1
            extra_vars_string += ' -e"'
            for key in extra_vars.keys():
                if counter < len(extra_vars):
                    spacer = " "
                else:
                    spacer = ""
                opt_string = str.format("{0}={1}{2}",key,extra_vars[key], spacer)
                extra_vars_string += opt_string
                counter += 1
            extra_vars_string += '"'
        
        if not inventory:
            inventory = ansible_default_inventory
            has_inv_access =  get_inventory_access(curr_user,  inventory)
            if not has_inv_access:
                resp = app.make_response((str.format("User does not have access to inventory {0}", inventory), 403))
                return resp

        inventory = str.format(" -i {0}", inventory)
        if forks:
            fork_string = str.format('-f {0}', str(forks))
        else:
            fork_string = ''

        if verbose_level and verbose_level != 0:
            verb_counter = 1
            verb_string = " -"
            while verb_counter <= verbose_level:
                verb_string += "v"
                verb_counter += 1
        else:
            verb_string = ''

        if become:
            become_string = ' --become'
        else:
            become_string = ''

        if become_method:
            become_method_string = str.format(' --become-method={0}', become_method)
        else:
            become_method_string = ''

        if become_user:
            become_user_string = str.format(' --become-user={0}', become_user)
        else:
            become_user_string = ''


        command = str.format("ansible {9} -m {0} {1} {2} {3}{4}{5}{6}{7}{8}", req_module, module_args_string, fork_string, verb_string, 
                             become_string, become_method_string, become_user_string, inventory, extra_vars_string ,host_pattern)
        task_result = celery_runner.do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout)
        result = {'task_id': task_result.id}
        return result