您的位置:首页 > 编程语言 > Java开发

Java Web Services (1) - 第1章 Web服务快速入门

2016-01-03 17:33 731 查看
SCRIPTS_DIR=/Users/liuzhaofu/opus-dev/product/tools/dev
PRODUCT_DIR=/Users/liuzhaofu/opus-dev/product
NODE_MODULES_DIR=/Users/liuzhaofu/opus-dev/product/node/common
NODE_SERVICES_DIR=/Users/liuzhaofu/opus-dev/product/node/services
NODE_CONNECTORS_DIR=/Users/liuzhaofu/opus-dev/product/node/connectors
NODE_AUX_DIR=/Users/liuzhaofu/opus-dev/product/node/aux
WEB_UI_DIR=/Users/liuzhaofu/opus-dev/product/web/ui
OSSA_ASSETS_DIR=/Users/liuzhaofu/opus-dev/product/assets

OSSA_WORKING_DIR=/Users/liuzhaofu/ossa-work

/Users/liuzhaofu/ossa-work/logs
/Users/liuzhaofu/ossa-work/pkg

OSSA_APPS_DIR=/Users/liuzhaofu/ossa-work/apps
SCHEMA_FOLDER=/Users/liuzhaofu/opus-dev/product/languages/schemas/v0.1
API_PATH=/Users/liuzhaofu/opus-dev/product/node/services/common/api

1.1 Web服务杂项
1.2 Web服务有什么好处
1.3 Web服务和面向服务的架构
1.4 Web服务简史
  1.4.1 从DCE/RPC到XML-RPC
  1.4.2 分布式对象架构:Java示例
  1.4.3 用Web服务来救援
1.5 什么是REST
  1.5.1 动词和不透明名词
1.6 回顾HTTP请求和响应

  HTTP响应头示例: (http://conkeyn.iteye.com/blog/687027)

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cokkie: JSESSIONID=...; Path=/predictions/;
Content-Type: text/html;charset=ISO-8895-1
Transfer-Encoding: chunked


1.7 HTTP作为API
  1.7.1 用Java编写的两个HTTP客户端
1.8 第一个REST式的例子
  1.8.1 Predictions Web服务的工作原理
  1.8.2 针对Predictions Web服务的客户端
1.9 为什么REST式Web服务要使用servlet
1.10 下一步是什么

#!/bin/bash

#
# OSSA startup script
# Copyright(c) 2015 Oracle Corporation
# author: Hemant Raju <hemant.raju@oracle.com>
#

####################################################################
#Script to start OSSA in dev env on a single machine setup#
#
####################################################################

echo "#############################################################"
echo "OSSA startup script"
echo "#############################################################"

#############################################################
#Initilization                                              #
#############################################################

log_info() {
echo "INFO: $1"
}

log_warn() {
echo "WARN: $1"
}

log_error() {
echo "ERRR: $1"
}

init() {
OSSA_ENABLE_ELK=true

currentDir=`pwd`
SCRIPTS_DIR=$currentDir

#All env variables that have fixed values must be defined in ossa.env
#env variables that are calculated must be defined in this start script.
source $SCRIPTS_DIR/ossa.env
if [[ -z $NIMBULA_ENV || "$NIMBULA_ENV" == "false" ]]; then
#User specific settings
if [ -f "${HOME}/.ossa_profile" ]; then
log_info ".ossa_profile exists. Sourcing from .ossa_profile"
source ${HOME}/.ossa_profile
else
log_info ".ossa_profile does not exist. Creating one with defaults"
cp $SCRIPTS_DIR/.ossa_profile_template ${HOME}/.ossa_profile
source ${HOME}/.ossa_profile
fi
fi

PRODUCT_DIR=${currentDir//\/tools\/dev}
log_info "Product dir - $PRODUCT_DIR"

NODE_MODULES_DIR=$PRODUCT_DIR/node/common
log_info "Node modules dir - $NODE_MODULES_DIR"

NODE_SERVICES_DIR=$PRODUCT_DIR/node/services
log_info "Node services dir - $NODE_SERVICES_DIR"

NODE_CONNECTORS_DIR=$PRODUCT_DIR/node/connectors
log_info "Node connectors dir - $NODE_CONNECTORS_DIR"

NODE_AUX_DIR=$PRODUCT_DIR/node/aux
log_info "Node Auxilliary dir - $NODE_AUX_DIR"

WEB_UI_DIR=$PRODUCT_DIR/web/ui
log_info "Web UI dir - $WEB_UI_DIR"

OSSA_ASSETS_DIR=$PRODUCT_DIR/assets

SERVICE_PACKAGE_JSON_FILE=package.json
SERVICE_PORT_NUMBER=8550
SERVICE_HOST_NAME=localhost

if [ -z "$OSSA_WORKING_DIR" ]; then
log_warn "OSSA_WORKING_DIR env variable not defined. Defaulting to <user.home>/ossa-work"
USER_HOME=${HOME}
OSSA_WORKING_DIR="$USER_HOME/ossa-work"
eval export OSSA_WORKING_DIR=$OSSA_WORKING_DIR
fi

if [ ! -d "$OSSA_WORKING_DIR" ]; then
log_info "Creating OSSA working dir $OSSA_WORKING_DIR"
mkdir $OSSA_WORKING_DIR
mkdir $OSSA_WORKING_DIR/logs
fi

if [ ! -d "$OSSA_WORKING_DIR/logs" ]; then
mkdir $OSSA_WORKING_DIR/logs
fi

if [ ! -d "$OSSA_WORKING_DIR/pkg" ]; then
mkdir $OSSA_WORKING_DIR/pkg
fi
OSSA_PKG_DIR=$OSSA_WORKING_DIR/pkg

if [ ! -d "$OSSA_WORKING_DIR/apps" ]; then
mkdir $OSSA_WORKING_DIR/apps
fi

if [[ ! -z $NIMBULA_ENV && "$NIMBULA_ENV" == "true" ]]; then
log_info ".ossa_profile does not exist. Creating one with defaults under ${OSSA_WORKING_DIR}"
cp $SCRIPTS_DIR/.ossa_profile_template ${OSSA_WORKING_DIR}/.ossa_profile
source ${OSSA_WORKING_DIR}/.ossa_profile
fi

OSSA_APPS_DIR=$OSSA_WORKING_DIR/apps

export SCHEMA_FOLDER=$PRODUCT_DIR/languages/schemas/v0.1
export API_PATH=$PRODUCT_DIR/node/services/common/api

#if [ ! -d "$USERCONTEXTSTORE_SAMPLE_DIR" ]; then
#    mkdir $USERCONTEXTSTORE_SAMPLE_DIR
#fi

#if [ ! -d "$RECIPESTORE_SAMPLE_DIR" ]; then
#    mkdir $RECIPESTORE_SAMPLE_DIR
#fi

#if [ ! -d "$RECIPECATALOGSTORE_SAMPLE_DIR" ]; then
#    mkdir $RECIPECATALOGSTORE_SAMPLE_DIR
#fi

OS=`uname -s`
if [[ "$OS" == "Linux" ]]; then
log_info "Running on $OS, adding /usr/sbin & /usr/local/packages/aime/ias to the path"
export PATH=$PATH:/usr/sbin:/usr/local/packages/aime/ias
if [[ -n $NODEJS_HOME ]]; then
export PATH=$PATH:$NODEJS_HOME/bin
fi
if [[ -n $MONGO_HOME ]]; then
export PATH=$PATH:$MONGO_HOME/bin
fi
fi

if [ "$OS" == "Linux" ]; then
eval export REVERSEPROXY_HOST=`hostname`
fi

if [ "$DB_TYPE" == "oracle" ]; then
FINAL_TRY_SLEEP_TIME=20
else
FINAL_TRY_SLEEP_TIME=10
fi

log_info "OSSA working dir(logs, db, etc) - $OSSA_WORKING_DIR"
}

delete_logs() {
log_info "Deleting $OSSA_WORKING_DIR/logs/-"
if [ -d "$OSSA_WORKING_DIR/logs" ]; then
rm -f $OSSA_WORKING_DIR/logs/-
fi
}

cleanup() {
log_info "Stopping services..."
$SCRIPTS_DIR/stop

delete_logs

#log_info "Deleting $USERCONTEXTSTORE_SAMPLE_DIR"
#if [ -d "$USERCONTEXTSTORE_SAMPLE_DIR" ]; then
#     rm -f $USERCONTEXTSTORE_SAMPLE_DIR/-
#fi

#log_info "Deleting $RECIPESTORE_SAMPLE_DIR"
#if [ -d "$RECIPESTORE_SAMPLE_DIR" ]; then
#    rm -rf $RECIPESTORE_SAMPLE_DIR/-
#fi

#log_info "Deleting $RECIPECATALOGSTORE_SAMPLE_DIR"
#if [ -d "$RECIPECATALOGSTORE_SAMPLE_DIR" ]; then
#    rm -f $RECIPECATALOGSTORE_SAMPLE_DIR/-
#fi

log_info "Stopping MongoDB. Deleting $OSSA_WORKING_DIR/mongodb"
pkill -f "mongod"
sleep 3
mongoDBProcess=`pgrep mongod`
if [ "mongodrunning$mongoDBProcess" == "mongodrunning" ]; then
log_info "MongoDB stopped"
else
sleep 5
fi
if [ -d "$OSSA_WORKING_DIR/mongodb" ]; then
rm -rf $OSSA_WORKING_DIR/mongodb
fi

if [ -d "$OSSA_WORKING_DIR/nginx" ]; then
rm -rf $OSSA_WORKING_DIR/nginx
fi

if [[ -z $NIMBULA_ENV || "$NIMBULA_ENV" == "false" ]]; then
log_info "Stopping NGINX. Deleting $OSSA_WORKING_DIR/nginx"

if [ "$OS" == "Linux" ]; then
if [ -d "/etc/nginx/ssl" ]; then
log_info "Deleting /etc/nginx/ssl folder"
run_as_root "rm -rf /etc/nginx/ssl"
fi
run_as_root "pkill nginx"
else
pkill nginx
fi
fi

if [ "$OSSA_ENABLE_ELK" == "true" ]; then
$SCRIPTS_DIR/elk.sh -cleanup $OSSA_WORKING_DIR
fi

}

#############################################################
# Node Service functions                                          #
#############################################################
get_service_name() {
local serviceFolder=$1
local validateServiceName=$2
unset SERVICE_NAME
unset SERVICE_VERSION
unset serviceName

if [ ! -f "$serviceFolder/$SERVICE_PACKAGE_JSON_FILE" ]; then
log_error "Missing $SERVICE_PACKAGE_JSON_FILE file under $serviceFolder"
return 0
fi

serviceName=`python $SCRIPTS_DIR/lib/getservicename.py package.json`
if [ "serviceName$serviceName" == "serviceName" ]; then
log_error "Service 'name' is not set in $serviceFolder/$SERVICE_PACKAGE_JSON_FILE"
return 0
fi

if [ "$validateServiceName" == "true" ]; then
serviceNameValid=`echo "$serviceName" | tr -d '-' | tr -d ' '`
if [ "$serviceNameValid" != "$serviceName" ]; then
log_error "Service 'name' in $serviceFolder/$SERVICE_PACKAGE_JSON_FILE has invalid characters"
return 0
fi
fi

SERVICE_NAME=`echo "$serviceName" | tr '[:lower:]' '[:upper:]'`

return 1
}

setup_service_env() {
local serviceFolder=$1
if [ -z $2 ]; then
local servicePath=$NODE_SERVICES_DIR/$serviceFolder
else
local servicePath=$2/$serviceFolder
fi

cd $servicePath
get_service_name $servicePath

envHostVar=`env | grep ${SERVICE_NAME}_SERVICE_HOST`
envPortVar=`env | grep ${SERVICE_NAME}_SERVICE_PORT`
if [ "host$envHostVar" != "host" ]; then
if [ "port$envPortVar" != "port" ]; then
#log_info "Env variables ${SERVICE_NAME}_SERVICE_HOST and ${SERVICE_NAME}_SERVICE_PORT already defined"
#log_info $envHostVar
#log_info $envPortVar
return 1
fi
fi

local lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`

while [ "portavailable$lsofRet" != "portavailable" ];
do
log_warn "Port $SERVICE_PORT_NUMBER not available. Trying next available port...."
SERVICE_PORT_NUMBER=`expr $SERVICE_PORT_NUMBER + 1`
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
done

log_info "Port $SERVICE_PORT_NUMBER is available"

eval export ${SERVICE_NAME}_SERVICE_PORT=$SERVICE_PORT_NUMBER
eval export ${SERVICE_NAME}_SERVICE_HOST=$SERVICE_HOST_NAME

#log_info "${SERVICE_NAME}_SERVICE_PORT=$SERVICE_PORT_NUMBER"
#log_info "${SERVICE_NAME}_SERVICE_HOST=$SERVICE_HOST_NAME"

#echo "export ${SERVICE_NAME}_SERVICE_PORT=$SERVICE_PORT_NUMBER" >> $SCRIPTS_DIR/ossa.env
#echo "export ${SERVICE_NAME}_SERVICE_HOST=$SERVICE_HOST_NAME" >> $SCRIPTS_DIR/ossa.env

SERVICE_PORT_NUMBER=`expr $SERVICE_PORT_NUMBER + 1`

return 1
}

separator() {
echo
echo "**********************************************************************************"
}

start_node_service() {

local serviceFolder=$1
if [ -z $2 ]; then
local servicePath=$NODE_SERVICES_DIR/$serviceFolder
else
local servicePath=$2/$serviceFolder
fi

if [ "$3" == "--no-validation" ]; then
local validateServiceName=false
local portCheck=false
else
local validateServiceName=true
local portCheck=true
fi

separator
log_info "Service $serviceFolder - starting..."

cd $servicePath
get_service_name $servicePath $validateServiceName

if [ $? == 1 ]; then
SERVICE_PORT_NUMBER=`env | grep ${SERVICE_NAME}_SERVICE_PORT | awk -F= '{ print $NF }'`
if [ "$portCheck" == "true" ]; then
local serviceUid=OSSA_SERVICE_${SERVICE_NAME}_$SERVICE_PORT_NUMBER
else
local serviceUid=OSSA_SERVICE_${SERVICE_NAME}
fi

local ossaServiceProcess=`pgrep -f $serviceUid`
if [ "ossaServiceRunning$ossaServiceProcess" != "ossaServiceRunning" ]; then
log_info "$serviceUid already running. Skipping"
return 1
fi

if [ -f $OSSA_WORKING_DIR/logs/$serviceName-stdout.log ]; then
rm -f $OSSA_WORKING_DIR/logs/$serviceName-stdout.log
fi
if [ -f $OSSA_WORKING_DIR/logs/$serviceName.log ]; then
rm -f $OSSA_WORKING_DIR/logs/$serviceName.log
fi

if [[ -z $NPM_INSTALL || "$NPM_INSTALL" == "true" ]]; then
log_info "Service $serviceFolder - npm install/update..."
npm config set registry https://artifactory-slc.oraclecorp.com/artifactory/api/npm/npm-virtual if [ -d $servicePath/node_modules ]; then
log_info "Service $serviceFolder - npm update..."
npm update --depth=3 1>>$OSSA_WORKING_DIR/logs/$serviceName-stdout.log 2>&1
else
log_info "Service $serviceFolder - npm install..."
npm install 1>>$OSSA_WORKING_DIR/logs/$serviceName-stdout.log 2>&1
fi
fi
if [ "$NPM_LINK" == "true" ]; then
log_info "Doing npm linking"
for k in `find node_modules -maxdepth 1 -type d -name 'ossa*'`
do
dependency=$(basename $k)
log_info "Service $serviceFolder - linking to $dependency"
npm link $dependency
done
fi
log_info "Service $serviceFolder - npm start..."
npm start --uid $serviceUid 1>>$OSSA_WORKING_DIR/logs/$serviceName-stdout.log 2>&1 &

sleep 1

if [ "$portCheck" == "true" ]; then
sleep 1
local lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
sleep 3
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
log_warn "Service $SERVICE_NAME - waiting to bind to port $SERVICE_PORT_NUMBER"
sleep 10
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
log_warn "Service $SERVICE_NAME - still waiting to bind to port $SERVICE_PORT_NUMBER."
sleep $FINAL_TRY_SLEEP_TIME
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
if [ "$DB_TYPE" == "oracle" ]; then
log_warn "Service $SERVICE_NAME - possibly remote OracleDB connection, giving some more time..."
sleep $FINAL_TRY_SLEEP_TIME
fi
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
log_error "Service $SERVICE_NAME - failed to bind to port $SERVICE_PORT_NUMBER"
log_error
if [ -f $OSSA_WORKING_DIR/logs/$serviceName.log ]; then
cat $OSSA_WORKING_DIR/logs/$serviceName.log | grep error
else
cat $OSSA_WORKING_DIR/logs/$serviceName-stdout.log | grep Error
fi

exit 1;
fi
fi
fi
fi
fi
fi

local serviceProcess=`pgrep -f $serviceUid`
if [ "servicerunning$serviceProcess" == "servicerunning" ]; then
log_error "Service $SERVICE_NAME - failed to start"
exit 1;
fi
if [ "$portCheck" == "true" ]; then
log_info "Service $SERVICE_NAME - started successfully on port $SERVICE_PORT_NUMBER"
else
log_info "Service $SERVICE_NAME - started successfully"
fi
else
log_error "Service $serviceFolder - failed to setup env"
log_error "Service $serviceFolder - NOT STARTED"
exit 1
fi
}

#############################################################
# Seed OUDLs & ORELs
#############################################################
seed_oudls_orels() {
log_info "Seeding OUDLs and ORELs..."
local seedFile=$SCRIPTS_DIR/lib/ossa-seed-users.csv
local ossaUserOudl=$PRODUCT_DIR/languages/concrete/oudl/ossa.oudl
local orelFolder=$PRODUCT_DIR/languages/concrete/orel
local ossaOrelFolder=$PRODUCT_DIR/languages/concrete/orel/ossaUserRecipes

if [ -d "$USERCONTEXTSTORE_SAMPLE_DIR" ]; then
rm -f $USERCONTEXTSTORE_SAMPLE_DIR/-
else
log_error "$USERCONTEXTSTORE_SAMPLE_DIR not found"
exit 1
fi

if [ -d "$RECIPESTORE_SAMPLE_DIR" ]; then
rm -rf $RECIPESTORE_SAMPLE_DIR/-
else
log_error "$RECIPESTORE_SAMPLE_DIR not found"
exit 1
fi

awk -v seedOrelDir="$RECIPESTORE_SAMPLE_DIR" 'BEGIN {
FS=",";
}
{
userId=$2
gsub(" ","",userId);
gsub("@oracle.com","",userId);

cmd="mkdir "seedOrelDir"/"userId;
system(cmd);
}
END {

}
' $seedFile

#Public recipes
mkdir $RECIPESTORE_SAMPLE_DIR/public
cp $orelFolder/-.orel $RECIPESTORE_SAMPLE_DIR/public

for j in `find $ossaOrelFolder -name *.orel`
do
awk -v seedOrelDir="$RECIPESTORE_SAMPLE_DIR" -v ossaOrel="$j" 'BEGIN {
FS=",";
}
{
userName=$1
userId=$2
userFullId=$2
gsub(" ","",userId);
gsub("@oracle.com","",userId);
gsub(" ","",userFullId);

len=split(ossaOrel, a, "/");
ossaOrelFile=a[len];

cmd="cp "ossaOrel" "seedOrelDir"/"userId"/"ossaOrelFile".copy";
system(cmd);

cmd="sed -e '\''s/\"id\": \"ossa./\"id\": \""userFullId"_/'\''  -e '\''s/\"name\": \"/\"name\": \""userName" /'\'' -e '\''s/\"userId\": \"ossa\"/\"userId\": \""userFullId"\"/'\'' "seedOrelDir"/"userId"/"ossaOrelFile".copy > "seedOrelDir"/"userId"/"ossaOrelFile;
system(cmd);

cmd="rm "seedOrelDir"/"userId"/"ossaOrelFile".copy";
system(cmd);
}
END {

}
' $seedFile
done

awk -v seedOudlDir="$USERCONTEXTSTORE_SAMPLE_DIR" -v ossaUserOudl="$ossaUserOudl" 'BEGIN {
FS=",";
}
{
userName=$1
userFileName=$2
userFullId=$2
gsub(" ","",userFileName);
gsub("@oracle.com","",userFileName);
gsub(" ","",userFullId);

cmd="cp "ossaUserOudl" "seedOudlDir"/"userFileName;
system(cmd);

cmd="sed -e '\''s/\"id\": \"ossa\"/\"id\": \""userFullId"\"/'\''  -e '\''s/\"name\": \"ossa-user\"/\"name\": \""userName"\"/'\'' "seedOudlDir"/"userFileName " > "seedOudlDir"/"userFileName".oudl";
system(cmd);

cmd="rm "seedOudlDir"/"userFileName;
system(cmd);
}
END {

}
' $seedFile

log_info "Done seeding OUDLs and ORELs"
}

#############################################################
# Seed OUDLs & ORELs via REST API calls
#############################################################

init_seeding() {
export USERCONTEXTSTORE_CREATE_TABLE=true
export RECIPESTORE_CREATE_TABLE=true
export RECIPECATALOGSTORE_CREATE_TABLE=true
export CONNECTORSTORE_CREATE_TABLE=true
export OCDLSTORE_CREATE_TABLE=true
export SCHEDULERSTORE_CREATE_TABLE=true
export TIMER_CREATE_TABLE=true
}

seed_oudls_orels_restapi() {
separator
log_info "Seeding Users, Recipes, Catalogs..."

python $PRODUCT_DIR/tools/dev/lib/seeddb.py $PRODUCT_DIR

if [ $? != 0 ]; then
log_error "Seeding failed."
else
log_info "Seeding completed successfully."
fi
}

#############################################################
# Seed Recipe Catalog
#############################################################
seed_recipe_catalog() {
log_info "Seeding recipe catalog..."

local seedFile=$SCRIPTS_DIR/lib/ossa-seed-users.csv
local ossaMyRecipeCatalog=$PRODUCT_DIR/languages/concrete/catalog/ossaMyRecipes.json
local recipeCatalogDir=$PRODUCT_DIR/languages/concrete/catalog

if [ -d "$RECIPECATALOGSTORE_SAMPLE_DIR" ]; then
rm -f $RECIPECATALOGSTORE_SAMPLE_DIR/-
else
log_error "$RECIPECATALOGSTORE_SAMPLE_DIR not found"
exit 1
fi

recipeIds=`python $SCRIPTS_DIR/lib/getrecipeids.py $RECIPESTORE_SAMPLE_DIR/public`
sed -e s/@RECIPE_IDS@/$recipeIds/ $recipeCatalogDir/public.json > $RECIPECATALOGSTORE_SAMPLE_DIR/public.json

awk -v seedRecipeCatalogDir="$RECIPECATALOGSTORE_SAMPLE_DIR" -v ossaMyRecipeCatalog="$ossaMyRecipeCatalog" -v scriptsDir="$SCRIPTS_DIR" -v seedOrelDir="$RECIPESTORE_SAMPLE_DIR" 'BEGIN {
FS=",";
}
{
seedOrelFolderName=$2
userName=$1
userFileName=$1
userFullId=$2

gsub(" ","",seedOrelFolderName);
gsub("@oracle.com","",seedOrelFolderName);
gsub(" ","",userFileName);
gsub(" ","",userFullId);

seedMyRecipeFile=seedRecipeCatalogDir"/"userFileName"MyRecipes.json"
cmd="python "scriptsDir"/lib/getrecipeids.py "seedOrelDir"/"seedOrelFolderName
cmd | getline recipeIds
close(cmd)

cmd="sed -e '\''s/ossa/"userFullId"/'\''  -e '\''s/OSSA/"userName"/'\'' -e '\''s/@RECIPE_IDS@/"recipeIds"/'\'' "ossaMyRecipeCatalog" > "seedMyRecipeFile;
cmd="sed -e '\''s/ossa/"userFullId"/'\''  -e '\''s/OSSA/"userName"/'\'' -e '\''s/@RECIPE_IDS@/"recipeIds"/'\'' "ossaMyRecipeCatalog" > "seedMyRecipeFile;
system(cmd);
}
END {

}
' $seedFile

log_info "Done seeding recipe catalog"
}

#############################################################
# Backend Database MongoDB/Oracle NoSQL
#############################################################
start_mongodb() {
if [ ! -d "$OSSA_WORKING_DIR/mongodb" ]; then
mkdir $OSSA_WORKING_DIR/mongodb
fi

if [ ! -d "$OSSA_WORKING_DIR/mongodb/db" ]; then
mkdir $OSSA_WORKING_DIR/mongodb/db
fi

if [ ! -d "$OSSA_WORKING_DIR/mongodb/logs" ]; then
mkdir $OSSA_WORKING_DIR/mongodb/logs
fi
mongoDBProcess=`pgrep mongod`
if [ "mongodrunning$mongoDBProcess" == "mongodrunning" ]; then
log_warn "MongoDB not running. Attempting to start MongoDB..."
if ! type "mongod" > /dev/null; then
log_error "mongod executable not found"
log_info "Install MongoDB. If already installed, make sure mongod executable is in PATH."
exit 1
fi
mongod --fork --dbpath $OSSA_WORKING_DIR/mongodb/db --logpath $OSSA_WORKING_DIR/mongodb/logs/mongodb.log
sleep 10
mongoDBProcess=`pgrep mongod`
if [ "mongodrunning$mongoDBProcess" == "mongodrunning" ]; then
log_error "Unable to start MongoDB."
exit 1;
fi
log_info "MongoDB started successfully"
else
log_info "MongoDB already running"
fi
}

#############################################################
# NGINX
#############################################################
start_nginx() {

if [ "$OS" == "Linux" ]; then
if [ ! -f "/etc/nginx/ssl/nginx.crt" ]; then
if [ ! -d "/etc/nginx/ssl" ]; then
log_info "Creating /etc/nginx/ssl folder"
run_as_root "mkdir /etc/nginx/ssl"
fi
local serverName=`hostname`
local genCert="openssl req \
-new \
-newkey rsa:4096 \
-days 365 \
-nodes \
-x509 \
-subj \"/C=US/ST=CA/L=Redwood City/O=Oracle Corporation/CN=$serverName\" \
-keyout /etc/nginx/ssl/nginx.key \
-out /etc/nginx/ssl/nginx.crt"
echo $genCert
run_as_root "$genCert"
fi
fi

if [ ! -d "$OSSA_WORKING_DIR/nginx/logs" ]; then
mkdir $OSSA_WORKING_DIR/nginx/logs
fi

nginxProcess=`pgrep nginx`
if [ "nginxrunning$nginxProcess" == "nginxrunning" ]; then
log_warn "NGINX not running. Attempting to start NGINX..."
if ! type "nginx" > /dev/null; then
log_error "nginx executable not found"
log_info "Do 'brew install nginx' to install nginx. If already installed, make sure nginx is in PATH."
exit 1
fi

local nginxStart="nginx -c $OSSA_WORKING_DIR/nginx/config/nginx.conf"
if [ "$OS" == "Linux" ]; then
run_as_root "$nginxStart"
else
$nginxStart
fi

sleep 5
nginxProcess=`pgrep nginx`
if [ "nginxrunning$nginxProcess" == "nginxrunning" ]; then
log_error "Unable to start NGINX."
exit 1;
fi
log_info "NGINX started successfully"
else
log_info "NGINX already running"
fi
}

#############################################################
#
# Web UI
#
#############################################################
start_webui() {
if [ ! -d "$WEB_UI_DIR/public/js/libs/oj" ]; then
if [ ! -f "$OSSA_WORKING_DIR/pkg/oraclejet.zip" ]; then
log_info "Downloading JET..."
cd $OSSA_WORKING_DIR/pkg
curl -LOk http://jet.us.oracle.com/1.1.1/code/oraclejet.zip fi
unzip $OSSA_WORKING_DIR/pkg/oraclejet.zip -d $WEB_UI_DIR/public
fi

SERVICE_PORT_NUMBER=8550
setup_service_env ui $PRODUCT_DIR/web
start_node_service ui $PRODUCT_DIR/web
}

#############################################################
#
#Initilize the dependent node modules
#
#############################################################
init_node_modules() {
separator

if [ "$NPM_LINK" != "true" ]; then
log_info "Skipping ossa node modules initialization as NPM_LINK is not set to true"
return
fi

log_info "Initializing dependent node modules..."

if [ "$NPM_LINK" == "true" ]; then
log_info "Doing npm linking"
for i in `find $NODE_MODULES_DIR -maxdepth 2 -name package.json`
do
moduleFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`
if [[ $moduleFolder != 'ossa-ocdl-validator' ]]; then
log_info "Module $moduleFolder - linking"
cd $NODE_MODULES_DIR/$moduleFolder
npm link
fi
done
fi

for j in `find $NODE_MODULES_DIR -maxdepth 2 -name package.json`
do
moduleFolder=`echo $j | awk '{
len=split($1, a, "/")
print a[len-1]
}'`
log_info "Module $moduleFolder - initializing"
cd $NODE_MODULES_DIR/$moduleFolder
if [[ -z $NPM_INSTALL || "$NPM_INSTALL" == "true" ]]; then
npm config set registry https://artifactory-slc.oraclecorp.com/artifactory/api/npm/npm-virtual npm install
fi

if [ $moduleFolder != 'ossa-ocdl-validator' ]; then
if [ "$NPM_LINK" == "true" ]; then
log_info "Doing npm linking"
for k in `find node_modules -maxdepth 1 -type d -name 'ossa*'`
do
dependency=$(basename $k)
log_info "Module $moduleFolder - linking to $dependency"
npm link $dependency
done
fi
fi
done

log_info "Done initializing dependent node modules"
}

#############################################################
#
#Start the node.js microservices
#
#############################################################
start() {

if [ "$DB_TYPE" != "oracle" ]; then
start_mongodb
fi

#Before starting nginx, create the config files. Useful on nimbula instance machines, where nginx has to be started manually
if [ ! -d "$OSSA_WORKING_DIR/nginx" ]; then
mkdir $OSSA_WORKING_DIR/nginx
fi
if [ ! -d "$OSSA_WORKING_DIR/nginx/config" ]; then
mkdir $OSSA_WORKING_DIR/nginx/config
fi
if [ ! -f "$OSSA_WORKING_DIR/nginx/config/nginx.conf" ]; then
log_info "Creating nginx.conf file under $OSSA_WORKING_DIR/nginx/config"
if [ "$OS" == "Linux" ]; then
local serverName=`hostname`
sed -e s,'<%=@OSSA_WORKING_DIR%>',$OSSA_WORKING_DIR,g -e  s,'<%=@SCRIPTS_DIR%>',$SCRIPTS_DIR,g  -e s,'<%=@OSSA_ASSETS_DIR%>',$OSSA_ASSETS_DIR,g  -e  s,'<%=@SERVER_NAME%>',$serverName,g $SCRIPTS_DIR/nginx-linux.conf > $OSSA_WORKING_DIR/nginx/config/nginx.conf
else
sed -e s,@OSSA_WORKING_DIR@,$OSSA_WORKING_DIR,g -e  s,@SCRIPTS_DIR@,$SCRIPTS_DIR,g -e s,@OSSA_ASSETS_DIR@,$OSSA_ASSETS_DIR,g $SCRIPTS_DIR/nginx.conf > $OSSA_WORKING_DIR/nginx/config/nginx.conf
fi
fi

if [[ -z $NIMBULA_ENV || "$NIMBULA_ENV" == "false" ]]; then
start_nginx
fi

if [ "$OSSA_LOGGER_LOGSTASH" == "true" ] && [ "$OSSA_ENABLE_ELK" == "true" ]; then
$SCRIPTS_DIR/elk.sh -start $PRODUCT_DIR $OSSA_WORKING_DIR $OSSA_PKG_DIR $OSSA_APPS_DIR
fi
start_webui
init_node_modules

separator
log_info "Setting env variables for node services..."

#Fix port number for API_GW as Web UI cannot read env variables.
SERVICE_PORT_NUMBER=8577
setup_service_env apigw

SERVICE_PORT_NUMBER=8551

#Main node services
for i in `find $NODE_SERVICES_DIR -maxdepth 2 -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`

if [ $serviceFolder != 'opus-connector' ] &&
[ $serviceFolder != 'ossa-pod-manager' ] &&
[ $serviceFolder != 'test' ]; then
setup_service_env $serviceFolder
fi
done

#Node Connectors dir
for i in `find $NODE_CONNECTORS_DIR -maxdepth 2 -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`

setup_service_env $serviceFolder $NODE_CONNECTORS_DIR
done

#Node Aux dir
for i in `find $NODE_AUX_DIR -maxdepth 2 -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`

#if [ $serviceFolder != 'statsd-connector' ]; then
#setup_service_env $serviceFolder $NODE_AUX_DIR
#fi
done

separator
log_info "Starting node services..."

for i in `find $NODE_SERVICES_DIR -maxdepth 2 -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`

if [ $serviceFolder != 'opus-connector' ] &&
[ $serviceFolder != 'ossa-pod-manager' ] &&
[ $serviceFolder != 'test' ]; then
start_node_service $serviceFolder
fi
done

separator
log_info "Starting built-in connectors..."

for i in `find $NODE_CONNECTORS_DIR -maxdepth 2 -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`
start_node_service $serviceFolder $NODE_CONNECTORS_DIR
done

#    separator
#    log_info "Starting Auxilliary node services..."
#
#    for i in `find $NODE_AUX_DIR -maxdepth 2 -name app.js `
#    do
#        serviceFolder=`echo $i | awk '{
#            len=split($1, a, "/")
#            print a[len-1]
#        }'`
#
#
#        start_node_service $serviceFolder $NODE_AUX_DIR --no-validation
#    done

separator
log_info "All log files are under $OSSA_WORKING_DIR/logs"
echo
}

############################################################
# Get Service Folder from Service Name
############################################################
get_service_folder() {
local searchFolder=$1
local searchServiceName=$2

for i in `find $searchFolder -maxdepth 2 -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
if [ "$pkgServiceName" == "$searchServiceName" ]; then
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`
break
fi
done
}

############################################################
# Do an action based on arguments
############################################################
init
log_info "Done initialization."
separator
#log_info "Environment variables...."
#log_info
#env
#separator
if [ "$1" == "--reinstall" ]; then
cleanup

for i in `find $NODE_SERVICES_DIR -maxdepth 2 -name app.js`
do
serviceFolder=`echo $i | awk '{
len=split($1, a, "/")
print a[len-1]
}'`

log_info "Deleting $NODE_SERVICES_DIR/$serviceFolder/node_modules"
rm -rf $NODE_SERVICES_DIR/$serviceFolder/node_modules
done

for j in `find $NODE_MODULES_DIR -maxdepth 2 -name package.json`
do
moduleFolder=`echo $j | awk '{
len=split($1, a, "/")
print a[len-1]
}'`

log_info "Deleting $NODE_MODULES_DIR/$moduleFolder/node_modules"
rm -rf $NODE_MODULES_DIR/$moduleFolder/node_modules
done

log_info "Deleting $WEB_UI_DIR/node_modules"
rm -rf $WEB_UI_DIR/node_modules

log_info "Done cleanup."
separator
log_info "Starting all services..."
init_seeding
start
seed_oudls_orels_restapi

exit 0
elif [ "$1" == "--seed" ]; then
cleanup
log_info "Done cleanup."

separator
log_info "Starting all services..."
init_seeding
start
seed_oudls_orels_restapi

exit 0
elif [ "$1" == "--seedonly" ]; then
NPM_INSTALL=false
init_seeding
#$SCRIPTS_DIR/restart usercontextstore recipestore recipecatalogstore connectorstore ocdlstore
seed_oudls_orels_restapi

exit 0
elif [ "$1" == "--update" ]; then
log_info "Starting all services."
start
exit 0
elif [ -z $1 ]; then
NPM_INSTALL=false
log_info "Starting all services"
log_warn "***npm update is disabled. You may not get the latest published dependencies."
sleep 3
start
exit 0
else
for j in $*
do
if [ "$j" == "webui" ]; then
setup_service_env ui $PRODUCT_DIR/web
start_node_service ui $PRODUCT_DIR/web
continue
fi

serviceFolder="no-service-found"
get_service_folder $NODE_SERVICES_DIR $j

if [ -d $NODE_SERVICES_DIR/$serviceFolder ]; then
setup_service_env $serviceFolder
start_node_service $serviceFolder
else
get_service_folder $NODE_CONNECTORS_DIR $j
if [ -d $NODE_CONNECTORS_DIR/$serviceFolder ]; then
setup_service_env $serviceFolder $NODE_CONNECTORS_DIR
start_node_service $serviceFolder $NODE_CONNECTORS_DIR
else
get_service_folder $NODE_AUX_DIR $j
if [ -d $NODE_AUX_DIR/$serviceFolder ]; then
#setup_service_env $serviceFolder $NODE_AUX_DIR
start_node_service $serviceFolder $NODE_AUX_DIR --no-validation
else
log_error "'$j' is not a valid service id. Valid service id list: "
for i in `find $NODE_SERVICES_DIR -maxdepth 2 -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
echo "  $pkgServiceName"
done
for i in `find $NODE_CONNECTORS_DIR -maxdepth 2 -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
echo "  $pkgServiceName"
done
for i in `find $NODE_AUX_DIR -maxdepth 2 -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
echo "  $pkgServiceName"
done
fi
fi
fi
done
exit 0
fi

# eof
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: