Skip to content

Commit

Permalink
Merge pull request baidu#48 from minorcong/master
Browse files Browse the repository at this point in the history
add conda support
  • Loading branch information
李聪 authored May 3, 2018
2 parents 3f7e1f1 + 03d1fe4 commit 68193bf
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/curve/v1/api/data_dataName.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def post(self, dataName):
'traceId': '',
'data': {
'data': [(point[0] * 1000, point[1]) for point in thumb],
'name': data_name,
'name': 'thumb',
'type': 'line'
}
}, ensure_ascii=False))
Expand Down
6 changes: 4 additions & 2 deletions api/curve/v1/models/data_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
:copyright: (c) 2017-2018 by Baidu, Inc.
:license: Apache, see LICENSE for more details.
"""
import urllib

from app import db
from .common import (
auto_init,
Expand Down Expand Up @@ -45,8 +47,8 @@ def view(self):
"""
return {
'id': self.id,
'name': self.name,
'uri': '/v1/data/%s' % self.name,
'name': urllib.unquote(self.name.encode('utf-8')),
'uri': '/v1/data/%s' % urllib.unquote(self.name.encode('utf-8')),
'createTime': self.create_time * 1000,
'updateTime': self.update_time * 1000,
'labelRatio': self.label_ratio,
Expand Down
3 changes: 1 addition & 2 deletions api/curve/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ def str2time(time_str):
:param time_str:
:return:
"""
return int(
time.mktime(time.strptime(time_str[:10] + ' ' + time_str[11:19], RFCTimeFormat.time_format)))
return int(time.mktime(time.strptime(time_str[:10] + ' ' + time_str[11:19], RFCTimeFormat.time_format)))


E_TIME_FORMATTER = enum(unix=UnixTimeFormat, short=ShortTimeFormat, rfc=RFCTimeFormat)
16 changes: 8 additions & 8 deletions api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
requests==2.12.4 # don't change the order, latest requests lib has a critical bug
Flask-SQLAlchemy==2.3.2
Flask-RESTful==0.3.6
Flask-Cors==3.0.3
flask-compress==1.4.0
jsonschema==2.6.0
numpy==1.14.2
GitHub-Flask==3.2.0
uwsgi==2.0.17
Flask-SQLAlchemy>=2.3.2
Flask-RESTful>=0.3.6
Flask-Cors>=3.0.3
flask-compress>=1.4.0
jsonschema>=2.6.0
numpy>=1.14.0
GitHub-Flask>=3.2.0
uwsgi>=2.0.17
230 changes: 230 additions & 0 deletions control_conda.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#!/bin/bash

# System:
# 1. Darwin or Linux
# Python:
# 1. version: 2.7.3+/3.1.2+ is recommended
# 2. other:
# * python should belong to current user
# * pip is required
# Node.js:
# 1. version: 4.7.0+ is recommended
# 2. other:
# * npm is required

set -u
set -e

cd "$(dirname "$0")"
readonly G_ROOT_DIR=`pwd`
readonly G_WEB_DIR="${G_ROOT_DIR}/web"
readonly G_API_DIR="${G_ROOT_DIR}/api"

G_VERSION='none'
if [ -e .git ]; then
G_VERSION=`git rev-parse HEAD`
fi
G_CONDA=''


PS1='$'

cutoff() {
echo "============================================================="
}


help() {
echo "${0} <start|stop|reload|terminate|version>"
exit 1
}

version() {
if [ ${G_VERSION}x != 'x' ]; then
cutoff
echo "local Curve version: ${G_VERSION}"
cutoff
fi
}

check_web() {
readonly BUILD_PATH="${G_WEB_DIR}/build"
readonly BUILD_VERSION_FILE="${BUILD_PATH}/version"
BUILD_VERSION=''
if [ -e ${BUILD_VERSION_FILE} ]; then
BUILD_VERSION=`cat ${BUILD_VERSION_FILE}`
fi
readonly DEPLOY_PATH="${G_API_DIR}/curve/web"
readonly DEPLOY_VERSION_FILE="${DEPLOY_PATH}/version"
DEPLOY_VERSION=''
if [ -e ${DEPLOY_VERSION_FILE} ]; then
DEPLOY_VERSION=`cat ${DEPLOY_VERSION_FILE}`
fi

if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${DEPLOY_VERSION}x ]; then
return
fi

cutoff
echo "build web..."
if [ ${G_VERSION}x == 'x' -o ${G_VERSION}x != ${BUILD_VERSION}x ]; then
cd ${G_WEB_DIR}
npm install
npm run build
echo ${G_VERSION} > ${BUILD_VERSION_FILE}
fi
if [ -e ${DEPLOY_PATH} ]; then
rm -rf ${DEPLOY_PATH}
fi
mv ${BUILD_PATH} ${DEPLOY_PATH}
echo "web built."
cutoff
}

check_py() {
readonly VENV_VERSION_FILE="${G_ROOT_DIR}/pylib_version"
VENV_VERSION=''
if [ -e ${VENV_VERSION_FILE} ]; then
VENV_VERSION=`cat ${VENV_VERSION_FILE}`
fi

if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${VENV_VERSION}x ]; then
return
fi

cutoff
pip install -r ${G_API_DIR}/requirements.txt
echo ${G_VERSION} > ${VENV_VERSION_FILE}
cutoff
}

check_api() {
readonly SWAGGER_UI_DIR="${G_API_DIR}/curve/web/swagger-ui"
readonly SWAGGER_UI_VERSION_FILE="${SWAGGER_UI_DIR}/version"
SWAGGER_UI_VERSION=''
if [ -e ${SWAGGER_UI_VERSION_FILE} ]; then
SWAGGER_UI_VERSION=`cat ${SWAGGER_UI_VERSION_FILE}`
fi

if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${SWAGGER_UI_VERSION}x ]; then
return
fi

cutoff
echo "deploy api..."
cd ${G_ROOT_DIR}
pip install swagger-py-codegen==0.2.9
swagger_py_codegen --ui --spec -s doc/web_api.yaml api -p curve
deactivate
if [ -e ${G_API_DIR}/curve/web/swagger-ui ]; then
rm -rf ${G_API_DIR}/curve/web/swagger-ui
fi
mv ${G_API_DIR}/curve/static/swagger-ui ${G_API_DIR}/curve/web/
if [ -e ${G_API_DIR}/curve/web/static/v1 ]; then
rm -rf ${G_API_DIR}/curve/web/static/v1
fi
mv ${G_API_DIR}/curve/static/v1 ${G_API_DIR}/curve/web/static/
rm -rf ${G_API_DIR}/curve/static

patch ${G_API_DIR}/curve/web/swagger-ui/index.html ${G_ROOT_DIR}/opt/swagger-ui/index.html.patch
echo ${G_VERSION} > ${SWAGGER_UI_VERSION_FILE}
echo "api deployed."
cutoff
}

check_path() {
mkdir -p ${G_API_DIR}/log
}

check() {
check_web
check_py
check_api
check_path
}

start() {
if [ -e ${G_API_DIR}/uwsgi.pid ]; then
PID=`cat ${G_API_DIR}/uwsgi.pid`
if [ `ps -ef | fgrep uwsgi | fgrep ${PID} | wc -l` -gt 0 ]; then
echo "Curve is running."
return
fi
fi
check
if [ `ps -ef | fgrep uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then
ps -ef | fgrep uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9
fi
echo "start Curve..."
cd ${G_API_DIR}
uwsgi uwsgi.ini
echo "Curve started."
}

stop() {
echo "stop Curve..."
cd ${G_API_DIR}
[ -e uwsgi.pid ] && uwsgi --stop uwsgi.pid
echo "Curve stopped."
}

reload() {
check
if [ -e ${G_API_DIR}/uwsgi.pid ]; then
PID=`cat ${G_API_DIR}/uwsgi.pid`
if [ `ps -ef | fgrep uwsgi | fgrep ${PID} | wc -l` -gt 0 ]; then
echo "reload Curve..."
cd ${G_API_DIR}
uwsgi --reload uwsgi.pid
echo "Curve reloaded."
return
fi
fi
echo "clean Curve..."
if [ `ps -ef | fgrep uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then
ps -ef | fgrep uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9
fi
echo "start Curve..."
cd ${G_API_DIR}
uwsgi uwsgi.ini
echo "Curve reloaded."
}

terminate() {
echo "terminate Curve..."
if [ `ps -ef | fgrep uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then
ps -ef | fgrep uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9
echo "Curve terminated."
fi
echo "Curve is not running."
}

case "${1}" in
check)
version
check
;;
start)
version
start
;;
stop)
stop
;;
reload)
version
reload
;;
terminate)
terminate
;;
help)
help
;;
version)
version
;;
*)
help
;;
esac

0 comments on commit 68193bf

Please sign in to comment.