smartguard 0.2.10 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,196 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'smartguard'
4
+ require 'trollop'
4
5
  require 'colored'
5
- require 'dante'
6
6
 
7
- Smartkiosk::Common::Patches.dante_should_log_with Smartguard::Logging.logger
7
+ def start_application(application, opts, &block)
8
8
 
9
- runner = Dante::Runner.new('smartguard')
10
- runner.description = "Smartkiosk services control daemon"
9
+ Smartguard::Logging.logger.info "Starting application"
11
10
 
12
- runner.with_options do |opts|
13
- options[:port] = 10000
14
- options[:development] = false
15
-
16
- opts.on("-a", "--app APPLICATION", String, "Application to use") do |x|
17
- options[:application] = x
11
+ unless opts[:pid].nil?
12
+ begin
13
+ File.open(opts[:pid], "w") do |io|
14
+ io.write Process.pid.to_s
15
+ end
16
+ rescue => e
17
+ Smartguard::Logging.logger.error "Unable to write pidfile: #{e}"
18
+ end
18
19
  end
19
- opts.on("-x", "--path PATH", String, "Path to application") do |x|
20
- options[:path] = x
20
+
21
+ application = application.new opts[:path]
22
+
23
+ signal_handler = ->(signal) do
24
+ begin
25
+ if Smartguard.shutting_down
26
+ Smartguard::Logging.logger.info "Okay, okay."
27
+ else
28
+ Smartguard::Logging.logger.info "Catched signal, shutting down"
29
+
30
+ Smartguard.shutting_down = true
31
+ Smartguard::Logging.logger.info "Stopping services"
32
+ application.stop_services
33
+ end
34
+
35
+ rescue => e
36
+ Smartguard::Logging.logger.error "Shutdown failed: #{e}"
37
+
38
+ ensure
39
+ exit 1
40
+ end
21
41
  end
22
42
 
23
- opts.on("-p", "--port PORT", Integer, "Port to run DRB (default: #{options[:port]})") do |x|
24
- options[:port] = x
43
+ trap :HUP, signal_handler
44
+ trap :INT, signal_handler
45
+ trap :QUIT, signal_handler
46
+ trap :PIPE, signal_handler
47
+ trap :TERM, signal_handler
48
+ Smartguard::ProcessManager.init
49
+
50
+ at_exit do
51
+ begin
52
+ Smartguard::Logging.logger.info "Killing any remaining processes"
53
+ ensure
54
+ Process.kill :KILL, 0
55
+ end
25
56
  end
26
57
 
27
- opts.on("-v", "--version", "Output version") do |x|
28
- options[:version] = x
58
+ Smartguard::Logging.logger.info "Starting services"
59
+
60
+ begin
61
+ application.start_services do
62
+ Smartguard::Logging.logger.error "Startup failed, cleaning up and exiting"
63
+ application.stop_services
64
+ exit 1
65
+ end
66
+ rescue => e
67
+ Smartguard::Logging.logger.error "Exception catched during service startup: #{e}"
68
+ exit 1
29
69
  end
30
70
 
31
- opts.on("-d", "--development", "Run in development environment") do |x|
32
- options[:development] = x
71
+ Smartguard::Logging.logger.info "Services started"
72
+
73
+ DRb.start_service("druby://localhost:#{opts[:port]}", application)
74
+ Smartguard::Logging.logger.info "Smartguard ready"
75
+
76
+ yield if block_given?
77
+
78
+ DRb.thread.join
79
+ end
80
+
81
+ trap :TTIN do
82
+ Thread.list.each do |thread|
83
+ puts "Thread #{thread}:"
84
+ if thread.backtrace
85
+ puts thread.backtrace.join("\n")
86
+ else
87
+ puts "<no backtrace available>"
88
+ end
33
89
  end
34
90
  end
35
91
 
36
- runner.execute do |opts|
37
- abort "Smartguard version: #{Smartguard::VERSION}" if opts[:version]
92
+ opts = Trollop.options do
93
+ version "Smartguard #{Smartguard::VERSION}"
38
94
 
39
- abort "You should specify application (--app)" if opts[:application].blank?
95
+ opt :app, "Application to use", type: String
96
+ opt :path, "Path to application", type: String
97
+ opt :port, "Port to run DRB", default: 10000
98
+ opt :pid, "Pid file name", type: String
99
+ opt :log, "Log file name", type: String
100
+ opt :development, "Run in the development environment"
101
+ opt :daemon, "Daemonize Smartguard after initialization"
102
+ opt :kill, "Kill Smartguard by pidfile"
103
+ end
40
104
 
41
- if opts[:path].blank?
42
- if opts[:development]
43
- opts[:path] = Dir.getwd
44
- else
45
- abort "You should specify application path (--path)" if opts[:path].blank?
105
+ if opts[:kill]
106
+ Trollop::die :pid, "must be specified to kill" if opts[:pid].nil?
107
+
108
+ puts "Waiting for Smartguard to die."
109
+ begin
110
+ pid = File.read(opts[:pid]).to_i
111
+
112
+ Process.kill :TERM, pid
113
+ loop do
114
+ Process.kill 0, pid
115
+ sleep 0.5
46
116
  end
117
+ rescue
47
118
  end
48
119
 
49
- application = Smartguard::Applications.const_get(opts[:application].camelize) rescue nil
120
+ exit 0
121
+ end
122
+
123
+ Trollop::die :app, "must be specified" if opts[:app].nil?
50
124
 
51
- abort "Application `#{opts[:application]}` not supported" if application.nil?
125
+ begin
126
+ application = Smartguard::Applications.const_get opts[:app].camelize
127
+ rescue
128
+ Trollop::die :app, "is not supported"
129
+ end
52
130
 
131
+ if opts[:path].nil?
53
132
  if opts[:development]
54
- Smartguard.environment = :development
133
+ opts[:path] = Dir.getwd
55
134
  else
56
- Smartguard.environment = :production
135
+ Trollop::die :path, "must be specified in production"
57
136
  end
137
+ end
58
138
 
59
- application = application.new(opts[:path])
60
- DRb.start_service("druby://localhost:#{opts[:port]}", application)
61
- application.start_services
62
- DRb.thread.join
139
+ if opts[:development]
140
+ Smartguard.environment = :development
141
+ else
142
+ Smartguard.environment = :production
143
+ end
144
+
145
+ if opts[:log].nil?
146
+ Trollop::die :log, "must be specified for daemon" if opts[:daemon]
147
+ else
148
+ Smartguard::Logging.destination = opts[:log]
149
+ end
150
+
151
+ Smartguard::Logging.init
152
+ Smartguard::Logging.logger.info "Initializing Smartguard"
153
+
154
+ if opts[:daemon]
155
+ rd, wr = IO.pipe
156
+
157
+ pid = Process.fork
158
+ if pid.nil?
159
+ begin
160
+ Process.setsid
161
+ Dir.chdir '/'
162
+ File.umask 0
163
+ File.open("/dev/null", "r+") do |null|
164
+ [ STDIN, STDOUT, STDERR ].each do |io|
165
+ io.reopen null
166
+ end
167
+ end
168
+
169
+ rd.close
170
+
171
+ start_application(application, opts) do
172
+ wr.puts "1"
173
+ wr.flush
174
+ wr.close
175
+ end
176
+
177
+ exit 0
178
+ rescue
179
+ exit 1
180
+ end
181
+ end
182
+
183
+ wr.close
184
+
185
+ begin
186
+ rd.readline
187
+
188
+ puts "smartguard started"
189
+ exit 0
190
+ rescue => e
191
+ puts "smartguard initialization failed"
192
+ exit 1
193
+ end
194
+ else
195
+ start_application application, opts
63
196
  end
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "trollop"
4
+ require "smartguard"
5
+
6
+ module CommandHandler
7
+ def self.cmd_start(drb)
8
+ status = drb.start_services
9
+ end
10
+
11
+ def self.cmd_stop(drb)
12
+ drb.stop_services
13
+ end
14
+
15
+ def self.cmd_restart(drb)
16
+ drb.restart
17
+ end
18
+
19
+ def self.cmd_status(drb)
20
+ status = drb.status
21
+
22
+ status.each do |service, (pid, active)|
23
+ printf "%-16s: ", service
24
+
25
+ if active
26
+ puts "running, pid #{pid}"
27
+ else
28
+ puts "stopped"
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.cmd_switch_release(drb)
34
+ if ARGV.empty?
35
+ warn "release must be specified"
36
+ exit 1
37
+ end
38
+
39
+ release = ARGV.shift
40
+ status = drb.switch_release release
41
+ if status
42
+ puts "releases switched"
43
+ else
44
+ warn "switch failed"
45
+ exit 1
46
+ end
47
+ end
48
+ end
49
+
50
+ opts = Trollop.options do
51
+ version "Smartguard CLI #{Smartguard::VERSION}"
52
+ opt :port, "DRB Port", default: 10000
53
+ stop_on_unknown
54
+ end
55
+
56
+ drb = DRb::DRbObject.new_with_uri "druby://localhost:#{opts[:port]}"
57
+
58
+ if ARGV.empty?
59
+ warn "command must be specified"
60
+ exit 1
61
+ end
62
+
63
+ cmd = :"cmd_#{ARGV.shift}"
64
+
65
+ unless CommandHandler.respond_to? cmd
66
+ warn "unsupported command"
67
+ exit 1
68
+ end
69
+
70
+ CommandHandler.send cmd, drb
@@ -22,5 +22,8 @@ require 'smartguard/applications/smartkiosk/thin'
22
22
  module Smartguard
23
23
  class << self
24
24
  attr_accessor :environment
25
+ attr_accessor :shutting_down
25
26
  end
27
+
28
+ self.shutting_down = false
26
29
  end
@@ -11,11 +11,17 @@ module Smartguard
11
11
  log_path = @path.join('log/sidekiq_log')
12
12
  pidfile = @path.join('tmp/pids/sidekiq.pid')
13
13
 
14
+ opts = []
15
+ if Smartguard.environment == :production
16
+ opts << "-L"
17
+ opts << "#{log_path}"
18
+ end
19
+
14
20
  FileUtils.rm_f pidfile
15
21
  if !run(@path,
16
22
  {},
17
23
  "bundle", "exec",
18
- "sidekiq", "-e", Smartguard.environment.to_s, "--config=#{config_path}", "--pidfile=#{pidfile}"
24
+ "sidekiq", "-e", Smartguard.environment.to_s, "--config=#{config_path}", "--pidfile=#{pidfile}", *opts
19
25
  )
20
26
  return false
21
27
  end
@@ -8,8 +8,13 @@ module Smartguard
8
8
  log_file = @path.join('log/smartware.log')
9
9
  config_file = @path.join('config/services/smartware.yml')
10
10
 
11
+ opts = []
12
+ if Smartguard.environment == :production
13
+ opts << "--log=#{log_file}"
14
+ end
15
+
11
16
  Logging.logger.info "Starting smartware"
12
- if !run(@path, {}, "bundle", "exec", "smartware", "--log=#{log_file}", "--config-file=#{config_file}")
17
+ if !run(@path, {}, "bundle", "exec", "smartware", "--config-file=#{config_file}", *opts)
13
18
  return false
14
19
  end
15
20
 
@@ -12,18 +12,22 @@ module Smartguard
12
12
  Logging.logger.info "Starting thin"
13
13
  end
14
14
 
15
+ log_path = @path.join('log/thin.log')
16
+
15
17
  if !run(@path,
16
18
  {},
17
19
  "bundle", "exec",
18
- "thin", "-e", Smartguard.environment.to_s, "-p", 3000,
20
+ "thin", "-e", Smartguard.environment.to_s, "-p", "3000", "-l", "#{log_path}",
19
21
  "start"
20
22
  )
21
23
  return false
22
24
  end
23
25
 
24
- without_respawn do
25
- wait_for_port port
26
+ result = without_respawn do
27
+ wait_for_port 3000
26
28
  end
29
+
30
+ result
27
31
  end
28
32
 
29
33
  def stop
@@ -105,7 +105,7 @@ module Smartguard
105
105
  @active = false
106
106
  @pid = nil
107
107
 
108
- died if @wanted && !@starting
108
+ died if @wanted && !@starting && !Smartguard.shutting_down
109
109
  end
110
110
 
111
111
  if defined? Bundler
@@ -33,7 +33,8 @@ module Smartguard
33
33
  end
34
34
  end
35
35
 
36
+ def self.init
37
+ trap :CHLD, ProcessManager.method(:handle_sigchld)
38
+ end
36
39
  end
37
-
38
- trap 'CHLD', ProcessManager.method(:handle_sigchld)
39
40
  end
@@ -1,3 +1,3 @@
1
1
  module Smartguard
2
- VERSION = "0.2.10"
2
+ VERSION = "0.3"
3
3
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.add_dependency 'smartkiosk-common'
21
21
  gem.add_dependency 'i18n'
22
22
  gem.add_dependency 'activesupport'
23
- gem.add_dependency 'dante'
23
+ gem.add_dependency 'trollop'
24
24
  gem.add_dependency 'colored'
25
25
 
26
26
  gem.add_development_dependency 'pry'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartguard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: smartkiosk-common
16
- requirement: &70155562307000 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70155562307000
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: i18n
27
- requirement: &70155562306420 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70155562306420
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: activesupport
38
- requirement: &70155562305820 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70155562305820
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
- name: dante
49
- requirement: &70155562304860 !ruby/object:Gem::Requirement
63
+ name: trollop
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *70155562304860
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: colored
60
- requirement: &70155562304200 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ! '>='
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: '0'
66
86
  type: :runtime
67
87
  prerelease: false
68
- version_requirements: *70155562304200
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: pry
71
- requirement: &70155562303400 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ! '>='
@@ -76,12 +101,18 @@ dependencies:
76
101
  version: '0'
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *70155562303400
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
80
110
  description: Smartguard is the Smartkiosk services control daemon
81
111
  email:
82
112
  - boris@roundlake.ru
83
113
  executables:
84
114
  - smartguard
115
+ - smartguardctl
85
116
  extensions: []
86
117
  extra_rdoc_files: []
87
118
  files:
@@ -91,6 +122,7 @@ files:
91
122
  - README.md
92
123
  - Rakefile
93
124
  - bin/smartguard
125
+ - bin/smartguardctl
94
126
  - lib/smartguard.rb
95
127
  - lib/smartguard/application.rb
96
128
  - lib/smartguard/applications/smartkiosk.rb
@@ -124,9 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
156
  version: '0'
125
157
  requirements: []
126
158
  rubyforge_project:
127
- rubygems_version: 1.8.15
159
+ rubygems_version: 1.8.24
128
160
  signing_key:
129
161
  specification_version: 3
130
162
  summary: Smartguard is the Smartkiosk services control daemon
131
163
  test_files: []
132
- has_rdoc: