Skip to content

Commit 3d2c9ba

Browse files
author
Fuyuan.Chu
committed
feature(init): init commit
0 parents  commit 3d2c9ba

8 files changed

+546
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
*.csv
3+
*.db
4+
notice.sh
5+
var
6+
env

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Quick Start
2+
3+
``mailprovider.py`` 根据 [open-falcon](http://open-falcon.org/) 组件 ``sender`` 预定义好的规范实现的一个简单的[邮件报警接口](https://book.open-falcon.org/zh/quick_install/judge_components.html).
4+
5+
部署同样遵循 ``open-falcon`` 各个组件的打包部署方式, 组织结构如下:
6+
7+
```
8+
mailprovider
9+
`+- env
10+
+- var
11+
+- control
12+
+- mailprovider.py
13+
+- gunicorn.conf
14+
+- wsgi.py
15+
+- requirements.txt
16+
+- install.sh
17+
```
18+
19+
# 安装 & 启动
20+
21+
1. 执行安装脚本进行环境的初始化. 该脚本会在同级目录下创建一个 python 虚拟环境,并安装所需依赖.
22+
23+
> sh install.sh
24+
25+
2. 执行控制脚本启动程序.
26+
27+
> sh control start
28+
29+
# 使用
30+
31+
* ``open-falcon````sender`` 中配置
32+
33+
> "mail": "http://ip_address:9988/mail"
34+
35+
* 脚本或人工调用
36+
37+
> url=http://ip_address:9988/mail
38+
39+
> curl -X POST $url -d "content=xxx&tos=[email protected]&subject=xxx"
40+
41+
# 附加功能
42+
43+
为帮助记录每天运维发布系统的频次,并定时自动邮件通知相关同事,增加了一些额外的接口. 对这类接口使用 HTTP Basic Authentication 做访问认证.
44+
45+
``POST /db/deployInfo``
46+
47+
参数 ``date`` ``time`` ``target_host`` ``project`` ``app_name`` ``deploy_host`` ``user``
48+
49+
该接口会保存发布记录到 sqlite 数据库中,并且带有是否已发送通知的标记.
50+
51+
``POST /db/notice``
52+
53+
参数 ``tos``
54+
55+
该接口会将库中所有未发送过通知的记录通过 ``/mail`` 接口发送出去,且标记为已发送.
56+
57+
PS 该脚本中还有一个 /deployInfo 接口为 /db/deployInfo 的原型,使用 csv 文件作为本地数据存储格式.并通过外部脚本实现发送通知功能.

control

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
3+
WORKSPACE=$(cd $(dirname $0)/; pwd)
4+
cd $WORKSPACE
5+
6+
mkdir -p var
7+
8+
module=mail-provider
9+
app=falcon-$module
10+
pidfile=var/app.pid
11+
logfile=var/app.log
12+
13+
function check_pid() {
14+
if [ -f $pidfile ];then
15+
pid=`cat $pidfile`
16+
if [ -n $pid ]; then
17+
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
18+
return $running
19+
fi
20+
fi
21+
return 0
22+
}
23+
24+
function start() {
25+
source env/bin/activate
26+
hash gunicorn 2>&- || { echo >&2 "I require gunicorn but it's not installed. Aborting."; exit 1; }
27+
28+
check_pid
29+
running=$?
30+
if [ $running -gt 0 ];then
31+
echo -n "$app now is running already, pid="
32+
cat $pidfile
33+
return 1
34+
fi
35+
36+
gunicorn -c gunicorn.conf mailprovider:app -D -t 6000 --pid $pidfile --error-logfile var/error.log --log-level info \
37+
--enable-stdio-inheritance --log-file=$logfile --access-logfile var/access.log &> $logfile
38+
sleep 1
39+
echo -n "$app started..., pid="
40+
cat $pidfile
41+
}
42+
43+
function stop() {
44+
pid=`cat $pidfile`
45+
kill $pid
46+
echo "$app quit..."
47+
}
48+
49+
function kill9() {
50+
pid=`cat $pidfile`
51+
kill -9 $pid
52+
echo "$app stoped..."
53+
}
54+
55+
function restart() {
56+
stop
57+
sleep 2
58+
start
59+
}
60+
61+
function status() {
62+
check_pid
63+
running=$?
64+
if [ $running -gt 0 ];then
65+
echo -n "$app now is running, pid="
66+
cat $pidfile
67+
else
68+
echo "$app is stoped"
69+
fi
70+
}
71+
72+
function tailf() {
73+
tail -f var/*.log
74+
}
75+
76+
function show_version() {
77+
cat gitversion
78+
}
79+
80+
function pack() {
81+
git log -1 --pretty=%h > gitversion
82+
file_list="README.md control frame gunicorn.conf metrics pip_requirements.txt scripts web wsgi.py"
83+
find -name "*.pyc" | xargs rm -rf
84+
gitversion=`cat gitversion`
85+
tar -zcf $app-$gitversion.tar.gz gitversion $file_list
86+
}
87+
88+
function help() {
89+
echo "$0 start|stop|restart|status|tail|kill9|version|pack"
90+
}
91+
92+
if [ "$1" == "" ]; then
93+
help
94+
elif [ "$1" == "stop" ];then
95+
stop
96+
elif [ "$1" == "kill9" ];then
97+
kill9
98+
elif [ "$1" == "start" ];then
99+
start
100+
elif [ "$1" == "restart" ];then
101+
restart
102+
elif [ "$1" == "status" ];then
103+
status
104+
elif [ "$1" == "tail" ];then
105+
tailf
106+
elif [ "$1" == "pack" ];then
107+
pack
108+
elif [ "$1" == "version" ];then
109+
show_version
110+
else
111+
help
112+
fi

gunicorn.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
workers = 4
2+
bind = '0.0.0.0:9988'
3+
proc_name = 'mailprovider'
4+
pidfile = 'var/app.pid'
5+
limit_request_field_size = 0
6+
limit_request_line = 0

install.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
#########################################################################
3+
# File Name: install.sh
4+
# Author: Chufuyuan
5+
6+
# Created Time: Tue Dec 20 23:50:23 2016
7+
#########################################################################
8+
9+
WS=$(cd $(dirname $0);pwd)
10+
11+
pip install virtualenv
12+
13+
cd $WS
14+
virtualenv env
15+
16+
. env/bin/activate
17+
pip install -r requirements.txt

0 commit comments

Comments
 (0)