Skip to content

Commit

Permalink
Moving rake tasks for managing neo4j server into class structure so t…
Browse files Browse the repository at this point in the history
…hat they can be tested
  • Loading branch information
cheerfulstoic committed Jul 4, 2015
1 parent 57a5340 commit 6af51ca
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 39 deletions.
59 changes: 20 additions & 39 deletions lib/neo4j/tasks/neo4j_server.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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)
Expand Down Expand Up @@ -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.'
Expand All @@ -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]'
Expand All @@ -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
Expand All @@ -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'
Expand Down
82 changes: 82 additions & 0 deletions lib/neo4j/tasks/server_manager.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions lib/neo4j/tasks/starnix_server_manager.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions lib/neo4j/tasks/windows_server_manager.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6af51ca

Please sign in to comment.