-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving rake tasks for managing neo4j server into class structure so t…
…hat they can be tested
- Loading branch information
1 parent
57a5340
commit 6af51ca
Showing
4 changed files
with
159 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |