init 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,22 @@
1
1
  #!/usr/bin/env ruby
2
- #
3
- # Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2010
4
- #
5
- # This file is part of Init.
6
- #
7
- # Init is free software: you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation, either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
2
+ # encoding: UTF-8
3
+ =begin
4
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
5
+
6
+ This file is part of Init.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted, provided that the above
10
+ copyright notice and this permission notice appear in all copies.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
14
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
+ PERFORMANCE OF THIS SOFTWARE.
19
+ =end
19
20
 
20
21
  require 'aef/init'
21
22
 
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Init.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require File.expand_path('../lib/aef/init/version', __FILE__)
21
+
22
+ Gem::Specification.new do |s|
23
+ s.name = 'init'
24
+ s.version = Aef::Init::VERSION.dup
25
+ s.authors = ['Alexander E. Fischer']
26
+ s.email = ['aef@raxys.net']
27
+ s.homepage = 'http://github.com/aef/init'
28
+ s.license = 'ISC'
29
+ s.summary = 'Clean and simple *nix init scripts with Ruby'
30
+ s.description = <<-DESCRIPTION
31
+ Init is a lightweight framework for writing readable, reusable *nix init
32
+ scripts in Ruby.
33
+ DESCRIPTION
34
+
35
+ s.rubyforge_project = nil
36
+ s.has_rdoc = 'yard'
37
+ s.extra_rdoc_files = ['HISTORY.md', 'LICENSE.md']
38
+
39
+ s.files = `git ls-files`.lines.map(&:chomp)
40
+ s.test_files = `git ls-files -- {test,spec,features}/*`.lines.map(&:chomp)
41
+ s.executables = `git ls-files -- bin/*`.lines.map{|f| File.basename(f.chomp) }
42
+ s.require_paths = ["lib"]
43
+
44
+ s.required_ruby_version = '>= 1.8.7'
45
+
46
+ s.add_development_dependency('bundler', '~> 1.0.22')
47
+ s.add_development_dependency('rake', '~> 0.9.2')
48
+ s.add_development_dependency('rspec', '~> 2.8.0')
49
+ s.add_development_dependency('simplecov', '~> 0.6.1')
50
+ s.add_development_dependency('pry', '~> 0.9.8')
51
+ s.add_development_dependency('yard', '~> 0.7.5')
52
+
53
+ s.cert_chain = "#{ENV['GEM_CERT_CHAIN']}".split(':')
54
+ s.signing_key = ENV['GEM_SIGNING_KEY']
55
+ end
@@ -1,29 +1,133 @@
1
- # Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2010
2
- #
3
- # This file is part of Init.
4
- #
5
- # Init is free software: you can redistribute it and/or modify
6
- # it under the terms of the GNU General Public License as published by
7
- # the Free Software Foundation, either version 3 of the License, or
8
- # (at your option) any later version.
9
- #
10
- # This program is distributed in the hope that it will be useful,
11
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- # GNU General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU General Public License
16
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Init.
17
6
 
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'set'
18
21
  require 'pathname'
22
+ require 'aef/init/version'
19
23
 
20
- # Namespace for projects of Alexander E. Fischer <aef@raxys.net>
21
- #
24
+ # Namespace for projects of Alexander E. Fischer <aef@raxys.net>.
25
+ #
22
26
  # If you want to be able to simply type Example instead of Aef::Example to
23
27
  # address classes in this namespace simply write the following before using the
24
- # classes:
28
+ # classes.
25
29
  #
26
- # include Aef
30
+ # @example Including the namespace
31
+ # include Aef
32
+ # @author Alexander E. Fischer
27
33
  module Aef
28
- autoload :Init, 'aef/init/init'
34
+
35
+ # Clean and simple *nix init scripts with Ruby
36
+ class Init
37
+
38
+ class << self
39
+
40
+ # The default command to be called if no command is specified on the
41
+ # commandline.
42
+ #
43
+ # @note This used to be implicitly set to "restart" but in practice
44
+ # caused far too much unwanted service restarts, so it is now not
45
+ # enabled by default.
46
+ #
47
+ # @return [Symbol] a command's name
48
+ attr_writer :default_command
49
+
50
+ # The default command to be called if no command is specified on the
51
+ # commandline.
52
+ #
53
+ # @note This used to be implicitly set to "restart" but in practice
54
+ # caused far too much unwanted service restarts, so it is now not
55
+ # enabled by default.
56
+ def default_command(command = false)
57
+ if command.equal?(false)
58
+ @default_command
59
+ else
60
+ @default_command = command.to_sym
61
+ end
62
+ end
63
+
64
+ # The delay in seconds between the call of the stop and the start method
65
+ # in the predefined restart method.
66
+ #
67
+ # @return [Float] time in seconds
68
+ attr_writer :stop_start_delay
69
+
70
+ # The delay in seconds between the call of the stop and the start method
71
+ # in the predefined restart method.
72
+ #
73
+ # @param [Numeric, false] time in seconds, if false is given, the value
74
+ # will be returned and not modified.
75
+ # @return [Float] time in seconds
76
+ def stop_start_delay(seconds = false)
77
+ if seconds.equal?(false)
78
+ @stop_start_delay ||= 0.0
79
+ else
80
+ @stop_start_delay = seconds.to_f
81
+ end
82
+ end
83
+
84
+ # Call this to begin commandline parse.
85
+ #
86
+ # If an invalid command is specified on the commandline, a usage example
87
+ # is displayed. If no command is specified, the default command is
88
+ # started, if one is set.
89
+ def parse
90
+ command = ARGV.shift || :default
91
+
92
+ valid_commands = []
93
+
94
+ ancestors.each do |klass|
95
+ valid_commands += klass.public_instance_methods(false)
96
+ break if klass == Aef::Init
97
+ end
98
+
99
+ valid_commands = valid_commands.sort.map(&:to_sym).uniq!
100
+
101
+ command = command.to_sym
102
+
103
+ if command == :default && default_command
104
+ new.send(default_command)
105
+ elsif valid_commands.include?(command)
106
+ new.send(command)
107
+ else
108
+ puts "Usage: #$PROGRAM_NAME {#{valid_commands.join('|')}}"; exit false
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ # The start method needs to be implemented in a subclass
115
+ def start
116
+ warn 'start method needs to be implemented'
117
+ exit false
118
+ end
119
+
120
+ # The stop method needs to be implemented in a subclass
121
+ def stop
122
+ warn 'stop method needs to be implemented'
123
+ exit false
124
+ end
125
+
126
+ # By default restart simply calls stop and then start
127
+ def restart
128
+ stop
129
+ sleep self.class.stop_start_delay
130
+ start
131
+ end
132
+ end
29
133
  end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Init.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ module Aef
21
+ class Init
22
+
23
+ # The currently loaded library version
24
+ #
25
+ # Using Semantic Versioning (2.0.0-rc.1) rules
26
+ # @see http://semver.org/
27
+ VERSION = '2.0.0'.freeze
28
+
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Init.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ # Helper file to allow loading by gem name.
21
+
22
+ require 'aef/init'
@@ -1,24 +1,22 @@
1
1
  #!/usr/bin/env ruby
2
- #
3
- # Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2010
4
- #
5
- # This file is part of Init.
6
- #
7
- # Init is free software: you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation, either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
2
+ # encoding: UTF-8
3
+ =begin
4
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
19
5
 
20
- # This is a simple mock daemon which will write all commandline arguments it
21
- # recevies to a file specified as first commandline option
6
+ This file is part of Init.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted, provided that the above
10
+ copyright notice and this permission notice appear in all copies.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
14
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
+ PERFORMANCE OF THIS SOFTWARE.
19
+ =end
22
20
 
23
21
  if ARGV.empty?
24
22
  puts "Usage: #$PROGRAM_NAME output_file [arguments]"
@@ -1,21 +1,22 @@
1
1
  #!/usr/bin/env ruby
2
- #
3
- # Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2010
4
- #
5
- # This file is part of Init.
6
- #
7
- # Init is free software: you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation, either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
2
+ # encoding: UTF-8
3
+ =begin
4
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
5
+
6
+ This file is part of Init.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted, provided that the above
10
+ copyright notice and this permission notice appear in all copies.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
14
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
+ PERFORMANCE OF THIS SOFTWARE.
19
+ =end
19
20
 
20
21
  require 'rbconfig'
21
22
 
@@ -34,13 +35,15 @@ class MiddleInit < Aef::Init
34
35
  protected
35
36
 
36
37
  def middle_protected
37
- warn 'This should not be executable as a command'; exit false
38
+ warn 'This should not be executable as a command'
39
+ exit false
38
40
  end
39
41
 
40
42
  private
41
43
 
42
44
  def middle_private
43
- warn 'This should not be executable as a command'; exit false
45
+ warn 'This should not be executable as a command'
46
+ exit false
44
47
  end
45
48
  end
46
49
 
@@ -68,13 +71,15 @@ class SimpleInit < MiddleInit
68
71
  protected
69
72
 
70
73
  def end_protected
71
- warn 'This should not be executable as a command'; exit false
74
+ warn 'This should not be executable as a command'
75
+ exit false
72
76
  end
73
77
 
74
78
  private
75
79
 
76
80
  def end_private
77
- warn 'This should not be executable as a command'; exit false
81
+ warn 'This should not be executable as a command'
82
+ exit false
78
83
  end
79
84
  end
80
85
 
@@ -1,23 +1,23 @@
1
- # Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2010
2
- #
3
- # This file is part of Init.
4
- #
5
- # Init is free software: you can redistribute it and/or modify
6
- # it under the terms of the GNU General Public License as published by
7
- # the Free Software Foundation, either version 3 of the License, or
8
- # (at your option) any later version.
9
- #
10
- # This program is distributed in the hope that it will be useful,
11
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- # GNU General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU General Public License
16
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
-
18
- require './spec/spec_helper'
19
-
20
- require 'facets/timer'
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Init.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'spec_helper'
21
21
 
22
22
  describe Aef::Init do
23
23
  before(:each) do
@@ -73,13 +73,12 @@ describe Aef::Init do
73
73
  it "should wait 3 seconds between stop and start through the restart command" do
74
74
  restart_output = @temp_dir + 'restart_output'
75
75
 
76
- timer = Timer.new
77
- timer.start
76
+ start_time = Time.now
78
77
 
79
78
  `#{executable} restart #{restart_output}`.should be_true
80
79
 
81
- timer.stop
82
- (timer.total_time.should > 1.5).should be_true
80
+ stop_time = Time.now
81
+ (stop_time - start_time).should > 1.5
83
82
  end
84
83
 
85
84
  it "should display a usage example if a wrong command is specified" do