proctor 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.rspec +1 -0
  2. data/TODO +28 -0
  3. data/bin/proctor +55 -85
  4. data/bin/showlog +5 -0
  5. data/bin/showtime +5 -0
  6. data/lib/proctor/command/asset_list_cmd.rb +56 -0
  7. data/lib/proctor/{presenter/show.rb → command/asset_show_cmd.rb} +7 -2
  8. data/lib/proctor/command/file_cleanup_cmd.rb +81 -0
  9. data/lib/proctor/command/file_export_cmd.rb +79 -0
  10. data/lib/proctor/command/helpers/command.rb +27 -0
  11. data/lib/proctor/command/render_group_cmd.rb +39 -0
  12. data/lib/proctor/command/render_service_cmd.rb +34 -0
  13. data/lib/proctor/command/start_cmd.rb +70 -0
  14. data/lib/proctor/config/app_file.rb +54 -0
  15. data/lib/proctor/config/app_files.rb +50 -0
  16. data/lib/proctor/config/env.rb +35 -0
  17. data/lib/proctor/config/template_file.rb +41 -0
  18. data/lib/proctor/config/template_files.rb +39 -0
  19. data/lib/proctor/manager.rb +36 -3
  20. data/lib/proctor/managers.rb +38 -0
  21. data/lib/proctor/node.rb +26 -0
  22. data/lib/proctor/nodes.rb +38 -0
  23. data/lib/proctor/resource/erb.rb +95 -0
  24. data/lib/proctor/resource/lookup.rb +82 -0
  25. data/lib/proctor/service.rb +28 -3
  26. data/lib/proctor/services.rb +38 -0
  27. data/lib/proctor/util/helpers.rb +13 -0
  28. data/lib/proctor/util/version.rb +3 -0
  29. data/proctor.gemspec +2 -1
  30. data/proctor/Proctorfile +35 -34
  31. data/proctor/templates/foreman_default_g.erb +3 -0
  32. data/proctor/templates/monit_default_s.erb +3 -6
  33. data/proctor/templates/monit_faye_s.erb +3 -3
  34. data/proctor/templates/upstart_default_g.erb +1 -0
  35. data/proctor/templates/upstart_default_s.erb +1 -3
  36. data/proctor/templates/upstart_passenger_s.erb +3 -0
  37. data/spec/acceptance/asset_list_spec.rb +16 -0
  38. data/spec/acceptance/asset_show_spec.rb +16 -0
  39. data/spec/acceptance/file_cleanup_spec.rb +16 -0
  40. data/spec/acceptance/file_export_spec.rb +16 -0
  41. data/spec/acceptance/render_group_spec.rb +15 -0
  42. data/spec/acceptance/render_service_spec.rb +16 -0
  43. data/spec/acceptance/start_spec.rb +16 -0
  44. data/spec/spec_helper.rb +22 -0
  45. data/spec/unit/command/file_export_cmd_spec.rb +46 -0
  46. data/spec/unit/command/file_list_cmd_spec.rb +30 -0
  47. data/spec/unit/command/render_group_cmd_spec.rb +44 -0
  48. data/spec/unit/command/render_service_cmd_spec.rb +42 -0
  49. data/spec/unit/{app_file_spec.rb → config/app_file_spec.rb} +27 -7
  50. data/spec/unit/config/app_files_spec.rb +55 -0
  51. data/spec/unit/config/env_spec.rb +37 -0
  52. data/spec/unit/config/template_file_spec.rb +55 -0
  53. data/spec/unit/config/template_files_spec.rb +32 -0
  54. data/spec/unit/manager_spec.rb +30 -10
  55. data/spec/unit/managers_spec.rb +40 -0
  56. data/spec/unit/node_spec.rb +34 -0
  57. data/spec/unit/nodes_spec.rb +36 -0
  58. data/spec/unit/service_spec.rb +31 -10
  59. data/spec/unit/services_spec.rb +34 -0
  60. metadata +86 -31
  61. data/Proctorfile +0 -4
  62. data/lib/proctor/app_config.rb +0 -23
  63. data/lib/proctor/app_file.rb +0 -44
  64. data/lib/proctor/cmd_state.rb +0 -39
  65. data/lib/proctor/presenter/list.rb +0 -67
  66. data/lib/proctor/presenter/render.rb +0 -39
  67. data/lib/proctor/template.rb +0 -5
  68. data/lib/proctor/template_config.rb +0 -22
  69. data/lib/proctor/template_file.rb +0 -44
  70. data/lib/proctor/util.rb +0 -12
  71. data/lib/proctor/version.rb +0 -3
  72. data/proctor/templates/foreman_default_n.erb +0 -9
  73. data/proctor/templates/monit_web_s.erb +0 -0
  74. data/proctor/templates/upstart_default_n.erb +0 -0
  75. data/spec/acceptance/help_spec.rb +0 -36
  76. data/spec/acceptance/list_spec.rb +0 -17
  77. data/spec/unit/app_config_spec.rb +0 -30
  78. data/spec/unit/template_config_spec.rb +0 -12
  79. data/spec/unit/template_file_spec.rb +0 -16
  80. data/spec/unit/template_spec.rb +0 -8
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --format progress
3
+ --fail-fast
data/TODO ADDED
@@ -0,0 +1,28 @@
1
+ Next Steps:
2
+
3
+ - Finish use cases
4
+ - Refactor
5
+
6
+ Use Cases:
7
+ - Manager: upstart, monit, foreman
8
+ - Services: showtime, showlog
9
+ - Pid files to shared/pid
10
+ - Logging to log directory
11
+
12
+ Features:
13
+ - Multi-level template import
14
+ - Add 'generate' command [-p (proctorfile) -t (templates)]
15
+ - Enforce unique asset names
16
+ - Global option '-d EXPORT_DIRECTORY'
17
+ - Global option '-t TEMPLATE_DIRECTORY'
18
+
19
+ - Full-screen status demon
20
+ - Asset inheritance "extends: XXX"
21
+ - Asset override: "manager.setting: YYY"
22
+ - Tab completion / 1st level / 2nd level / 3rd level
23
+ - Check should deep-check all Proctorfiles
24
+ - Better ERB error messages
25
+
26
+ Docco:
27
+ - intro video
28
+ - github website
data/bin/proctor CHANGED
@@ -10,171 +10,141 @@ require 'proctor'
10
10
  include GLI
11
11
  include Proctor
12
12
 
13
- APP_CONFIG=
14
-
15
13
  # ---- global options ----------------------------------------------------
16
14
 
17
15
  version Proctor::VERSION
18
16
  program_desc 'proctor - an application process manager'
19
17
 
20
18
  desc 'Select Proctorfile'
21
- default_value 'Proctorfile'
22
19
  arg_name 'PROCTORFILE'
23
20
  flag [:f,:proctorfile]
24
21
 
25
22
  # ----- commands ---------------------------------------------------------
26
23
 
27
- desc 'Validates your application Proctorfile'
28
- command :check do |c|
24
+ desc 'Lists handles, names and paths for proctor assets'
25
+ long_desc <<-EOF
26
+ Lists the name or path of each Proctor asset. Asset types include Templates,
27
+ AppFiles, Managers, Services, and Nodes. Managers, Services and Nodes are
28
+ defined within an AppFile. AppFile values from multiple files are merged
29
+ together. When there is a conflict, the last value take precedence.
30
+ EOF
31
+ command :asset_list do |c|
29
32
  c.action do |global,options,args|
30
- puts "OK"
33
+ Command::AssetListCmd.new(global, options, args).list
31
34
  end
32
35
  end
33
36
 
34
- desc 'Removes exported proctor assets'
37
+ desc 'Shows content of a proctor asset'
35
38
  long_desc <<-EOF
36
- Requires a NODE and a MANAGER name. To see
37
- all NODEs and MANAGERs, use 'proctor list'.
39
+ Displays a raw asset (Template, AppFile, Manager, Service, Node)
40
+ to the console. View asset handles using 'proctor list'. (asset
41
+ handles are three character strings at the start of each line)
38
42
  EOF
39
- arg_name "NODE MANAGER"
40
- command :cleanup do |c|
43
+ arg_name 'HANDLE'
44
+ command :asset_show do |c|
41
45
  c.action do |global,options,args|
42
- puts "CLEANUP"
46
+ Command::AssetShowCmd.new(global, options, args).show
43
47
  end
44
48
  end
45
49
 
46
- desc 'Exports assets to a management tool'
50
+ desc 'Removes exported proctor assets'
47
51
  long_desc <<-EOF
48
- Requires a NODE and a MANAGER name. To see
49
- all NODEs and MANAGERs, use 'proctor list'.
52
+ Requires a MANAGER and a NODE name. To see
53
+ all MANAGERs and NODEs , use 'proctor list'.
50
54
  EOF
51
- arg_name "NODE MANAGER"
52
- command :export do |c|
55
+ arg_name "MANAGER NODE"
56
+ command :file_cleanup do |c|
53
57
  c.action do |global,options,args|
54
- puts "EXPORT"
58
+ Command::FileCleanupCmd.new(global, options, args).cleanup
55
59
  end
56
60
  end
57
61
 
58
- desc 'Lists handles, names and paths for proctor assets'
62
+ desc 'Exports assets to a management tool'
59
63
  long_desc <<-EOF
60
- Lists the name or path of each Proctor asset. Asset types include Templates,
61
- AppFiles, Managers, Services, and Nodes. Managers, Services and Nodes are
62
- defined within an AppFile.
63
-
64
- AppFile values from multiple files are merged together. When there is a conflict,
65
- the last value take precedence.
64
+ Requires a MANAGER and a NODE name. To see
65
+ all MANAGERs and NODEs , use 'proctor list'.
66
66
  EOF
67
- command :list do |c|
68
- opt_arr = %w(templates appfiles managers services manifest all)
69
- opt_str = opt_arr.join('|')
70
- c.desc "One of: [#{opt_str}]"
71
- c.default_value 'all'
72
- c.arg_name 'ASSET_TYPE'
73
- c.flag [:t, :type]
67
+ arg_name "MANAGER NODE"
68
+ command :file_export do |c|
74
69
  c.action do |global,options,args|
75
- unless opt_arr.include?(options[:type])
76
- raise "unrecognized type (#{options[:type]}) must be one of [#{opt_str}]"
77
- end
78
- Presenter::List.new(global, options, args).render
70
+ Command::FileExportCmd.new(global, options, args).export
79
71
  end
80
72
  end
81
73
 
82
- desc 'Renders an ERB template to the console'
83
- arg_name 'MANAGER SERVICE'
74
+ desc 'Renders a Group-ERB template to the console'
75
+ arg_name 'MANAGER NODE'
84
76
  long_desc <<-EOF
85
- Requires a MANAGER and a SERVICE. To see all
86
- MANAGERs and SERVICEs, use 'proctor list'.
77
+ Requires a MANAGER and a NODE. To see all
78
+ MANAGERs and NODEs, use 'proctor asset_list'.
87
79
  EOF
88
- command :render do |c|
80
+ command :render_group do |c|
89
81
  c.action do |global,options,args|
90
- raise "usage 'render ROLE MANAGER'" unless args.length == 2
91
-
92
- Presenter::Render.new(global, options, args).render
82
+ Command::RenderGroupCmd.new(global, options, args).render
93
83
  end
94
84
  end
95
85
 
96
- desc 'Shows content of a proctor asset'
86
+ desc 'Renders a Service-ERB template to the console'
87
+ arg_name 'MANAGER SERVICE'
97
88
  long_desc <<-EOF
98
- Displays a raw asset (Template, AppFile, Manager, Service, Node)
99
- to the console. View asset handles using 'proctor list'. (asset
100
- handles are three character strings at the start of each line)
89
+ Requires a MANAGER and a SERVICE. To see all
90
+ MANAGERs and SERVICEs, use 'proctor asset_list'.
101
91
  EOF
102
- arg_name 'HANDLE'
103
- command :show do |c|
92
+ command :render_service do |c|
104
93
  c.action do |global,options,args|
105
- raise "need a handle!" if args.length != 1
106
- Presenter::Show.new(global, options, args).show
94
+ Command::RenderServiceCmd.new(global, options, args).render
107
95
  end
108
96
  end
109
97
 
110
98
  desc 'Starts the application'
111
- arg_name "NODE MANAGER"
99
+ arg_name 'MANAGER NODE'
112
100
  long_desc <<-EOF
113
101
  Requires a NODE and a MANAGER name. To see
114
- all NODEs and MANAGERs, use 'proctor list'.
102
+ all NODEs and MANAGERs, use 'proctor asset_list'.
115
103
  EOF
116
104
  command :start do |c|
117
-
118
105
  c.action do |global_options,options,args|
119
- puts "START"
106
+ Command::StartCmd.new(global_options, options, args).start
120
107
  end
121
108
  end
122
109
 
123
110
  desc 'Stops the application'
124
- arg_name "NODE MANAGER"
111
+ arg_name 'MANAGER NODE'
125
112
  long_desc <<-EOF
126
- Requires a NODE and a MANAGER name. To see
127
- all NODEs and MANAGERs, use 'proctor list'.
113
+ Requires a MANAGER and a NODE name. To see
114
+ all MANAGERs and NODEs, use 'proctor asset_list'.
128
115
  EOF
129
116
  command :stop do |c|
130
- c.desc 'Describe a switch to start'
131
- c.switch :s
132
-
133
- c.desc 'Describe a flag to start'
134
- c.default_value 'default'
135
- c.flag :f
136
117
  c.action do |global_options,options,args|
137
- puts "I AM IN STOP"
138
- # raise "that command made no sense"
118
+ raise "Usage 'stop MANAGER NODE'" unless args.length == 2
119
+ puts "STOP"
139
120
  end
140
121
  end
141
122
 
142
123
  desc 'Displays the application status'
143
- arg_name "NODE MANAGER"
124
+ arg_name 'MANAGER NODE'
144
125
  long_desc <<-EOF
145
- Requires a NODE and a MANAGER name. To see
146
- all NODEs and MANAGERs, use 'proctor list'.
126
+ Requires a MANAGER and a NODE name. To see
127
+ all MANAGERs and NODEs, use 'proctor asset_list'.
147
128
  EOF
148
129
  command :status do |c|
149
- c.desc 'Describe a switch to start'
150
- c.switch :s
151
-
152
- c.desc 'Describe a flag to start'
153
- c.default_value 'default'
154
- c.flag :f
155
130
  c.action do |global,options,args|
156
- puts "I AM IN STATUS"
131
+ raise "Usage 'status MANAGER NODE'" unless args.length == 2
132
+ puts "STATUS"
157
133
  end
158
134
  end
159
135
 
160
-
161
136
  # ----- pre/post --------------------------------------------------------------
162
137
 
163
138
  pre do |global,command,options,args|
164
- options[:app_config] = AppConfig.load(global) unless command.nil? || command.name == :help
165
- options[:templates] = TemplateConfig.load(global) unless command.nil? || command.name == :help
166
-
167
- # Return true to proceed; false to abort and not call the chosen command
168
- # Use skips_pre before a command to skip this block on that command only
139
+ params = [global, options, args]
140
+ options['env'] = Proctor::Config::Env.new(params)
169
141
  true
170
142
  end
171
143
 
172
144
  post do |global,command,options,args|
173
- # Use skips_post before a command to skip this block on that command only
174
145
  end
175
146
 
176
147
  on_error do |exception|
177
- # return false to skip default error handling
178
148
  true
179
149
  end
180
150
 
data/bin/showlog ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ cmd = "tail -f /var/log/syslog"
4
+
5
+ system cmd
data/bin/showtime ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ cmd = "while [ true ] ; do date ; sleep 5 ; done"
4
+
5
+ system cmd
@@ -0,0 +1,56 @@
1
+ module Proctor
2
+ module Command
3
+ class AssetListCmd
4
+
5
+ def initialize(global, options, args)
6
+ @global = global
7
+ @options = options
8
+ @args = args
9
+ @env = @options['env']
10
+ end
11
+
12
+ def gen_list
13
+ list_all
14
+ end
15
+
16
+ def list
17
+ puts gen_list
18
+ end
19
+
20
+ private
21
+
22
+ def format_list(list)
23
+ list.map {|item| "#{item.handle}: #{item.name}\n"}.join
24
+ end
25
+
26
+ def list_templates
27
+ "[Templates]\n" + format_list(@env.template_files.all)
28
+ end
29
+
30
+ def list_appfiles
31
+ "[AppFiles]\n" + format_list(@env.app_files.all)
32
+ end
33
+
34
+ def list_managers
35
+ "[Managers]\n" + format_list(@env.managers.all)
36
+ end
37
+
38
+ def list_services
39
+ "[Services]\n" + format_list(@env.services.all)
40
+ end
41
+
42
+ def list_nodes
43
+ "[Nodes]\n" + format_list(@env.nodes.all)
44
+ end
45
+
46
+ def list_all
47
+ list_templates +
48
+ list_appfiles +
49
+ list_managers +
50
+ list_services +
51
+ list_nodes
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -1,11 +1,16 @@
1
+ require_relative './helpers/command'
2
+
1
3
  module Proctor
2
- module Presenter
3
- class Show
4
+ module Command
5
+ class AssetShowCmd
6
+
7
+ include Helpers::Command
4
8
 
5
9
  def initialize(global, options, args)
6
10
  @global = global
7
11
  @options = options
8
12
  @args = args
13
+ check_number_of_args(args, 1, 'show HANDLE')
9
14
  validate_handle(args[0])
10
15
  end
11
16
 
@@ -0,0 +1,81 @@
1
+ require_relative "./helpers/command"
2
+ require_relative "../resource/erb"
3
+ require_relative "../resource/lookup"
4
+
5
+ module Proctor
6
+ module Command
7
+ class FileCleanupCmd
8
+
9
+ include Helpers::Command
10
+ include Proctor::Resource::Erb
11
+ include Proctor::Resource::Lookup
12
+
13
+ def initialize(global, options, args)
14
+ @env = options['env']
15
+ @manager = args[0]
16
+ @node = args[1]
17
+ opts = {'MANAGERS' => @env.managers.names, 'NODES' => @env.nodes.names}
18
+ check_number_of_args(args, 2, 'cleanup MANAGER NODE', opts)
19
+ end
20
+
21
+ def cleanup
22
+ manager = lookup_valid_manager(@manager, @env)
23
+ #update_manager_with_cl_options(manager)
24
+ use_node = manager.data["use_node_template"] # default = false
25
+ use_service = manager.data["use_service_template"]
26
+ use_service = true if use_service.nil?
27
+ node = lookup_valid_node(@node, @env)
28
+ env = @env
29
+ manager.data.each_pair do |k,v|
30
+ manager.data[k] = ERB.new(v).result(binding) if v.class == String
31
+ end
32
+ if use_node
33
+ services = lookup_valid_services_hash(node.name, @env)
34
+ template = lookup_valid_group_template(manager.name, node.name, @env)
35
+ remove_node_text_from_disk(manager, node, @env)
36
+ end
37
+ if use_service
38
+ node.services.each do |service_name|
39
+ service = lookup_valid_service(service_name, @env)
40
+ template = lookup_valid_service_template(manager.name, service.name, @env)
41
+ remove_service_text_from_disk(manager, service, @env)
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def remove_service_text_from_disk(manager, service, env)
49
+ export_dir = manager.data["export_directory"]
50
+ export_file = manager.data["service_filename"] || env.app['name']
51
+ export_path = "#{export_dir}/#{export_file}"
52
+ begin
53
+ system "rm -f #{export_path}" if File.exist?(export_path)
54
+ rescue
55
+ msg = "couldn't remove service file (#{export_path})"
56
+ raise FileRemoveError, msg
57
+ end
58
+ puts "Removed service template for #{manager.name}/#{service.name} from #{export_path}"
59
+ end
60
+
61
+ def remove_node_text_from_disk(manager, node, env)
62
+ export_dir = manager.data["export_directory"]
63
+ export_file = manager.data["node_filename"] || env.app['name']
64
+ export_path = "#{export_dir}/#{export_file}"
65
+ begin
66
+ system "rm -f #{export_path}" if File.exist?(export_path)
67
+ rescue
68
+ msg = "couldn't remove node file (#{export_path})"
69
+ raise FileRemoveError, msg
70
+ end
71
+ puts "Removed group template for #{manager.name}/#{node.name} from #{export_path}"
72
+ end
73
+
74
+ end
75
+
76
+ class FileRemoveError < RuntimeError
77
+ end
78
+
79
+ end
80
+ end
81
+
@@ -0,0 +1,79 @@
1
+ require_relative "./helpers/command"
2
+ require_relative "../resource/erb"
3
+ require_relative "../resource/lookup"
4
+
5
+ module Proctor
6
+ module Command
7
+ class FileExportCmd
8
+
9
+ include Helpers::Command
10
+ include Proctor::Resource::Erb
11
+ include Proctor::Resource::Lookup
12
+
13
+ def initialize(global, options, args)
14
+ @env = options['env']
15
+ @manager = args[0]
16
+ @node = args[1]
17
+ opts = {'MANAGERS' => @env.managers.names, 'NODES' => @env.nodes.names}
18
+ check_number_of_args(args, 2, 'file_export MANAGER NODE', opts)
19
+ end
20
+
21
+ def export
22
+ manager = lookup_valid_manager(@manager, @env)
23
+ #update_manager_with_cl_options(manager)
24
+ node = lookup_valid_node(@node, @env)
25
+ if manager.data["group_data"]
26
+ services = lookup_valid_services_hash(node.name, @env)
27
+ template = lookup_valid_group_template(manager.name, node.name, @env)
28
+ text = render_group_template(manager, node, services, template, @env)
29
+ write_group_text_to_disk(manager, node, text, @env)
30
+ end
31
+ if manager.data["service_data"]
32
+ node.services.each do |service_name|
33
+ service = lookup_valid_service(service_name, @env)
34
+ template = lookup_valid_service_template(manager.name, service.name, @env)
35
+ text = render_service_template(manager, service, template, @env)
36
+ write_service_text_to_disk(manager, service, text, @env)
37
+ end
38
+ end
39
+ nil
40
+ end
41
+
42
+ private
43
+
44
+ def write_service_text_to_disk(manager, service, text, env)
45
+ export_dir = manager.data["export_directory"]
46
+ export_file = manager.data["service_filename"] || env.app['name']
47
+ export_path = "#{export_dir}/#{export_file}"
48
+ begin
49
+ system "mkdir -p #{export_dir}"
50
+ File.open("#{export_dir}/#{export_file}", 'w') {|f| f.puts text}
51
+ rescue
52
+ msg = "couldn't open export file (#{export_dir}/#{export_file})"
53
+ raise FileWriteError, msg
54
+ end
55
+ #puts "Exported service template for #{manager.name}/#{service.name} to #{export_path}"
56
+ end
57
+
58
+ def write_group_text_to_disk(manager, node, text, env)
59
+ export_dir = manager.data["export_directory"]
60
+ export_file = manager.data["group_data"]["conf_filename"] || env.app['name']
61
+ export_path = "#{export_dir}/#{export_file}"
62
+ begin
63
+ system "mkdir -p #{export_dir}"
64
+ File.open("#{export_dir}/#{export_file}", 'w') {|f| f.puts text}
65
+ rescue
66
+ msg = "couldn't open export file (#{export_dir}/#{export_file})"
67
+ raise FileWriteError, msg
68
+ end
69
+ #puts "Exported group template for #{manager.name}/#{node.name} to #{export_path}"
70
+ end
71
+
72
+ end
73
+
74
+ class FileWriteError < RuntimeError
75
+ end
76
+
77
+ end
78
+ end
79
+