<commit_before>package com.busymachines.commons.installer

import java.io.File
import java.nio.file.attribute.PosixFilePermission._

import com.busymachines.commons.Implicits._

object InstallOnUbuntuCommand {
  import com.busymachines.commons.installer.InstallCommand._

  def install(name: String, description: String, version: String, user: Option[String], vmArgs: String, app: App, args: String) {
    try install2(name, description, version, user, vmArgs, app, args)
    catch { 
      case e: Exception =>
        println("Error: " + e.getMessage)
      }
    }
  
  def install2(name: String, description: String, version: String, userName: Option[String], vmArgs: String, app: App, args: String) {
    println(s"Installing $name")
    
    // check user principal
    val user = getUserPrincipal(userName)

    // make sure we have root rights
    val libDir = new File("/var/lib/" + name)
    libDir.mkdirs()
    setOwner(libDir, user)

    // copy jars into lib
    val jars = copyJars(libDir)
    val jarFileNames = jars.map(new File(libDir, _).getAbsolutePath).mkString(",")
    val appClassName = app.getClass.getName.stripSuffix("$")


    // write start script
    val startScript = new File("/usr/bin/" + name)
    if (canOverwrite(startScript)) {
      startScript.getParentFile.mkdirs()

      val startScriptContent = s"""#!/bin/bash
$genmarker
java $vmArgs -Dconfig.file=/etc/$name.conf -Dlogback.configurationFile=/etc/$name.d/cli-logback.xml -cp $jarFileNames $appClassName $$*
"""

      startScriptContent.copyTo(startScript)
      setPermissions(startScript, OWNER_EXECUTE, OWNER_READ, OWNER_WRITE, GROUP_EXECUTE, GROUP_READ, OTHERS_EXECUTE, OTHERS_READ)
      println("Created " + startScript)
    } else {
      println("Warning: existing file not overwritten: " + startScript)
    }

    // write init script
    val initScript = new File("/etc/init.d/", name)
    if (canOverwrite(initScript)) {
      initScript.getParentFile.mkdirs()
      try {

        val command = s"java $vmArgs -Dconfig.file=/etc/$name.conf -Dlogback.configurationFile=/etc/$name.d/logback.xml -cp $jarFileNames $appClassName $args"

        val initScriptContent = mkInitScript(name, description, command, "", userName.getOrElse("root"))
        initScriptContent.copyTo(initScript)
        setPermissions(initScript, OWNER_EXECUTE, OWNER_READ, OWNER_WRITE, GROUP_EXECUTE, GROUP_READ, OTHERS_EXECUTE, OTHERS_READ)
        println("Created " + initScript)
      } catch {
        case e: Exception => println("Warning: couldn't create: " + initScript + ": " + e.getMessage)
      }
    } else {
      println("Warning: existing file not overwritten: " + initScript)
    }

    // write application.conf
    writeApplicationConf(new File("/etc/" + name + ".conf"))

    // write logback.xml
    writeResource("logback.xml", new File("/etc/" + name + ".d/logback.xml"))
    writeResource("cli-logback.xml", new File("/etc/" + name + ".d/cli-logback.xml"))

    // change owner of log file
    val logFile = new File(s"/var/log/$name.log")
    if (!logFile.exists()) "".copyTo(logFile)
    setOwner(logFile, user)
  }
  
  def mkInitScript(name: String, description: String, command: String, args: String, user: String) =
    initScriptTemplate.
      replace("<GEN_MARKER>", genmarker).
      replaceAll("<NAME>", name).
      replaceAll("<DESCRIPTION>", description).
      replaceAll("<COMMAND>", command).
      replaceAll("<USER>", user).
      replace("<ARGS>", args)


  val initScriptTemplate = """#!/bin/bash
<GEN_MARKER>
### BEGIN INIT INFO
# Provides:          <NAME>
# Required-Start:    cassandra elasticsearch networking
# Required-Stop:     cassandra elasticsearch networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       <DESCRIPTION>
### END INIT INFO

SCRIPT="<COMMAND> <ARGS>"
RUNAS=<USER>

PIDFILE=/var/run/<NAME>.pid
LOGFILE=/var/log/<NAME>.log

start() {
  if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting <NAME>...' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo '<NAME> not running' >&2
    return 1<commit_msg>debugging info for raw metrics<commit_after>package com.busymachines.commons.installer

import java.io.File
import java.nio.file.attribute.PosixFilePermission._

import com.busymachines.commons.Implicits._

object InstallOnUbuntuCommand {
  import com.busymachines.commons.installer.InstallCommand._

  def install(name: String, description: String, version: String, user: Option[String], vmArgs: String, app: App, args: String) {
    try install2(name, description, version, user, vmArgs, app, args)
    catch { 
      case e: Exception =>
        println("Error: " + e.getMessage)
      }
    }
  
  def install2(name: String, description: String, version: String, userName: Option[String], vmArgs: String, app: App, args: String) {
    println(s"Installing $name")
    
    // check user principal
    val user = getUserPrincipal(userName)

    // make sure we have root rights
    val libDir = new File("/var/lib/" + name)
    libDir.mkdirs()
    setOwner(libDir, user)

    // copy jars into lib
    val jars = copyJars(libDir)
    val jarFileNames = jars.map(new File(libDir, _).getAbsolutePath).mkString(",")
    val appClassName = app.getClass.getName.stripSuffix("$")


    // write start script
    val startScript = new File("/usr/bin/" + name)
    if (canOverwrite(startScript)) {
      startScript.getParentFile.mkdirs()

      val startScriptContent = s"""#!/bin/bash
$genmarker
java $vmArgs -Dconfig.file=/etc/$name.conf -Dlogback.configurationFile=/etc/$name.d/cli-logback.xml -cp $jarFileNames $appClassName $$*
"""

      startScriptContent.copyTo(startScript)
      setPermissions(startScript, OWNER_EXECUTE, OWNER_READ, OWNER_WRITE, GROUP_EXECUTE, GROUP_READ, OTHERS_EXECUTE, OTHERS_READ)
      println("Created " + startScript)
    } else {
      println("Warning: existing file not overwritten: " + startScript)
    }

    // write init script
    val initScript = new File("/etc/init.d/", name)
    if (canOverwrite(initScript)) {
      initScript.getParentFile.mkdirs()
      try {

        val command = s"java $vmArgs -Dconfig.file=/etc/$name.conf -Dlogback.configurationFile=/etc/$name.d/logback.xml -cp $jarFileNames $appClassName $args"

        val initScriptContent = mkInitScript(name, description, command, "", userName.getOrElse("root"))
        initScriptContent.copyTo(initScript)
        setPermissions(initScript, OWNER_EXECUTE, OWNER_READ, OWNER_WRITE, GROUP_EXECUTE, GROUP_READ, OTHERS_EXECUTE, OTHERS_READ)
        println("Created " + initScript)
      } catch {
        case e: Exception => println("Warning: couldn't create: " + initScript + ": " + e.getMessage)
      }
    } else {
      println("Warning: existing file not overwritten: " + initScript)
    }

    // write application.conf
    writeApplicationConf(new File("/etc/" + name + ".conf"))

    // write logback.xml
    writeResource("logback.xml", new File("/etc/" + name + ".d/logback.xml"))
    writeResource("cli-logback.xml", new File("/etc/" + name + ".d/cli-logback.xml"))

    // change owner of log file
    val logFile = new File(s"/var/log/$name.log")
    if (!logFile.exists()) "".copyTo(logFile)
    setOwner(logFile, user)
  }
  
  def mkInitScript(name: String, description: String, command: String, args: String, user: String) =
    initScriptTemplate.
      replace("<GEN_MARKER>", genmarker).
      replaceAll("<NAME>", name).
      replaceAll("<DESCRIPTION>", description).
      replaceAll("<COMMAND>", command).
      replaceAll("<USER>", user).
      replace("<ARGS>", args)


  val initScriptTemplate = """#!/bin/bash
<GEN_MARKER>
### BEGIN INIT INFO
# Provides:          <NAME>
# Required-Start:    cassandra elasticsearch networking
# Required-Stop:     cassandra elasticsearch networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       <DESCRIPTION>
### END INIT INFO

SCRIPT="<COMMAND> <ARGS>"
RUNAS=<USER>

PIDFILE=/var/run/<NAME>.pid
LOGFILE=/var/log/<NAME>.log

start() {
  if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting <NAME>...' >&2
  local CMD="$SCRIPT > \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo '<NAME> not running' >&2
    return 1
{'fraction_non_alphanumeric': 0.1427409988385598, 'fraction_numerical': 0.003948896631823461, 'mean_word_length': 3.3821882951653945, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112755', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2708, 'n_words': 663}