slugrunner 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/slugrunner +6 -0
- data/lib/slugrunner/cli.rb +38 -0
- data/lib/slugrunner/runner.rb +184 -0
- data/lib/slugrunner/version.rb +3 -0
- data/lib/slugrunner.rb +3 -0
- data/slugrunner.gemspec +23 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 lxfontes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Slugrunner::Rb
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'slugrunner-rb'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install slugrunner-rb
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/slugrunner-rb/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/slugrunner
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
|
3
|
+
module Slugrunner
|
4
|
+
module CLI
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def run
|
8
|
+
opts = Trollop::options do
|
9
|
+
opt :slug, 'Slug file', type: :string
|
10
|
+
opt :worker, 'Worker type', type: :string, default: 'web'
|
11
|
+
opt :instance, 'Instance number', default: 1
|
12
|
+
opt :delayed_bind, 'Port bind allowance', default: 0
|
13
|
+
opt :ping, 'Notify state updates', type: :string
|
14
|
+
opt :ping_interval, 'Update interval in seconds', default: 30
|
15
|
+
opt :bash, 'Run interactive shell', default: false
|
16
|
+
end
|
17
|
+
|
18
|
+
Trollop::die :slug, "must be present" if opts[:slug].nil?
|
19
|
+
Trollop::die :worker, "must be present" if opts[:worker].nil?
|
20
|
+
|
21
|
+
runner = Slugrunner::Runner.new(opts[:slug], opts[:worker], opts[:instance])
|
22
|
+
|
23
|
+
unless opts[:ping].nil?
|
24
|
+
runner.ping_url = opts[:ping]
|
25
|
+
runner.ping_interval = opts[:ping_interval]
|
26
|
+
end
|
27
|
+
|
28
|
+
runner.shell = opts[:bash]
|
29
|
+
|
30
|
+
if opts[:delayed_bind] > 0
|
31
|
+
runner.bind_delay = opts[:delayed_bind]
|
32
|
+
end
|
33
|
+
|
34
|
+
# will setup/forward signals and block until stopped
|
35
|
+
exit(runner.run)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'socket'
|
6
|
+
require 'timeout'
|
7
|
+
require 'open-uri'
|
8
|
+
|
9
|
+
module Slugrunner
|
10
|
+
class Runner
|
11
|
+
attr_accessor :bind_delay
|
12
|
+
attr_accessor :ping_url
|
13
|
+
attr_accessor :ping_interval
|
14
|
+
attr_accessor :shell
|
15
|
+
|
16
|
+
def initialize(slug, process_type, instance_number)
|
17
|
+
@slug = slug
|
18
|
+
@process_type = process_type
|
19
|
+
@ping = false
|
20
|
+
@bind_delay = 0
|
21
|
+
@ping_url = nil
|
22
|
+
@ping_interval = 0
|
23
|
+
@alive = false
|
24
|
+
@shell = false
|
25
|
+
@hostname = "#{@process_type}.#{instance_number}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def run
|
29
|
+
Dir.mktmpdir do |tmpdir|
|
30
|
+
logger("Scratch directory -> #{tmpdir}")
|
31
|
+
@slug_dir = tmpdir
|
32
|
+
@ping = true if !@ping_url.nil? && @ping_interval > 0
|
33
|
+
download_and_run
|
34
|
+
return 0
|
35
|
+
end
|
36
|
+
rescue => e
|
37
|
+
logger("Error: #{e}")
|
38
|
+
return 1
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def download_and_run
|
44
|
+
notify(:setup) if @ping
|
45
|
+
|
46
|
+
fetch_slug
|
47
|
+
proc_command = if @shell
|
48
|
+
'/bin/bash'
|
49
|
+
else
|
50
|
+
extract_command_from_procfile || extract_command_from_release
|
51
|
+
end
|
52
|
+
fail "Couldn't find command for process type #{@process_type}" unless proc_command
|
53
|
+
|
54
|
+
command = prepare_env(proc_command)
|
55
|
+
@child = fork { exec(command) }
|
56
|
+
trap_signals
|
57
|
+
|
58
|
+
@alive = check_port
|
59
|
+
kill_child unless @alive
|
60
|
+
|
61
|
+
start_ping if @ping
|
62
|
+
|
63
|
+
Process.wait
|
64
|
+
|
65
|
+
@alive = false
|
66
|
+
notify(:stop) if @ping
|
67
|
+
end
|
68
|
+
|
69
|
+
def check_port
|
70
|
+
port = (ENV['PORT'] || '0').to_i
|
71
|
+
|
72
|
+
return true if @bind_delay == 0 || port == 0
|
73
|
+
|
74
|
+
start_ts = Time.now
|
75
|
+
stop_ts = start_ts.to_i + @bind_delay
|
76
|
+
while Time.now.to_i < stop_ts
|
77
|
+
return true if is_port_open?(port)
|
78
|
+
end
|
79
|
+
|
80
|
+
logger("Port hasn't been open after #{@bind_delay} seconds.")
|
81
|
+
false
|
82
|
+
end
|
83
|
+
|
84
|
+
def start_ping
|
85
|
+
return unless @alive
|
86
|
+
|
87
|
+
notify(:start)
|
88
|
+
|
89
|
+
Thread.new do
|
90
|
+
while @alive
|
91
|
+
sleep(@ping_interval)
|
92
|
+
notify(:update)
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def trap_signals
|
99
|
+
%w{INT HUP TERM QUIT}.each do |sig|
|
100
|
+
Signal.trap(sig) do
|
101
|
+
logger("Forwarding #{sig} to #{@child}")
|
102
|
+
Process.kill(sig, @child)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def kill_child
|
108
|
+
if @child
|
109
|
+
logger("Killing process #{@child} with SIGTERM.")
|
110
|
+
Process.kill('TERM', @child)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def notify(state)
|
115
|
+
# these are fire and forget for now
|
116
|
+
ap = "state=#{state}&hostname=#{@hostname}"
|
117
|
+
if @ping_url =~ /\?/
|
118
|
+
open("#{@ping_url}&#{ap}")
|
119
|
+
else
|
120
|
+
open("#{@ping_url}?#{ap}")
|
121
|
+
end
|
122
|
+
rescue => e
|
123
|
+
logger("Failed to notify #{@ping_url}: #{e}")
|
124
|
+
end
|
125
|
+
|
126
|
+
def logger(str)
|
127
|
+
puts("[slugrunner] #{str}")
|
128
|
+
end
|
129
|
+
|
130
|
+
def is_port_open?(port)
|
131
|
+
Timeout::timeout(1) do
|
132
|
+
begin
|
133
|
+
s = TCPSocket.new('localhost', port)
|
134
|
+
s.close
|
135
|
+
return true
|
136
|
+
rescue
|
137
|
+
return false
|
138
|
+
end
|
139
|
+
end
|
140
|
+
rescue
|
141
|
+
return false
|
142
|
+
end
|
143
|
+
|
144
|
+
def fetch_slug
|
145
|
+
if @slug =~ /^http/
|
146
|
+
`curl -s "#{@slug}" | tar -zxC #{@slug_dir}`
|
147
|
+
else
|
148
|
+
`tar zx -C #{@slug_dir} -f #{@slug}`
|
149
|
+
end
|
150
|
+
|
151
|
+
fail 'Failed to decompress slug' if $?.exitstatus != 0
|
152
|
+
end
|
153
|
+
|
154
|
+
def extract_command_from_procfile
|
155
|
+
return nil unless File.exists?("#{@slug_dir}/Procfile")
|
156
|
+
|
157
|
+
procfile = YAML.load_file("#{@slug_dir}/Procfile")
|
158
|
+
return procfile[@process_type] if procfile.key?(@process_type)
|
159
|
+
end
|
160
|
+
|
161
|
+
def extract_command_from_release
|
162
|
+
return nil unless File.exists?("#{@slug_dir}/.release")
|
163
|
+
|
164
|
+
release = YAML.load_file("#{@slug_dir}/.release")
|
165
|
+
return release['default_process_types'][@process_type] if release['default_process_types'].key?(@process_type)
|
166
|
+
end
|
167
|
+
|
168
|
+
def prepare_env(cmd)
|
169
|
+
<<-eos
|
170
|
+
env - bash -c '
|
171
|
+
cd #{@slug_dir}
|
172
|
+
export HOME=#{@slug_dir}
|
173
|
+
export APP_DIR=#{@slug_dir}
|
174
|
+
export HOSTNAME="#{@hostname}"
|
175
|
+
export SLUGRUNNER=1
|
176
|
+
export SLUG_ENV=1
|
177
|
+
PORT=#{ENV['PORT'] || 0}
|
178
|
+
for file in .profile.d/*; do source $file; done
|
179
|
+
exec #{cmd}
|
180
|
+
'
|
181
|
+
eos
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
data/lib/slugrunner.rb
ADDED
data/slugrunner.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slugrunner/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slugrunner"
|
8
|
+
spec.version = Slugrunner::VERSION
|
9
|
+
spec.authors = ["lxfontes"]
|
10
|
+
spec.email = ["lxfontes@gmail.com"]
|
11
|
+
spec.summary = %q{Running application slugs}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_runtime_dependency 'trollop'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slugrunner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- lxfontes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.5'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: trollop
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description:
|
63
|
+
email:
|
64
|
+
- lxfontes@gmail.com
|
65
|
+
executables:
|
66
|
+
- slugrunner
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/slugrunner
|
76
|
+
- lib/slugrunner.rb
|
77
|
+
- lib/slugrunner/cli.rb
|
78
|
+
- lib/slugrunner/runner.rb
|
79
|
+
- lib/slugrunner/version.rb
|
80
|
+
- slugrunner.gemspec
|
81
|
+
homepage: ''
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.23
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: Running application slugs
|
106
|
+
test_files: []
|