icflorescu / openshift-cartridge-nodejs

Custom cartridge for OpenShift providing the lastest version of Node.js.
ISC License
220 stars 136 forks source link

Log files location? #85

Open perqa opened 7 years ago

perqa commented 7 years ago

I installed this cartridge without problems - thanks for great software! But I can't find the log files anymore. Are there any, or if not, how can I enable some node logs similar to Openshift's originals, located in nodejs/logs ?

The reason I'm asking is that my application doesn't start automatically when pushing to Openshift, but it starts if I do it manually using the same command as in package.json.

During the automated startup, I see the following (logshifter not found):

remote: Preparing build for deployment        
remote: Deployment id is a4df8975        
remote: Activating deployment        
remote: CLIENT_MESSAGE: Starting Node.js application...        
remote: /var/lib/openshift/58ef631bf9d320e77axxxxxx/nodejs/bin/control: line 46: /usr/bin/logshifter: No such file or directory        
remote: CLIENT_RESULT: Node.js application started.        
remote: Result: success        
remote: Activation status: success        
remote: Deployment completed with status: success

However, even though it looks like everything went fine, the app is inaccessible (Service Unavailable) until I start it manually using "node server.js"

icflorescu commented 7 years ago

That's strange. Everything works fine on a test deployment I've just created minutes ago. /usr/bin/logshifter is present and the logs are created in ${OPENSHIFT_HOMEDIR}/app-root/logs/.

Just out of curiosity, what kind of account are you using?

The cartridge is tested and works perfectly on OpenShift Online, but it probably doesn't on OpenShift Enterprise, and most likely never will, since OpenShift didn't really provide any kind of feedback for past issues raised by Enterprise customers trying to use this cartridge...

perqa commented 7 years ago

Yes, you're probably right. The account belongs to the company I'm working for, and it says "Openshift Origin" top left.

I'm thinking I could create a standard NodeJS app on that account, and copy the bin/control file from there into this account (possibly with some adaptations) ... Do you think that would work?

perqa commented 7 years ago

Got it working in my Openshift environment with some minor changes to your bin/control file. I added two environment variables and changed a couple of lines in the start function.

# 
# Copy this file to $HOME/nodejs/bin/control  ("control" is the filename)
# 
export OPENSHIFT_NODEJS_LOG_DIR="/var/lib/openshift/<insert account-id>/nodejs/"
export OPENSHIFT_LOG_DIR=$OPENSHIFT_NODEJS_LOG_DIR

source $OPENSHIFT_CARTRIDGE_SDK_BASH
source $HOME/nodejs/lib/util

START_COMMAND=$(node -e "var p = require('$OPENSHIFT_REPO_DIR/package.json'); console.log(p.scripts.start);")
STOP_COMMAND=$(node -e "var p = require('$OPENSHIFT_REPO_DIR/package.json'); console.log(p.scripts.stop || '');")

function is_running() {
  if [ ! -z "$(ps -ef | grep "$START_COMMAND" | grep -v grep)" ]; then
    return 0
  else
    return 1
  fi
}

function pre-repo-archive() {
  if [ -d ${OPENSHIFT_REPO_DIR}node_modules ]; then
    rm -rf ${OPENSHIFT_DATA_DIR}node_modules
    mv -f ${OPENSHIFT_REPO_DIR}node_modules ${OPENSHIFT_DATA_DIR}
  fi
}

function build() {
  update_nodejs
  local INIT_DIR=`pwd`
  cd ${OPENSHIFT_REPO_DIR}
  if [ -d ${OPENSHIFT_DATA_DIR}node_modules ]; then
    rm -rf ./node_modules
    mv -f ${OPENSHIFT_DATA_DIR}node_modules ./
    rm -rf ${OPENSHIFT_DATA_DIR}node_modules
  fi
  npm prune --production
  npm i --production
  cd ${INIT_DIR}
  client_result 'Node.js modules installed.'
}

function start() {
  logf="$OPENSHIFT_NODEJS_LOG_DIR/node.log"
  if is_running; then
    client_result 'Application is already running.'
  else
    client_message 'Starting Node.js application...'
    local INIT_DIR=`pwd`
    cd ${OPENSHIFT_REPO_DIR}
    #nohup ${START_COMMAND} |& /usr/bin/logshifter -tag nodejs & 
    nohup ${START_COMMAND} >> $logf 2>&1 & 
    # nodejs_context "nohup $node_cmd >> $logf 2>&1 &"
    cd ${INIT_DIR}
    i=0
    while ! is_running && [ $i -lt 60 ]; do
      sleep 1
      i=$(($i + 1))
    done
    if is_running; then
      client_result 'Node.js application started.'
    else
      client_result 'Warning! Could not start Node.js application!'
      exit 1
    fi
  fi
}

function stop() {
  if ! is_running; then
    client_result 'Application is already stopped.'
  else
    client_message 'Stopping Node.js application...'
    if [[ ! -z "$STOP_COMMAND"  ]]; then
        local INIT_DIR=`pwd`
        cd ${OPENSHIFT_REPO_DIR}
        $STOP_COMMAND > /dev/null 2>&1
        cd ${INIT_DIR}
    else
        kill $(ps -ef | grep "$START_COMMAND" | grep -v grep | awk '{ print $2  }') > /dev/null 2>&1
    fi
    i=0
    while is_running && [ $i -lt 60 ]; do
      sleep 1
      i=$(($i + 1))
    done
    if is_running; then
      client_result 'Warning! Could not stop Node.js application!'
      exit 1
    else
      client_result 'Node.js application stopped.'
    fi
  fi
}

function restart() {
  stop
  start
}

function status() {
  if is_running; then
    client_result 'Node.js application appears to be running.'
  else
    client_result 'Node.js application appears to be stopped.'
  fi
}

function tidy() {
  shopt -s dotglob
  client_message "Emptying logs in ${OPENSHIFT_LOG_DIR}..."
  rm -rf ${OPENSHIFT_LOG_DIR}/*.log*
  client_message 'Done tidying up Node.js cartridge.'
}

case ${1} in
  pre-repo-archive) pre-repo-archive ;;
  build)            build            ;;
  start)            start            ;;
  stop)             stop             ;;
  restart)          restart          ;;
  status)           status           ;;
  tidy)             tidy             ;;
  *)                exit 0
esac