<commit_before>#!/bin/bash
# <%= @name %> generated by cerner_tomcat for <%= @new_resource.to_s %>

<%- content = @new_resource.enclosing_provider.new_resource -%>
<%# coding: UTF-8%>
### BEGIN INIT INFO
<% content.init_info.each do |k, v| -%>
# <%= k %>: <%= v %>
<% end -%>
# Provides: <%= content.instance_name %>
# Short-Description: <%= "#{content.instance_name} tomcat server" %>
### END INIT INFO

# Set up the path to CATALINA_HOME (base of Tomcat)
CATALINA_HOME=<%= content.install_dir %>
SLEEP_TIME=5
TIMEOUT=<%= content.shutdown_timeout %>
USER=<%= content.user %>
NAME=<%= content.instance_name %>
START_CLASS="org.apache.catalina.startup.Bootstrap start"

if [[ ! -f $CATALINA_HOME/bin/catalina.sh ]]; then
  echo "$NAME may not be installed..."
  exit 1
fi

start() {
  find_pid

  if [[ $PID -gt 0 ]]; then
    echo "$NAME is already started"
  else
    echo "Starting $NAME:"
    su -l $USER -c $CATALINA_HOME/bin/startup.sh

    if [[ $? -ne 0 ]]; then
      echo "Error starting $NAME"
      exit 1
    fi

    <% for hc in content.health_checks %>
    echo "Waiting for application at <%= hc.uri.to_s %> to start"
    HC_CODE=$(healthCheck <%= hc.uri.to_s %> <%= hc.http_method %> <%= hc.time_bound %> <%= hc.args %>)
    for BACKOFF in <%= hc.backoff.join(' ') %>
    do

        if $(wasSuccessful $HC_CODE); then
            echo "Health check passed with status $HC_CODE"
            break
        fi

        echo "Health check failed with status $HC_CODE"
        echo "Sleeping for $BACKOFF"
        sleep $BACKOFF
        HC_CODE=$(healthCheck <%= hc.uri.to_s %> <%= hc.http_method %> <%= hc.time_bound %> <%= hc.args %>)

    done
    if ! $(wasSuccessful $HC_CODE); then
        echo "Application at <%= hc.uri.to_s %> failed to start."
        exit 1
    fi
    <% end %>

    echo
  fi
}

stop() {
  SHUTDOWN_CMD="$CATALINA_HOME/bin/shutdown.sh"

  # We are not supplying the "force" option initially, as we want to
  # capture thread dumps to catalina.out if the service is not responding
  # on normal shutdown. Using this option, and defining CATALINA_PID,
  # it will also attempt to send a SIGTERM OS signal to the process
  # if the shutdown port is not responding.
  su -l $USER -c "$SHUTDOWN_CMD $TIMEOUT"

  # Failed to shutdown both through the shutdown port / SIGTERM. Will
  # capture a diagnostic bundle, and then do a force (SIGKILL) shutdown.
  find_pid
  if [[ $PID -gt 0 ]]; then
    echo "Capturing diagnostics and then forcing shutdown..."
    diagnostic
    su -l $USER -c "$SHUTDOWN_CMD 1 -force"
  fi
}

status() {
  find_pid
  if [[ $PID -gt 0 ]]; then
    echo "$NAME ( pid $PID ) is running..."
  else
    echo "$NAME is stopped" ; exit 1
  fi
}

health() {
    EXIT_CODE=0
    HC_CODE=0
    <% for hc in content.health_checks %>
    echo -n "Testing application at <%= hc.uri.to_s %> ..."
    HC_CODE=$(healthCheck <%= hc.uri.to_s %> <%= hc.http_method %> <%= hc.time_bound %> <%= hc.args %>)
    if $(wasSuccessful $HC_CODE); then
        echo "Health check passed with status $HC_CODE"
    else
        echo "Health check failed with status $HC_CODE"
        EXIT_CODE=1
    fi
    <% end %>
    exit $EXIT_CODE
}

diagnostic() {
    find_pid
    TMP_DIR=<%= Chef::Config[:file_cache_path] %>
    PARENTDIR=`mktemp -d ${TMP_DIR}/${NAME}.XXXXXX`
    DIR=$PARENTDIR/diag
    mkdir $DIR
    chown $USER $PARENTDIR $DIR
    echo "Capturing JVM metrics of $NAME ($PID):"

    echo "Capturing native thread state..."
    FILE="${DIR}/${NAME}_native_thread.log"
    su $USER -c "top -b -n3 -H -p $PID > $FILE"

    echo "Capturing JVM performance metrics..."
    FILE="${DIR}/${NAME}_perfcount.log"
    su $USER -c "jcmd $PID PerfCounter.print > $FILE"

    echo "Capturing JVM thread dump..."
    FILE="${DIR}/${NAME}_thread_dump.log"
    su $USER -c "jcmd $PID Thread.print > $FILE"

    echo "Capturing JVM class histogram..."
    FILE="${DIR}/${NAME}_gc_class_histogram.log"
    su $USER -c "jcmd $PID GC.class_histogram > $FILE"

    # Create a tar.gz file with the diagnostic files
    echo "Compressing contents..."
    TIMESTAMP=`date +"%Y%m%d%H%M"`
    HOST=`hostname`
    TARFILE="${PARENTDIR}/${NAME}_${HOST}_${PID}_${TIMESTAMP}.tar.gz"
    tar -czvf $TARFILE -C $DIR . > /dev/null 2>&1
    # Remove the files which are now included in the archive<commit_msg>Use folder guarantee to have correction permissions for diagnostics (#60)<commit_after>#!/bin/bash
# <%= @name %> generated by cerner_tomcat for <%= @new_resource.to_s %>

<%- content = @new_resource.enclosing_provider.new_resource -%>
<%# coding: UTF-8%>
### BEGIN INIT INFO
<% content.init_info.each do |k, v| -%>
# <%= k %>: <%= v %>
<% end -%>
# Provides: <%= content.instance_name %>
# Short-Description: <%= "#{content.instance_name} tomcat server" %>
### END INIT INFO

# Set up the path to CATALINA_HOME (base of Tomcat)
CATALINA_HOME=<%= content.install_dir %>
SLEEP_TIME=5
TIMEOUT=<%= content.shutdown_timeout %>
USER=<%= content.user %>
NAME=<%= content.instance_name %>
START_CLASS="org.apache.catalina.startup.Bootstrap start"

if [[ ! -f $CATALINA_HOME/bin/catalina.sh ]]; then
  echo "$NAME may not be installed..."
  exit 1
fi

start() {
  find_pid

  if [[ $PID -gt 0 ]]; then
    echo "$NAME is already started"
  else
    echo "Starting $NAME:"
    su -l $USER -c $CATALINA_HOME/bin/startup.sh

    if [[ $? -ne 0 ]]; then
      echo "Error starting $NAME"
      exit 1
    fi

    <% for hc in content.health_checks %>
    echo "Waiting for application at <%= hc.uri.to_s %> to start"
    HC_CODE=$(healthCheck <%= hc.uri.to_s %> <%= hc.http_method %> <%= hc.time_bound %> <%= hc.args %>)
    for BACKOFF in <%= hc.backoff.join(' ') %>
    do

        if $(wasSuccessful $HC_CODE); then
            echo "Health check passed with status $HC_CODE"
            break
        fi

        echo "Health check failed with status $HC_CODE"
        echo "Sleeping for $BACKOFF"
        sleep $BACKOFF
        HC_CODE=$(healthCheck <%= hc.uri.to_s %> <%= hc.http_method %> <%= hc.time_bound %> <%= hc.args %>)

    done
    if ! $(wasSuccessful $HC_CODE); then
        echo "Application at <%= hc.uri.to_s %> failed to start."
        exit 1
    fi
    <% end %>

    echo
  fi
}

stop() {
  SHUTDOWN_CMD="$CATALINA_HOME/bin/shutdown.sh"

  # We are not supplying the "force" option initially, as we want to
  # capture thread dumps to catalina.out if the service is not responding
  # on normal shutdown. Using this option, and defining CATALINA_PID,
  # it will also attempt to send a SIGTERM OS signal to the process
  # if the shutdown port is not responding.
  su -l $USER -c "$SHUTDOWN_CMD $TIMEOUT"

  # Failed to shutdown both through the shutdown port / SIGTERM. Will
  # capture a diagnostic bundle, and then do a force (SIGKILL) shutdown.
  find_pid
  if [[ $PID -gt 0 ]]; then
    echo "Capturing diagnostics and then forcing shutdown..."
    diagnostic
    su -l $USER -c "$SHUTDOWN_CMD 1 -force"
  fi
}

status() {
  find_pid
  if [[ $PID -gt 0 ]]; then
    echo "$NAME ( pid $PID ) is running..."
  else
    echo "$NAME is stopped" ; exit 1
  fi
}

health() {
    EXIT_CODE=0
    HC_CODE=0
    <% for hc in content.health_checks %>
    echo -n "Testing application at <%= hc.uri.to_s %> ..."
    HC_CODE=$(healthCheck <%= hc.uri.to_s %> <%= hc.http_method %> <%= hc.time_bound %> <%= hc.args %>)
    if $(wasSuccessful $HC_CODE); then
        echo "Health check passed with status $HC_CODE"
    else
        echo "Health check failed with status $HC_CODE"
        EXIT_CODE=1
    fi
    <% end %>
    exit $EXIT_CODE
}

diagnostic() {
    find_pid
    TMP_DIR=<%= content.download_dir %>
    PARENTDIR=`mktemp -d ${TMP_DIR}/${NAME}.XXXXXX`
    DIR=$PARENTDIR/diag
    mkdir $DIR
    chown $USER $PARENTDIR $DIR
    echo "Capturing JVM metrics of $NAME ($PID):"

    echo "Capturing native thread state..."
    FILE="${DIR}/${NAME}_native_thread.log"
    su $USER -c "top -b -n3 -H -p $PID > $FILE"

    echo "Capturing JVM performance metrics..."
    FILE="${DIR}/${NAME}_perfcount.log"
    su $USER -c "jcmd $PID PerfCounter.print > $FILE"

    echo "Capturing JVM thread dump..."
    FILE="${DIR}/${NAME}_thread_dump.log"
    su $USER -c "jcmd $PID Thread.print > $FILE"

    echo "Capturing JVM class histogram..."
    FILE="${DIR}/${NAME}_gc_class_histogram.log"
    su $USER -c "jcmd $PID GC.class_histogram > $FILE"

    # Create a tar.gz file with the diagnostic files
    echo "Compressing contents..."
    TIMESTAMP=`date +"%Y%m%d%H%M"`
    HOST=`hostname`
    TARFILE="${PARENTDIR}/${NAME}_${HOST}_${PID}_${TIMESTAMP}.tar.gz"
    tar -czvf $TARFILE -C $DIR . > /dev/null 2>&1
    # Remove the files which are now included in the archive
{'fraction_non_alphanumeric': 0.1487232574189096, 'fraction_numerical': 0.004140786749482402, 'mean_word_length': 2.9184317260027037, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5814689', 'n_tokens_mistral': 3273, 'n_tokens_neox': 2939, 'n_words': 888}