class Neo4jTest::Installer

Constants

BadEditionError

Public Class Methods

bootstrap(edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 10
def bootstrap(edition)
  raise BadEditionError if edition.empty?

  download_neo4j_unless_exists edition
  unzip_neo4j edition
  rake_auth_toggle :disable
end
clear_install_location() click to toggle source
# File lib/neo4j_test_server/installer.rb, line 111
def clear_install_location
  FileUtils.rmtree(install_location)
end
config(source_text, port) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 126
def config(source_text, port)
  s = set_property(source_text, 'org.neo4j.server.webserver.https.enabled', 'false')
  set_property(s, 'org.neo4j.server.webserver.port', port)
end
config_location() click to toggle source
# File lib/neo4j_test_server/installer.rb, line 115
def config_location
  "#{install_location}/conf/neo4j-server.properties"
end
download_neo4j(edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 39
def download_neo4j(edition)
  success = false

  File.open(download_to(edition), 'wb') do |file|
    file << request_url(download_url(edition))
    success = true
  end

  download_to(edition)
ensure
  File.delete(file_name) unless success
end
download_neo4j_unless_exists(edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 34
def download_neo4j_unless_exists(edition)
  download_neo4j(edition) unless File.exist?(download_to(edition))
  download_to(edition)
end
download_to(edition = '') click to toggle source
# File lib/neo4j_test_server/installer.rb, line 25
def download_to(edition = '')
  # We want to ensure that we download the Neo4j archive to the gem location.  Not the project's location
  File.join(File.expand_path('../../..', here), file_name(edition))
end
download_url(edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 30
def download_url(edition)
  "http://dist.neo4j.org/neo4j-#{edition}-#{OS::Underlying.windows? ? 'windows.zip' : 'unix.tar.gz'}"
end
file_name(edition = '') click to toggle source
# File lib/neo4j_test_server/installer.rb, line 18
def file_name(edition = '')
  suffix = OS::Underlying.windows? ? 'neo4j.zip' : 'neo4j-unix.tar.gz'
  prefix = edition.empty? ? '' : "#{edition}-"

  [prefix, suffix].join ''
end
get_environment() click to toggle source
# File lib/neo4j_test_server/installer.rb, line 101
def get_environment
  'development'
end
here() click to toggle source

Defining a method that represents the current location of this file for testing purposes since I cant seem to be able to mock out “__FILE__” in the rspec tests. I CAN however mock out this method during testing… :-)

# File lib/neo4j_test_server/installer.rb, line 147
def here
  __FILE__
end
install_location() click to toggle source
# File lib/neo4j_test_server/installer.rb, line 105
def install_location
  path = File.expand_path('../../../tmp/db/neo4j', __FILE__)
  FileUtils.mkdir_p(path)
  "#{path}/#{get_environment}"
end
rake_auth_toggle(status) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 119
def rake_auth_toggle(status)
  location = config_location
  text = File.read(location)
  replace = toggle_auth(status, text)
  File.open(location, 'w') { |file| file.puts replace }
end
request_url(url) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 94
def request_url(url)
  status = HTTParty.head(url).code
  fail "#{edition} is not available to download, try a different version" if status < 200 || status >= 300

  HTTParty.get(url)
end
set_property(source_text, property, value) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 131
def set_property(source_text, property, value)
  source_text.gsub(/#{property}\s*=\s*(\w+)/, "#{property}=#{value}")
end
toggle_auth(status, source_text) click to toggle source

Toggles the status of Neo4j 2.2’s basic auth

# File lib/neo4j_test_server/installer.rb, line 136
def toggle_auth(status, source_text)
  status_string = status == :enable ? 'true' : 'false'
  %w(dbms.security.authorization_enabled dbms.security.auth_enabled).each do |key|
    source_text = set_property(source_text, key, status_string)
  end
  source_text
end
unzip_for_unix(downloaded_file, edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 88
def unzip_for_unix(downloaded_file, edition)
  `tar -xvf #{downloaded_file}`
  `mv neo4j-#{edition} #{install_location}`
  puts 'Neo4j Installed in to neo4j directory.'
end
unzip_for_windows(downloaded_file, edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 64
def unzip_for_windows(downloaded_file, edition)
  # Extract and move to neo4j directory
  unless File.exist?(install_location)
    Zip::ZipFile.open(downloaded_file) do |zip_file|
      zip_file.each do |f|
        f_path = File.join('.', f.name)
        FileUtils.mkdir_p(File.dirname(f_path))
        begin
          zip_file.extract(f, f_path) unless File.exist?(f_path)
        rescue
          puts "#{f.name} failed to extract."
        end
      end
    end
    FileUtils.mv "neo4j-#{edition}", install_location
  end

  # Install if running with Admin Privileges
  if `reg query "HKU\\S-1-5-19"`.size > 0
    `"#{install_location}/bin/neo4j install"`
    puts 'Neo4j Installed as a service.'
  end
end
unzip_neo4j(edition) click to toggle source
# File lib/neo4j_test_server/installer.rb, line 52
def unzip_neo4j(edition)
  downloaded_file = download_to(edition)

  clear_install_location

  if OS::Underlying.windows?
    unzip_for_windows downloaded_file, edition
  else
    unzip_for_unix downloaded_file, edition
  end
end