daemon-overlord 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 17aa7dde11842194e86584ae13ede9a1460f20d7
4
+ data.tar.gz: 14c22770833b6d44be76dadf88564f08fa7f0dcb
5
+ SHA512:
6
+ metadata.gz: dbff09891ffedfa23795988974215e099527cbe5dbfb538cb26a4bdd72df4747fe0cf4ae8c0391ba737f6ecd443d3056aa69c3382167368f43bd4b60792e6ade
7
+ data.tar.gz: 28d1a69c2be2d0700bfbb2b2b58e19696b1be7598d9fd12e5d1b13762d53385f3a04e43927548cc492f8d6c6c113141f443d7fb81cea6785dd49c0e7f67fae47
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in overlord.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dustin Morrill
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Overlord
2
+
3
+ [![Build Status](https://travis-ci.org/dmorrill10/overlord.svg?branch=master)](https://travis-ci.org/dmorrill10/overlord)
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/overlord`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ TODO: Delete this and the text above, and describe your gem
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'overlord'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install overlord
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/overlord.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "overlord"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,165 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'yaml'
4
+ require 'csv'
5
+ require 'process_runner'
6
+
7
+ module Overlord
8
+
9
+ DEFAULT_CONFIG_FILE_PATH = File.expand_path('../../config/overlord.yml', __FILE__)
10
+ DEFAULT_PIDS_FILE_PATH = File.expand_path('../../tmp/pids', __FILE__)
11
+
12
+ def self.with_config_and_pids_files(options)
13
+ config_path = File.absolute_path options[:config]
14
+ pids_path = File.absolute_path options[:pids]
15
+ File.open(config_path, 'r') do |config_file|
16
+ config = YAML.load(config_file) || {}
17
+ if !File.exists?(pids_path)
18
+ File.open(pids_path, 'w') do |pids_file|
19
+ yield config_file, config, pids_file, {} if block_given?
20
+ end
21
+ else
22
+ File.open(pids_path, 'r+') do |pids_file|
23
+ pids = YAML.load(pids_file) || {}
24
+ pids_file.seek(0, IO::SEEK_SET)
25
+ pids_file.truncate(0)
26
+ yield config_file, config, pids_file, pids if block_given?
27
+ end
28
+ end
29
+ end
30
+ end
31
+ def self.process_exists?(pid)
32
+ begin
33
+ Process.getpgid pid
34
+ true
35
+ rescue Errno::ESRCH
36
+ false
37
+ end
38
+ end
39
+ def self.kill_process(pid) Process.kill('TERM', pid) end
40
+ def self.force_kill_process(pid) Process.kill('KILL', pid) end
41
+
42
+ module Actions
43
+ def start_daemon(app, config, pids, pids_file_path)
44
+ if pids[app]
45
+ if Overlord.process_exists?(pids[app])
46
+ say "WARNING: #{app} is already started (PID: #{pids[app]})."
47
+ return
48
+ else
49
+ say "WARNING: #{app} was supposed to be running on with PID #{pids[app]} but it has died. Updating #{pids_file_path}."
50
+ pids.delete app
51
+ end
52
+ end
53
+ command = config[app]
54
+
55
+ desc = "`#{command}` from #{Dir.pwd}"
56
+
57
+ say_status :run_as_daemon, desc
58
+ pid = Process.detach(Process.spawn(command)).pid
59
+ pids[app] = pid
60
+ say_status :start, "#{app} started with PID #{pid}."
61
+ end
62
+
63
+ def stop_daemon(app, config, pids, pids_file_path)
64
+ if pids[app]
65
+ if Overlord.process_exists?(pids[app])
66
+ Overlord.kill_process pids[app]
67
+ sleep 1 # Give the process a chance to exit
68
+
69
+ if Overlord.process_exists?(pids[app])
70
+ Overlord.force_kill_process pids[app]
71
+ sleep 1 # Give the process a chance to exit
72
+
73
+ if Overlord.process_exists?(pids[app])
74
+ say "ERROR: #{app} (PID: #{pids[app]}) could not be stopped."
75
+ end
76
+ end
77
+ else
78
+ say "WARNING: #{app} was supposed to be running on with PID #{pids[app]} but it has died. Updating #{pids_file_path}."
79
+ end
80
+ say_status :stop, "#{app} stopped."
81
+ pids.delete app
82
+ else
83
+ say "WARNING: #{app} was not running."
84
+ end
85
+ end
86
+ end
87
+
88
+ class App < Thor
89
+ include Thor::Actions
90
+ include Actions
91
+
92
+ def self.with_config
93
+ method_option :config, :aliases => ['-c'], :default => DEFAULT_CONFIG_FILE_PATH, :desc => "YAML Config file listing applications to manage."
94
+ end
95
+ def self.with_pids
96
+ method_option :pids, :aliases => ['-p'], :default => DEFAULT_PIDS_FILE_PATH, :desc => "Path to SSV file where application PIDs will be saved."
97
+ end
98
+
99
+ desc "status", "Show the status of managed applications."
100
+ with_config
101
+ with_pids
102
+ def status
103
+ Overlord.with_config_and_pids_files(options) do |config_file, config, pids_file, pids|
104
+ config.keys.each do |app|
105
+ if pids[app]
106
+ if Overlord.process_exists?(pids[app])
107
+ say "#{app} is running (PID: #{pids[app]})."
108
+ else
109
+ say "WARNING: #{app} was supposed to be running on with PID #{pids[app]} but it has died. Updating #{pids_file.path}."
110
+ pids.delete app
111
+ end
112
+ else
113
+ say "#{app} is not running."
114
+ end
115
+ end
116
+ pids_file.write(YAML.dump(pids)) unless pids.empty?
117
+ end
118
+ end
119
+
120
+ desc "start", "Start an application."
121
+ with_config
122
+ with_pids
123
+ method_option :app, :aliases => ['-a'], :desc => "Application to start. Starts all if none specified."
124
+ def start
125
+ Overlord.with_config_and_pids_files(options) do |config_file, config, pids_file, pids|
126
+ if options[:app]
127
+ start_daemon(options[:app], config, pids, pids_file.path)
128
+ else
129
+ config.keys.each do |app|
130
+ start_daemon(app, config, pids, pids_file.path)
131
+ end
132
+ end
133
+ pids_file.write(YAML.dump(pids)) unless pids.empty?
134
+ end
135
+ end
136
+
137
+ desc "stop", "Stop an application."
138
+ with_config
139
+ with_pids
140
+ method_option :app, :aliases => ['-a'], :desc => "Application to stop. Stops all if none specified."
141
+ def stop
142
+ Overlord.with_config_and_pids_files(options) do |config_file, config, pids_file, pids|
143
+ if options[:app]
144
+ stop_daemon(options[:app], config, pids, pids_file.path)
145
+ else
146
+ config.keys.each do |app|
147
+ stop_daemon(app, config, pids, pids_file.path)
148
+ end
149
+ end
150
+ pids_file.write(YAML.dump(pids)) unless pids.empty?
151
+ end
152
+ end
153
+
154
+ desc "restart", "Restart an application."
155
+ with_config
156
+ with_pids
157
+ method_option :app, :aliases => ['-a'], :desc => "Application to restart. Restarts all if none specified."
158
+ def restart
159
+ stop
160
+ start
161
+ end
162
+ end
163
+ end
164
+
165
+ Overlord::App.start
@@ -0,0 +1,5 @@
1
+ require "overlord/version"
2
+
3
+ module Overlord
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module Overlord
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'overlord/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "daemon-overlord"
8
+ spec.version = Overlord::VERSION
9
+ spec.authors = ["Dustin Morrill"]
10
+ spec.email = ["dmorrill10@gmail.com"]
11
+
12
+ spec.summary = %q{Start and stop daemons with minimal monitoring.}
13
+ spec.homepage = "https://github.com/dmorrill10/overlord"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "process_runner"
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daemon-overlord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dustin Morrill
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: process_runner
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description:
84
+ email:
85
+ - dmorrill10@gmail.com
86
+ executables:
87
+ - overlord
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - exe/overlord
100
+ - lib/overlord.rb
101
+ - lib/overlord/version.rb
102
+ - overlord.gemspec
103
+ homepage: https://github.com/dmorrill10/overlord
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Start and stop daemons with minimal monitoring.
127
+ test_files: []