local_pac 0.5.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.travis.yml +1 -1
  2. data/Gemfile.lock +1 -3
  3. data/app/controllers/git_hook_controller.rb +25 -4
  4. data/app/controllers/lookup_controller.rb +1 -0
  5. data/app/locales/en.yml +9 -1
  6. data/bin/local_pac +5 -129
  7. data/config.ru +2 -2
  8. data/features/{show_status.feature_ → show_status.feature} +2 -1
  9. data/features/step_definitions.rb +4 -0
  10. data/lib/local_pac.rb +9 -2
  11. data/lib/local_pac/actions/get_system_information.rb +2 -2
  12. data/lib/local_pac/actions/handle_error.rb +26 -0
  13. data/lib/local_pac/actions/initialize_application.rb +71 -0
  14. data/lib/local_pac/actions/reload_local_storage.rb +2 -0
  15. data/lib/local_pac/actions/show_application_status.rb +39 -0
  16. data/lib/local_pac/actions/show_pac_file.rb +29 -0
  17. data/lib/local_pac/actions/validate_pac_file.rb +34 -0
  18. data/lib/local_pac/cli/main.rb +122 -0
  19. data/lib/local_pac/cli/reload.rb +8 -4
  20. data/lib/local_pac/cli/show.rb +34 -0
  21. data/lib/local_pac/cli/validate.rb +22 -0
  22. data/lib/local_pac/config.rb +1 -1
  23. data/lib/local_pac/error_handler.rb +60 -0
  24. data/lib/local_pac/exceptions.rb +6 -0
  25. data/lib/local_pac/git_storage.rb +30 -10
  26. data/lib/local_pac/pac_file_validator.rb +17 -2
  27. data/lib/local_pac/version.rb +1 -1
  28. data/local_pac.gemspec +0 -2
  29. data/script/acceptance_test +4 -0
  30. data/script/bootstrap +5 -0
  31. data/script/ci +3 -0
  32. data/script/release +3 -0
  33. data/script/unit_test +3 -0
  34. data/spec/actions/handle_error_spec.rb +29 -0
  35. data/spec/{initializer_spec.rb → actions/initialize_application_spec.rb} +3 -3
  36. data/spec/{application_status_spec.rb → actions/show_application_status_spec.rb} +5 -5
  37. data/spec/actions/show_pac_file_spec.rb +36 -0
  38. data/spec/actions/validate_pac_file_spec.rb +62 -0
  39. data/spec/error_handler_spec.rb +70 -0
  40. data/spec/features/check_git_push_spec.rb +34 -6
  41. data/spec/git_repository_spec.rb +1 -0
  42. data/spec/git_storage_spec.rb +33 -0
  43. data/spec/pac_file_validator_spec.rb +19 -10
  44. metadata +30 -26
  45. data/lib/local_pac/application_status.rb +0 -37
  46. data/lib/local_pac/initializer.rb +0 -69
@@ -4,4 +4,4 @@ rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
6
  - 2.1.0
7
- script: bundle exec rake test:coveralls
7
+ script: script/ci
@@ -1,13 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- local_pac (0.4.0)
4
+ local_pac (0.5.0)
5
5
  activesupport
6
6
  addressable
7
7
  bootstrap-sass
8
8
  compass
9
9
  facter
10
- git_hook-pre_receive (~> 0.1.0)
11
10
  haml
12
11
  i18n
13
12
  pac
@@ -105,7 +104,6 @@ GEM
105
104
  ruby-progressbar (~> 1.3)
106
105
  gherkin (2.12.2)
107
106
  multi_json (~> 1.3)
108
- git_hook-pre_receive (0.1.0)
109
107
  github-markup (1.0.1)
110
108
  haml (4.0.5)
111
109
  tilt
@@ -48,19 +48,40 @@ module LocalPac
48
48
  )
49
49
  end
50
50
 
51
+ error Exceptions::ApiKeyInvalid do
52
+ halt 403, json(
53
+ error_summary: I18n.t('errors.invalid_api_key.summary'),
54
+ error_details: I18n.t('errors.invalid_api_key.details', name: env['sinatra.error']),
55
+ result: :failure,
56
+ )
57
+ end
58
+
59
+ error Exceptions::ReloadOfLocalStorageFailed do
60
+ halt 500, json(
61
+ error_summary: I18n.t('errors.reload_of_local_storage_failed.summary'),
62
+ error_details: I18n.t('errors.reload_of_local_storage_failed.details'),
63
+ result: :failure,
64
+ )
65
+ end
66
+
67
+ before do
68
+ fail Exceptions::ApiKeyInvalid unless params['api_key'] == LocalPac.config.api_key
69
+ end
70
+
51
71
  post '/pre-receive' do
52
72
  param :old_commit_id, String, required: true
53
73
  param :new_commit_id, String, required: true
54
74
  param :reference, String, required: true
55
75
  param :api_key, String, required: true
56
76
 
57
- repo = GitRepository.new(LocalPac.config.local_storage)
58
- files = repo.added_files(params[:old_commit_id], params[:new_commit_id])
59
77
  validator = PacFileValidator.new
60
78
 
61
- LocalPac.ui_logger.debug "Files found: " + files.to_s
79
+ repo = GitStorage.new(LocalPac.config.local_storage)
80
+ repo.each_added_pac_file(params[:old_commit_id], params[:new_commit_id]) do |f|
81
+ fail Exceptions::PacFileInvalid, f.path unless validator.valid?(f)
82
+ end
62
83
 
63
- fail Exceptions::PacFileInvalid if files.any? { |f| !validator.valid?(f) }
84
+ LocalPac::Actions::ReloadLocalStorage.new([LocalPac::App::FileServeController, LocalPac::App::LookupController]).run
64
85
 
65
86
  json result: :success
66
87
  end
@@ -17,6 +17,7 @@ module LocalPac
17
17
  @file = local_storage.find(params[:name].to_s)
18
18
  @uri = Addressable::URI.heuristic_parse(params[:url].to_s)
19
19
 
20
+
20
21
  fail Sinatra::NotFound, params[:name].to_s if @file.nil?
21
22
 
22
23
  parser = LocalPac::ProxyPac::PacParser.new(file: @file)
@@ -1,7 +1,15 @@
1
1
  ---
2
2
  en:
3
3
  errors:
4
- invalid_proxy_pac: The requested proxy.pac-file "%{name}" is invalid.
4
+ reload_of_local_storage_failed:
5
+ summary: Reload failure...
6
+ details: The reload of local storage failed. Please ask the administrator for support.
7
+ invalid_api_key:
8
+ summary: Invalid API-key...
9
+ details: Your request does not contain a valid api key.
10
+ invalid_proxy_pac:
11
+ summary: Invalid ProxyPac-file...
12
+ details: The requested proxy.pac-file "%{name}" is invalid.
5
13
  unknown_proxy_pac:
6
14
  summary: Unknown ProxyPac-file...
7
15
  details: Sorry, but I cannot find proxy.pac-file "%{name}".
@@ -1,135 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: utf-8
3
- $LOAD_PATH <<::File.expand_path('../../lib', __FILE__)
3
+ $LOAD_PATH << ::File.expand_path('../../lib', __FILE__)
4
4
  require 'local_pac'
5
5
 
6
- module LocalPac
7
- module Cli
8
- class Main < Thor
9
- class_option :config_file, type: :string, desc: 'Config file'
10
- class_option :log_level, default: 'info', type: :string, desc: 'Log level for ui logging'
11
- class_option :debug_mode, type: :boolean, desc: 'Run application in debug mode'
12
6
 
13
- no_commands {
14
- include LocalPac::Cli::Helper
15
- }
16
-
17
- desc 'serve', 'Serve pacfiles'
18
- option :access_log, type: :string, desc: 'File to write access log to'
19
- option :listen, type: :string, desc: 'Listen for requests'
20
- option :environment, type: :string, desc: 'Rack environment for application'
21
- option :with, type: :string, default: 'puma', desc: 'Server used to serve proxy pac'
22
- def serve
23
- LocalPac.config = LocalPac::Config.new(options[:config_file]) if options[:config_file]
24
- LocalPac.config.access_log = options[:access_log] if options[:access_log]
25
- LocalPac.config.log_level = options[:log_level] if options[:log_level]
26
- LocalPac.config.debug_mode = options[:debug_mode] if options[:debug_mode]
27
- LocalPac.config.environment = options[:environment] if options[:environment]
28
- LocalPac.config.listen = options[:listen] if options[:listen]
29
- LocalPac.config.lock
30
-
31
- LocalPac.ui_logger.level = LocalPac.config.log_level
32
- LocalPac.enable_debug_mode if LocalPac.config.debug_mode
33
-
34
- LocalPac.ui_logger.debug('Options: ' + options.to_s)
35
- LocalPac.ui_logger.debug("Config:\n" + LocalPac.config.to_s)
36
-
37
- command_klass = case options[:with].to_sym
38
- when :puma
39
- ServerCommands::Puma
40
- when :rackup
41
- ServerCommands::Rackup
42
- else
43
- ServerCommands::Rackup
44
- end
45
-
46
- command = command_klass.new(
47
- listen: LocalPac.config.listen,
48
- environment: LocalPac.config.environment,
49
- )
50
-
51
- ENV['DEBUG'] = LocalPac.config.debug_mode.to_s if LocalPac.config.debug_mode
52
- ENV['ACCESS_LOG'] = LocalPac.config.access_log.to_s if LocalPac.config.access_log
53
- ENV['LOG_LEVEL'] = LocalPac.config.log_level.to_s if LocalPac.config.log_level
54
-
55
- Server.new(command).start
56
- end
57
-
58
- desc 'init', 'Create files/directories to use local_pac in dir or $PWD'
59
- option :force, type: :boolean, default: false, desc: 'Overwrite existing files?'
60
- option :pre_seed, type: :boolean, default: false, desc: 'Add some example files to git repository'
61
- option :config_file, type: :string, desc: 'Path to config file'
62
-
63
- option :local_storage, type: :string, desc: 'Path to local storage'
64
- option :pid_file, type: :string, desc: 'Path to pid file'
65
- option :access_log, type: :string, desc: 'Path to access log'
66
- option :sass_cache, type: :string, desc: 'Path to sass cache'
67
- option :reload_config_signal, type: :string, desc: 'Signal to reload config'
68
- option :reload_storage_signal, type: :string, desc: 'Signal to reload local storage'
69
- option :api_key, type: :string, desc: 'API key for communication between git hook and web application'
70
- option :listen, type: :string, desc: 'Listen statement for rack server'
71
- option :environment, type: :string, desc: 'Default environment for rack server'
72
-
73
- option :create_pid_directory, type: :boolean, desc: 'Create pid directory', default: true
74
- option :create_log_directory, type: :boolean, desc: 'Create log directory', default: true
75
- option :create_sass_cache, type: :boolean, desc: 'Create sass cache directory', default: true
76
- option :create_local_storage, type: :boolean, desc: 'Create local storage directory', default: true
77
- option :create_pre_receive_hook, type: :boolean, desc: 'Create pre receive hook', default: true
78
- option :create_config_file, type: :boolean, desc: 'Create config_directory', default: true
79
- def init
80
- LocalPac.config = LocalPac::Config.new(options[:config_file]) if options[:config_file]
81
-
82
- LocalPac.config.log_level = options[:log_level] if options[:log_level]
83
- LocalPac.config.debug_mode = options[:debug_mode] if options[:debug_mode]
84
- LocalPac.config.local_storage = options[:local_storage] if options[:local_storage]
85
- LocalPac.config.pid_file = options[:pid_file] if options[:pid_file]
86
- LocalPac.config.access_log = options[:access_log] if options[:access_log]
87
- LocalPac.config.sass_cache = options[:sass_cache] if options[:sass_cache]
88
- LocalPac.config.reload_config_signal = options[:reload_config_signal] if options[:reload_config_signal]
89
- LocalPac.config.reload_storage_signal = options[:reload_storage_signal] if options[:reload_storage_signal]
90
- LocalPac.config.log_level = options[:log_level] if options[:log_level]
91
- LocalPac.config.api_key = options[:api_key] if options[:api_key]
92
- LocalPac.config.listen = options[:listen] if options[:listen]
93
- LocalPac.config.environment = options[:environment] if options[:environment]
94
-
95
- LocalPac.config.lock
96
-
97
- LocalPac.ui_logger.level = LocalPac.config.log_level
98
- LocalPac.enable_debug_mode if LocalPac.config.debug_mode
99
-
100
- LocalPac.ui_logger.debug('Options: ' + options.to_s)
101
- LocalPac.ui_logger.debug("Config:\n" + LocalPac.config.to_s)
102
-
103
- Initializer.new(
104
- force: options[:force],
105
- pre_seed: options[:pre_seed],
106
- create_pid_directory: options[:create_pid_directory],
107
- create_log_directory: options[:create_log_directory],
108
- create_sass_cache: options[:create_sass_cache],
109
- create_local_storage: options[:create_local_storage],
110
- create_pre_receive_hook: options[:create_pre_receive_hook],
111
- create_config_file: options[:create_config_file],
112
- ).run
113
- end
114
-
115
- desc 'status', 'Show status of local_pac: configuration, known proxy pacs, server running etc.'
116
- def status
117
- LocalPac.config = LocalPac::Config.new(options[:config_file]) if options[:config_file]
118
- LocalPac.config.log_level = options[:log_level] if options[:log_level]
119
- LocalPac.config.debug_mode = options[:debug_mode] if options[:debug_mode]
120
- LocalPac.config.lock
121
-
122
- LocalPac.ui_logger.level = LocalPac.config.log_level
123
- LocalPac.enable_debug_mode if LocalPac.config.debug_mode
124
-
125
- ApplicationStatus.new.show
126
- end
127
-
128
- desc 'reload', 'Reload configuration, local storage etc.'
129
- option :pid_file, type: :string, desc: 'Pid file of daemon'
130
- subcommand 'reload', LocalPac::Cli::Reload
131
- end
132
-
133
- end
7
+ begin
8
+ LocalPac::Cli::Main.start
9
+ rescue StandardError => e
10
+ LocalPac::Actions::HandleError.new(e).run
134
11
  end
135
- LocalPac::Cli::Main.start
data/config.ru CHANGED
@@ -17,7 +17,7 @@ trap LocalPac.config.reload_config_signal do
17
17
  LocalPac.ui_logger.warn 'Reload of configuration requested'
18
18
  LocalPac::Actions::ReloadConfiguration.new.run
19
19
  LocalPac.ui_logger.info 'Reload of configuration successful'
20
- rescue => e
20
+ rescue Exceptions::ReloadOfLocalStorageFailed => e
21
21
  LocalPac.ui_logger.fatal "Reload of configuration failed: #{e.message}"
22
22
  end
23
23
  end
@@ -27,7 +27,7 @@ trap LocalPac.config.reload_storage_signal do
27
27
  LocalPac.ui_logger.warn 'Reload of local storage requested'
28
28
  LocalPac::Actions::ReloadLocalStorage.new([LocalPac::App::FileServeController, LocalPac::App::LookupController]).run
29
29
  LocalPac.ui_logger.info 'Reload of local storage successful'
30
- rescue => e
30
+ rescue Exceptions::ReloadOfLocalStorageFailed => e
31
31
  LocalPac.ui_logger.fatal "Reload of local storage failed: #{e.message}"
32
32
  end
33
33
  end
@@ -4,6 +4,7 @@ Feature: Show status
4
4
  In order to make less mistakes
5
5
 
6
6
  @wip
7
+ @slow_process
7
8
  Scenario: Default
8
9
  Given a file named "config.yaml" with:
9
10
  """
@@ -12,7 +13,7 @@ Feature: Show status
12
13
  access_log: 'log/access.log'
13
14
  executable: 'bin/local_pac'
14
15
  """
15
- When I successfully run `local_pac status`
16
+ When I successfully run `local_pac status --config-file config.yaml`
16
17
  Then the stdout should contain:
17
18
  """
18
19
  pid_file: 'run/pid'
@@ -12,3 +12,7 @@ end
12
12
  Then(/^I got$/) do |string|
13
13
  expect(@result).to eq(string)
14
14
  end
15
+
16
+ Before('@slow_process') do
17
+ @aruba_timeout_seconds = 10
18
+ end
@@ -35,6 +35,7 @@ require 'active_support/cache'
35
35
  require 'active_support/core_ext/string/inflections'
36
36
 
37
37
  require 'local_pac/exceptions'
38
+ require 'local_pac/error_handler'
38
39
  require 'local_pac/config'
39
40
  require 'local_pac/ui_logger'
40
41
  require 'local_pac/main'
@@ -54,6 +55,8 @@ require 'local_pac/template_repository'
54
55
  require 'local_pac/template_file'
55
56
  require 'local_pac/pac_file_validator'
56
57
  require 'local_pac/actions/show_available_proxy_pac_files'
58
+ require 'local_pac/actions/show_pac_file'
59
+ require 'local_pac/actions/validate_pac_file'
57
60
  require 'local_pac/actions/get_system_information'
58
61
  require 'local_pac/actions/send_signal'
59
62
  require 'local_pac/actions/create_repository'
@@ -67,12 +70,16 @@ require 'local_pac/actions/print_title'
67
70
  require 'local_pac/actions/reload_configuration'
68
71
  require 'local_pac/actions/reload_local_storage'
69
72
  require 'local_pac/actions/add_examples_to_local_storage'
73
+ require 'local_pac/actions/show_application_status'
74
+ require 'local_pac/actions/initialize_application'
75
+ require 'local_pac/actions/handle_error'
70
76
  require 'local_pac/server_commands/rackup'
71
77
  require 'local_pac/server_commands/puma'
72
78
  require 'local_pac/cli/helper'
73
79
  require 'local_pac/cli/reload'
74
- require 'local_pac/initializer'
75
- require 'local_pac/application_status'
80
+ require 'local_pac/cli/show'
81
+ require 'local_pac/cli/validate'
82
+ require 'local_pac/cli/main'
76
83
  require 'local_pac/local_storage'
77
84
  require 'local_pac/server'
78
85
  require 'local_pac/proxy_pac/html_table_style'
@@ -21,11 +21,11 @@ module LocalPac
21
21
  private
22
22
 
23
23
  def data
24
- Timeout::timeout(5) do
24
+ Timeout::timeout(10) do
25
25
  Facter.to_hash.keep_if { |key,_| fields.include?(key.to_s) }
26
26
  end
27
27
  rescue Timeout::Error
28
- LocalPac.ui_logger.warn 'Getting environment information took to long. Please make sure the name resolver is avaiable. This might cause the latency.'
28
+ LocalPac.ui_logger.warn 'Getting environment information took to long. Please make sure the name resolver is available. This might cause the latency.'
29
29
  {}
30
30
  end
31
31
 
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ module Actions
4
+ class HandleError
5
+
6
+ private
7
+
8
+ attr_reader :exception, :original_message, :handler_klass
9
+
10
+ public
11
+
12
+ def initialize(exception, handler_klass = ErrorHandler)
13
+ @exception = exception.class
14
+ @original_message = exception.message
15
+ @handler_klass = handler_klass
16
+ end
17
+
18
+ def run
19
+ handler = handler_klass.find exception
20
+ handler.original_message = original_message
21
+
22
+ handler.run
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,71 @@
1
+ module LocalPac
2
+ module Actions
3
+ class InitializeApplication
4
+
5
+ private
6
+
7
+ attr_reader :config, :options
8
+
9
+ public
10
+
11
+ def initialize(options = {}, config = LocalPac.config)
12
+ @options = options
13
+ @config = config
14
+ end
15
+
16
+ def run
17
+ create_pid_directory if options[:create_pid_directory]
18
+ create_log_directory if options[:create_log_directory]
19
+ create_sass_cache if options[:create_sass_cache]
20
+ create_local_storage if options[:create_local_storage]
21
+ create_pre_receive_hook if options[:create_pre_receive_hook]
22
+ create_config_file if options[:create_config_file]
23
+ pre_seed if options[:pre_seed]
24
+
25
+ show_example_config
26
+ end
27
+
28
+ private
29
+
30
+ def create_pid_directory
31
+ LocalPac.ui_logger.info "Creating pid directory: #{::File.dirname(config.pid_file)}"
32
+ Actions::CreateDirectory.new(::File.dirname(config.pid_file), force: options[:force]).run
33
+ end
34
+
35
+ def create_log_directory
36
+ LocalPac.ui_logger.info "Creating log directory: #{::File.dirname(config.access_log)}"
37
+ Actions::CreateDirectory.new(::File.dirname(config.access_log), force: options[:force]).run
38
+ end
39
+
40
+ def create_sass_cache
41
+ LocalPac.ui_logger.info "Creating sass cache #{config.sass_cache}"
42
+ Actions::CreateDirectory.new(config.sass_cache, force: options[:force]).run
43
+ end
44
+
45
+ def create_local_storage
46
+ LocalPac.ui_logger.info "Creating local storage: #{config.local_storage}"
47
+ Actions::CreateRepository.new(config.local_storage, bare: true, force: options[:force]).run
48
+ end
49
+
50
+ def create_pre_receive_hook
51
+ LocalPac.ui_logger.info "Creating pre-receive hook in local storage \"#{config.local_storage}\"."
52
+ Actions::CreateFile.new(:'git-hook.rb', ::File.join(config.local_storage, 'hooks', 'pre-receive'), Data.new(config), force: options[:force], executable: true).run
53
+ end
54
+
55
+ def create_config_file
56
+ LocalPac.ui_logger.info "Creating config file at \"#{config.config_file}\"."
57
+ Actions::CreateFile.new(:'example-config', config.config_file, Data.new(config), force: options[:force], create_directories: true).run
58
+ end
59
+
60
+ def pre_seed
61
+ LocalPac.ui_logger.info "Adding examples to repository at #{config.local_storage}/examples"
62
+ Actions::AddExamplesToLocalStorage.new(config.local_storage).run
63
+ end
64
+
65
+ def show_example_config
66
+ LocalPac.ui_logger.info "Showing the configuration of local_pac on your system."
67
+ Actions::CreateOutput.new(:'example-config', $stdout, Data.new(config)).run
68
+ end
69
+ end
70
+ end
71
+ end
@@ -16,6 +16,8 @@ module LocalPac
16
16
 
17
17
  def run
18
18
  sinatra_apps.each { |app| app.set :local_storage, local_storage }
19
+ rescue StandardError => e
20
+ raise Exceptions::ReloadOfLocalStorageFailed, e.message
19
21
  end
20
22
  end
21
23
  end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ module Actions
4
+ class ShowApplicationStatus
5
+
6
+ private
7
+
8
+ include Pager
9
+
10
+ attr_reader :should_page
11
+
12
+ public
13
+
14
+ def initialize(options = {})
15
+ @should_page = options.fetch(:pager, :true)
16
+ config = options.fetch(:config, LocalPac.config)
17
+
18
+ @actions = []
19
+ @actions << Actions::PrintTitle.new('System Information')
20
+ @actions << Actions::GetSystemInformation.new
21
+ @actions << Actions::PrintNewline.new(2)
22
+ @actions << Actions::PrintTitle.new('Application Configuration')
23
+ @actions << Actions::ShowConfig.new
24
+ @actions << Actions::PrintNewline.new(2)
25
+ @actions << Actions::PrintTitle.new('Process Information')
26
+ @actions << Actions::ShowProcessInformation.new(config.pid_file)
27
+ @actions << Actions::PrintNewline.new(2)
28
+ @actions << Actions::PrintTitle.new('Available Proxy.Pac-Files')
29
+ @actions << Actions::ShowAvailableProxyPacFiles.new(config.local_storage)
30
+ end
31
+
32
+ def run
33
+ page if should_page
34
+
35
+ @actions.each(&:run)
36
+ end
37
+ end
38
+ end
39
+ end