This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
neoctl.py
131 lines (110 loc) · 4.17 KB
/
neoctl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (c) 2002-2016 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Usage: neoctl.py <cmd=arg>
--start=path/to/neo4j/home : start neo4j
--stop=path/to/neo4j/home : stop neo4j
--update-password=s3cr3tP4ssw0rd : update the neo4j password
-h : show this help message
Example: neoctl.py --start=./neo4j-enterprise-3.0.0-M01
neoctl.py -h
"""
from __future__ import print_function
from json import dumps as json_dumps
from base64 import b64encode
from sys import argv, exit, stdout, stderr
from os import name
import getopt
from subprocess import Popen, PIPE
try:
from urllib.request import Request, urlopen, HTTPError
except ImportError:
from urllib2 import Request, urlopen, HTTPError
is_windows = (name == 'nt')
def main():
if len(argv) <= 1:
print_help()
exit()
try:
opts, args = getopt.getopt(argv[1:], "h", ["start=", "stop=", "update-password="])
except getopt.GetoptError as err:
print(str(err))
print_help()
exit_code = 2
else:
exit_code = 0
for opt, arg in opts:
if opt == '-h':
print_help()
elif opt == "--start":
exit_code = neo4j_start(neo4j_home=arg) or 0
elif opt == "--stop":
exit_code = neo4j_stop(neo4j_home=arg) or 0
elif opt == "--update-password":
exit_code = neo4j_update_default_password("localhost", 7474, new_password=arg) or 0
else:
print("Bad option %s" % opt)
exit_code = 1
if exit_code != 0:
break
exit(exit_code)
def neo4j_start(neo4j_home):
if is_windows:
return powershell([neo4j_home + '/bin/neo4j.bat install-service;', neo4j_home + '/bin/neo4j.bat start'])
else:
return callsysshell([neo4j_home + "/bin/neo4j", "start"])
def neo4j_stop(neo4j_home):
if is_windows:
return powershell([neo4j_home + '/bin/neo4j.bat stop;', neo4j_home + '/bin/neo4j.bat uninstall-service'])
else:
return callsysshell([neo4j_home+"/bin/neo4j", "stop"])
def neo4j_update_default_password(host, http_port, new_password):
exit_code = 0
if new_password == 'neo4j':
exit_code = neo4j_update_password(host, http_port, "neo4j", "neo4j", "1234") \
or neo4j_update_password(host, http_port, "neo4j", "1234", "neo4j")
else:
exit_code = neo4j_update_password(host, http_port, "neo4j", "neo4j", new_password)
return exit_code
def neo4j_update_password(host, http_port, user, password, new_password):
print("Changing password...")
request = Request("http://%s:%s/user/neo4j/password" % (host, http_port),
json_dumps({"password": new_password}, ensure_ascii=True).encode("utf-8"),
{"Authorization": "Basic " + b64encode((user + ":" + password).encode("utf-8")).decode("ascii"),
"Content-Type": "application/json"})
try:
f = urlopen(request)
f.read()
f.close()
except HTTPError as error:
raise RuntimeError("Cannot update password [%s]" % error)
def callsysshell(cmd):
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
return_code = p.wait()
stdout.write(out)
stderr.write(err)
return return_code
def powershell(cmd):
cmd = ['powershell.exe'] + cmd
return callsysshell(cmd)
def print_help():
print(__doc__)
if __name__ == "__main__":
main()