Class: Tournament::WebguiInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/tournament/webgui_installer.rb

Constant Summary collapse

PRINCE_TARBALL =
'http://www.princexml.com/download/prince-6.0r8-linux.tar.gz'
RAILS_ENV =
ENV['RAILS_ENV'] || 'production'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(install_dir) ⇒ WebguiInstaller

Returns a new instance of WebguiInstaller.



13
14
15
16
# File 'lib/tournament/webgui_installer.rb', line 13

def initialize(install_dir)
  @install_dir = install_dir
  @source_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'webgui'))
end

Instance Attribute Details

#install_dirObject (readonly)

Returns the value of attribute install_dir.



8
9
10
# File 'lib/tournament/webgui_installer.rb', line 8

def install_dir
  @install_dir
end

#source_dirObject (readonly)

Returns the value of attribute source_dir.



9
10
11
# File 'lib/tournament/webgui_installer.rb', line 9

def source_dir
  @source_dir
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



7
8
9
# File 'lib/tournament/webgui_installer.rb', line 7

def tmp_dir
  @tmp_dir
end

Class Method Details

.download(url, local_file, binary = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/tournament/webgui_installer.rb', line 18

def self.download(url, local_file, binary = false)
  url = URI.parse(url)
  open_flag = binary ? "wb" : "w"
  Net::HTTP.start(url.host) do |http|
    File.open(local_file, open_flag) do |file|
      resp = http.get(url.path)
      file.write(resp.body)
    end
  end
end

Instance Method Details

#adjust_configuration(config_options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tournament/webgui_installer.rb', line 35

def adjust_configuration(config_options = {})
  config_file = File.expand_path(File.join(@source_dir, 'config', 'initializers', 'pool.rb'))
  target_config = File.expand_path(File.join(@install_dir, 'config', 'initializers', 'pool.rb'))
  puts "  -> Adjusting #{config_file} -> #{target_config}"
  config_contents = File.read(config_file)
  if config_options['email-server']
    smtp_config = {}
    smtp_config[:address] = config_options['email-server']
    smtp_config[:port] = config_options['email-port']
    smtp_config[:domain] = config_options['email-domain'] if config_options['email-domain']
    smtp_config[:user_name] = config_options['email-user'] if config_options['email-user']
    smtp_config[:password] = config_options['email-password'] if config_options['email-password']
    smtp_config[:authentication] = config_options['email-auth'].to_sym if config_options['email-auth']
    config_options['smtp-configuration'] = smtp_config
  end
  [
    ['site-name', 'TOURNAMENT_TITLE'],
    ['admin-email', 'ADMIN_EMAIL'],
    ['relative-root', 'RELATIVE_URL_ROOT'],
    ['smtp-configuration', 'SMTP_CONFIGURATION'],
    ['prince-path', 'PRINCE_PATH'],
    ['stats-processors', 'STATS_PROCESSORS']
  ].each do |config_name, constant_name|
    if config_options[config_name]
      puts "  -> Setting config option #{config_name} to #{config_options[config_name]}"
      re = /#{constant_name} =([^\n]+)/m
      config_contents.gsub!(re) do |m|
        "#{constant_name} = #{config_options[config_name].inspect}\n"
      end
    else
      puts "  -> Not setting config option #{config_name}"
    end
  end

  puts "  -> Writing #{target_config}"
  File.open(target_config, "w") do |f|
    f.write config_contents
  end
end

#install_prince(prince_install_dir) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tournament/webgui_installer.rb', line 75

def install_prince(prince_install_dir)
  # install prince-xml if necessary
  has_prince = ''
  Open3.popen3('which prince') do |stdin, stdout, stderr|
    has_prince << stdout.read
    has_prince << stderr.read
  end
  if has_prince.empty? || has_prince =~ /no prince/
    puts "Installing prince-xml ..."
    Tournament::WebguiInstaller.download(PRINCE_TARBALL, "#{@tmp_dir}/prince.tgz", true)
    system "tar xzf #{@tmp_dir}/prince.tgz -C #{@tmp_dir}"

    use_sudo = nil
    if !File.exist?(prince_install_dir)
      begin
        FileUtils.makedirs(prince_install_dir)
      rescue
        puts "Could not create install dir for prince, using sudo"
        use_sudo = "sudo "
      end
    elsif !File.writable?(prince_install_dir)
      puts "Prince install directory is not writable, using sudo. You may be asked for your password."
      use_sudo = "sudo "
    end
    puts "cd #{@tmp_dir}/prince-6.0r8-linux && #{use_sudo}bash install.sh"
    system "cd #{@tmp_dir}/prince-6.0r8-linux && #{use_sudo}bash install.sh"
  else
    puts "prince-xml already installed: #{has_prince}"
  end
end

#install_webguiObject



29
30
31
32
33
# File 'lib/tournament/webgui_installer.rb', line 29

def install_webgui
  raise "Installation target #{@install_dir} exists and is NOT a directory." if File.exist?(@install_dir) && !File.directory?(@install_dir)
  FileUtils.mkdir_p(@install_dir) unless File.exist?(@install_dir)
  FileUtils.cp_r("#{@source_dir}/.", @install_dir, :preserve => false, :verbose => true)
end