Skip to content

Commit

Permalink
Use With Blueprints
Browse files Browse the repository at this point in the history
  • Loading branch information
starsliao committed Jan 28, 2022
1 parent 9722135 commit 8d34008
Show file tree
Hide file tree
Showing 116 changed files with 199 additions and 166 deletions.
2 changes: 1 addition & 1 deletion docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ eth=$(ls /sys/class/net/ | grep -v "`ls /sys/devices/virtual/net/`")
ip=$(ip addr | grep $eth | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}' | head -1)
cd flask-consul
docker build -t flask-consul:latest .
cd ../nginx-consul
cd ../vue-consul
docker build -t nginx-consul:latest .

echo -e "\n\n自行编译的版本,注意修改docker-compose.yml中的镜像地址为本地仓库后再启动。"
Expand Down
4 changes: 2 additions & 2 deletions docker-push.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
vf=$1
vb=$2
vf=0.1.3
vb=0.2.0
docker login [email protected] registry.cn-shenzhen.aliyuncs.com

docker tag nginx-consul:latest registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:latest
Expand Down
2 changes: 2 additions & 0 deletions flask-consul/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
2 changes: 2 additions & 0 deletions flask-consul/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
5 changes: 3 additions & 2 deletions flask-consul/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM python:3-alpine
ADD . /
ADD . /flask
WORKDIR /flask
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
#&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
EXPOSE 2026
CMD ["python3","/flaskconsul.py"]
CMD ["python3","./manager.py"]
8 changes: 8 additions & 0 deletions flask-consul/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
from itsdangerous import TimedJSONWebSignatureSerializer

consul_token = os.environ.get('consul_token','a94d1ecb-81d3-ea0a-4dc8-5e6701e528c5')
consul_url = os.environ.get('consul_url','http://10.5.148.67:8500/v1')
admin_passwd = os.environ.get('admin_passwd','cass.007')
secret_key = os.environ.get('secret_key',consul_token)
s = TimedJSONWebSignatureSerializer(secret_key)
150 changes: 0 additions & 150 deletions flask-consul/flaskconsul.py

This file was deleted.

9 changes: 9 additions & 0 deletions flask-consul/manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
from flask import Flask
from views import login, blackbox

app = Flask(__name__)
app.register_blueprint(login.blueprint)
app.register_blueprint(blackbox.blueprint)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=2026)
61 changes: 61 additions & 0 deletions flask-consul/units/blackbox_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import requests,json
import sys
sys.path.append("..")
from config import consul_token,consul_url

headers = {'X-Consul-Token': consul_token}

def get_all_list(module,company,project,env):
module = f'and Meta.module=="{module}"' if module != '' else f'and Meta.module != ""'
company = f'and Meta.company=="{company}"' if company != '' else f'and Meta.company != ""'
project = f'and Meta.project=="{project}"' if project != '' else f'and Meta.project != ""'
env = f'and Meta.env=="{env}"' if env != '' else f'and Meta.env != ""'
url = f'{consul_url}/agent/services?filter=Service == blackbox_exporter {module} {company} {project} {env}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
info = response.json()
all_list = [i['Meta'] for i in info.values()]
module_list = sorted(list(set([i['module'] for i in all_list])))
company_list = sorted(list(set([i['company'] for i in all_list])))
project_list = sorted(list(set([i['project'] for i in all_list])))
env_list = sorted(list(set([i['env'] for i in all_list])))
return {'code': 20000,'all_list':all_list,'module_list':module_list,
'company_list':company_list,'project_list':project_list,'env_list':env_list}
else:
return {'code': 50000, 'data': f'{response.status_code}:{response.text}'}

def get_service():
response = requests.get(f'{consul_url}/agent/services?filter=Service == blackbox_exporter', headers=headers)
if response.status_code == 200:
info = response.json()
all_list = [i['Meta'] for i in info.values()]
module_list = sorted(list(set([i['module'] for i in all_list])))
company_list = sorted(list(set([i['company'] for i in all_list])))
project_list = sorted(list(set([i['project'] for i in all_list])))
env_list = sorted(list(set([i['env'] for i in all_list])))
return {'code': 20000,'all_list':all_list,'module_list':module_list,
'company_list':company_list,'project_list':project_list,'env_list':env_list}
else:
return {'code': 50000, 'data': f'{response.status_code}:{response.text}'}

def add_service(module,company,project,env,name,instance):
sid = f"{module}/{company}/{project}/{env}@{name}"
data = {
"id": sid,
"name": 'blackbox_exporter',
"tags": [module],
"Meta": {'module':module,'company':company,'project':project,'env':env,'name':name,'instance':instance}
}
reg = requests.put(f'{consul_url}/agent/service/register', headers=headers, data=json.dumps(data))
if reg.status_code == 200:
return {"code": 20000, "data": f"【{sid}】增加成功!"}
else:
return {"code": 50000, "data": f"{reg.status_code}{sid}{reg.text}"}

def del_service(module,company,project,env,name):
sid = f"{module}/{company}/{project}/{env}@{name}"
reg = requests.put(f'{consul_url}/agent/service/deregister/{sid}', headers=headers)
if reg.status_code == 200:
return {"code": 20000, "data": f"【{sid}】删除成功!"}
else:
return {"code": 50000, "data": f"{reg.status_code}{sid}{reg.text}"}
14 changes: 14 additions & 0 deletions flask-consul/units/token_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask_httpauth import HTTPTokenAuth
import sys
sys.path.append("..")
from config import s

auth = HTTPTokenAuth()

@auth.verify_token
def verify_token(token):
try:
data = s.loads(token)
except:
return False
return True
51 changes: 51 additions & 0 deletions flask-consul/views/blackbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from flask import Blueprint
from flask_restful import reqparse, Resource, Api
import sys
sys.path.append("..")
from units import token_auth,blackbox_manager

blueprint = Blueprint('blackbox',__name__)
api = Api(blueprint)

parser = reqparse.RequestParser()
parser.add_argument('module',type=str)
parser.add_argument('company',type=str)
parser.add_argument('project',type=str)
parser.add_argument('env',type=str)
parser.add_argument('name',type=str)
parser.add_argument('instance',type=str)
parser.add_argument('del_dict',type=dict)
parser.add_argument('up_dict',type=dict)

class GetAllList(Resource):
@token_auth.auth.login_required
def get(self):
args = parser.parse_args()
return blackbox_manager.get_all_list(args['module'],args['company'],args['project'],args['env'])

class BlackboxApi(Resource):
decorators = [token_auth.auth.login_required]
def get(self):
return blackbox_manager.get_service()
def post(self):
args = parser.parse_args()
return blackbox_manager.add_service(args['module'],args['company'],args['project'],
args['env'],args['name'],args['instance'])
def put(self):
args = parser.parse_args()
del_dict = args['del_dict']
up_dict = args['up_dict']
resp_del = blackbox_manager.del_service(del_dict['module'],del_dict['company'],
del_dict['project'],del_dict['env'],del_dict['name'])
resp_add = blackbox_manager.add_service(up_dict['module'],up_dict['company'],up_dict['project'],
up_dict['env'],up_dict['name'],up_dict['instance'])
if resp_del["code"] == 20000 and resp_add["code"] == 20000:
return {"code": 20000, "data": f"更新成功!"}
else:
return {"code": 50000, "data": f"更新失败!"}
def delete(self):
args = parser.parse_args()
return blackbox_manager.del_service(args['module'],args['company'],args['project'],args['env'],args['name'])

api.add_resource(GetAllList,'/api/blackbox/alllist')
api.add_resource(BlackboxApi, '/api/blackbox/service')
Empty file added flask-consul/views/consul.py
Empty file.
35 changes: 35 additions & 0 deletions flask-consul/views/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from flask import Blueprint
from flask_restful import reqparse, Resource, Api
import sys
sys.path.append("..")
from config import admin_passwd,s
from units import token_auth

blueprint = Blueprint('login',__name__)
api = Api(blueprint)

parser = reqparse.RequestParser()
parser.add_argument('username',type=str)
parser.add_argument('password',type=str)

class User(Resource):
@token_auth.auth.login_required
def get(self, user_opt):
if user_opt == 'info':
return {
"code": 20000,
"data": {"roles": ["admin"],"name": "admin","avatar": "/sl.png"}}
def post(self, user_opt):
if user_opt == 'login':
args = parser.parse_args()
username = args.get('username')
password = args.get('password')
if password == admin_passwd:
token = str(s.dumps(admin_passwd),encoding="utf-8")
return {"code": 20000,"data": {"token": "Bearer " + token}}
else:
return {"code": 40000, "data": "密码错误!"}
elif user_opt == 'logout':
return {"code": 20000,"data": "success"}

api.add_resource(User, '/api/user/<user_opt>')
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion nginx-consul/package.json → vue-consul/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:ci": "npm run lint && npm run test:unit"
},
"dependencies": {
"axios": "0.21.2",
"axios": "0.18.1",
"core-js": "3.6.5",
"element-ui": "2.15.7",
"file-saver": "2.0.1",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8d34008

Please sign in to comment.