Class: Webash

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

Class Method Summary collapse

Class Method Details

.configure(config_file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/webash.rb', line 11

def self.configure(config_file)
    begin
        if File.exists?(config_file)
            if File.file?(config_file)
                puts "Loading #{config_file}"
                @config = YAML::load(IO.read(config_file))
            else
                config_file.chomp!("/")
                @config = {}
                Dir["#{config_file}/*.yaml"].each do |cfg|
                    puts "Loading #{cfg}"
                    @config.deep_merge! YAML::load(IO.read(cfg)) 
                end
            end
        else
            puts "YAML configuration file couldn't be found at #{config_file}. Nothing to serve."
            exit
        end
    rescue Psych::SyntaxError
        puts "YAML configuration file at #{config_file} contains invalid syntax."
        exit
    end
end

.run(config_file) ⇒ 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
74
75
76
77
78
79
80
81
82
83
# File 'lib/webash.rb', line 35

def self.run(config_file)
    puts "Starting Webash"
    configure(config_file)
    @server = WEBrick::HTTPServer.new :Port => nil, :DoNotListen => true, :Logger => WEBrick::Log.new("/dev/null")
    trap 'INT' do @server.shutdown end

    @config["listeners"].each do |listener, listener_config|
        puts "Listening on #{listener}"
        @server.listen("0.0.0.0", listener)
        vhost = WEBrick::HTTPServer.new :Port => listener, :DoNotListen => true, :ServerName => nil, :Logger => WEBrick::Log.new("/dev/null")

        listener_config.each do |url_config|
            puts "Registering #{url_config["url"]} on #{listener}"
            vhost.mount_proc url_config["url"] do |req, res|
                export_params = []
                req.query.each do |key, value|
                    export_params.push("export #{key}=#{value}")
                end
                if export_params.any?
                    export_string = export_params.join(";") + ";"
                else
                    export_string = ""
                end
                res.body = %x(#{export_string} #{url_config["command"]})
                exit_code = $?.exitstatus
                if url_config["exit_codes"]
                    http_code = url_config["exit_codes"].select do |http_status, codes| 
                        codes.include?(exit_code)
                    end.keys.first
                else
                    http_code = nil
                end

                if http_code
                    res.status = http_code
                else
                    if exit_code == 0
                        res.status = 200
                    else
                        res.status = 503
                    end
                end
            end
        end

        @server.virtual_host vhost
    end
    @server.start
end

.show_sample_configObject



6
7
8
9
# File 'lib/webash.rb', line 6

def self.show_sample_config
    filename = File.expand_path('examples/config.yaml.sample', Gem.loaded_specs["webash"].full_gem_path)
    puts "Please specify config file. Sample config file can be found at #{filename}"
end