freyr 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.2
data/freyr.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{freyr}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tal Atlas"]
12
- s.date = %q{2011-01-24}
12
+ s.date = %q{2011-06-09}
13
13
  s.default_executable = %q{freyr}
14
14
  s.description = %q{Define all services you need to run and this will launch,daemonize,and monitor them for you.}
15
15
  s.email = %q{me@tal.by}
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
37
37
  "lib/freyr/command.rb",
38
38
  "lib/freyr/pinger.rb",
39
39
  "lib/freyr/service.rb",
40
+ "lib/freyr/service_group.rb",
40
41
  "lib/freyr/service_info.rb",
41
42
  "spec/freyr_spec.rb",
42
43
  "spec/spec.opts",
@@ -44,7 +45,7 @@ Gem::Specification.new do |s|
44
45
  ]
45
46
  s.homepage = %q{http://github.com/Talby/freyr}
46
47
  s.require_paths = ["lib"]
47
- s.rubygems_version = %q{1.3.7}
48
+ s.rubygems_version = %q{1.5.0}
48
49
  s.summary = %q{Service manager and runner}
49
50
  s.test_files = [
50
51
  "spec/freyr_spec.rb",
@@ -52,7 +53,6 @@ Gem::Specification.new do |s|
52
53
  ]
53
54
 
54
55
  if s.respond_to? :specification_version then
55
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
56
  s.specification_version = 3
57
57
 
58
58
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -1,6 +1,11 @@
1
1
  module Freyr
2
2
  class CLI < Thor
3
3
 
4
+ def initialize(*)
5
+ super
6
+ get_services
7
+ end
8
+
4
9
  private
5
10
  # Ugh, this is ugly, colorizing stuff is rough
6
11
  def list_all_services(args={})
@@ -87,13 +92,22 @@ module Freyr
87
92
  end
88
93
 
89
94
  def get_from_name name
90
- name ||= Dir.pwd.match(/.*\/(.+)/)[1]
95
+ group = ServiceGroup.new
96
+
97
+ unless name
98
+ s = Service.s.find {|svc| svc.dir == Dir.pwd}
99
+ return group << s if s
100
+ end
91
101
 
92
102
  if options.namespace && s = Service["#{options.namespace}:#{name}"].first
93
- [s] # only pickng one because if it's namespaced it's not a group
103
+ group << s # only pickng one because if it's namespaced it's not a group
94
104
  else
95
- Service[name]
105
+ Service[name].each do |s|
106
+ group << s
107
+ end
96
108
  end
109
+
110
+ group
97
111
  end
98
112
 
99
113
  def get_services
@@ -9,20 +9,14 @@ module Freyr
9
9
  names = services.collect {|s| s.name}
10
10
  say "Starting the " << set_color(names.join(', '), :blue) << ' services'
11
11
 
12
- changed_names = services.collect do |s|
13
- begin
14
- pid = s.start!
15
- s.name if pid
16
- rescue AdminRequired
17
- say "Please run in sudo to launch #{s.name}.", :red
18
- nil
19
- end
20
- end.compact
12
+ changed_names = services.run
21
13
 
22
14
  list_all_services(:highlight_state => changed_names).each {|l| say(l)}
23
15
  else
24
16
  say "Can't find the #{name} service", :red
25
17
  end
18
+ rescue AdminRequired
19
+ say "Please run in sudo to launch #{name}.", :red
26
20
  end
27
21
 
28
22
  desc 'stop [SERVICE=dirname]', 'Stop particular service'
@@ -31,43 +25,32 @@ module Freyr
31
25
  if !services.empty?
32
26
  names = services.collect {|s| s.name}
33
27
  say "Stopping the " << set_color(names.join(', '), :blue) << ' services'
34
- changed_names = services.collect {|s| s.name if s.alive?}.compact
35
28
 
36
- services.each do |s|
37
- begin
38
- s.stop!
39
- rescue AdminRequired
40
- say "Please run in sudo to stop #{s.name}.", :red
41
- end
42
- end
29
+ changed_names = services.stop
43
30
 
44
31
  list_all_services(:highlight_state => changed_names).each {|l| say(l)}
45
32
  else
46
33
  say "Can't find the #{name} service", :red
47
- end
34
+ end
35
+ rescue AdminRequired
36
+ say "Please run in sudo to stop #{name}.", :red
48
37
  end
49
38
 
50
39
  desc 'restart [SERVICE=dirname]', 'restart particular service'
51
40
  def restart(name=nil)
52
41
  services = get_from_name(name)
53
42
  if !services.empty?
54
- names = services.collect {|s| s.name}
55
- say "Restarting the " << set_color(names.join(', '), :blue) << ' services'
43
+ say "Restarting the " << set_color(services.collect {|s| s.name}.join(', '), :blue) << ' services'
56
44
 
57
- services.each do |s|
58
- begin
59
- s.restart!
60
- rescue AdminRequired
61
- say "Please run in sudo to launch #{s.name}.", :red
62
- name.delete(s.name)
63
- nil
64
- end
65
- end
45
+ names = services.restart
66
46
 
67
47
  list_all_services(:highlight_state => names).each {|l| say(l)}
68
48
  else
69
49
  say "Can't find the #{name} service", :red
70
50
  end
51
+
52
+ rescue AdminRequired
53
+ say "Please run in sudo to launch #{name}.", :red
71
54
  end
72
55
 
73
56
  end
data/lib/freyr/cli.rb CHANGED
@@ -7,11 +7,6 @@ module Freyr
7
7
  class CLI < Thor
8
8
  include Thor::Actions
9
9
 
10
- def initialize(*)
11
- super
12
- get_services
13
- end
14
-
15
10
  default_task :list
16
11
  class_option :'config-file', :desc => 'config file to use', :type => :string
17
12
  class_option :'ignore-local', :desc => "don't use the local Freyrfile or .freyrrc", :type => :boolean, :default => false
data/lib/freyr/command.rb CHANGED
@@ -75,8 +75,7 @@ module Freyr
75
75
  end
76
76
 
77
77
  def delete_if_dead
78
- return if admin? && !is_root?
79
- File.delete(pid_file) unless alive?
78
+ !alive?
80
79
  end
81
80
 
82
81
  def save
@@ -145,7 +144,7 @@ module Freyr
145
144
  puts 'x',"Couldn't reach #{name} service"
146
145
  end
147
146
  else
148
- puts 'x'
147
+ puts 'x', "Service died durring launch"
149
148
  end
150
149
  end
151
150
 
@@ -224,22 +223,24 @@ module Freyr
224
223
 
225
224
  def spawn(command)
226
225
  fork do
227
- # File.umask self.umask if self.umask
228
- # uid_num = Etc.getpwnam(self.uid).uid if uid
229
- # gid_num = Etc.getgrnam(self.gid).gid if gid
226
+ File.umask self.umask if self.umask
227
+ uid_num = Etc.getpwnam(self.uid).uid if uid
228
+ gid_num = Etc.getgrnam(self.gid).gid if gid
230
229
 
231
- # ::Dir.chroot(self.chroot) if self.chroot
230
+ ::Dir.chroot(self.chroot) if self.chroot
232
231
  ::Process.setsid
233
- # ::Process.groups = [gid_num] if self.gid
234
- # ::Process::Sys.setgid(gid_num) if self.gid
235
- # ::Process::Sys.setuid(uid_num) if self.uid
232
+ ::Process.groups = [gid_num] if self.gid
233
+ ::Process::Sys.setgid(gid_num) if self.gid
234
+ ::Process::Sys.setuid(uid_num) if self.uid
236
235
  chdir
237
236
  $0 = "freyr - #{name} (#{command})"
238
237
  STDIN.reopen "/dev/null"
239
238
  if log_cmd
240
239
  STDOUT.reopen IO.popen(log_cmd, "a")
241
- elsif log
240
+ elsif log && service.write_log?
242
241
  STDOUT.reopen log, "a"
242
+ else
243
+ STDOUT.reopen "/dev/null"
243
244
  end
244
245
  if err_log_cmd
245
246
  STDERR.reopen IO.popen(err_log_cmd, "a")
data/lib/freyr/service.rb CHANGED
@@ -17,7 +17,11 @@ module Freyr
17
17
  end
18
18
 
19
19
  def log
20
- @service_info.log || File.join(command.file_dir,"#{name}.log")
20
+ @service_info.read_log || @service_info.log || File.join(command.file_dir,"#{name}.log")
21
+ end
22
+
23
+ def write_log?
24
+ @service_info.log && !@service_info.read_log
21
25
  end
22
26
 
23
27
  def start!
@@ -51,6 +55,7 @@ module Freyr
51
55
 
52
56
  def tail!(size = 600, follow = true)
53
57
  f = follow ? 'f' : ''
58
+ Dir.chdir dir
54
59
  exec("tail -#{size}#{f} #{log}")
55
60
  end
56
61
 
@@ -96,12 +101,26 @@ module Freyr
96
101
  @all_services += services
97
102
  end
98
103
 
104
+ def alive?(name)
105
+ !!self[name].find do |ser|
106
+ ser.alive?
107
+ end
108
+ end
109
+
99
110
  def [](name)
111
+ group = ServiceGroup.new
112
+
100
113
  if ser = s.find {|sr| sr.matches?(name)}
101
- [ser]
114
+ group << ser
102
115
  else
103
- s.select {|sr| sr.is_group?(name)}
116
+ s.each do |sr|
117
+ if sr.is_group?(name)
118
+ group << sr
119
+ end
120
+ end
104
121
  end
122
+
123
+ group
105
124
  end
106
125
 
107
126
  end
@@ -0,0 +1,68 @@
1
+ module Freyr
2
+ class ServiceGroup < Array
3
+
4
+ def initialize(*args)
5
+ super(*args)
6
+ end
7
+
8
+ def find_by_name(n)
9
+ find {|s| s.name == n}
10
+ end
11
+
12
+ # Take care this can make a stack overflow
13
+ def run
14
+ return [] if empty?
15
+
16
+ needs_to_run = ServiceGroup.new
17
+
18
+ kill = false
19
+ names = []
20
+
21
+ each do |svc|
22
+
23
+ unless svc.dependencies.empty?
24
+ if n = svc.dependencies.find {|s| !Service.alive?(s)}
25
+ if find_by_name(n)
26
+ needs_to_run << svc
27
+ elsif s = Service[n].first
28
+ needs_to_run << s
29
+ needs_to_run << svc
30
+ else
31
+ puts "Can't run #{svc.name} because dependency #{n} cannot be found"
32
+ kill = true
33
+ end
34
+
35
+ next
36
+ end
37
+ end
38
+
39
+ pid = svc.start!
40
+ names << svc.name if pid
41
+ end
42
+
43
+ names += needs_to_run.run unless kill
44
+ names
45
+ end
46
+
47
+ def stop
48
+ changed_names = collect {|s| s.name if s.alive?}.compact
49
+ each do |svc|
50
+ svc.stop!
51
+ end
52
+
53
+ changed_names
54
+ end
55
+
56
+ def restart
57
+ names = collect {|s| s.name}
58
+
59
+ each do |s|
60
+ s.restart!
61
+ names.delete(s.name)
62
+ end
63
+
64
+ names
65
+ end
66
+
67
+ end
68
+ end
@@ -4,11 +4,12 @@ module Freyr
4
4
 
5
5
  ATTRS = [:name,:dir,:log_cmd,:log,:err_log_cmd,:err_log,:umask,
6
6
  :uid,:gid,:chroot,:proc_match,:restart,:stop,:stop_sig,
7
- :restart_sig,:sudo,:groups,:ping,:also]
7
+ :restart_sig,:sudo,:groups,:ping,:also,:dependencies,:read_log]
8
8
 
9
9
  def initialize(name=nil, args={}, &block)
10
10
  @groups = []
11
11
  @also = []
12
+ @dependencies = []
12
13
  if name.is_a?(Hash)
13
14
  @name = name.keys.first
14
15
  @groups << name[@name]
@@ -27,6 +28,10 @@ module Freyr
27
28
  @groups |= val
28
29
  end
29
30
 
31
+ def requires *val
32
+ @dependencies |= val
33
+ end
34
+
30
35
  def also_as(*val)
31
36
  @also |= val
32
37
  end
data/lib/freyr.rb CHANGED
@@ -4,6 +4,6 @@ module Freyr
4
4
  VERSION = File.open(File.expand_path(File.dirname(__FILE__)+'/../VERSION')).read
5
5
  end
6
6
 
7
- %w{service_info service command pinger}.each do |f|
7
+ %w{service_info service_group service command pinger}.each do |f|
8
8
  require File.expand_path(File.dirname(__FILE__)+"/freyr/#{f}.rb")
9
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freyr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tal Atlas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-24 00:00:00 -05:00
18
+ date: 2011-06-09 00:00:00 -04:00
19
19
  default_executable: freyr
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -91,6 +91,7 @@ files:
91
91
  - lib/freyr/command.rb
92
92
  - lib/freyr/pinger.rb
93
93
  - lib/freyr/service.rb
94
+ - lib/freyr/service_group.rb
94
95
  - lib/freyr/service_info.rb
95
96
  - spec/freyr_spec.rb
96
97
  - spec/spec.opts
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  requirements: []
126
127
 
127
128
  rubyforge_project:
128
- rubygems_version: 1.3.7
129
+ rubygems_version: 1.5.0
129
130
  signing_key:
130
131
  specification_version: 3
131
132
  summary: Service manager and runner