<commit_before>#!/usr/bin/ruby

require 'sha1'
require 'base64'
require 'fileutils'
require 'yaml'

RAILS_ROOT = File.dirname(__FILE__) + "/.."

def run_db_command(command,use_db=true)
     if use_db
       system("mysql",'-u',@username,"--password=#{@pw}",'-e',command,@db_name)
     else
       system("mysql",'-u',@username,"--password=#{@pw}",'-e',command)
     end
end


class WebivaQuickInstall


  def initialize(input)
    @input = input
  end
  

  def run
    display_introduction()
    get_mysql_user_and_db_name()
    create_master_database()
    create_master_users()
    create_initial_yml_files()
    rebuild_gems()
    migrate_system_db()
    initialize_system()
    create_domain_db()

    display_success_message()
  end

  private

  def report_error(ok,msg)
    return if ok
    puts(msg);
    exit(1)
  end


  def input_value(str,default='')
    print(str)
    val = @input.gets.strip
    val = default if val.to_s == ''
    val
  end

  protected

  def display_introduction
    puts( <<-INTRODUCTION)


Webiva Quick Installer
======================

This script will create the initial configuration files for Webiva
( cms.yml, cms_migrator.yml, workling.yml and defaults.yml )
and initialize the mater database and an initial site database to get
you started.

First thing first, we need the user name and password of a mysql user
that has admin access and can create the initial Webiva database and
grant permissions for webiva specific users:

INTRODUCTION

  end

  def get_mysql_user_and_db_name()
    @username = input_value("Mysql Admin User name (root):",'root')
    @pw = input_value("Mysql Admin User password ([blank]):",'')
    @db_name = input_value("Master database name (webiva):",'webiva')
    @db_name_short = @db_name[0..12] # Max mysql user name is 16 char and we add a suffix 

  end

  def create_master_database
    ok = run_db_command( "create database `#{@db_name}`",false)
    report_error(ok,"Error creating database: #{@db_name}")
  end

  def create_master_users
    
    @user_password =  Base64.encode64(Digest::SHA1.hexdigest("#{rand(1<<64)}/#{Time.now.to_f}/#{Process.pid}"))[0..7]
    @migrator_password = Base64.encode64(Digest::SHA1.hexdigest("#{rand(1<<64)}/#{Time.now.to_f}/#{Process.pid}"))[0..7]


    # Create the initial webiva database user
    ok = run_db_command("GRANT SELECT,INSERT,UPDATE,DELETE ON  #{@db_name}.*  to '#{@db_name_short}_u'@localhost identified by '#{@user_password}'",false)

    report_error(ok,"Error creating master db user")

    # Create the initial migrator user
    ok = run_db_command("grant  all on *.* to '#{@db_name_short}_m'@localhost identified by '#{@migrator_password}' WITH GRANT OPTION")

    report_error(ok,"Error creating master db migrator user")
  end

  def create_initial_yml_files

    if File.exists?("#{RAILS_ROOT}/config/workling.yml")
      puts('workling.yml already exists, not overwriting')
    else
      FileUtils.cp("#{RAILS_ROOT}/config/workling.yml.example","#{RAILS_ROOT}/config/workling.yml")
    end

    if File.exists?("#{RAILS_ROOT}/config/defaults.yml")
      puts("defaults.yml already exists, not overwriting")
    else
       FileUtils.cp("#{RAILS_ROOT}/config/defaults.yml.example","#{RAILS_ROOT}/config/defaults.yml")
    end

    print('Creating cms.yml...')
    write_db_yml_file('cms.yml', { 'username' => "#{@db_name_short}_u" ,'password' => <PASSWORD>, 'database'  => @db_name })
    print("Done!\n")

    print('Creating cms_migrator.yml...')
    write_db_yml_file('cms_migrator.yml',  { 'username' => "#{@db_name_short}_m" ,'password' => <PASSWORD>, 'database' => @db_name })
    print("Done!\n")
  end


  def write_db_yml_file(filename,args)

    cms_yml_example_file = YAML.load_file("#{RAILS_ROOT}/config/#{filename}.example")
    %w(adapter socket host encoding).each do |arg|
      args[arg] = cms_yml_example_file['production'][arg]
    end

    cms_yml_output_file = { 'production' => args, 'development' => args }
    File.open("#{RAILS_ROOT}/config/#{filename}","w") { |fd| fd.write(YAML.dump(cms_yml_output_file)) }
  end

  def rebuild_gems
    puts('Rebuilding local gems...')
    ok = system('rake gems:build:force')
    report_error(ok,'Could not build local gems (run "rake --trace gems:build:force" manually to see errors)')
    puts('Done rebuildings local gems...')
  end


  def migrate_system_db()
    ok = system('rake cms:migrate_system_db')
    report_error(ok,'Could not migrate system database (run "rake --trace cms:migrate_system_db" manually to see errors')
  end

  def initialize_system()
<commit_msg>Test mysql socket file<commit_after>#!/usr/bin/ruby

require 'sha1'
require 'base64'
require 'fileutils'
require 'yaml'

RAILS_ROOT = File.dirname(__FILE__) + "/.."

def run_db_command(command,use_db=true)
     if use_db
       system("mysql",'-u',@username,"--password=#{@pw}",'-e',command,@db_name)
     else
       system("mysql",'-u',@username,"--password=#{@pw}",'-e',command)
     end
end


class WebivaQuickInstall


  def initialize(input)
    @input = input
  end
  

  def run
    display_introduction()
    get_mysql_user_and_db_name()
    create_master_database()
    create_master_users()
    create_initial_yml_files()
    rebuild_gems()
    migrate_system_db()
    initialize_system()
    create_domain_db()

    display_success_message()
  end

  private

  def report_error(ok,msg)
    return if ok
    puts(msg);
    exit(1)
  end


  def input_value(str,default='')
    print(str)
    val = @input.gets.strip
    val = default if val.to_s == ''
    val
  end

  protected

  def display_introduction
    puts( <<-INTRODUCTION)


Webiva Quick Installer
======================

This script will create the initial configuration files for Webiva
( cms.yml, cms_migrator.yml, workling.yml and defaults.yml )
and initialize the mater database and an initial site database to get
you started.

First thing first, we need the user name and password of a mysql user
that has admin access and can create the initial Webiva database and
grant permissions for webiva specific users:

INTRODUCTION

  end

  def get_mysql_user_and_db_name()
    @username = input_value("Mysql Admin User name (root):",'root')
    @pw = input_value("Mysql Admin User password ([blank]):",'')
    @db_name = input_value("Master database name (webiva):",'webiva')
    @db_name_short = @db_name[0..12] # Max mysql user name is 16 char and we add a suffix 

  end

  def create_master_database
    ok = run_db_command( "create database `#{@db_name}`",false)
    report_error(ok,"Error creating database: #{@db_name}")
  end

  def create_master_users
    
    @user_password =  Base64.encode64(Digest::SHA1.hexdigest("#{rand(1<<64)}/#{Time.now.to_f}/#{Process.pid}"))[0..7]
    @migrator_password = Base64.encode64(Digest::SHA1.hexdigest("#{rand(1<<64)}/#{Time.now.to_f}/#{Process.pid}"))[0..7]


    # Create the initial webiva database user
    ok = run_db_command("GRANT SELECT,INSERT,UPDATE,DELETE ON  #{@db_name}.*  to '#{@db_name_short}_u'@localhost identified by '#{@user_password}'",false)

    report_error(ok,"Error creating master db user")

    # Create the initial migrator user
    ok = run_db_command("grant  all on *.* to '#{@db_name_short}_m'@localhost identified by '#{@migrator_password}' WITH GRANT OPTION")

    report_error(ok,"Error creating master db migrator user")
  end

  def create_initial_yml_files

    if File.exists?("#{RAILS_ROOT}/config/workling.yml")
      puts('workling.yml already exists, not overwriting')
    else
      FileUtils.cp("#{RAILS_ROOT}/config/workling.yml.example","#{RAILS_ROOT}/config/workling.yml")
    end

    if File.exists?("#{RAILS_ROOT}/config/defaults.yml")
      puts("defaults.yml already exists, not overwriting")
    else
       FileUtils.cp("#{RAILS_ROOT}/config/defaults.yml.example","#{RAILS_ROOT}/config/defaults.yml")
    end

    print('Creating cms.yml...')
    @db_socket = File.exists?('/var/lib/mysql/mysql.sock') ? '/var/lib/mysql/mysql.sock' : '/var/run/mysqld/mysqld.sock'
    write_db_yml_file('cms.yml', { 'username' => "#{@db_name_short}_u" ,'password' => <PASSWORD>, 'database'  => @db_name, 'socket' => @db_socket })
    print("Done!\n")

    print('Creating cms_migrator.yml...')
    write_db_yml_file('cms_migrator.yml',  { 'username' => "#{@db_name_short}_m" ,'password' => <PASSWORD>, 'database' => @db_name, 'socket' => @db_socket })
    print("Done!\n")
  end


  def write_db_yml_file(filename,args)

    cms_yml_example_file = YAML.load_file("#{RAILS_ROOT}/config/#{filename}.example")
    %w(adapter socket host encoding).each do |arg|
      args[arg] ||= cms_yml_example_file['production'][arg]
    end

    cms_yml_output_file = { 'production' => args, 'development' => args }
    File.open("#{RAILS_ROOT}/config/#{filename}","w") { |fd| fd.write(YAML.dump(cms_yml_output_file)) }
  end

  def rebuild_gems
    puts('Rebuilding local gems...')
    ok = system('rake gems:build:force')
    report_error(ok,'Could not build local gems (run "rake --trace gems:build:force" manually to see errors)')
    puts('Done rebuildings local gems...')
  end


  def migrate_system_db()
    ok = system('rake cms:migrate_system_db')
    report_error(ok,'Could not migrate system database (run "rake --trace cms:migrate_system_db" manually to see errors')
  end

  def initialize_system()

{'fraction_non_alphanumeric': 0.1467309753483387, 'fraction_numerical': 0.006216505894962487, 'mean_word_length': 4.163807415605977, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '3065115', 'n_tokens_mistral': 3301, 'n_tokens_neox': 3128, 'n_words': 691}