-
Notifications
You must be signed in to change notification settings - Fork 7
/
user_auth.py
143 lines (121 loc) · 4.6 KB
/
user_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# FileName: user_auth.py
# Desc: 2015-15/1/16:下午3:43
# Author: 苦咖啡
# Email: voilet@qq.com
# HomePage: http://blog.kukafei520.net
# History:
# =============================================================================
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
import time
import tornado.websocket
from tornado.escape import json_encode
from salt_api.salt_https_api import token_id, salt_api_token, salt_api_useradd
import nmap
from functools import partial, wraps
from concurrent.futures import ThreadPoolExecutor
import json
from websocket import create_connection
import tornado.gen
import tornado.concurrent
from tornado.concurrent import run_on_executor
# from salt.output import display_output
import yaml
from tornado.options import define, options
define('salt_api_url')
define('salt_api_pass')
define('salt_api_user')
EXECUTOR = ThreadPoolExecutor(max_workers=4)
def unblock(f):
@tornado.web.asynchronous
@wraps(f)
def wrapper(*args, **kwargs):
self = args[0]
def callback(future):
self.write(future.result())
self.finish()
EXECUTOR.submit(
partial(f, *args, **kwargs)
).add_done_callback(
lambda future: tornado.ioloop.IOLoop.instance().add_callback(
partial(callback, future)))
return wrapper
# class push_data(tornado.web.RequestHandler):
class user_add(tornado.web.RequestHandler):
"""注册方法"""
# @tornado.web.asynchronous
def get(self):
self.write(json.dumps({"status": 403, "result": "error"}, indent=4))
executor = ThreadPoolExecutor(2)
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
user = self.get_argument("user")
gid = self.get_argument("gid")
uid = self.get_argument("uid")
key = self.get_argument("key")
group = self.get_argument("group")
self.write(json.dumps({"status": 200, "result": "OK"}, indent=4))
self.finish()
self.useradd(user, gid, uid, key, group)
@run_on_executor
def useradd(self, user, gid, uid, key, group):
token_api_id = token_id()
gid = "gid=%s" % (gid)
uid = "uid=%s" % (uid)
home = "home=/home/users/%s" %(user)
shell = "shell=/bin/bash"
print "111"
# s = requests.post(salt_api_url, headers=headers, data='"client"="local", "fun"="user.add", "tgt"="hadoop01", [("arg",user), ("arg", gid), ("arg", uid), ("arg", home), ("arg", shell)]')
list = salt_api_useradd(
{'client': 'local', 'fun': 'user.add', 'tgt': "hadoop01",
"arg": [user, gid, uid, home, shell], 'timeout': 100}, options.salt_api_url,
{"X-Auth-Token": token_api_id}
)
master_status = list.run()
print master_status
# key = "%s" % (key)
# add_key = salt_api_useradd(
# {'client': 'local', 'fun': 'cmd.script', 'tgt': "hadoop01",
# "arg": ["salt://usr/user_key.sh", user, key, group], 'timeout': 100}, salt_api_url,
# {"X-Auth-Token": token_api_id}
# )
# print add_key
# master_status = add_key.run()
# print "#" * 100
# print master_status
return True
# class push_data(tornado.web.RequestHandler):
class user_del(tornado.web.RequestHandler):
"""注册方法"""
# @tornado.web.asynchronous
def get(self):
self.write(json.dumps({"status": 403, "result": "error"}, indent=4))
executor = ThreadPoolExecutor(2)
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
user = self.get_argument("user")
fqdn = self.get_argument("fqdn")
self.write(json.dumps({"status": 200, "result": "OK"}, indent=4))
self.finish()
self.userdel(user, fqdn)
@run_on_executor
def userdel(self, user, fqdn):
token_api_id = token_id()
user = "gid=%s" % (user)
# s = requests.post(salt_api_url, headers=headers, data='"client"="local", "fun"="user.add", "tgt"="hadoop01", [("arg",user), ("arg", gid), ("arg", uid), ("arg", home), ("arg", shell)]')
list = salt_api_useradd(
{'client': 'local', 'fun': 'user.delete', 'tgt': fqdn,
"arg": [user, 'remove=True', 'force=True'], 'timeout': 100}, options.salt_api_url,
{"X-Auth-Token": token_api_id}
)
master_status = list.run()
print master_status
return True