forked from laeg/neokit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neoctl.py
117 lines (99 loc) · 3.64 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
#!/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
from os import name
import getopt
from subprocess import call, 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_password("localhost", 7474, "neo4j", "neo4j", 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:
call([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:
call([neo4j_home+"/bin/neo4j", "stop"])
def neo4j_update_password(host, http_port, user, password, new_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 powershell(cmd):
cmd = ['powershell.exe'] + cmd
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
return_code = p.wait()
print(out)
print(err)
return return_code
def print_help():
print(__doc__)
if __name__ == "__main__":
main()