From 7cbb919afbc779491f04109d8e1b90d5f3fd7bd4 Mon Sep 17 00:00:00 2001 From: "Welliam.Cao" <303350019@qq.com> Date: Sat, 23 Dec 2017 14:09:20 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96ansible=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E7=94=A8=E6=88=B7sudo=202.=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E6=B7=BB=E5=8A=A0=E5=89=A7=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=AE=B0=E5=BD=95=E6=97=A5=E5=BF=97bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpsManage/sudoterminal.py | 102 ------------------------------ OpsManage/utils/ansible_api_v2.py | 8 +-- OpsManage/views/ansible.py | 7 +- 3 files changed, 7 insertions(+), 110 deletions(-) delete mode 100644 OpsManage/sudoterminal.py diff --git a/OpsManage/sudoterminal.py b/OpsManage/sudoterminal.py deleted file mode 100644 index 284ddfb7..00000000 --- a/OpsManage/sudoterminal.py +++ /dev/null @@ -1,102 +0,0 @@ -import paramiko -import re -try: - import simplejson as json -except ImportError: - import json -from django.utils.encoding import smart_unicode - -class ShellHandler(object): - - def __init__(self, ip, username, port, method, credential, timeout = 3, channel_name = None): - if method not in ['key','password']: - raise Exception('Authication must be key or password') - self.ssh = paramiko.SSHClient() - self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - if method == 'password': - self.ssh.connect(ip, port=port, username=username, password=credential, timeout=timeout) - else: - self.ssh.connect(ip, port=port, username=username, key_filename=credential, timeout=timeout) - - channel = self.ssh.invoke_shell() - self.stdin = channel.makefile('wb') - self.stdout = channel.makefile('r') - self.channel_name = channel_name - - - def __del__(self): - self.ssh.close() - - @staticmethod - def _print_exec_out(cmd, out_buf, err_buf, exit_status, channel_name=None ): - from OpsManage.asgi import channel_layer - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('command executed: {}'.format(cmd))])}) - print('command executed: {}'.format(cmd)) - print('STDOUT:') - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('STDOUT:')])}) - for line in out_buf: - print line, "end=" - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode(line.strip('\n'))])}) - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('end of STDOUT')])}) - print('end of STDOUT') - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('STDERR:')])}) - print('STDERR:') - for line in err_buf: - print line, "end=" - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode(line, "end=")])}) - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('end of STDERR')])}) - print('end of STDERR') - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('finished with exit status: {}'.format(exit_status))])}) - print('finished with exit status: {}'.format(exit_status)) - channel_layer.send(channel_name, {'text': json.dumps(['stdout',smart_unicode('------------------------------------')])}) - print('------------------------------------') - - def execute(self, cmd): - """ - - :param cmd: the command to be executed on the remote computer - :examples: execute('ls') - execute('finger') - execute('cd folder_name') - """ - cmd = cmd.strip('\n') - self.stdin.write(cmd + '\n') - finish = 'end of stdOUT buffer. finished with exit status' - echo_cmd = 'echo {} $?'.format(finish) - self.stdin.write(echo_cmd + '\n') - shin = self.stdin - self.stdin.flush() - - shout = [] - sherr = [] - exit_status = 0 - for line in self.stdout: - if str(line).startswith(cmd) or str(line).startswith(echo_cmd): - # up for now filled with shell junk from stdin - shout = [] - elif str(line).startswith(finish): - # our finish command ends with the exit status - exit_status = int(str(line).rsplit()[-1]) - if exit_status: - # stderr is combined with stdout. - # thus, swap sherr with shout in a case of failure. - sherr = shout - shout = [] - break - else: - # get rid of 'coloring and formatting' special characters - shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line). - replace('\b', '').replace('\r', '')) - - # first and last lines of shout/sherr contain a prompt - if shout and echo_cmd in shout[-1]: - shout.pop() - if shout and cmd in shout[0]: - shout.pop(0) - if sherr and echo_cmd in sherr[-1]: - sherr.pop() - if sherr and cmd in sherr[0]: - sherr.pop(0) - - self._print_exec_out(cmd=cmd, out_buf=shout, err_buf=sherr, exit_status=exit_status,channel_name=self.channel_name) - return shin, shout, sherr \ No newline at end of file diff --git a/OpsManage/utils/ansible_api_v2.py b/OpsManage/utils/ansible_api_v2.py index 1bd22f7c..b0fb4843 100755 --- a/OpsManage/utils/ansible_api_v2.py +++ b/OpsManage/utils/ansible_api_v2.py @@ -369,11 +369,11 @@ def __initializeData(self,kwargs): self.variable_manager = VariableManager() self.loader = DataLoader() self.options = Options(connection='smart', module_path=None, forks=100, timeout=10, - remote_user='root', ask_pass=False, private_key_file=None, ssh_common_args=None, + remote_user=kwargs.get('remote_user','root'), ask_pass=False, private_key_file=None, ssh_common_args=None, ssh_extra_args=None,sftp_extra_args=None, scp_extra_args=None, become=None, - become_method=None,become_user='root', ask_value_pass=False, - verbosity=kwargs.get('verbosity',None),check=False, listhosts=False, - listtasks=False, listtags=False, syntax=False) + become_method=kwargs.get('become_method',None),become_user=kwargs.get('become_user','root'), + verbosity=kwargs.get('verbosity',None),check=False, listhosts=False, + listtasks=False, listtags=False, syntax=False,ask_value_pass=False, ) self.passwords = dict(sshpass=None, becomepass=None) self.inventory = MyInventory(self.resource, self.loader, self.variable_manager).inventory diff --git a/OpsManage/views/ansible.py b/OpsManage/views/ansible.py index 248287b3..965a3085 100755 --- a/OpsManage/views/ansible.py +++ b/OpsManage/views/ansible.py @@ -390,8 +390,8 @@ def apps_playbook_modf(request,pid): Ansible_Playbook_Number.objects.filter(playbook=playbook,playbook_server=ip).delete() else: for server in numberList: - Ansible_Playbook_Number.objects.filter(playbook=playbook,playbook_server=server.playbook_server).delete() - AnsibleRecord.PlayBook.insert(user=str(request.user),ans_id=playbook.id,ans_name=playbook.playbook_name,ans_content="修改Ansible剧本",ans_server=None) + Ansible_Playbook_Number.objects.filter(playbook=playbook,playbook_server=server.playbook_server).delete() + AnsibleRecord.PlayBook.insert(user=str(request.user),ans_id=playbook.id,ans_name=playbook.playbook_name,ans_content="修改Ansible剧本",ans_server=','.join(sList)) return HttpResponseRedirect('/apps/playbook/modf/{id}/'.format(id=pid)) @@ -448,7 +448,6 @@ def apps_playbook_online_modf(request,pid): if sip not in tagret_server_list: Ansible_Playbook_Number.objects.create(playbook=playbook,playbook_server=sip) except Exception,e: - print e return render(request,'apps/apps_playbook_modf.html',{"user":request.user, "errorInfo":"目标服务器信息修改错误:%s" % str(e)}, ) @@ -459,7 +458,7 @@ def apps_playbook_online_modf(request,pid): else: for server in numberList: Ansible_Playbook_Number.objects.filter(playbook=playbook,playbook_server=server.playbook_server).delete() - AnsibleRecord.PlayBook.insert(user=str(request.user),ans_id=playbook.id,ans_name=playbook.playbook_name,ans_content="修改Ansible剧本",ans_server=None) + AnsibleRecord.PlayBook.insert(user=str(request.user),ans_id=playbook.id,ans_name=playbook.playbook_name,ans_content="修改Ansible剧本",ans_server=','.join(sList)) return JsonResponse({'msg':"更新成功","code":200,'data':[]}) @login_required(login_url='/login')