From 6af51cabc61b9f9f443f7d70682bb65acd812fe2 Mon Sep 17 00:00:00 2001 From: Brian Underwood Date: Fri, 3 Jul 2015 23:19:37 -0600 Subject: [PATCH] Moving rake tasks for managing neo4j server into class structure so that they can be tested --- lib/neo4j/tasks/neo4j_server.rake | 59 ++++++---------- lib/neo4j/tasks/server_manager.rb | 82 +++++++++++++++++++++++ lib/neo4j/tasks/starnix_server_manager.rb | 16 +++++ lib/neo4j/tasks/windows_server_manager.rb | 41 ++++++++++++ 4 files changed, 159 insertions(+), 39 deletions(-) create mode 100644 lib/neo4j/tasks/server_manager.rb create mode 100644 lib/neo4j/tasks/starnix_server_manager.rb create mode 100644 lib/neo4j/tasks/windows_server_manager.rb diff --git a/lib/neo4j/tasks/neo4j_server.rake b/lib/neo4j/tasks/neo4j_server.rake index 6f387721..4466c770 100644 --- a/lib/neo4j/tasks/neo4j_server.rake +++ b/lib/neo4j/tasks/neo4j_server.rake @@ -6,6 +6,9 @@ require 'zip' require 'httparty' require 'pathname' require File.expand_path('../config_server', __FILE__) +require File.expand_path('../windows_server_manager', __FILE__) +require File.expand_path('../starnix_server_manager', __FILE__) + namespace :neo4j do def file_name @@ -52,32 +55,10 @@ namespace :neo4j do install_location!(args).join('conf/neo4j-server.properties') end - def start_server(command, args) - puts "Starting Neo4j #{get_environment(args)}..." - if OS::Underlying.windows? - start_windows_server(command, args) - else - start_starnix_server(command, args) - end - end - def system_or_fail(command) system(command.to_s) or fail "Unable to run: #{command}" # rubocop:disable Style/AndOr end - def start_windows_server(command, args) - if local_service? - system_or_fail(install_location!(args).join("/bin/Neo4j.bat #{command}")) # start service - else - puts 'Starting Neo4j directly, not as a service.' - system_or_fail(install_location!(args).join('/bin/Neo4j.bat')) - end - end - - def start_starnix_server(command, args) - system_or_fail(install_location!(args).join("bin/neo4j #{command}")) - end - def get_edition(args) edition_string = args[:edition] @@ -90,10 +71,14 @@ namespace :neo4j do end end - def local_service? + def nt_admin? system_or_fail('reg query "HKU\\S-1-5-19"').size > 0 end + def server_manager(environment) + ::Neo4j::Tasks::ServerManager.new_for_os(environment) + end + desc 'Install Neo4j with auth disabled in v2.2+, example neo4j:install[community-latest,development]' task :install, :edition, :environment do |_, args| edition = get_edition(args) @@ -121,7 +106,7 @@ namespace :neo4j do end # Install if running with Admin Privileges - if local_service? + if nt_admin? bin_path = install_location!(args).join('bin/neo4j') system_or_fail("\"#{bin_path} install\"") puts 'Neo4j Installed as a service.' @@ -139,12 +124,14 @@ namespace :neo4j do desc 'Start the Neo4j Server' task :start, :environment do |_, args| - start_server('start', args) + server_manager = server_manager(args[:environment]) + server_manager.start end desc 'Start the Neo4j Server asynchronously' task :start_no_wait, :environment do |_, args| - start_server('start-no-wait', args) + server_manager = server_manager(args[:environment]) + server_manager.start(false) end desc 'Configure Server, e.g. rake neo4j:config[development,8888]' @@ -159,7 +146,7 @@ namespace :neo4j do def validate_is_system_admin! return unless OS::Underlying.windows? - return if local_service? + return if nt_admin? fail 'You do not have administrative rights to stop the Neo4j Service' end @@ -172,26 +159,20 @@ namespace :neo4j do desc 'Stop the Neo4j Server' task :stop, :environment do |_, args| - puts "Stopping Neo4j #{get_environment(args)}..." - validate_is_system_admin! - - run_neo4j_command_or_fail!(args, :stop) + server_manager = server_manager(args[:environment]) + server_manager.stop end desc 'Get info the Neo4j Server' task :info, :environment do |_, args| - puts "Info from Neo4j #{get_environment(args)}..." - validate_is_system_admin! - - run_neo4j_command_or_fail!(args, :info) + server_manager = server_manager(args[:environment]) + server_manager.info end desc 'Restart the Neo4j Server' task :restart, :environment do |_, args| - puts "Restarting Neo4j #{get_environment(args)}..." - validate_is_system_admin! - - run_neo4j_command_or_fail!(args, :restart) + server_manager = server_manager(args[:environment]) + server_manager.restart end desc 'Reset the Neo4j Server' diff --git a/lib/neo4j/tasks/server_manager.rb b/lib/neo4j/tasks/server_manager.rb new file mode 100644 index 00000000..839ec2e5 --- /dev/null +++ b/lib/neo4j/tasks/server_manager.rb @@ -0,0 +1,82 @@ +require 'pathname' + +puts 'server manager!' + +module Neo4j + module Tasks + class ServerManager + BASE_INSTALL_DIR = Pathname.new('db/neo4j') + + def initialize(environment) + @environment = environment || 'development' + end + + # MAIN COMMANDS + + def start(wait = true) + puts "Starting Neo4j #{@environment}..." + system_or_fail(binary_command(start_command(wait))) + end + + def stop + validate_is_system_admin! + + puts "Stopping Neo4j #{@environment}..." + run_neo4j_command_or_fail!(:stop) + end + + def info + validate_is_system_admin! + + puts "Info from Neo4j #{@environment}..." + run_neo4j_command_or_fail!(:info) + end + + def restart + validate_is_system_admin! + + puts "Restarting Neo4j #{@environment}..." + run_neo4j_command_or_fail!(:restart) + end + + # END MAIN COMMANDS + + def self.new_for_os(environment) + manager_class = OS::Underlying.windows? ? WindowsServerManager : StarnixServerManager + + manager_class.new(environment) + end + + protected + + def start_argument(wait) + wait ? 'start' : 'start-no-wait' + end + + def binary_command(binary_file) + install_location!.join('bin', binary_file) + end + + def validate_is_system_admin! + nil + end + + def system_or_fail(command) + system(command.to_s) or fail "Unable to run: #{command}" # rubocop:disable Style/AndOr + end + + private + + def run_neo4j_command_or_fail!(command) + system_or_fail(binary_command("#{neo4j_binary} #{command}")) + end + + def install_location! + FileUtils.mkdir_p(BASE_INSTALL_DIR) + + BASE_INSTALL_DIR.join(@environment) + end + + end + end +end \ No newline at end of file diff --git a/lib/neo4j/tasks/starnix_server_manager.rb b/lib/neo4j/tasks/starnix_server_manager.rb new file mode 100644 index 00000000..4e068f2e --- /dev/null +++ b/lib/neo4j/tasks/starnix_server_manager.rb @@ -0,0 +1,16 @@ +require File.expand_path('../server_manager', __FILE__) + +module Neo4j + module Tasks + class StarnixServerManager < ServerManager + + def neo4j_binary + 'neo4j' + end + + def start_command(wait) + "#{neo4j_binary} #{start_argument(wait)}" + end + end + end +end \ No newline at end of file diff --git a/lib/neo4j/tasks/windows_server_manager.rb b/lib/neo4j/tasks/windows_server_manager.rb new file mode 100644 index 00000000..267690b0 --- /dev/null +++ b/lib/neo4j/tasks/windows_server_manager.rb @@ -0,0 +1,41 @@ +require File.expand_path('../server_manager', __FILE__) + +module Neo4j + module Tasks + class WindowsServerManager < ServerManager + + def neo4j_binary + 'Neo4j.bat' + end + + def start_command(wait) + binary_file = neo4j_binary + + if local_service? + binary_file += " #{start_argument(wait)}" + else + puts 'Starting Neo4j directly, not as a service.' + end + + binary_file + end + + def local_service? + system_or_fail('reg query "HKU\\S-1-5-19"').size > 0 + end + + def validate_is_system_admin! + return if nt_admin? + + fail 'You do not have administrative rights to stop the Neo4j Service' + end + + private + + def nt_admin? + system_or_fail('reg query "HKU\\S-1-5-19"').size > 0 + end + + end + end +end \ No newline at end of file