Class: FontanaClientSupport::ConfigServer

Inherits:
Object
  • Object
show all
Defined in:
lib/fontana_client_support/config_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ ConfigServer



9
10
11
# File 'lib/fontana_client_support/config_server.rb', line 9

def initialize(config = {})
  @config = config
end

Class Method Details

.start_daemon(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fontana_client_support/config_server.rb', line 81

def start_daemon(options = {})
  pid_dir = File.join(FontanaClientSupport.root_dir, "tmp/pids")
  pid_file = File.join(pid_dir, "config_server.pid")
  if File.exist?(pid_file)
    raise "Can't start config server daemon because #{pid_file} already exists."
  end
  FileUtils.mkdir_p(pid_dir)
  pid = fork do
    $PROGRAM_NAME = __FILE__
    open(pid_file, "w") do |f|
      f.write(Process.pid)
    end
    $stdout.reopen("/dev/null")
    $stderr.reopen("/dev/null")
    # $stdout.reopen("stdout.log")
    # $stderr.reopen("stderr.log")
    begin
      self.new(options).launch
    ensure
      File.delete(pid_file)
    end
  end
  # puts "#{Process.pid} launches child process by #{name}.start_daemon returns #{pid}"
  pid
end

.stop_daemonObject



107
108
109
110
111
112
113
# File 'lib/fontana_client_support/config_server.rb', line 107

def stop_daemon
  pid_dir = File.join(FontanaClientSupport.root_dir, "tmp/pids")
  Dir[File.join(pid_dir, "config_server.pid")].each do |filepath|
    pid = File.read(filepath)
    Process.kill("INT", pid.to_i)
  end
end

Instance Method Details

#launchObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
74
75
76
77
# File 'lib/fontana_client_support/config_server.rb', line 13

def launch
  $stdout.puts "pwd                  : #{Dir.pwd.inspect}"
  require 'webrick'
  root_dir = FontanaClientSupport.root_dir
  document_root_source = @config[:document_root] || (root_dir ? File.join(root_dir, "config_server") : ".")
  $stdout.puts "config_server options: #{@config.inspect}"
  $stdout.puts("root_dir             : #{root_dir.inspect}")
  $stdout.puts("document_root_source : #{document_root_source.inspect}")
  Dir.mktmpdir("fontana_config_server") do |dir|
    if @config[:document_root] or root_dir.nil?
      document_root = document_root_source
    else
      git_dir = File.join(dir, "workspace")
      document_root = File.join(dir, "document_root")
      FileUtils.cp_r(document_root_source, document_root)
    end

    $stdout.puts("document_root        : #{document_root.inspect}")
    $stdout.puts("git_dir              : #{git_dir.inspect}")

    server_config = {
      :DocumentRoot => document_root,
      :BindAddress => @config[:address] || ENV['GSS_CONFIG_SERVER_ADDRESS'],
      :Port => (@config[:port] || ENV['GSS_CONFIG_SERVER_PORT'] || 80).to_i
    }
    server = WEBrick::HTTPServer.new(server_config)
    if FontanaClientSupport.root_dir
      server.mount_proc('/switch_config_server') do |req, res|
        buf = []
        unless Dir.exist?(git_dir)
          if git_repo_url = @config[:git_repo_url] || ENV['GSS_CONFIG_SERVER_REPO_URL']
            Dir.chdir(dir) do
              cmd = "git clone #{git_repo_url} #{File.basename(git_dir)}"
              if system(cmd)
                buf << "SUCCESS: #{cmd}"
              else
                buf << "ERROR: #{cmd}"
              end
            end
          else
            FileUtils.cp_r(root_dir, git_dir)
            buf << "SUCCESS: cp -r #{root_dir} #{git_dir}"
          end
        end
        tag = req.path.sub(%r{\A/switch_config_server/}, '')
        if tag.nil? || tag.empty?
          bug << "no tag or SHA1 given"
        else
          Dir.chdir(git_dir) do
            if system("git reset --hard #{tag}")
              FileUtils.rm_rf(document_root)
              FileUtils.cp_r(File.join(git_dir, "config_server"), document_root)
              buf << "SUCCESS"
            else
              buf << "ERROR if you use in submodule, set $GSS_CONFIG_SERVER_REPO_URL.\nlike this:\n$ rake config_server:launch GSS_CONFIG_SERVER_PORT=3002 [email protected]:groovenauts/fontana_sample.git"
            end
          end
        end
        res.body = (buf + ["", "files: "] + Dir["#{document_root}/**/*"].to_a).join("\n  ")
      end
    end
    Signal.trap(:INT){ server.shutdown }
    server.start
  end
end