webash 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/webash +17 -0
- data/examples/config.yaml.sample +16 -0
- data/lib/webash.rb +86 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b796fcc5da99ab6cb6df0b0154437116c85717c0
|
4
|
+
data.tar.gz: e4c16cca16939b53eb12c9018ef9e5d29bb317b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd19c0f115abd52e77993a84aae22fcec024e69bf4a4cefc67a58f960c3c2e14d1462d1fb8ac5be355e90505e1a5ed5675e55c918d4b5e74a30782b8d3c3301f
|
7
|
+
data.tar.gz: 51fd9b76cbbaa1553aa643f81692078eaedd633ea00715447db5c7352c7626dc09f37b60c314386ea9dfd6b3a3d936286ccf3dabfdefb18faf470eb1cc1a5a8a
|
data/bin/webash
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'webash'
|
6
|
+
|
7
|
+
options = OpenStruct.new
|
8
|
+
|
9
|
+
OptionParser.new do |opt|
|
10
|
+
opt.on('-c', '--config CONFIG', 'Config File location') { |o| options.config_file = o }
|
11
|
+
end.parse!
|
12
|
+
|
13
|
+
if options.config_file
|
14
|
+
Webash.run options.config_file
|
15
|
+
else
|
16
|
+
Webash.show_sample_config
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
listeners:
|
2
|
+
8080:
|
3
|
+
- url: "/"
|
4
|
+
command: "echo 'Hello World!'"
|
5
|
+
- url: "/random"
|
6
|
+
command: "EXIT=$((RANDOM%4)); echo \"$EXIT on 8080 random\"; exit $EXIT"
|
7
|
+
- url: "/param"
|
8
|
+
command: "echo \"param is $param\""
|
9
|
+
8081:
|
10
|
+
- url: "/"
|
11
|
+
command: "echo 'another port'"
|
12
|
+
- url: "/exitcodes"
|
13
|
+
command: "EXIT=$((RANDOM%4)); echo \"$EXIT on 8081 exitcodes\"; exit $EXIT"
|
14
|
+
exit_codes:
|
15
|
+
200: [2]
|
16
|
+
503: [0, 1]
|
data/lib/webash.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'webrick'
|
3
|
+
require 'deep_merge'
|
4
|
+
|
5
|
+
class Webash
|
6
|
+
def self.show_sample_config
|
7
|
+
matches = Gem::Specification.find_all_by_name 'webash'
|
8
|
+
spec = matches.first
|
9
|
+
filename = File.expand_path('examples/config.yaml.sample', spec.full_gem_path)
|
10
|
+
puts "Please specify config file. Sample config file can be found at #{filename}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configure(config_file)
|
14
|
+
begin
|
15
|
+
if File.exists?(config_file)
|
16
|
+
if File.file?(config_file)
|
17
|
+
puts "Loading #{config_file}"
|
18
|
+
@config = YAML::load(IO.read(config_file))
|
19
|
+
else
|
20
|
+
config_file.chomp!("/")
|
21
|
+
@config = {}
|
22
|
+
Dir["#{config_file}/*.yaml"].each do |cfg|
|
23
|
+
puts "Loading #{cfg}"
|
24
|
+
@config.deep_merge! YAML::load(IO.read(cfg))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
else
|
28
|
+
puts "YAML configuration file couldn't be found at #{config_file}. Nothing to serve."
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
rescue Psych::SyntaxError
|
32
|
+
puts "YAML configuration file at #{config_file} contains invalid syntax."
|
33
|
+
exit
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.run(config_file)
|
38
|
+
puts "Starting Webash"
|
39
|
+
configure(config_file)
|
40
|
+
@server = WEBrick::HTTPServer.new :Port => nil, :DoNotListen => true, Logger: WEBrick::Log.new("/dev/null")
|
41
|
+
trap 'INT' do @server.shutdown end
|
42
|
+
|
43
|
+
@config["listeners"].each do |listener, listener_config|
|
44
|
+
puts "Listening on #{listener}"
|
45
|
+
@server.listen("0.0.0.0", listener)
|
46
|
+
vhost = WEBrick::HTTPServer.new :Port => listener, :DoNotListen => true, :ServerName => nil, Logger: WEBrick::Log.new("/dev/null")
|
47
|
+
|
48
|
+
listener_config.each do |url_config|
|
49
|
+
puts "Registering #{url_config["url"]} on #{listener}"
|
50
|
+
vhost.mount_proc url_config["url"] do |req, res|
|
51
|
+
export_params = []
|
52
|
+
req.query.each do |key, value|
|
53
|
+
export_params.push("export #{key}=#{value}")
|
54
|
+
end
|
55
|
+
if export_params.any?
|
56
|
+
export_string = export_params.join(";") + ";"
|
57
|
+
else
|
58
|
+
export_string = ""
|
59
|
+
end
|
60
|
+
res.body = %x(#{export_string} #{url_config["command"]})
|
61
|
+
exit_code = $?.exitstatus
|
62
|
+
if url_config["exit_codes"]
|
63
|
+
http_code = url_config["exit_codes"].select do |http_status, codes|
|
64
|
+
codes.include?(exit_code)
|
65
|
+
end.keys.first
|
66
|
+
else
|
67
|
+
http_code = nil
|
68
|
+
end
|
69
|
+
|
70
|
+
if http_code
|
71
|
+
res.status = http_code
|
72
|
+
else
|
73
|
+
if exit_code == 0
|
74
|
+
res.status = 200
|
75
|
+
else
|
76
|
+
res.status = 503
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
@server.virtual_host vhost
|
83
|
+
end
|
84
|
+
@server.start
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Pekurovsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: deep_merge
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
description: Expose your bash scripts to browser
|
28
|
+
email: alex.pekurovskiy@gmail.com
|
29
|
+
executables:
|
30
|
+
- webash
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- bin/webash
|
35
|
+
- examples/config.yaml.sample
|
36
|
+
- lib/webash.rb
|
37
|
+
homepage: http://rubygems.org/gems/webash
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.6.11
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Webash
|
61
|
+
test_files: []
|