Skip to content

Commit

Permalink
Add option to restart Neo4j after a DB switch
Browse files Browse the repository at this point in the history
Also bump Neo4j target verion to 3.3.0, matching my Homebrew version.
  • Loading branch information
tomshafer committed Jan 2, 2018
1 parent 5642158 commit 5a8da84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .neo4jdbprofile
@@ -1,4 +1,4 @@
{
"NEO4J_CONF": "/usr/local/Cellar/neo4j/3.1.2/libexec/conf/neo4j.conf",
"NEO4J_DB_DIR": "/usr/local/Cellar/neo4j/3.1.2/libexec/data/databases"
"NEO4J_CONF": "/usr/local/Cellar/neo4j/3.3.0/libexec/conf/neo4j.conf",
"NEO4J_DB_DIR": "/usr/local/Cellar/neo4j/3.3.0/libexec/data/databases"
}
26 changes: 15 additions & 11 deletions neo4j-db-manager.py
Expand Up @@ -2,7 +2,7 @@
Usage:
neo4j-db-manager.py [--db-path=PATH] [--conf-file=PATH] ls
neo4j-db-manager.py [--conf-file=PATH] sw DATABASE
neo4j-db-manager.py [--conf-file=PATH] [--restart] sw DATABASE
neo4j-db-manager.py [-f] [--db-path=PATH] rm DATABASE
This tool offers three verbs:
Expand All @@ -14,6 +14,7 @@
Options:
-h --help Show this screen.
-f --force Remove database without additional confirmation.
-r --restart Restart Neo4j.
--db-path=PATH Location of the Neo4j database directory.
--conf-file=PATH Location of the Neo4j conf file.
Expand Down Expand Up @@ -45,49 +46,49 @@ def confirm_delete():

if __name__ == "__main__":
args = docopt.docopt(__doc__, version="1")

# Location of the Neo4j databases and conf file
# --------------------------------------------------------------------------
neo_db_basedir = None
neo_conf_file = None
# 1. Look in ~/.neo4jprofile

# 1. Look in ~/.neo4jprofile
profile_path = os.path.expanduser("~/.neo4jdbprofile")
if os.path.exists(profile_path):
with open(profile_path, "r") as fh:
json_payload = dict(json.load(fh))
if "NEO4J_DB_DIR" in json_payload.iterkeys():
neo_db_basedir = str(json_payload["NEO4J_DB_DIR"])
neo_conf_file = str(json_payload["NEO4J_CONF"])

# 2a. Look in --db-path for basedir
if args["--db-path"] is not None:
neo_db_basedir = os.path.expanduser(str(args["--db-path"]))

# 2b. Look in --conf-file for the config
if args["--conf-file"] is not None:
neo_conf_file = os.path.expanduser(str(args["--conf-file"]))

# 3. Make sure the necessary data exists
# Database path
if args["ls"] or args["rm"]:
if neo_db_basedir is None or not os.path.isdir(neo_db_basedir):
quit("Error: Neo4j database path was not found. Use either",
quit("Error: Neo4j database path was not found. Use either " +
"~/.neo4jdbprofile or --db-path to accomplish this.")

# Config file path
if args["ls"] or args["sw"]:
if neo_conf_file is None or not os.path.exists(neo_conf_file):
quit("Error: Neo4j config path was not found. Use either",
quit("Error: Neo4j config path was not found. Use either " +
"~/.neo4jdbprofile or --conf-file to accomplish this.")

# Carry out the program
# --------------------------------------------------------------------------
# MODE list
if args["ls"]:
cmd = shlex.split('grep dbms.active_database "%s"' % neo_conf_file)
current_db = "=".join(sp.check_output(cmd).split("=")[1:]).strip()

for db in sorted(glob.iglob(os.path.join(neo_db_basedir, "*.db"))):
db_basename = os.path.basename(db)
flag = "*" if db_basename == current_db else ""
Expand All @@ -110,3 +111,6 @@ def confirm_delete():
"-e 's/^\\s*#*\\s*dbms\\.active_database.*$/dbms.active_database=%s/' " +
"\"%s\"")
sp.check_call(args=shlex.split(cmd % (args["DATABASE"], neo_conf_file)))

if args["--restart"]:
sp.check_call(args = shlex.split("neo4j restart"))

0 comments on commit 5a8da84

Please sign in to comment.