daemontools 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
4
+ require 'daemontools'
5
+
6
+ environment = ARGV[0] || "production"
7
+ roles = ARGV[1] || ""
8
+ puts "Building svc services.."
9
+
10
+ builder = Daemontools::Builder.new("#{Dir.pwd}/config/services.rb")
11
+ p builder.all_roles
12
+
13
+ builder.gen(roles, environment)
data/lib/daemontools.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "daemontools/version"
2
+ require "daemontools/service_builder"
2
3
  require 'etc'
3
4
  require 'erb'
4
5
 
@@ -0,0 +1,34 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :daemontools do
3
+ desc "Update daemontools services using services.rb"
4
+ task :update do
5
+ builder = Daemontools::Builder.new("#{Dir.pwd}/config/services.rb")
6
+
7
+ servers = find_servers.inject({}) do |map, server|
8
+ map[server] = role_names_for_host(server) & builder.all_roles
9
+ map
10
+ end.select {|server, roles| roles.size > 0}
11
+
12
+ if servers.any?
13
+ if task_call_frames[0].task.fully_qualified_name == 'deploy:rollback'
14
+ path = fetch(:previous_release)
15
+ puts "rollback 1"
16
+ else
17
+ path = fetch(:release_path)
18
+
19
+ servers.each do |server, roles|
20
+ command = "cd #{path} && daemontools-gen #{fetch :rails_env, "production"} #{roles.join(',')}"
21
+ run command
22
+ end
23
+ end
24
+
25
+ on_rollback do
26
+ puts "rollback 2"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ before "deploy:finalize_update", "daemontools:update"
33
+ after "deploy:rollback", "daemontools:update"
34
+ end
@@ -0,0 +1,50 @@
1
+ module Daemontools
2
+ class Builder
3
+ attr_accessor :environment, :all_roles, :services, :curr_service_name
4
+
5
+ def initialize(filename)
6
+ @all_roles = []
7
+ @services = {}
8
+ eval(File.read(filename), binding())
9
+ end
10
+
11
+ def run_command(command_name, param)
12
+ self.class.send(:define_method, command_name) do |task, options = {}|
13
+ p = param.gsub(':task', task)
14
+ options.each {|key, val| p.gsub!(":#{key}", val.to_s) }
15
+ command(p, options)
16
+ end
17
+ end
18
+
19
+ def command(param, options = {})
20
+ Daemontools.stop(@curr_service_name) if Daemontools.exists?(@curr_service_name)
21
+ cmd = "/bin/bash -l -c '#{param.gsub(':environment', @environment)}' 2>&1"
22
+ Daemontools.add(@curr_service_name, cmd)
23
+ Daemontools.make_run_status_up(@curr_service_name)
24
+ Daemontools.start(@curr_service_name)
25
+ end
26
+
27
+ def service(name, opts = {}, &block)
28
+ if opts[:roles]
29
+ opts[:roles].each do |role|
30
+ role = role.to_sym
31
+ @all_roles << role unless @all_roles.member?(role)
32
+ @services[role] = [] unless @services[role]
33
+ @services[role] << [name, block]
34
+ end
35
+ else
36
+ raise "service without roles"
37
+ end
38
+ end
39
+
40
+ def gen(roles, env)
41
+ @environment = env
42
+ roles.split(',').each do |role|
43
+ (@services[role.to_sym] || []).each do |service|
44
+ @curr_service_name = service[0].to_s
45
+ service[1].call
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module Daemontools
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/templates/log.erb ADDED
@@ -0,0 +1,6 @@
1
+ #!/bin/sh
2
+
3
+ LOGDIR=<%= @log_dir %>
4
+ mkdir -p $LOGDIR
5
+ <%= "chmod 0777 $LOGDIR" if @user %>
6
+ exec <%= "setuidgid #{@user}" if @user %> multilog t s16000000 n25 $LOGDIR
data/templates/run.erb ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/sh
2
+ <%= "sleep #{@sleep}" if @sleep && @sleep.to_i > 0 %>
3
+ exec 2>&1
4
+ <%= @pre_command if @pre_command %>
5
+ exec <%= "setuidgid #{@user}" if @user %> <%= @command %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daemontools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-22 00:00:00.000000000 Z
12
+ date: 2013-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -46,7 +46,8 @@ dependencies:
46
46
  description: Distributed storage
47
47
  email:
48
48
  - cntyrf@gmail.com
49
- executables: []
49
+ executables:
50
+ - daemontools-gen
50
51
  extensions: []
51
52
  extra_rdoc_files: []
52
53
  files:
@@ -55,9 +56,14 @@ files:
55
56
  - LICENSE.txt
56
57
  - README.md
57
58
  - Rakefile
59
+ - bin/daemontools-gen
58
60
  - daemontools.gemspec
59
61
  - lib/daemontools.rb
62
+ - lib/daemontools/capistrano.rb
63
+ - lib/daemontools/service_builder.rb
60
64
  - lib/daemontools/version.rb
65
+ - templates/log.erb
66
+ - templates/run.erb
61
67
  homepage: ''
62
68
  licenses: []
63
69
  post_install_message:
@@ -72,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
78
  version: '0'
73
79
  segments:
74
80
  - 0
75
- hash: 2101739418026685474
81
+ hash: -4588921154621621218
76
82
  required_rubygems_version: !ruby/object:Gem::Requirement
77
83
  none: false
78
84
  requirements:
@@ -81,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
87
  version: '0'
82
88
  segments:
83
89
  - 0
84
- hash: 2101739418026685474
90
+ hash: -4588921154621621218
85
91
  requirements: []
86
92
  rubyforge_project:
87
93
  rubygems_version: 1.8.24