daemontools 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in daemontools.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 sh84
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,31 @@
1
+ # Daemontools
2
+
3
+ Manage daemontools from ruby code.
4
+
5
+ ## Installation
6
+
7
+ chmod 0777 /etc/service/ && sudo
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'daemontools'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install daemontools
20
+
21
+ ## Usage
22
+
23
+
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'daemontools/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ["sh"]
8
+ gem.email = ["cntyrf@gmail.com"]
9
+ gem.description = %q{Distributed storage}
10
+ gem.summary = %q{Distributed storage}
11
+ gem.homepage = ""
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "daemontools"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Daemontools::VERSION
19
+
20
+ gem.add_development_dependency "bundler"
21
+ gem.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,120 @@
1
+ require "daemontools/version"
2
+ require 'etc'
3
+ require 'erb'
4
+
5
+ module Daemontools
6
+ class << self
7
+ attr_accessor :svc_root, :log_root
8
+ end
9
+ @svc_root = '/etc/service'
10
+ @log_root = '/var/log/svc'
11
+
12
+ def self.exists?(name)
13
+ check_service_exists(name, false)
14
+ end
15
+
16
+ def self.status(name)
17
+ check_service_exists(name)
18
+ r = `sudo svstat #{@path} 2>&1`
19
+ raise r if $?.exitstatus != 0
20
+ raise "Unknown status" unless r.match(/.*?:\s*(\S+).*(\d+) seconds.*/)
21
+ [$1, $2.to_i]
22
+ end
23
+
24
+ def self.up?(name)
25
+ status(name)[0] == "up"
26
+ end
27
+
28
+ def self.down?(name)
29
+ status(name)[0] == "down"
30
+ end
31
+
32
+ def self.stop(name)
33
+ run_svc(name, 'd')
34
+ end
35
+
36
+ def self.start(name)
37
+ run_svc(name, 'u')
38
+ end
39
+
40
+ def self.restart(name)
41
+ run_svc(name, 't')
42
+ end
43
+
44
+ def self.add(name, command, options = {})
45
+ @name = name
46
+ @command = command
47
+ @log_dir = options[:log_dir] || "#{@log_root}/#{@name}"
48
+ @pre_command = options[:pre_command]
49
+ @sleep = options[:sleep] || 3
50
+ @path = "#{@svc_root}/#{name}"
51
+
52
+ if Dir.exists?(@path)
53
+ stop(name)
54
+ else
55
+ Dir.mkdir(@path)
56
+ end
57
+ File.open("#{@path}/down", 'w') {|f| f.write('')}
58
+ Dir.mkdir("#{@path}/log") unless Dir.exists?("#{@path}/log")
59
+ File.open("#{@path}/log/run", 'w', 0755) {|f| f.write(run_template('log.erb'))}
60
+ File.open("#{@path}/run", 'w', 0755) {|f| f.write(run_template('run.erb'))}
61
+ true
62
+ end
63
+
64
+ def self.delete(name)
65
+ check_service_exists(name)
66
+ stop(name)
67
+ r = `sudo rm -rf #{@path} 2>&1`
68
+ raise r if $?.exitstatus != 0
69
+ true
70
+ end
71
+
72
+ def self.run_status(name)
73
+ check_service_exists(name)
74
+ File.exists?("#{@path}/down") ? "down" : "up"
75
+ end
76
+
77
+ def self.run_status_up?(name)
78
+ run_status(name) == "up"
79
+ end
80
+
81
+ def self.run_status_down?(name)
82
+ run_status(name) == "down"
83
+ end
84
+
85
+ def self.make_run_status_up(name)
86
+ File.delete("#{@path}/down")
87
+ true
88
+ end
89
+
90
+ def self.make_run_status_down(name)
91
+ check_service_exists(name)
92
+ File.open("#{@path}/down", 'w') {|f| f.write('')}
93
+ true
94
+ end
95
+
96
+ private
97
+
98
+ def self.check_service_exists(name, raise_error = true)
99
+ @path = "#{@svc_root}/#{name}"
100
+ if raise_error
101
+ raise "Service #{name} not exists" unless Dir.exists?(@path)
102
+ else
103
+ Dir.exists?(@path)
104
+ end
105
+ end
106
+
107
+ def self.run_svc(name, command)
108
+ check_service_exists(name)
109
+ r = `sudo svc -#{command} #{@path} 2>&1`
110
+ raise r if $?.exitstatus != 0
111
+ raise r if ! r.empty?
112
+ true
113
+ end
114
+
115
+ def self.run_template(template_name)
116
+ @user = Etc.getlogin
117
+ template_path = File.expand_path(File.dirname(__FILE__))+'/../templates/'+template_name
118
+ ERB.new(File.read(template_path)).result(binding())
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ module Daemontools
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daemontools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - sh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-22 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: '0'
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: '0'
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
+ description: Distributed storage
47
+ email:
48
+ - cntyrf@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - daemontools.gemspec
59
+ - lib/daemontools.rb
60
+ - lib/daemontools/version.rb
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ segments:
74
+ - 0
75
+ hash: 2101739418026685474
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ segments:
83
+ - 0
84
+ hash: 2101739418026685474
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.24
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Distributed storage
91
+ test_files: []