<commit_before>#!/usr/bin/env ruby

# This is a simple, stand-alone web application that provides a way to start, stop, restart, status check, and deploy the application. Execute this program with a "-h" flag for usage instructions.

require 'rubygems'
require 'ramaze'
require 'optparse'

PROG = File.basename(__FILE__)
OWN_FILE = File.expand_path(__FILE__)
PID_FILE = OWN_FILE+".pid"
SOCK_FILE = OWN_FILE+".sock"
RAILS_ENV = ENV['CALAGATOR_DEVELOPMENT'] ? "development" : "production"
RAILS_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__)))

# Are we running in production mode?
def production?; RAILS_ENV == "production"; end

# Is process running? Returns PID if true, false otherwise.
def running?
  if File.exist?(PID_FILE)
    pid = File.read(PID_FILE).to_i
    begin
      # Running
      Process.kill(0, pid)
      return pid
    rescue Errno::ESRCH
      # Removed stale PID file
      File.unlink(PID_FILE)
      return false
    end
  else
    # Not running
    return false
  end
end

options = {}
opts = OptionParser.new do |opts|
  opts.banner = <<-HERE
Usage: #{PROG} [options]

Examples:
  #{PROG} start  # Starts daemon
  #{PROG} attach # Attaches to daemon
  #{PROG} stop   # Stops daemon

Options, meant for internal use:
  HERE
  opts.on("-d", "--daemonize", "Daemonize script") do |v|
    options[:daemonize] = v
  end
  opts.on("-a", "--attach", "Attach to script") do |v|
    options[:attach] = v
  end
  opts.on("-h", "--help", "Show this help")
  args = opts.parse!(ARGV)

  if args.include? "start"
    puts "Starting"
    exec "dtach -n #{SOCK_FILE} #{OWN_FILE} --daemonize"
  elsif args.include? "stop"
    if pid = running?
      Process.kill("TERM", pid)
      puts "Stopped PID #{pid}"
      exit 0
    else
      puts "Not running"
      exit 1
    end
  elsif args.include? "status"
    if pid = running?
      puts "Running at PID #{pid}"
      exit 0
    else
      puts "Not running"
      exit 7
    end
  elsif args.include? "attach" || options[:attach]
    exec "dtach -a #{SOCK_FILE}"
  end
  if !options[:daemonize]
    puts opts.help
    puts "ERROR: Unknown arguments #{args.size > 0 ? (' -- '+args.inspect) : ''}"
    exit 1
  end
end

class MainController < Ramaze::Controller
  engine :Haml

  if production?
    def error() 'An error occurred. Contact one of the developers.' end
  end

  def index
    case request["action"]
    when "deploy", "deploy_and_migrate"
      @message = `(cd #{RAILS_ROOT} && svn cleanup && svn update -r #{request["revision"].match(/(\w+)/)[1]} && rake restart) 2>&1`
    when "restart", "start", "stop", "status"
      @message = `(cd #{RAILS_ROOT} && rake #{request["action"]}) 2>&1`
    end

    %(
!!!
%html
  %head
    %title calagator admin console
  %body
    %h1 calagator admin console
    %p 
      %form{:method=>"post"}
        %label
          %input{:type=>"radio", :name=>"action", :value=>"status", :checked=>"checked"}
          status
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"deploy_and_migrate"}
          deploy and migrate revision
          %input{:type=>"text", :name=>"revision", :value=>"HEAD"}
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"deploy"}
          deploy revision
          %input{:type=>"text", :name=>"revision", :value=>"HEAD"}
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"restart"}
          restart
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"start"}<commit_msg>Added webadmin restart. Made migrate a default on migrate<commit_after>#!/usr/bin/env ruby

# This is a simple, stand-alone web application that provides a way to start, stop, restart, status check, and deploy the application. Execute this program with a "-h" flag for usage instructions.

require 'rubygems'
require 'ramaze'
require 'optparse'

PROG = File.basename(__FILE__)
OWN_FILE = File.expand_path(__FILE__)
PID_FILE = OWN_FILE+".pid"
SOCK_FILE = OWN_FILE+".sock"
RAILS_ENV = ENV['CALAGATOR_DEVELOPMENT'] ? "development" : "production"
RAILS_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__)))

# Are we running in production mode?
def production?; RAILS_ENV == "production"; end

# Is process running? Returns PID if true, false otherwise.
def running?
  if File.exist?(PID_FILE)
    pid = File.read(PID_FILE).to_i
    begin
      # Running
      Process.kill(0, pid)
      return pid
    rescue Errno::ESRCH
      # Removed stale PID file
      File.unlink(PID_FILE)
      return false
    end
  else
    # Not running
    return false
  end
end

options = {}
opts = OptionParser.new do |opts|
  opts.banner = <<-HERE
Usage: #{PROG} [options]

Examples:
  #{PROG} attach  # Attaches to daemon
  #{PROG} start   # Starts daemon
  #{PROG} stop    # Stops daemon
  #{PROG} restart # Restarts daemon

Options, meant for internal use:
  HERE
  opts.on("-d", "--daemonize", "Daemonize script") do |v|
    options[:daemonize] = v
  end
  opts.on("-a", "--attach", "Attach to script") do |v|
    options[:attach] = v
  end
  opts.on("-h", "--help", "Show this help")
  args = opts.parse!(ARGV)

  if args.include? "start"
    puts "Starting"
    exec "dtach -n #{SOCK_FILE} #{OWN_FILE} --daemonize"
  elsif args.include? "restart"
    puts "Restarting"
    system "#{__FILE__} stop; sleep 3; #{__FILE__} start"
    exit 0
  elsif args.include? "stop"
    if pid = running?
      Process.kill("TERM", pid)
      puts "Stopped PID #{pid}"
      exit 0
    else
      puts "Not running"
      exit 1
    end
  elsif args.include? "status"
    if pid = running?
      puts "Running at PID #{pid}"
      exit 0
    else
      puts "Not running"
      exit 7
    end
  elsif args.include? "attach" || options[:attach]
    exec "dtach -a #{SOCK_FILE}"
  end
  if !options[:daemonize]
    puts opts.help
    puts "ERROR: Unknown arguments #{args.size > 0 ? (' -- '+args.inspect) : ''}"
    exit 1
  end
end

class MainController < Ramaze::Controller
  engine :Haml

  if production?
    def error() 'An error occurred. Contact one of the developers.' end
  end

  def index
    case request["action"]
    when "deploy"
      @message = `(cd #{RAILS_ROOT} && svn cleanup && svn update -r #{request["revision"].match(/(\w+)/)[1]} && rake db:migrate restart) 2>&1`
    when "restart", "start", "stop", "status"
      @message = `(cd #{RAILS_ROOT} && rake #{request["action"]}) 2>&1`
    end

    %(
!!!
%html
  %head
    %title calagator admin console
  %body
    %h1 calagator admin console
    %p 
      %form{:method=>"post"}
        %label
          %input{:type=>"radio", :name=>"action", :value=>"status", :checked=>"checked"}
          status
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"deploy"}
          deploy and migrate revision
          %input{:type=>"text", :name=>"revision", :value=>"HEAD"}
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"restart"}
          restart
        %br
        %label
          %input{:type=>"radio", :name=>"action", :value=>"start"}
{'fraction_non_alphanumeric': 0.1490377862059278, 'fraction_numerical': 0.003933136676499509, 'mean_word_length': 2.914238592633315, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5130252', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2415, 'n_words': 687}