Skip to content

Commit 57cee58

Browse files
committed
修复无法识别roles_path问题
1 parent 33633ec commit 57cee58

12 files changed

Lines changed: 165 additions & 54 deletions

File tree

-38 Bytes
Binary file not shown.
-16 Bytes
Binary file not shown.

Ops/celery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# set the default Django settings module for the 'celery' program.
1818
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Ops.settings')
1919

20-
app = Celery('Ops', broker='amqp://rabbitmq:rabbitmq@localhost:5672/myvhost')
20+
app = Celery('Ops', broker='redis://127.0.0.1:6379/0')
2121

2222
app.conf.task_queues = (
2323
Queue('default', Exchange('default', type='direct'), routing_key='default'),

Ops/routing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
path(r'ws/deploy/', DeployConsumer),
3232
path(r'ws/ans_module_log/', AnsModuleConsumer),
3333
path(r'ws/ans_playbook_log/', AnsPlaybookConsumer),
34-
re_path(r'ws/fortssh/([0-9]+)/([0-9]+)/(?P<group_name>.*)/', FortConsumer),
35-
re_path(r'ws/webssh/([0-9]+)/(?P<group_name>.*)/', SSHConsumer),
34+
re_path(r'ws/fortssh/([0-9]+)/([0-9]+)/', FortConsumer),
35+
re_path(r'ws/webssh/([0-9]+)/', SSHConsumer),
3636
re_path(r'ws/fort_guacamole/([0-9]+)/([0-9]+)/(?P<group_name>.*)/', GuacamoleConsumer),
3737
re_path(r'ws/admin_guacamole/([0-9]+)/(?P<group_name>.*)/', AdminGuacamole),
3838
]),

Ops/settings.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# This example app uses the Redis channel layer implementation channels_redis
3636
"BACKEND": "channels_redis.core.RedisChannelLayer",
3737
"CONFIG": {
38-
"hosts": [('10.1.19.10', 6379)],
38+
"hosts": [('127.0.0.1', 6379)],
3939
},
4040
},
4141
}
@@ -56,13 +56,13 @@
5656
}
5757

5858
# 执行ansible命令使用的redis信息
59-
REDIS_HOST = '10.1.19.10'
59+
REDIS_HOST = '127.0.0.1'
6060
REDIS_PORT = 6379
6161
REDIS_DB = 2
6262
REDIS_PASSWORD = None
6363

6464
# mongodb配置信息
65-
MONGODB_HOST = '10.1.19.10'
65+
MONGODB_HOST = '127.0.0.1'
6666
MONGODB_PORT = 27017
6767
COMMANDS_DB = 'commands'
6868

@@ -152,9 +152,9 @@
152152
'default': {
153153
'ENGINE': 'django.db.backends.mysql',
154154
'NAME': 'ops',
155-
'USER': 'root',
156-
'PASSWORD': 'Aa123456!',
157-
'HOST': '10.1.19.10'
155+
'USER': 'ops',
156+
'PASSWORD': '123456',
157+
'HOST': '127.0.0.1'
158158
}
159159
}
160160

@@ -208,21 +208,22 @@
208208

209209
# zabbix配置
210210
ZABBIX_INFO = {
211-
'api_url': 'http://10.1.19.10/zabbix/api_jsonrpc.php',
212-
'graph_url': 'http://10.1.19.10/zabbix/chart2.php',
213-
'login_url': 'http://10.1.19.10/zabbix/index.php',
211+
'api_url': 'http://127.0.0.1/zabbix/api_jsonrpc.php',
212+
'graph_url': 'http://127.0.0.1/zabbix/chart2.php',
213+
'login_url': 'http://127.0.0.1/zabbix/index.php',
214214
'username': 'admin',
215215
'password': '123456'
216216
}
217217

218-
ANSIBLE_ROLE_PATH = os.path.join(MEDIA_ROOT, 'roles')
218+
# ANSIBLE_ROLE_PATH = os.path.join(MEDIA_ROOT, 'roles')
219+
ANSIBLE_ROLE_PATH = '/usr/share/ansible/roles'
219220

220-
GUACD_HOST = '10.1.19.10'
221+
GUACD_HOST = '127.0.0.1'
221222
GUACD_PORT = 4822
222223

223224
# email配置
224225
EMAIL_HOST = 'smtp.163.com'
225226
EMAIL_PORT = 25
226-
EMAIL_HOST_USER = 'XXXXXXXXXXX'
227-
EMAIL_HOST_PASSWORD = 'XXXXXXXXXXXXXXXX'
227+
EMAIL_HOST_USER = 'XXXXXXXXXXXX'
228+
EMAIL_HOST_PASSWORD = 'XXXXXXXXXXX'
228229
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

assets/utils/webssh.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,23 @@ def __init__(self, chan):
2020
self.start_time = time.time()
2121
self.current_time = time.strftime(settings.TIME_FORMAT)
2222
self.stdout = []
23-
self.read_lock = threading.RLock()
2423

2524
def stop(self):
2625
self._stop_event.set()
2726

2827
def run(self):
29-
with self.read_lock:
30-
while not self._stop_event.is_set():
31-
time.sleep(0.1)
32-
try:
33-
data = self.chan.chan.recv(1024)
34-
if data:
35-
str_data = bytes.decode(data)
36-
self.chan.send(str_data)
37-
self.stdout.append([time.time() - self.start_time, 'o', str_data])
38-
except timeout:
39-
break
40-
self.chan.send('\n由于长时间没有操作,连接已断开!')
41-
self.stdout.append([time.time() - self.start_time, 'o', '\n由于长时间没有操作,连接已断开!'])
42-
self.chan.close()
28+
while not self._stop_event.is_set() or not self.chan.chan.exit_status_ready():
29+
time.sleep(0.1)
30+
try:
31+
data = self.chan.chan.recv(1024)
32+
if data:
33+
str_data = bytes.decode(data)
34+
self.chan.send(str_data)
35+
self.stdout.append([time.time() - self.start_time, 'o', str_data])
36+
except timeout:
37+
self.chan.send('\n由于长时间没有操作,连接已断开!', close=True)
38+
self.stdout.append([time.time() - self.start_time, 'o', '\n由于长时间没有操作,连接已断开!'])
39+
break
4340

4441
def record(self):
4542
record_path = os.path.join(settings.MEDIA_ROOT, 'admin_ssh_records', self.chan.scope['user'].username,
@@ -88,7 +85,6 @@ class SSHConsumer(WebsocketConsumer):
8885
def __init__(self, *args, **kwargs):
8986
super(SSHConsumer, self).__init__(*args, **kwargs)
9087
self.ssh = paramiko.SSHClient()
91-
self.group_name = self.scope['url_route']['kwargs']['group_name']
9288
self.server = ServerAssets.objects.select_related('assets').get(id=self.scope['path'].split('/')[3])
9389
self.host_ip = self.server.assets.asset_management_ip
9490
self.width = 150
@@ -98,7 +94,10 @@ def __init__(self, *args, **kwargs):
9894
self.chan = None
9995

10096
def connect(self):
101-
self.accept()
97+
if self.scope["user"].is_anonymous:
98+
self.close(code=1007)
99+
else:
100+
self.accept()
102101

103102
username = self.server.username
104103
try:
@@ -122,6 +121,6 @@ def receive(self, text_data=None, bytes_data=None):
122121
def disconnect(self, close_code):
123122
try:
124123
self.t1.record()
124+
self.t1.stop()
125125
finally:
126126
self.ssh.close()
127-
self.t1.stop()

fort/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ def terminal(request, server_id, fort_user_id):
9494
fort_users = gen_fort_users(request.user)
9595

9696
if fort_user_obj in fort_users:
97-
group_name = str(uuid.uuid4())
9897
remote_ip = request.META.get('REMOTE_ADDR')
9998
if fort_user_obj.fort_server.server_protocol == 'ssh':
10099
return render(request, 'fort/terminal.html', locals())
101100
else:
101+
group_name = str(uuid.uuid4())
102102
return render(request, 'fort/guacamole.html', locals())
103103
else:
104104
return HttpResponseForbidden('<h1>403!您无权连接此主机!</h1>')

logs/all.log

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
INFO 2019-04-01 16:36:27 _log Connected (version 2.0, client OpenSSH_7.4)
2-
INFO 2019-04-01 16:36:27 _log Authentication (publickey) successful!
3-
INFO 2019-04-01 16:36:27 _log [chan 2] Opened sftp connection (server version 3)
4-
INFO 2019-04-01 16:36:28 _log [chan 3] Opened sftp connection (server version 3)
5-
INFO 2019-04-01 16:36:28 _log [chan 3] sftp session closed.
6-
INFO 2019-04-01 17:20:53 _log Connected (version 2.0, client OpenSSH_7.4)
7-
INFO 2019-04-01 17:20:53 _log Authentication (publickey) successful!
8-
INFO 2019-04-01 17:20:54 _log [chan 2] Opened sftp connection (server version 3)
9-
INFO 2019-04-01 17:20:54 _log [chan 3] Opened sftp connection (server version 3)
10-
INFO 2019-04-01 17:20:54 _log [chan 3] sftp session closed.
1+
INFO 2019-04-16 16:56:07 _log Connected (version 2.0, client OpenSSH_7.4)
2+
INFO 2019-04-16 16:56:07 _log Authentication (publickey) failed.
3+
INFO 2019-04-16 16:56:07 _log Authentication (password) successful!
4+
INFO 2019-04-16 17:13:54 _log Connected (version 2.0, client OpenSSH_5.3)
5+
INFO 2019-04-16 17:13:55 _log Authentication (publickey) failed.
6+
INFO 2019-04-16 17:13:55 _log Authentication (password) successful!

logs/ansible.log

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,116 @@
1-
ERROR 2019-03-06 15:17:34 run_playbook 执行/testops/Ops/media/playbook/2018/12/18/test.yml失败,原因: __init__() missing 1 required positional argument: 'sock'
1+
ERROR 2019-04-15 14:24:53 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
2+
3+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 6, column 7, but may
4+
be elsewhere in the file depending on the exact syntax problem.
5+
6+
The offending line appears to be:
7+
8+
roles:
9+
- nginx
10+
^ here
11+
12+
ERROR 2019-04-15 14:25:14 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
13+
14+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 6, column 7, but may
15+
be elsewhere in the file depending on the exact syntax problem.
16+
17+
The offending line appears to be:
18+
19+
roles:
20+
- nginx
21+
^ here
22+
23+
ERROR 2019-04-15 14:27:10 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
24+
25+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 6, column 7, but may
26+
be elsewhere in the file depending on the exact syntax problem.
27+
28+
The offending line appears to be:
29+
30+
roles:
31+
- nginx
32+
^ here
33+
34+
ERROR 2019-04-15 14:28:39 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
35+
36+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 7, column 7, but may
37+
be elsewhere in the file depending on the exact syntax problem.
38+
39+
The offending line appears to be:
40+
41+
roles:
42+
- nginx
43+
^ here
44+
45+
ERROR 2019-04-15 14:33:49 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
46+
47+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 7, column 7, but may
48+
be elsewhere in the file depending on the exact syntax problem.
49+
50+
The offending line appears to be:
51+
52+
roles:
53+
- nginx
54+
^ here
55+
56+
ERROR 2019-04-15 14:44:59 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
57+
58+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 6, column 7, but may
59+
be elsewhere in the file depending on the exact syntax problem.
60+
61+
The offending line appears to be:
62+
63+
roles:
64+
- nginx
65+
^ here
66+
67+
ERROR 2019-04-15 14:49:27 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
68+
69+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 6, column 7, but may
70+
be elsewhere in the file depending on the exact syntax problem.
71+
72+
The offending line appears to be:
73+
74+
roles:
75+
- nginx
76+
^ here
77+
78+
ERROR 2019-04-15 14:55:32 run_playbook 执行/testops/Ops/media/playbook/2018/12/18/tomcat.yml失败,原因: the role 'tomcat' was not found in /testops/Ops/media/playbook/2018/12/18/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2018/12/18
79+
80+
The error appears to have been in '/testops/Ops/media/playbook/2018/12/18/tomcat.yml': line 7, column 7, but may
81+
be elsewhere in the file depending on the exact syntax problem.
82+
83+
The offending line appears to be:
84+
85+
roles:
86+
- tomcat
87+
^ here
88+
89+
ERROR 2019-04-15 15:02:03 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: the role 'nginx' was not found in /testops/Ops/media/playbook/2019/4/15/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/testops/Ops/media/playbook/2019/4/15
90+
91+
The error appears to have been in '/testops/Ops/media/playbook/2019/4/15/install_nginx.yml': line 6, column 7, but may
92+
be elsewhere in the file depending on the exact syntax problem.
93+
94+
The offending line appears to be:
95+
96+
roles:
97+
- nginx
98+
^ here
99+
100+
ERROR 2019-04-15 17:47:08 run_playbook 执行/testops/Ops/media/playbook/2019/4/15/install_nginx.yml失败,原因: Syntax Error while loading YAML.
101+
102+
103+
The error appears to have been in '/usr/share/ansible/roles/nginx/defaults/main.yml': line 68, column 3, but may
104+
be elsewhere in the file depending on the exact syntax problem.
105+
106+
The offending line appears to be:
107+
108+
nginx_upstreams: []
109+
- name: myapp1
110+
^ here
111+
112+
exception type: <class 'yaml.parser.ParserError'>
113+
exception: while parsing a block mapping
114+
in "<unicode string>", line 3, column 1
115+
did not find expected key
116+
in "<unicode string>", line 68, column 3

task/utils/ansible_api_v2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def v2_runner_on_unreachable(self, result):
4444
else:
4545
data = '<code style="color: #FF0000">\n{host} | unreachable >> \n{stdout}\n</code>'.format(
4646
host=result._host.name,
47-
stdout=json.dumps(result._result, indent=4))
47+
stdout=json.dumps(result._result, indent=4, ensure_ascii=False))
4848
if self.sock:
4949
self.sock.send(data)
5050
self.module_results.append(data)
@@ -53,19 +53,19 @@ def v2_runner_on_ok(self, result, *args, **kwargs):
5353
if 'rc' in result._result and 'stdout' in result._result:
5454
data = '<code style="color: #008000">\n{host} | success | rc={rc} >> \n{stdout}\n</code>'.format(
5555
host=result._host.name, rc=result._result.get('rc'),
56-
stdout=result._result.get('stdout'))
56+
stdout=result._result.get('stdout').encode().decode('utf-8'))
5757
elif 'results' in result._result and 'rc' in result._result:
5858
data = '<code style="color: #008000">\n{host} | success | rc={rc} >> \n{stdout}\n</code>'.format(
5959
host=result._host.name, rc=result._result.get('rc'),
60-
stdout=result._result.get('results')[0])
60+
stdout=result._result.get('results')[0].encode().decode('utf-8'))
6161
elif 'module_stdout' in result._result and 'rc' in result._result:
6262
data = '<code style="color: #008000">\n{host} | success | rc={rc} >> \n{stdout}\n</code>'.format(
6363
host=result._host.name, rc=result._result.get('rc'),
6464
stdout=result._result.get('module_stdout').encode().decode('utf-8'))
6565
else:
6666
data = '<code style="color: #008000">\n{host} | success >> \n{stdout}\n</code>'.format(
6767
host=result._host.name,
68-
stdout=json.dumps(result._result, indent=4))
68+
stdout=json.dumps(result._result, indent=4, ensure_ascii=False))
6969
if self.sock:
7070
self.sock.send(data)
7171
self.module_results.append(data)
@@ -84,7 +84,7 @@ def v2_runner_on_failed(self, result, *args, **kwargs):
8484
else:
8585
data = '<code style="color: #FF0000">\n{host} | failed >> \n{stdout}\n</code>'.format(
8686
host=result._host.name,
87-
stdout=json.dumps(result._result, indent=4))
87+
stdout=json.dumps(result._result, indent=4, ensure_ascii=False))
8888
if self.sock:
8989
self.sock.send(data)
9090
self.module_results.append(data)

0 commit comments

Comments
 (0)