spawngebob 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in spawngebob.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/spawn ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ SPAWNGEBOB_BASE_PATH = File.expand_path('../..', __FILE__)
4
+ require File.join(SPAWNGEBOB_BASE_PATH, 'lib/spawngebob')
5
+ Spawngebob::Runner.boot(ARGV)
@@ -0,0 +1,86 @@
1
+ module Spawngebob
2
+ module Compilers
3
+ class Nginx
4
+ include Constants
5
+
6
+ def initialize
7
+ @config_file_path = File.join(BASE_CONFIG_PATH, CONFIG_FILE)
8
+ @container = ''
9
+ end
10
+
11
+ def prepare
12
+ if !File.exist?(@config_file_path)
13
+ raise "apps.yml not found in #{NGINX_CONF_PATH}"
14
+ end
15
+
16
+ @config = ERB.new(File.read(@config_file_path)).result(binding)
17
+ @config = YAML.load(@config)
18
+
19
+ compile!
20
+ end
21
+
22
+ def compile!
23
+ location_string = ''
24
+
25
+ if @config['nginx'].include? 'listen'
26
+ NGINX_DEFAULTS.update(@config['nginx'])
27
+ end
28
+
29
+ if @config['nginx'].include? 'port'
30
+ NGINX_DEFAULTS.update(@config['nginx'])
31
+ end
32
+ puts NGINX_DEFAULTS.inspect
33
+
34
+ if @config['nginx']['ssl']
35
+ location_template = NGINX_SSL_LOCATION_TEMPLATE
36
+ else
37
+ location_template = NGINX_HTTP_LOCATION_TEMPLATE
38
+ end
39
+
40
+ # apps
41
+ @config['applications'].each do |k, v|
42
+ s = NGINX_UPSTREAM_TEMPLATE.gsub('%app%', k)
43
+ @container.concat(s.gsub('%port%', "#{v["port"]}"))
44
+ end
45
+
46
+ # hosts
47
+ @config['hosts'].each do |k, v|
48
+ v['host'] = [v['host']] unless v['host'].is_a? Array
49
+
50
+ v['host'].each do |h|
51
+ # routes
52
+ location_string = ''
53
+
54
+ v['routes'].each do |r|
55
+ s = location_template.gsub('%route%', r.first)
56
+ s = s.gsub('%proxy%', r.last)
57
+ location_string.concat(s + "\n")
58
+ end
59
+
60
+ if @config['nginx']['ssl_rewrite']
61
+ s = NGINX_SSL_SERVER_TEMPLATE.gsub('%host%', h)
62
+ s = s.gsub('%cert_path%', @config['nginx']['cert_path'])
63
+ s = s.gsub('%key_path%', @config['nginx']['key_path'])
64
+ else
65
+ s = NGINX_SERVER_TEMPLATE.gsub('%host%', h)
66
+ end
67
+
68
+ s = s.gsub('%ip%', NGINX_DEFAULTS['listen'])
69
+ s = s.gsub('%port%', NGINX_DEFAULTS['port'].to_s)
70
+ s = s.gsub('%location%', location_string)
71
+
72
+ @container.concat(s)
73
+ end
74
+
75
+ confd_path = File.join(NGINX_CONFIG_PATH, 'conf.d')
76
+
77
+ FileUtils.mkdir_p(confd_path) if !File.exist? confd_path
78
+
79
+ File.open(File.join(confd_path, 'caresharing.conf'), 'w') do |f|
80
+ f.write(@container)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ module Spawngebob
2
+ module Compilers
3
+ autoload :Nginx, (File.join(SPAWNGEBOB_LIB_ROOT, 'compilers/nginx'))
4
+ end
5
+ end
@@ -0,0 +1,88 @@
1
+ module Spawngebob
2
+ module Constants
3
+ NGINX_DIRS = ['tmp', 'run', 'logs', 'conf.d', 'conf']
4
+ CONFIG_FILE = 'apps.yml'
5
+
6
+ NGINX_DEFAULTS = {
7
+ 'listen' => '127.0.0.1',
8
+ 'port' => '8088'
9
+ }
10
+
11
+ COLORS = {
12
+ :green => "\033[1;32m",
13
+ :red => "\033[1;31m",
14
+ :yellow =>"\033[1;33m",
15
+ :blank => "\033[0m"
16
+ }
17
+
18
+ NGINX_UPSTREAM_TEMPLATE = <<NUT
19
+ # %app%
20
+ upstream %app%_proxy {
21
+ server 127.0.0.1:%port%;
22
+ }
23
+ NUT
24
+
25
+ NGINX_SERVER_TEMPLATE = <<NST
26
+ server {
27
+ listen %ip%:%port%;
28
+ server_name %host%;
29
+ access_log logs/%host%-access.log;
30
+
31
+ %location%
32
+ }
33
+ NST
34
+
35
+ NGINX_SSL_SERVER_TEMPLATE = <<NSSL
36
+ # %host%
37
+ server {
38
+ listen %ip%:80;
39
+ server_name %host%;
40
+
41
+ location / {
42
+ rewrite ^/(.*)$ https://%host%/$1 permanent;
43
+ }
44
+ }
45
+
46
+ server {
47
+ listen %ip%:443;
48
+ server_name %host%;
49
+ access_log logs/%host%-access.log;
50
+ ssl on;
51
+ ssl_certificate %cert_path%;
52
+ ssl_certificate_key %key_path%;
53
+ ssl_protocols SSLv3 TLSv1;
54
+ ssl_ciphers ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP;
55
+
56
+ %location%
57
+ include /usr/local/nginx/conf.d/denied_locations.inc;
58
+ }
59
+ NSSL
60
+
61
+ NGINX_SSL_LOCATION_TEMPLATE = <<NSLT
62
+ location %route% {
63
+ proxy_set_header X-Real-IP $remote_addr;
64
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
65
+ proxy_set_header Host $http_host;
66
+ proxy_set_header X_FORWARDED_PROTO https;
67
+ proxy_redirect off;
68
+ proxy_connect_timeout 900;
69
+ proxy_send_timeout 900;
70
+ proxy_read_timeout 900;
71
+ proxy_pass http://%proxy%;
72
+ }
73
+ NSLT
74
+
75
+ NGINX_HTTP_LOCATION_TEMPLATE = <<NHLT
76
+ location %route% {
77
+ proxy_set_header X-Real-IP $remote_addr;
78
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
79
+ proxy_set_header Host $http_host;
80
+ proxy_redirect off;
81
+ proxy_connect_timeout 900;
82
+ proxy_send_timeout 900;
83
+ proxy_read_timeout 900;
84
+ proxy_pass http://%proxy%;
85
+ }
86
+ NHLT
87
+ end
88
+ end
@@ -0,0 +1,86 @@
1
+ require 'optparse'
2
+
3
+ module Spawngebob
4
+ class Runner
5
+ include Constants
6
+
7
+ def self.boot(args)
8
+ Spawngebob::Constants.const_set('BASE_CONFIG_PATH', File.join(ENV['HOME'], '.spawngebob'))
9
+ Spawngebob::Constants.const_set('NGINX_CONFIG_PATH', File.join(BASE_CONFIG_PATH, 'nginx', '/'))
10
+ Spawngebob::Constants.const_set('NGINX_CONF', File.join(NGINX_CONFIG_PATH, 'conf', 'nginx.conf'))
11
+ Spawngebob::Constants.const_set('NGINX_PID', File.join(NGINX_CONFIG_PATH, 'run', 'nginx.pid'))
12
+ Spawngebob::Constants.const_set('NGINX_BIN', `which nginx 2>/dev/null`.strip)
13
+ Spawngebob::Constants.const_set('KILL_BIN', `which kill 2>/dev/null`.strip)
14
+
15
+ self.new(args)
16
+ end
17
+
18
+ def initialize(args)
19
+ @options = {}
20
+ @options[:verbose] = false
21
+
22
+ optparse = OptionParser.new do |opts|
23
+ opts.banner = "Spawngebob and Patrick!"
24
+ opts.separator "Options:"
25
+
26
+ opts.on('-v', '--verbose', 'Verbose') do
27
+ @options[:verbose] = true
28
+ end
29
+
30
+ opts.on('-V', '--version', 'Version') do
31
+ puts Spawngebob::VERSION
32
+ exit
33
+ end
34
+
35
+ opts.on('-h', '--help', 'Display this screen') do
36
+ puts opts
37
+ exit
38
+ end
39
+ end
40
+
41
+ optparse.parse!
42
+
43
+ if args.length == 0
44
+ Utils.say optparse.help
45
+ else
46
+ # create necessary directories if not exist
47
+ NGINX_DIRS.each do |d|
48
+ full_path = File.join(NGINX_CONFIG_PATH, d)
49
+
50
+ if !File.exists? full_path
51
+ Utils.say FileUtils.mkdir_p(full_path)
52
+ end
53
+ end
54
+
55
+ if Constants.const_defined?("NGINX_BIN")
56
+ if File.exist?(NGINX_CONF) and !File.zero?(NGINX_CONF)
57
+ self.run(args)
58
+ else
59
+ raise "Missing nginx.conf in #{NGINX_CONF}!"
60
+ end
61
+ else
62
+ raise "Nginx is not installed!"
63
+ end
64
+
65
+ end
66
+ end
67
+
68
+ def run(args)
69
+ case args.shift
70
+ when 'start'
71
+ Spawner.start(@options)
72
+ when 'stop'
73
+ Spawner.stop
74
+ when 'restart'
75
+ Spawner.stop
76
+ Spawner.start(@options)
77
+ when 'check'
78
+ Spawner.check
79
+ when 'configure'
80
+ Compilers::Nginx.new.prepare
81
+ else
82
+ puts "I don't know what to do."
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,71 @@
1
+ module Spawngebob
2
+ class Spawner
3
+ include Constants
4
+
5
+ def self.start(opts, flag = false)
6
+ if get_pid && get_worker_pids
7
+ if flag
8
+ Utils.say "nginx #{COLORS[:green]}STARTED!#{COLORS[:blank]}"
9
+ else
10
+ Utils.say "nginx already #{COLORS[:yellow]}EXISTS!#{COLORS[:blank]}"
11
+ check
12
+ end
13
+ exit
14
+ else
15
+ Utils.say_with_time "starting nginx..." do
16
+ Dir.chdir(NGINX_CONFIG_PATH) do
17
+ if opts[:verbose]
18
+ `#{NGINX_BIN} -c #{NGINX_CONF} -p #{NGINX_CONFIG_PATH}`
19
+ else
20
+ `#{NGINX_BIN} -c #{NGINX_CONF} -p #{NGINX_CONFIG_PATH} 2>&1`
21
+ end
22
+ end
23
+ end
24
+
25
+ check
26
+ end
27
+
28
+ start(opts, true)
29
+ end
30
+
31
+ def self.has_pid?
32
+ File.exist?(NGINX_PID) && !File.zero?(NGINX_PID)
33
+ end
34
+
35
+ def self.get_pid
36
+ if has_pid?
37
+ File.read(NGINX_PID).chomp!
38
+ else
39
+ Utils.say "#{COLORS[:red]}pid does not exist!#{COLORS[:blank]}"
40
+ false
41
+ end
42
+ end
43
+
44
+ def self.stop
45
+ pid = get_pid
46
+
47
+ if pid
48
+ Utils.say_with_time "killing #{COLORS[:yellow]}#{pid}#{COLORS[:blank]}..." do
49
+ `#{KILL_BIN} #{pid}`
50
+ end
51
+
52
+ if FileUtils.rm_rf(NGINX_PID)
53
+ Utils.say "#{COLORS[:green]}pid removed!#{COLORS[:blank]}"
54
+ end
55
+ else
56
+ Utils.say "#{COLORS[:red]}nginx not running!#{COLORS[:blank]}"
57
+ exit
58
+ end
59
+ end
60
+
61
+ def self.get_worker_pids
62
+ worker_pids = `ps ax | grep 'nginx' | grep -v grep | awk '{print $1}'`
63
+ end
64
+
65
+ def self.check
66
+ if get_pid && get_worker_pids
67
+ Utils.say "nginx is running: #{COLORS[:green]}#{get_pid}#{COLORS[:blank]}"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ require 'benchmark'
2
+
3
+ module Spawngebob
4
+ module Utils
5
+ include Constants
6
+
7
+ def self.say(message, subitem = false)
8
+ puts "#{subitem ? " ->" : "--"} #{message}"
9
+ end
10
+
11
+ def self.say_with_time(message)
12
+ say(message)
13
+ result = nil
14
+ time = Benchmark.measure { result = yield }
15
+ say "%.4fs" % time.real, :subitem
16
+ say("#{result} rows", :subitem) if result.is_a?(Integer)
17
+ result
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,3 @@
1
+ module Spawngebob
2
+ VERSION = "0.1.0"
3
+ end
data/lib/spawngebob.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ require 'fileutils'
4
+ require File.join(File.expand_path('../spawngebob/version', __FILE__))
5
+
6
+ SPAWNGEBOB_LIB_ROOT = File.join(File.dirname(__FILE__), 'spawngebob')
7
+
8
+ module Spawngebob
9
+
10
+ autoload :Runner, (File.join(SPAWNGEBOB_LIB_ROOT, 'runner'))
11
+ autoload :Compilers, (File.join(SPAWNGEBOB_LIB_ROOT, 'compilers'))
12
+ autoload :Spawner, (File.join(SPAWNGEBOB_LIB_ROOT, 'spawner'))
13
+ autoload :Constants, (File.join(SPAWNGEBOB_LIB_ROOT, 'constants'))
14
+ autoload :Utils, (File.join(SPAWNGEBOB_LIB_ROOT, 'utils'))
15
+ end
@@ -0,0 +1,137 @@
1
+ <%
2
+ domain = "local"
3
+ sites = %w{ diabetescirkel gho ghogo hagroweert hco hznk meditta roha rohwn symfonie test zorgmobiel }
4
+ %>
5
+
6
+ nginx:
7
+ listen: 127.0.0.1
8
+ port: 8088
9
+ ssl: false
10
+ ssl_rewrite: false
11
+ cert_path:
12
+ key_path:
13
+
14
+ applications:
15
+ account:
16
+ port: 20101
17
+
18
+ base:
19
+ port: 20201
20
+
21
+ base2:
22
+ port: 20210
23
+
24
+ base2api:
25
+ port: 1980
26
+
27
+ chronic:
28
+ port: 20301
29
+
30
+ group:
31
+ port: 20401
32
+
33
+ group2:
34
+ port: 20410
35
+
36
+ bizsp:
37
+ port: 20801
38
+
39
+ clinic:
40
+ port: 20501
41
+
42
+ fundus:
43
+ port: 20601
44
+
45
+ ptlist:
46
+ port: 20701
47
+
48
+ benchmark:
49
+ port: 21101
50
+
51
+ logistics:
52
+ port: 21301
53
+
54
+ hub:
55
+ port: 21401
56
+
57
+ wapi:
58
+ port: 21501
59
+
60
+ supporthub:
61
+ port: 21601
62
+
63
+ datamon:
64
+ port: 21801
65
+
66
+ insurance:
67
+ port: 21901
68
+
69
+ reporting:
70
+ port: 22001
71
+
72
+ helpy:
73
+ port: 27101
74
+
75
+ hosts:
76
+ account:
77
+ host:
78
+ <%=
79
+ sites.collect do |s|
80
+ " - \"#{s}.#{domain}\""
81
+ end.join("\n")
82
+ %>
83
+ routes:
84
+ '/': account_proxy
85
+ '/group': group2_proxy
86
+ '/hub': hub_proxy
87
+ '/insurance': insurance_proxy
88
+ '/reporting': reporting_proxy
89
+ '/logistics': logistics_proxy
90
+ '/clinic': clinic_proxy
91
+ '/fundus': fundus_proxy
92
+ '/ptlist': ptlist_proxy
93
+ '/group1': group_proxy
94
+
95
+
96
+ base2api:
97
+ host: <%= "base2-api.#{domain}" %>
98
+ routes:
99
+ '/': base2api_proxy
100
+
101
+ helpy:
102
+ host: <%= "helpy.#{domain}" %>
103
+ routes:
104
+ '/': helpy_proxy
105
+
106
+ supporthub:
107
+ host: <%= "supporthub.#{domain}" %>
108
+ routes:
109
+ '/': supporthub_proxy
110
+
111
+ bizsp:
112
+ host: <%= "bizsupport.#{domain}" %>
113
+ routes:
114
+ '/': bizsp_proxy
115
+
116
+ chronic:
117
+ host:
118
+ <%=
119
+ sites.collect do |s|
120
+ " - \"chronic-#{s}.#{domain}\""
121
+ end.join("\n")
122
+ %>
123
+ routes:
124
+ '/': chronic_proxy
125
+
126
+ wapi:
127
+ host:
128
+ <%=
129
+ sites.collect do |s|
130
+ " - \"wapi.#{s}.#{domain}\""
131
+ end.join("\n")
132
+ %>
133
+ routes:
134
+ '/': wapi_proxy
135
+
136
+ server:
137
+ default_host: account
@@ -0,0 +1,31 @@
1
+ # Nginx Configuration File
2
+ worker_processes 4;
3
+ error_log logs/error.log;
4
+ pid run/nginx.pid;
5
+
6
+ events {
7
+ worker_connections 1024;
8
+ }
9
+
10
+ http {
11
+ include config/mime.types;
12
+ default_type application/octet-stream;
13
+ server_names_hash_bucket_size 256;
14
+ access_log logs/access.log;
15
+ sendfile on;
16
+ keepalive_timeout 0;
17
+ tcp_nodelay on;
18
+ gzip on;
19
+ gzip_min_length 1024;
20
+ gzip_buffers 4 8k;
21
+ gzip_types application/x-javascript text/css text/plain;
22
+ client_max_body_size 100m;
23
+
24
+ client_body_temp_path tmp/client_body;
25
+ proxy_temp_path tmp/proxy;
26
+ fastcgi_temp_path tmp/fastcgi;
27
+ uwsgi_temp_path tmp/uwsgi;
28
+ scgi_temp_path tmp/scgi;
29
+
30
+ include ../conf.d/*.conf;
31
+ }
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "spawngebob/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "spawngebob"
7
+ s.version = Spawngebob::VERSION
8
+ s.authors = ["Jan Mendoza"]
9
+ s.email = ["poymode@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{user based webserver non-root-runner and configurator}
12
+ s.description = %q{}
13
+
14
+ s.rubyforge_project = "spawngebob"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spawngebob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Mendoza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-07 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ''
15
+ email:
16
+ - poymode@gmail.com
17
+ executables:
18
+ - spawn
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - Rakefile
25
+ - bin/spawn
26
+ - lib/spawngebob.rb
27
+ - lib/spawngebob/compilers.rb
28
+ - lib/spawngebob/compilers/nginx.rb
29
+ - lib/spawngebob/constants.rb
30
+ - lib/spawngebob/runner.rb
31
+ - lib/spawngebob/spawner.rb
32
+ - lib/spawngebob/utils.rb
33
+ - lib/spawngebob/version.rb
34
+ - lib/templates/apps.yml.sample
35
+ - lib/templates/nginx.conf.sample
36
+ - spawngebob.gemspec
37
+ homepage: ''
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project: spawngebob
57
+ rubygems_version: 1.8.10
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: user based webserver non-root-runner and configurator
61
+ test_files: []