Skip to content

Commit ee3019e

Browse files
author
hustcc
committed
add start manager && fixed #28
1 parent 21f0e7d commit ee3019e

File tree

5 files changed

+131
-11
lines changed

5 files changed

+131
-11
lines changed

app/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
from celery import Celery, platforms
1212
from app.utils.validator import Validator
1313
from flask_socketio import SocketIO
14+
import eventlet
1415

1516

1617
# 版本号
1718
__version__ = '0.0.4'
1819

20+
# monkey_patch
21+
eventlet.monkey_patch()
22+
1923
# flask
2024
app = Flask(__name__)
2125
app.config.from_object('app.config_default')

app/static/dist/webhook.bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/static/src/server.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const Server = React.createClass({
9797
<th width="20%">IP</th>
9898
<th width="8%">Port</th>
9999
<th width="17%">Account</th>
100-
<th width="10%">PKey</th>
100+
<th width="10%">PKey / PassWord</th>
101101
<th width="15%">Time</th>
102102
<th width="10%">Operate</th>
103103
</tr>
@@ -148,7 +148,7 @@ const Server = React.createClass({
148148
</div>
149149
</div>
150150
<div className="field">
151-
<label>SSH Private Key <Link to="/doc/pkey"><i className="ui icon help"></i></Link></label>
151+
<label>SSH Private Key / Account PassWord<Link to="/doc/pkey"><i className="ui icon help"></i></Link></label>
152152
<textarea ref="pkey" defaultValue={this.state.editServer.pkey} rows="6"></textarea>
153153
</div>
154154
</form>

app/utils/SshUtil.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ def do_ssh_cmd(ip, port, account, pkey, shell, push_data='', timeout=300):
2222
except:
2323
port = 22
2424

25-
pkey = pkey.strip() + '\n' # 注意最后有一个换行
26-
27-
pkey_file = StringIO.StringIO(pkey)
28-
private_key = paramiko.RSAKey.from_private_key(pkey_file)
29-
3025
s = paramiko.SSHClient()
31-
3226
s.load_system_host_keys()
3327
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
3428

35-
s.connect(ip, port, account, pkey=private_key)
29+
try:
30+
# 首先以 ssh 密钥方式登陆
31+
pkey_file = StringIO.StringIO(pkey.strip() + '\n') # 注意最后有一个换行
32+
private_key = paramiko.RSAKey.from_private_key(pkey_file)
33+
s.connect(ip, port, account, pkey=private_key, timeout=5)
34+
pkey_file.close()
35+
except:
36+
# 如果出现异常,则使用 用户密码登陆的方式
37+
s.connect(ip, port, account, password=pkey, timeout=5)
38+
3639
# if push_data:
3740
# shell = shell + (" '%s'" % push_data)
3841
shell = shell.split('\n')
@@ -50,7 +53,6 @@ def do_ssh_cmd(ip, port, account, pkey, shell, push_data='', timeout=300):
5053
log = err
5154

5255
s.close()
53-
pkey_file.close()
5456

5557
if success:
5658
success = is_log_success(log)

manage.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
Created on 2016-11-23
4+
5+
@author: hustcc
6+
'''
7+
8+
import subprocess
9+
import sys
10+
from flask_script import Manager, Command, Server as _Server, Option
11+
from app import SQLAlchemyDB as db, socketio, app, __version__
12+
13+
14+
manager = Manager(app)
15+
16+
17+
class Server(_Server):
18+
help = description = 'Runs the Git-WebHook web server'
19+
20+
def get_options(self):
21+
options = (
22+
Option('-h', '--host',
23+
dest='host',
24+
default='0.0.0.0'),
25+
26+
Option('-p', '--port',
27+
dest='port',
28+
type=int,
29+
default=18340),
30+
31+
Option('-d', '--debug',
32+
action='store_true',
33+
dest='use_debugger',
34+
help=('enable the Werkzeug debugger (DO NOT use in '
35+
'production code)'),
36+
default=self.use_debugger),
37+
Option('-D', '--no-debug',
38+
action='store_false',
39+
dest='use_debugger',
40+
help='disable the Werkzeug debugger',
41+
default=self.use_debugger),
42+
Option('-r', '--reload',
43+
action='store_true',
44+
dest='use_reloader',
45+
help=('monitor Python files for changes (not 100%% safe '
46+
'for production use)'),
47+
default=self.use_reloader),
48+
Option('-R', '--no-reload',
49+
action='store_false',
50+
dest='use_reloader',
51+
help='do not monitor Python files for changes',
52+
default=self.use_reloader),
53+
)
54+
return options
55+
56+
def __call__(self, app, host, port, use_debugger, use_reloader):
57+
print host, port, use_debugger, use_reloader
58+
# override the default runserver command to start a Socket.IO server
59+
if use_debugger is None:
60+
use_debugger = app.debug
61+
if use_debugger is None:
62+
use_debugger = True
63+
if use_reloader is None:
64+
use_reloader = app.debug
65+
socketio.run(app,
66+
host=host,
67+
port=port,
68+
debug=use_debugger,
69+
use_reloader=use_reloader,
70+
**self.server_options)
71+
72+
manager.add_command("runserver", Server())
73+
74+
75+
class CeleryWorker(Command):
76+
"""Starts the celery worker."""
77+
name = 'celery'
78+
capture_all_args = True
79+
80+
def run(self, argv):
81+
cmd = ['celery', '-A', 'app.celeryInstance', 'worker'] + argv
82+
ret = subprocess.call(cmd)
83+
sys.exit(ret)
84+
85+
86+
manager.add_command("celery", CeleryWorker())
87+
88+
89+
@manager.command
90+
def createdb(drop_first=False):
91+
"""Creates the database."""
92+
if drop_first:
93+
db.drop_all()
94+
db.create_all()
95+
print('OK')
96+
97+
98+
@manager.command
99+
def lint():
100+
"""Runs code linter."""
101+
lint = subprocess.call(['flake8']) == 0
102+
if lint:
103+
print('OK')
104+
sys.exit(lint)
105+
106+
107+
@manager.command
108+
def version():
109+
"get the version"
110+
print __version__
111+
112+
113+
if __name__ == '__main__':
114+
manager.run()

0 commit comments

Comments
 (0)