Example #1
0
 def post(self):
     data = Tool.parsejson(self.request.body)
     path = data['p']
     filename = data['f']
     content = data['c']
     sign = data['s']
     if not filename or not content or not sign or path \
             not in ['script', 'playbook', 'authkeys']:
         self.write(
             Tool.jsonal({
                 'error': "Lack of necessary parameters",
                 'rc': Tool.ERRCODE_SYS
             }))
     hotkey = path + filename + Config.Get('sign_key')
     check_str = Tool.getmd5(hotkey)
     if sign != check_str:
         self.write(
             Tool.jsonal({
                 'error': "Sign is error",
                 'rc': Tool.ERRCODE_BIZ
             }))
     else:
         file_path = Config.Get('dir_' + path) + filename
         if path == 'authkeys':  # allow mkdir in this mode
             dir_name = os.path.dirname(file_path)
             os.path.isdir(dir_name) == False and os.mkdir(dir_name)
         file_object = open(file_path, 'w')
         file_object.write(content)
         file_object.close()
         Tool.reporting("write to file: " + file_path)
         self.write(Tool.jsonal({'ret': True}))
Example #2
0
 def post(self):
     data = Tool.parsejson(self.request.body)
     path = data['p']
     filename = data['f']
     content = data['c'].encode('utf-8').decode()
     sign = data['s']
     if not filename or not content or not sign or path \
             not in ['script', 'playbook', 'authkeys']:
         self.write(
             Tool.jsonal({
                 'error': "Lack of necessary parameters",
                 'rc': ErrorCode.ERRCODE_SYS
             }))
     hotkey = path + filename + Config.Get('sign_key')
     check_str = Tool.getmd5(hotkey)
     if sign != check_str:
         self.write(
             Tool.jsonal({
                 'error': "Sign is error",
                 'rc': ErrorCode.ERRCODE_BIZ
             }))
     else:
         file_path = Config.Get('dir_' + path) + filename
         if path == 'authkeys':  # allow mkdir in this mode
             dir_name = os.path.dirname(file_path)
             os.path.isdir(dir_name) == False and os.mkdir(dir_name)
         result = yield executor.submit(self.write_file, file_path, content)
         self.write(Tool.jsonal({'ret': result}))
Example #3
0
    def post(self):
        data = Tool.parsejson(self.request.body)
        name = data['n']
        hosts = data['h']
        sign = data['s']
        yml_file = data['f']
        forks = data.get('c', 50)
        if not hosts or not yml_file or not sign:
            self.write(
                Tool.jsonal({
                    'error': "Lack of necessary parameters",
                    'rc': Tool.ERRCODE_SYS
                }))
        else:
            hotkey = name + hosts + yml_file + Config.Get('sign_key')
            check_str = Tool.getmd5(hotkey)
            if sign != check_str:
                self.write(
                    Tool.jsonal({
                        'error': "Sign is error",
                        'rc': Tool.ERRCODE_BIZ
                    }))
            else:
                myvars = {'hosts': hosts}
                # injection vars in playbook (rule: vars start with "v_" in
                # post data)
                for (k, v) in data.items():
                    if k[0:2] == "v_":
                        myvars[k[2:]] = v
                yml_file = Config.Get('dir_playbook') + yml_file
                if os.path.isfile(yml_file):
                    Tool.reporting(
                        "playbook: {0}, host: {1}, forks: {2}".format(
                            yml_file, hosts, forks))
                    try:
                        response = yield executor.submit(Api.runPlaybook,
                                                         palyname=name,
                                                         yml_file=yml_file,
                                                         myvars=myvars,
                                                         forks=forks)
                    except BaseException as e:
                        Tool.reporting("A {0} error occurs: {1}".format(
                            type(e), e.message))
                        self.write(
                            Tool.jsonal({
                                'error': e.message,
                                'rc': Tool.ERRCODE_BIZ
                            }))
                    else:
                        self.write(response)

                else:
                    self.write(
                        Tool.jsonal({
                            'error':
                            "yml file(" + yml_file + ") is not existed",
                            'rc': Tool.ERRCODE_SYS
                        }))
Example #4
0
    async def post(self):
        data = Tool.parsejson(self.request.body)
        Tool.LOGGER.debug("MORE DETAIL: data %s" % data)
        name = data['n'].encode('utf-8').decode()
        hosts = data['h']
        sign = data['s']
        yml_file = data['f'].encode('utf-8').decode()
        mode = data.get('i', False)
        forks = data.get('c', 50)
        if not hosts or not yml_file or not sign:
            self.finish(Tool.jsonal(
                {'error': "Lack of necessary parameters", 'rc': ErrorCode.ERRCODE_SYS}))
        else:
            hotkey = name + hosts + yml_file + Config.Get('sign_key')
            Tool.LOGGER.debug("MORE DETAIL: hot key %s" % hotkey)
            check_str = Tool.getmd5(hotkey)
            if sign != check_str:
                self.finish(Tool.jsonal(
                    {'error': "Sign is error", 'rc': ErrorCode.ERRCODE_BIZ}))
            else:
                myvars = {'hosts': hosts}
                # injection vars in playbook (rule: vars start with "v_" in
                # post data)
                for (k, v) in data.items():
                    if k[0:2] == "v_":
                        myvars[k[2:]] = v
                yml_file = Config.Get('dir_playbook') + yml_file

                Tool.LOGGER.debug("MORE DETAIL: yml file %s" % yml_file)
                if os.path.isfile(yml_file):
                    Tool.LOGGER.info("playbook: {0}, host: {1}, forks: {2}".format(
                        yml_file, hosts, forks))
                    if mode:
                        self.finish(
                            {'rc': ErrorCode.ERRCODE_NONE, 'async': True})  # async execute task release http connection
                        await tornado.ioloop.IOLoop.current().run_in_executor(
                            AsyncPool, Api.run_play_book, name, yml_file, hosts, forks, myvars)
                    else:
                        try:
                            response = await tornado.ioloop.IOLoop.current().run_in_executor(
                                SyncPool, Api.run_play_book, name, yml_file, hosts, forks, myvars)
                        except BaseException as e:
                            Tool.LOGGER.exception('A serious error occurs')
                            self.finish(Tool.jsonal(
                                {'error': str(e), 'rc': ErrorCode.ERRCODE_BIZ}))
                        else:
                            self.finish(response)

                else:
                    self.finish(Tool.jsonal(
                        {'error': "yml file(" + yml_file + ") is not existed", 'rc': ErrorCode.ERRCODE_SYS}))
Example #5
0
 def post(self):
     data = Tool.parsejson(self.request.body)
     badcmd = [
         'reboot', 'su', 'sudo', 'dd', 'mkfs', 'shutdown', 'half', 'top'
     ]
     name = data['n']
     module = data['m']
     arg = data['a']
     target = data['t']
     sign = data['s']
     sudo = True if data['r'] else False
     forks = data.get('c', 50)
     cmdinfo = arg.split(' ', 1)
     Tool.reporting('run: {0}, {1}, {2}, {3}, {4}, {5}'.format(
         name, target, module, arg, sudo, forks))
     hotkey = name + module + target + Config.Get('sign_key')
     check_str = Tool.getmd5(hotkey)
     if sign != check_str:
         self.write(
             Tool.jsonal({
                 'error': "Sign is error",
                 'rc': Tool.ERRCODE_BIZ
             }))
     else:
         if module in ('shell', 'command') and cmdinfo[0] in badcmd:
             self.write(
                 Tool.jsonal({
                     'error': "This is danger shell: " + cmdinfo[0],
                     'rc': Tool.ERRCODE_BIZ
                 }))
         else:
             try:
                 response = yield executor.submit(Api.runCmd,
                                                  name=name,
                                                  target=target,
                                                  module=module,
                                                  arg=arg,
                                                  sudo=sudo,
                                                  forks=forks)
             except BaseException as e:
                 Tool.reporting("A {0} error occurs: {1}".format(
                     type(e), e.message))
                 self.write(
                     Tool.jsonal({
                         'error': e.message,
                         'rc': Tool.ERRCODE_BIZ
                     }))
             else:
                 self.write(response)
Example #6
0
    def post(self):
        data = Tool.parsejson(self.request.body)
        badcmd = [
            'reboot', 'su', 'sudo', 'dd', 'mkfs', 'shutdown', 'half', 'top'
        ]
        name = data['n'].encode('utf-8').decode()
        module = data['m']
        arg = data['a'].encode('utf-8').decode()
        target = data['t']
        #sign = data['s']
        sudo = True if data['r'] else False
        forks = data.get('c', 50)
        cmdinfo = arg.split(' ', 1)
        Tool.LOGGER.info('run: {0}, {1}, {2}, {3}, {4}, {5}'.format(
            name, target, module, arg, sudo, forks))
        #hotkey = name + module + target + Config.Get('sign_key')
        #check_str = Tool.getmd5(hotkey)
        #if sign != check_str:
        #    self.write(Tool.jsonal(
        #        {'error': "Sign is error", 'rc': ErrorCode.ERRCODE_BIZ}))
        #else:

        if module in ('shell', 'command') and cmdinfo[0] in badcmd:
            self.write(
                Tool.jsonal({
                    'error': "This is danger shell: " + cmdinfo[0],
                    'rc': ErrorCode.ERRCODE_BIZ
                }))
        else:
            try:
                response = yield executor.submit(Api.runCmd,
                                                 name=name,
                                                 target=target,
                                                 module=module,
                                                 arg=arg,
                                                 sudo=sudo,
                                                 forks=forks)
            except BaseException as e:
                Tool.LOGGER.exception('A serious error occurs')
                self.write(
                    Tool.jsonal({
                        'error': str(e),
                        'rc': ErrorCode.ERRCODE_BIZ
                    }))
            else:
                self.write(response)
Example #7
0
 async def post(self):
     data = Tool.parsejson(self.request.body)
     path = data['p']
     filename = data['f']
     content = data['c'].encode('utf-8').decode()
     sign = data['s']
     if not filename or not content or not sign or path \
             not in ['script', 'playbook']:
         self.finish(Tool.jsonal(
             {'error': "Lack of necessary parameters", 'rc': ErrorCode.ERRCODE_SYS}))
     hotkey = path + filename + Config.Get('sign_key')
     check_str = Tool.getmd5(hotkey)
     if sign != check_str:
         self.finish(Tool.jsonal(
             {'error': "Sign is error", 'rc': ErrorCode.ERRCODE_BIZ}))
     else:
         file_path = Config.Get('dir_' + path) + filename
         result = await tornado.ioloop.IOLoop.current().run_in_executor(SyncPool, self.write_file, file_path, content)
         self.finish(Tool.jsonal({'ret': result}))
Example #8
0
 async def post(self):    # Change the async method to python3 async, this performance better than gen.coroutine
     data = Tool.parsejson(self.request.body)
     badcmd = ['reboot', 'su', 'sudo', 'dd',
               'mkfs', 'shutdown', 'half', 'top']
     name = data['n'].encode('utf-8').decode()
     module = data['m']
     arg = data['a'].encode('utf-8').decode()
     target = data['t']
     sign = data['s']
     sudo = True if data['r'] else False
     mode = data.get('i', False) #True for async
     forks = data.get('c', 50)
     cmdinfo = arg.split(' ', 1)
     Tool.LOGGER.info('run: {0}, {1}, {2}, {3}, {4}, {5}'.format(
         name, target, module, arg, sudo, forks))
     hotkey = name + module + target + Config.Get('sign_key')
     check_str = Tool.getmd5(hotkey)
     if sign != check_str:
         self.finish(Tool.jsonal(
             {'error': "Sign is error", 'rc': ErrorCode.ERRCODE_BIZ}))
     else:
         if module in ('shell', 'command') and cmdinfo[0] in badcmd:
             self.finish(Tool.jsonal(
                 {'error': "This is danger shell: " + cmdinfo[0], 'rc': ErrorCode.ERRCODE_BIZ}))
         else:
             host_list = target.split(",")
             if mode:
                 self.finish({'rc': ErrorCode.ERRCODE_NONE, 'async': True })  # async execute task release http connection
                 await tornado.ioloop.IOLoop.current().run_in_executor(
                     AsyncPool, Api.run_cmd, name, host_list, module, arg, sudo, forks)
             else:
                 try:
                     response = await tornado.ioloop.IOLoop.current().run_in_executor(
                         SyncPool, Api.run_cmd, name, host_list, module, arg, sudo, forks)
                     self.finish(response)   # sync task wait for response
                 except Exception as e:
                     self.finish(Tool.jsonal(
                         {'error': str(e), 'rc': ErrorCode.ERRCODE_BIZ}))