cognizant 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.travis.yml +17 -0
  4. data/Gemfile +4 -1
  5. data/{LICENSE → License.md} +4 -2
  6. data/Rakefile +5 -0
  7. data/Readme.md +95 -0
  8. data/bin/cognizant +76 -122
  9. data/bin/cognizantd +28 -61
  10. data/cognizant.gemspec +8 -4
  11. data/examples/apps/redis-server.cz +42 -0
  12. data/examples/apps/redis-server.yml +29 -0
  13. data/examples/apps/redis-server_dsl.cz +54 -0
  14. data/examples/apps/resque.cz +17 -0
  15. data/examples/apps/thin.cz +32 -0
  16. data/examples/apps/thin.yml +48 -0
  17. data/examples/cognizantd.yml +18 -47
  18. data/features/child_process.feature +62 -0
  19. data/features/commands.feature +65 -0
  20. data/features/cpu_usage.feature +45 -0
  21. data/features/daemon.feature +12 -0
  22. data/features/flapping.feature +39 -0
  23. data/features/memory_usage.feature +45 -0
  24. data/features/shell.feature +30 -0
  25. data/features/step_definitions/common_steps.rb +14 -0
  26. data/features/step_definitions/daemon_steps.rb +25 -0
  27. data/features/step_definitions/shell_steps.rb +96 -0
  28. data/features/support/env.rb +54 -0
  29. data/lib/cognizant.rb +1 -5
  30. data/lib/cognizant/application.rb +122 -0
  31. data/lib/cognizant/application/dsl_proxy.rb +23 -0
  32. data/lib/cognizant/client.rb +61 -0
  33. data/lib/cognizant/commands.rb +164 -0
  34. data/lib/cognizant/commands/actions.rb +30 -0
  35. data/lib/cognizant/commands/help.rb +10 -0
  36. data/lib/cognizant/commands/load.rb +10 -0
  37. data/lib/cognizant/commands/shutdown.rb +7 -0
  38. data/lib/cognizant/commands/status.rb +11 -0
  39. data/lib/cognizant/commands/use.rb +15 -0
  40. data/lib/cognizant/controller.rb +17 -0
  41. data/lib/cognizant/daemon.rb +279 -0
  42. data/lib/cognizant/interface.rb +17 -0
  43. data/lib/cognizant/log.rb +25 -0
  44. data/lib/cognizant/process.rb +138 -94
  45. data/lib/cognizant/process/actions.rb +30 -41
  46. data/lib/cognizant/process/actions/restart.rb +73 -17
  47. data/lib/cognizant/process/actions/start.rb +35 -12
  48. data/lib/cognizant/process/actions/stop.rb +38 -17
  49. data/lib/cognizant/process/attributes.rb +41 -10
  50. data/lib/cognizant/process/children.rb +36 -0
  51. data/lib/cognizant/process/{condition_check.rb → condition_delegate.rb} +11 -13
  52. data/lib/cognizant/process/conditions.rb +7 -4
  53. data/lib/cognizant/process/conditions/cpu_usage.rb +5 -6
  54. data/lib/cognizant/process/conditions/memory_usage.rb +2 -6
  55. data/lib/cognizant/process/dsl_proxy.rb +23 -0
  56. data/lib/cognizant/process/execution.rb +16 -9
  57. data/lib/cognizant/process/pid.rb +16 -6
  58. data/lib/cognizant/process/status.rb +14 -2
  59. data/lib/cognizant/process/trigger_delegate.rb +57 -0
  60. data/lib/cognizant/process/triggers.rb +19 -0
  61. data/lib/cognizant/process/triggers/flapping.rb +68 -0
  62. data/lib/cognizant/process/triggers/transition.rb +22 -0
  63. data/lib/cognizant/process/triggers/trigger.rb +15 -0
  64. data/lib/cognizant/shell.rb +142 -0
  65. data/lib/cognizant/system.rb +16 -0
  66. data/lib/cognizant/system/ps.rb +1 -1
  67. data/lib/cognizant/system/signal.rb +2 -2
  68. data/lib/cognizant/util/dsl_proxy_methods_handler.rb +25 -0
  69. data/lib/cognizant/util/fixnum_percent.rb +5 -0
  70. data/lib/cognizant/util/transform_hash_keys.rb +33 -0
  71. data/lib/cognizant/validations.rb +142 -142
  72. data/lib/cognizant/version.rb +1 -1
  73. metadata +131 -71
  74. data/README.md +0 -221
  75. data/examples/redis-server.rb +0 -28
  76. data/examples/resque.rb +0 -10
  77. data/images/logo-small.png +0 -0
  78. data/images/logo.png +0 -0
  79. data/images/logo.pxm +0 -0
  80. data/lib/cognizant/logging.rb +0 -33
  81. data/lib/cognizant/process/conditions/flapping.rb +0 -57
  82. data/lib/cognizant/process/conditions/trigger_condition.rb +0 -52
  83. data/lib/cognizant/server.rb +0 -14
  84. data/lib/cognizant/server/commands.rb +0 -80
  85. data/lib/cognizant/server/daemon.rb +0 -277
  86. data/lib/cognizant/server/interface.rb +0 -86
  87. data/lib/cognizant/util/symbolize_hash_keys.rb +0 -19
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a93e2f1d168e2683901d0a11ec11f306e76e0a74
4
+ data.tar.gz: 969dfb60b5efe2138fc44c7a37a687aaf75e5b46
5
+ SHA512:
6
+ metadata.gz: 4d7dc8a641709996e05be712f59db9fb9b3c1c56176ae5314f21a8c2adc6b9e1b1b5163ec438db285268cbea12d0c55162ef209afb037e5750a2ae32befb2382
7
+ data.tar.gz: a34bb4420411f4a61722ae1887dad40cf04520df5e1684b4804981d40ffeb8f84278ae358497acc78043d433a0242ec4fdfc65ac6166488c23dc20270e7858ef
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
  .DS_Store
19
19
  .rbenv*
20
+ .rbx
@@ -0,0 +1,17 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby-head
8
+ - rbx-19mode
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ - rvm: rbx-19mode
13
+ branches:
14
+ only:
15
+ - master
16
+ notifications:
17
+ email: false
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in cognizant.gemspec
3
+ group :test do
4
+ gem 'coveralls', require: false
5
+ end
6
+
4
7
  gemspec
@@ -1,6 +1,8 @@
1
- Copyright (c) 2012 Gurpartap Singh
1
+ **Copyright (c) 2012 Gurpartap Singh**
2
2
 
3
- MIT License
3
+ **All rights reserved**
4
+
5
+ **MIT License**
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining
6
8
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require "cucumber/rake/task"
4
+
5
+ Cucumber::Rake::Task.new(:features)
6
+
7
+ task :default => [:features]
@@ -0,0 +1,95 @@
1
+ ![Cognizant](http://f.cl.ly/items/442w3l1n0i3V41220g0z/cognizant.png)
2
+
3
+ Simple and reliable process monitoring framework written in Ruby
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/cognizant.png)](http://badge.fury.io/rb/cognizant) [![Dependency Status](https://gemnasium.com/Gurpartap/cognizant.png)](https://gemnasium.com/Gurpartap/cognizant) [![Code Climate](https://codeclimate.com/github/Gurpartap/cognizant.png)](https://codeclimate.com/github/Gurpartap/cognizant) [![Build Status](https://travis-ci.org/Gurpartap/cognizant.png?branch=master)](https://travis-ci.org/Gurpartap/cognizant) [![Coverage Status](https://coveralls.io/repos/Gurpartap/cognizant/badge.png?branch=master)](https://coveralls.io/r/Gurpartap/cognizant)
6
+
7
+
8
+ ## Quick start
9
+
10
+ ###### Install Cognizant
11
+ ```bash
12
+ $ gem install cognizant
13
+ ```
14
+
15
+ ###### Example thin server cluster configuration
16
+ ```bash
17
+ $ vim thin_cluster.cz
18
+ ```
19
+
20
+ ```ruby
21
+ app_root = "/apps/acmecorp.com"
22
+ servers = 5
23
+ port = 4000
24
+
25
+ Cognizant.application "acmecorp.com" do |app|
26
+ servers.times do |n|
27
+ app.monitor "thin-#{n}" do
28
+ autostart!
29
+ group "thin"
30
+ uid "www-data"
31
+ gid "www-data"
32
+
33
+ env RACK_ENV: "production"
34
+ chdir "#{app_root}/current"
35
+
36
+ daemonize false
37
+ pidfile "#{app_root}/shared/tmp/pids/thin.400#{n}.pid"
38
+
39
+ start_command "bundle exec thin start --only #{n} --servers #{servers} --port #{port}"
40
+ stop_command "bundle exec thin stop --only #{n} --servers #{servers} --port #{port}"
41
+ restart_command "bundle exec thin restart --only #{n} --servers #{servers} --port #{port}"
42
+
43
+ check :flapping, times: 3, within: 1.minute, retry_after: 15.seconds, retries: 10
44
+ check :transition, from: :running, to: :stopped do |process|
45
+ `say a thin server has stopped` # send an email, etc.
46
+ end
47
+
48
+ check :cpu_usage, above: 50.percent, every: 5.seconds, times: 5, do: :restart
49
+ check :memory_usage, above: 300.megabytes, every: 5.seconds, times: [3, 5], do: :restart
50
+ end
51
+ end
52
+ end
53
+ ```
54
+
55
+ **YAML** version of this example is [available in the wiki](https://github.com/Gurpartap/cognizant/wiki/Thin-Server-Cluster).
56
+
57
+ ###### Start the daemon and load the configuration
58
+ ```bash
59
+ $ cognizantd
60
+ $ cognizant load thin_cluster.cz
61
+ ```
62
+
63
+ ###### Enter the Cognizant shell and view the status of managed processes
64
+ ```bash
65
+ $ cognizant
66
+ ```
67
+ ```
68
+ Welcome Gurpartap! You are speaking to the Cognizant Monitoring Daemon.
69
+ Enter 'help' if you're not sure what to do.
70
+
71
+ Type 'quit' or 'exit' to quit at any time.
72
+ > use acmecorp.com
73
+ OK
74
+ ```
75
+
76
+ ```
77
+ (acmecorp.com)> status
78
+ +---------+-------+------------------------+-------+-------+--------+
79
+ | Process | Group | State | PID | % CPU | Memory |
80
+ +---------+-------+------------------------+-------+-------+--------+
81
+ | thin-0 | thin | running since 1 minute | 59825 | 0.0 | 47 MiB |
82
+ +---------+-------+------------------------+-------+-------+--------+
83
+ | thin-1 | thin | running since 1 minute | 59828 | 0.0 | 47 MiB |
84
+ +---------+-------+------------------------+-------+-------+--------+
85
+ | thin-2 | thin | running since 1 minute | 59829 | 0.0 | 47 MiB |
86
+ +---------+-------+------------------------+-------+-------+--------+
87
+ 2013-03-18 10:00:29 +0530
88
+ ```
89
+
90
+ ## Further information
91
+ Cognizant has an [**extensively documented wiki**](https://github.com/Gurpartap/cognizant/wiki) for that.
92
+
93
+ ## About
94
+
95
+ Cognizant is a project of [Gurpartap Singh](http://gurpartap.com/). Feel free to get in touch.
@@ -1,149 +1,105 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "cognizant/version"
4
- require "gli"
5
- require "formatador"
6
- require "socket"
7
- require "json"
8
-
9
- include GLI::App
10
-
11
3
  # Set the process name.
12
4
  $0 = File.basename(__FILE__)
13
5
 
14
- # Set to flush standard output/error immediately.
6
+ # Flush standard output/error immediately.
15
7
  $stdout.sync = true
16
8
  $stderr.sync = true
17
9
 
18
- program_desc "administration utility for cognizantd"
19
- @version = Cognizant::VERSION
20
-
21
- sort_help :manually
22
-
23
- desc "The socket lock file of the daemon server"
24
- arg_name "FILE"
25
- flag :socket, :type => String, :default_value => "/var/run/cognizant/cognizantd.sock"
26
-
27
- desc "The server address of the daemon server"
28
- arg_name "ADDRESS"
29
- flag "bind-address", :type => String
30
-
31
- desc "The server port of the daemon server"
32
- arg_name "PORT"
33
- flag :port, :type => Integer
34
-
35
- desc "Username to use for authentication with server"
36
- arg_name "USERNAME"
37
- flag :username, :type => String
38
-
39
- desc "Password to use for authentication with server"
40
- arg_name "PASSWORD"
41
- flag :password, :type => String
42
-
43
- desc "Print the version number and exit"
44
- switch :version, :negatable => false
45
-
46
- desc "Turn on tracing, enabling full backtrace"
47
- switch :trace, :negatable => false
48
-
49
- desc "Display status of managed process(es) or group(s)"
50
- arg_name "process_name", :optional
51
- command :status do |c|
52
- c.action do |global_options, options, args|
53
- connect_and_perform(global_options, :status, args) do |output|
54
- begin
55
- JSON.parse(output)
56
-
57
- status_data = JSON.parse(output)
58
- status_data.each_with_index do |process, index|
59
- status_data[index]["State"] = "[green]#{process["State"]}[/] since #{distance_of_time_in_words(Time.now.to_i - process["Since"])}"
60
- status_data[index]["Memory"] = number_to_human_size(process["Memory"] * 1024) # Convert kilobytes to bytes.
61
- end
10
+ require "cognizant/version"
11
+ require "cognizant/shell"
12
+ require "commander/import"
13
+ require "formatador"
14
+ require "shellwords"
15
+ require "socket"
62
16
 
63
- formatador = Formatador.new
64
- formatador.display_line
65
- formatador.display_table(status_data, ["Process", "Group", "State", "PID", "% CPU", "Memory"])
66
- formatador.display_line(Time.now)
67
- formatador.display_line
68
- rescue => e
69
- Formatador.display_line("[red]#{output}[/]")
17
+ program :version, Cognizant::VERSION
18
+ program :description, "administration utility for cognizantd"
19
+
20
+ global_option("-a", "--app NAME", String, "The name of the application container (required)")
21
+ global_option("-s", "--sockfile FILE", String, "The socket lock file of the application server (required)")
22
+
23
+ default_command :shell
24
+
25
+ command :shell do |c|
26
+ c.syntax = "cognizant shell"
27
+ c.description = "Enter the Cognizant shell for easy control"
28
+ c.action do |args, options|
29
+ Cognizant::Shell.new(app: options.app, socket: options.sockfile, shell: true).run do |output, cmd|
30
+ if cmd == "shutdown"
31
+ # TODO: Use Shell#emit instead of puts.
32
+ puts "The daemon has been shutdown successfuly."
33
+ exit(0)
34
+ else
35
+ format_status_table(output)
70
36
  end
71
37
  end
72
38
  end
73
39
  end
74
40
 
75
- desc "Loads the process information from specified Ruby file"
76
- arg_name "file"
77
- command :load do |c|
78
- c.action do |global_options, options, args|
79
- connect_and_perform(global_options, :load, args)
80
- end
81
- end
82
-
83
- desc "Monitor the specified process or group"
84
- arg_name "process_name"
85
- command :monitor do |c|
86
- c.action do |global_options, options, args|
87
- connect_and_perform(global_options, :monitor, args)
88
- end
89
- end
90
-
91
- desc "Unmonitor the specified process or group"
92
- arg_name "process_name"
93
- command :unmonitor do |c|
94
- c.action do |global_options, options, args|
95
- connect_and_perform(global_options, :unmonitor, args)
41
+ command :status do |c|
42
+ c.syntax = "cognizant status [process or group name]"
43
+ c.description = "Display status of managed process(es) or group(s)"
44
+ c.action do |args, options|
45
+ connect_and_perform(:status, options, args) do |output|
46
+ format_status_table(output)
47
+ end
96
48
  end
97
49
  end
98
50
 
99
- desc "Start the specified process or group"
100
- arg_name "process_name"
101
- command :start do |c|
102
- c.action do |global_options, options, args|
103
- connect_and_perform(global_options, :start, args)
104
- end
51
+ [
52
+ [:load, "FILE", "Loads the process information from specified Ruby file"],
53
+ [:monitor, "[PROCESS OR GROUP NAME]", "Monitor the specified process or group"],
54
+ [:unmonitor, "[PROCESS OR GROUP NAME]", "Unmonitor the specified process or group"],
55
+ [:start, "[PROCESS OR GROUP NAME]", "Start the specified process or group"],
56
+ [:stop, "[PROCESS OR GROUP NAME]", "Stop the specified process or group"],
57
+ [:restart, "[PROCESS OR GROUP NAME]", "Restart the specified process or group"]
58
+ ].each do |command, arg_name, description|
59
+ eval <<-heredoc
60
+ command "#{command}" do |c|
61
+ c.syntax = "cognizant #{command} #{arg_name}"
62
+ c.description = "#{description}"
63
+ c.action do |args, options|
64
+ connect_and_perform(:#{command}, options, args) do |output|
65
+ format_status_table(output)
66
+ end
67
+ end
68
+ end
69
+ heredoc
105
70
  end
106
71
 
107
- desc "Stop the specified process or group"
108
- arg_name "process_name"
109
- command :stop do |c|
110
- c.action do |global_options, options, args|
111
- connect_and_perform(global_options, :stop, args)
72
+ command "shutdown" do |c|
73
+ c.syntax = "cognizant shutdown"
74
+ c.description = "Stop the monitoring daemon without affecting managed processes"
75
+ c.action do |args, options|
76
+ connect_and_perform(:shutdown, options, args) do |output|
77
+ #
78
+ end
112
79
  end
113
80
  end
114
81
 
115
- desc "Restart the specified process or group"
116
- arg_name "process_name"
117
- command :restart do |c|
118
- c.action do |global_options, options, args|
119
- connect_and_perform(global_options, :restart, args)
120
- end
121
- end
82
+ def format_status_table(status_data)
83
+ if status_data.is_a?(Array)
84
+ status_data.each_with_index do |process, index|
85
+ status_data[index]["State"] = "[green]#{process["State"]}[/] since #{distance_of_time_in_words(Time.now.to_i - process["Since"])}"
86
+ status_data[index]["Memory"] = number_to_human_size(process["Memory"] * 1024) # Convert kilobytes to bytes.
87
+ end
122
88
 
123
- desc "Stop the monitoring daemon without affecting processes"
124
- command :shutdown do |c|
125
- c.action do |global_options, options, args|
126
- connect_and_perform(global_options, :shutdown, args)
89
+ formatador = Formatador.new
90
+ formatador.display_line
91
+ formatador.display_table(status_data, ["Process", "Group", "State", "PID", "% CPU", "Memory"])
92
+ formatador.display_line(Time.now)
93
+ formatador.display_line
94
+ elsif status_data.is_a?(Hash)
95
+ puts status_data['message']
127
96
  end
97
+ rescue => e
98
+ Formatador.display_line("[red]#{status_data}[/]")
128
99
  end
129
100
 
130
- def connect_and_perform(global_options, task, args, &block)
131
- output_handler = Proc.new do |socket|
132
- socket.write("#{task.to_s} #{args.join(' ')}")
133
- socket.flush
134
- output = socket.gets
135
- if block
136
- block.call(output)
137
- else
138
- Formatador.display_line(output)
139
- end
140
- end
141
-
142
- if global_options[:socket]
143
- output_handler.call(UNIXSocket.new(global_options[:socket]))
144
- else
145
- output_handler.call(TCPSocket.new(global_options["bind-address"], global_options["port"]))
146
- end
101
+ def connect_and_perform(task, options, args, &block)
102
+ Cognizant::Shell.new(app: options.app, socket: options.sockfile, shell: false).run_command(task, args, &block)
147
103
  end
148
104
 
149
105
  def distance_of_time_in_words(seconds)
@@ -174,5 +130,3 @@ end
174
130
  def pluralize(count, singular, plural)
175
131
  "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : plural)
176
132
  end
177
-
178
- exit run(ARGV)
@@ -10,6 +10,8 @@ $stderr.sync = true
10
10
  begin
11
11
  require "optparse"
12
12
  require "cognizant/version"
13
+ require "cognizant/controller"
14
+ require "cognizant/util/transform_hash_keys"
13
15
 
14
16
  options = {}
15
17
 
@@ -31,64 +33,24 @@ GLOBAL OPTIONS:
31
33
  options[:daemonize] = value
32
34
  end
33
35
 
34
- opts.on("--pidfile FILE", String, "The pid (process identifier) lock file for the daemon.") do |value|
36
+ opts.on("--sockfile FILE", String, "The socket lock file of the command server.") do |value|
35
37
  options[:pidfile] = value
36
38
  end
37
39
 
38
- opts.on("--logfile FILE", String, "The file to log the daemon's operational information into.") do |value|
39
- options[:logfile] = value
40
- end
41
-
42
- opts.on("--loglevel LEVEL", String, "The level of information to log.") do |value|
43
- options[:loglevel] = value
44
- end
45
-
46
- opts.on("--env ENV", String, "Environment variables for managed processes to inherit.") do |value|
47
- options[:env] = value
48
- end
49
-
50
- opts.on("--chdir DIRECTORY", String, "The current working directory for the managed processes to start with.") do |value|
51
- options[:chdir] = value
52
- end
53
-
54
- opts.on("--umask UMASK", String, "Permission mode limitations for file and directory creation.") do |value|
55
- options[:umask] = value
56
- end
57
-
58
- opts.on("--user USER", String, "Run the daemon and managed processes as the given user.") do |value|
59
- options[:user] = value
60
- end
61
-
62
- opts.on("--group GROUP", String, "Run the daemon and managed processes as the given user group.") do |value|
63
- options[:group] = value
64
- end
65
-
66
- opts.on("--pids-dir DIRECTORY", String, "Directory to store the pid files of managed processes, when required.") do |value|
67
- options[:pids_dir] = value
68
- end
69
-
70
- opts.on("--logs-dir DIRECTORY", String, "Directory to store the log files of managed processes, when required.") do |value|
71
- options[:logs_dir] = value
72
- end
73
-
74
- opts.on("-s FILE", "--socket FILE", String, "The socket lock file for the server") do |value|
75
- options[:socket] = value
76
- end
77
-
78
- opts.on("-b ADDRESS", "--bind-address ADDRESS", String, "The interface to bind the TCP server to.") do |value|
79
- options[:bind_address] = value
40
+ opts.on("--pidfile FILE", String, "The pid (process identifier) lock file for the daemon.") do |value|
41
+ options[:pidfile] = value
80
42
  end
81
43
 
82
- opts.on("-p PORT", "--port PORT", Integer, "The TCP port to start the server with.") do |value|
83
- options[:port] = value
44
+ opts.on("--syslog", String, "Log the daemon's operational information into syslog.") do |_|
45
+ options[:syslog] = true
84
46
  end
85
47
 
86
- opts.on("--username USERNAME", String, "Username for securing server access.") do |value|
87
- options[:username] = value
48
+ opts.on("--logfile FILE", String, "Log the daemon's operational information into a file.") do |value|
49
+ options[:logfile] = value
88
50
  end
89
51
 
90
- opts.on("--password PASSWORD", String, "Password to accompany the username.") do |value|
91
- options[:password] = value
52
+ opts.on("--loglevel LEVEL", String, "The level of information to log.") do |value|
53
+ options[:loglevel] = value
92
54
  end
93
55
 
94
56
  opts.on("-t", "--trace", "Turn on tracing, enable full backtrace.") do
@@ -102,28 +64,33 @@ GLOBAL OPTIONS:
102
64
  end
103
65
  end.parse!
104
66
 
105
- if config_file = ARGV.shift
106
- require "cognizant/util/symbolize_hash_keys"
67
+ config = {}
68
+ config_file = ARGV.shift
107
69
 
108
- if config_file.eql?("-")
109
- config = YAML.load(ARGF.read)
110
- else
70
+ if config_file == "-"
71
+ config = YAML.load(ARGF.read)
72
+
73
+ elsif config_file.to_s.size > 0
74
+ if File.exists?(config_file)
111
75
  config = YAML.load_file(config_file)
76
+ elsif File.exists?(default_config_file = "/etc/cognizant/cognizantd.yml")
77
+ config = YAML.load_file(default_config_file)
112
78
  end
113
- config = config.inject({}) { |c,(k,v)| c[k.gsub("-", "_").to_sym] = v; c }
114
- config.deep_symbolize_keys!
115
- options = config.merge(options)
116
79
  end
117
80
 
118
- require "cognizant/server"
119
- Cognizant::Server.start(options)
81
+ config.deep_symbolize_keys!
82
+ config = config.merge(options)
83
+ trace = config[:trace]
84
+
85
+ Cognizant::Controller.start_daemon(config)
120
86
  rescue => exception
121
87
  if exception.instance_of?(SystemExit)
122
88
  raise
123
89
  else
124
- $stderr.puts "ERROR: While executing #{$0} ... (#{exception.class})"
90
+ $stderr.puts "ERROR While executing #{$0} ... (#{exception.class})"
125
91
  $stderr.puts " #{exception.message}\n\n"
126
- if options[:trace]
92
+
93
+ if trace
127
94
  $stderr.puts exception.backtrace.join("\n")
128
95
  $stderr.puts "\n(See usage by running #{$0} with --help)"
129
96
  else