Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加跳板机连接数据库功能 #736

Merged
merged 16 commits into from
Jun 3, 2020
Merged

添加跳板机连接数据库功能 #736

merged 16 commits into from
Jun 3, 2020

Conversation

EliasChiang
Copy link
Contributor

@EliasChiang EliasChiang commented May 11, 2020

你好:
我在项目上添加了通过ssh隧道连接,实现跳板机连接数据库功能。fix #731
实现是通过sshtunnel包,连通跳板机之后映射目标端口到本地,这个方案在我之前的项目中使用过。在本项目中我只测试了MySQL连接,但是理论上其他数据库也是可行的。
我主要做了以下修改:
1.新增了ssh_tunnel模型,并且修改instance,增加到ssh_tunnel的外键字段。
2.新增了ssh_tunnel.py,但是需要通过类的方式实现。
3.修改 sql/engines/mysql.py 增加了__del__(self)函数,需要注意其他引擎可能也都需要增加该函数,否则隧道无法关闭。
需要注意的是:
1.初始化需要增加步骤:python3 manage.py dbshell<src/init_sql/ssh_tunnel.sql,添加无隧道初始化信息,无隧道是根据ssh_tunnel.tunnel_name == 'None'判断,这部分我写死在了代码里。
2.我不会做界面,所以没有做“添加ssh隧道界面”以及相关的界面,因此如果实际使用还需要额外新增界面部分代码。
如有疑问,请随时联系 QQ362424547

Copy link
Collaborator

@LeoQuote LeoQuote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢提交,问题不大,单元测试看一下

sql/engines/__init__.py Outdated Show resolved Hide resolved
sql/models.py Outdated Show resolved Hide resolved
sql/engines/__init__.py Show resolved Hide resolved
src/init_sql/ssh_tunnel.sql Outdated Show resolved Hide resolved
sql/engines/tests.py Outdated Show resolved Hide resolved
sql/models.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #736 into master will decrease coverage by 0.06%.
The diff coverage is 60.41%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #736      +/-   ##
==========================================
- Coverage   78.51%   78.45%   -0.07%     
==========================================
  Files          79       80       +1     
  Lines       11004    11049      +45     
==========================================
+ Hits         8640     8668      +28     
- Misses       2364     2381      +17     
Impacted Files Coverage Δ
sql/utils/ssh_tunnel.py 35.00% <35.00%> (ø)
sql/engines/__init__.py 70.37% <50.00%> (-1.98%) ⬇️
sql/models.py 96.33% <94.44%> (-0.07%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 424479f...62a9585. Read the comment docs.

@LeoQuote
Copy link
Collaborator

初始化脚本还是要的, 要用sql语句改一下库的

@EliasChiang
Copy link
Contributor Author

初始化脚本还是要的, 要用sql语句改一下库的

我在此处是删除了我新增的一个初始化sql,因为原先是一对多所以必须写一条初始化数据,现在已经不需要就删除了。

@LeoQuote
Copy link
Collaborator

哦哦, 那后续还是要加一下tunnel 表以及instance 表 的改表建表语句

@hhyo hhyo added the enhancement New feature or request label May 24, 2020
@EliasChiang
Copy link
Contributor Author

已添加tunnel 表以及instance 表 的改表建表语句,文件为 v1.7.8_v1.7.9.sql
并新增了密钥认证隧道的方式,默认会使用密钥,密钥地址为空时使用密码。

sql/utils/ssh_tunnel.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@LeoQuote LeoQuote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题不大, 几个小点修改完就能merge了

sql/engines/__init__.py Outdated Show resolved Hide resolved
sql/utils/ssh_tunnel.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@LeoQuote LeoQuote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅 不好意思, 还有两个问题

  1. requirements 冲突
  2. 现在最新的已经到了 1.7.11 所以这个sql 语句还要变一下, 建议merge 下最新代码, 根据最新的版本更名一下.

@EliasChiang
Copy link
Contributor Author

已修改了冲突部分

Copy link
Collaborator

@LeoQuote LeoQuote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@LeoQuote LeoQuote merged commit de3149e into hhyo:master Jun 3, 2020
@LeoQuote
Copy link
Collaborator

LeoQuote commented Jun 3, 2020

merge 了, cc @hhyo

@stardemo
Copy link

所以这个功能没有界面可以配置么?

@EliasChiang
Copy link
Contributor Author

所以这个功能没有界面可以配置么?

已添加管理页面,在 管理后台>隧道配置 ,可以进行界面配置

@EliasChiang
Copy link
Contributor Author

所以这个功能没有界面可以配置么?

我想起来隧道配置的界面还没有realase,可以先参考:
#844

@stardemo
Copy link

ERROR   | Could not establish connection from ('127.0.0.1', 34510) to remote side of the tunnel

容器部署的,连接就出现这个错误

@EliasChiang
Copy link
Contributor Author

ERROR   | Could not establish connection from ('127.0.0.1', 34510) to remote side of the tunnel

容器部署的,连接就出现这个错误

可能是由于gunicorn工作模式造成的,需要修改为sync模式
可以参考
https://github.com/hhyo/archery/wiki/FAQ
配置SSH隧道后无法连接
需要在supervisord.conf 文件中 ,修改并发方式为sync模式,如下所示:
command=gunicorn -w 4 -k sync-b 127.0.0.1:8000 --timeout 600 archery.wsgi:application

@stardemo
Copy link

修为为sync还是有问题,我再看看

@stardemo
Copy link

确定到问题了,docker模式启动的使用脚本是startup.sh
我修改启动模式是改了项目里的supervisord.conf
改了startup.sh的脚本可以用了

@LeoQuote LeoQuote mentioned this pull request Sep 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ 功能建议 ] 建议在添加实例配置时,增加通过跳板机连接数据库的功能
4 participants