Class: Neo4j::RakeTasks::ServerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/rake_tasks/server_manager.rb

Overview

Represents and manages a server installation at a specific path

Direct Known Subclasses

StarnixServerManager, WindowsServerManager

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ServerManager



8
9
10
11
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 8

def initialize(path)
  @path = Pathname.new(path)
  FileUtils.mkdir_p(@path)
end

Class Method Details

.change_password!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 88

def self.change_password!
  puts 'This will change the password for a Neo4j server'

  address, old_password, new_password = prompt_for_address_and_passwords!

  body = change_password_request(address, old_password, new_password)
  if body['errors']
    puts "An error was returned: #{body['errors'][0]['message']}"
  else
    puts 'Password changed successfully! Please update your app to use:'
    puts 'username: neo4j'
    puts "password: #{new_password}"
  end
end

.class_for_osObject



140
141
142
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 140

def self.class_for_os
  OS::Underlying.windows? ? WindowsServerManager : StarnixServerManager
end

.new_for_os(path) ⇒ Object



144
145
146
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 144

def self.new_for_os(path)
  class_for_os.new(path)
end

Instance Method Details

#config_auth_enabeled!(enabled) ⇒ Object



103
104
105
106
107
108
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 103

def config_auth_enabeled!(enabled)
  value = enabled ? 'true' : 'false'
  modify_config_file(
    'dbms.security.authorization_enabled' => value,
    'dbms.security.auth_enabled' => value)
end

#config_port!(port) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 110

def config_port!(port)
  puts "Config ports #{port} / #{port - 1}"

  if server_version >= '3.0.0'
    # These are not ideal, perhaps...
    modify_config_file('dbms.connector.https.enabled' => false,
                       'dbms.connector.http.enabled' => true,
                       'dbms.connector.http.address' => "0.0.0.0:#{port}",
                       'dbms.connector.https.address' => "localhost:#{port - 1}")
  else
    modify_config_file('org.neo4j.server.webserver.https.enabled' => false,
                       'org.neo4j.server.webserver.port' => port,
                       'org.neo4j.server.webserver.https.port' => port - 1)
  end
end

#consoleObject



46
47
48
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 46

def console
  system_or_fail(neo4j_command_path(:console))
end

#infoObject



60
61
62
63
64
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 60

def info
  validate_is_system_admin!

  system_or_fail(neo4j_command_path(:info))
end

#install(edition_string) ⇒ Object

MAIN COMMANDS



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 15

def install(edition_string)
  version = version_from_edition(edition_string)
  puts "Installing neo4j-#{version}"

  return false if neo4j_binary_path.exist?

  archive_path = download_neo4j(version)
  extract!(archive_path)

  FileUtils.rm archive_path

  puts "Neo4j installed to: #{@path}"
end

#modify_config_contents(contents, properties) ⇒ Object



134
135
136
137
138
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 134

def modify_config_contents(contents, properties)
  properties.inject(contents) do |r, (property, value)|
    r.gsub(/^\s*(#\s*)?#{property}\s*=\s*(.+)/, "#{property}=#{value}")
  end
end

#modify_config_file(properties) ⇒ Object

END MAIN COMMANDS



128
129
130
131
132
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 128

def modify_config_file(properties)
  contents = File.read(property_configuration_path)

  File.open(property_configuration_path, 'w') { |file| file << modify_config_contents(contents, properties) }
end

#resetObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 72

def reset
  validate_is_system_admin!

  stop

  delete_path = @path.join('data/graph.db/*')
  puts "Deleting all files matching #{delete_path}"
  FileUtils.rm_rf(Dir.glob(delete_path))

  delete_path = @path.join('data/log/*')
  puts "Deleting all files matching #{delete_path}"
  FileUtils.rm_rf(Dir.glob(delete_path))

  start
end

#restartObject



66
67
68
69
70
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 66

def restart
  validate_is_system_admin!

  system_or_fail(neo4j_command_path(:restart))
end

#shellObject



50
51
52
53
54
55
56
57
58
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 50

def shell
  not_started = !pid_path.exist?

  start if not_started

  system_or_fail(neo4j_shell_binary_path.to_s)

  stop if not_started
end

#start(wait = true) ⇒ Object



29
30
31
32
33
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 29

def start(wait = true)
  system_or_fail(neo4j_command_path(start_argument(wait))).tap do
    @pid = pid_path.read.to_i
  end
end

#stop(timeout = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/neo4j/rake_tasks/server_manager.rb', line 35

def stop(timeout = nil)
  validate_is_system_admin!

  Timeout.timeout(timeout) do
    system_or_fail(neo4j_command_path(:stop))
  end
rescue Timeout::Error
  puts 'Shutdown timeout reached, killing process...'
  Process.kill('KILL', @pid) if @pid
end