1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | """
|
22 | Usage: neoctl.py <cmd=arg>
|
23 | --start=path/to/neo4j/home : start neo4j
|
24 | --stop=path/to/neo4j/home : stop neo4j
|
25 | --update-password=s3cr3tP4ssw0rd : update the neo4j password
|
26 | -h : show this help message
|
27 |
|
28 | Example: neoctl.py --start=./neo4j-enterprise-3.0.0-M01
|
29 | neoctl.py -h
|
30 | """
|
31 |
|
32 | from __future__ import print_function
|
33 | from json import dumps as json_dumps
|
34 | from base64 import b64encode
|
35 | from sys import argv, exit, stdout, stderr
|
36 | from os import name
|
37 | import getopt
|
38 | from subprocess import Popen, PIPE
|
39 | try:
|
40 | from urllib.request import Request, urlopen, HTTPError
|
41 | except ImportError:
|
42 | from urllib2 import Request, urlopen, HTTPError
|
43 |
|
44 | is_windows = (name == 'nt')
|
45 |
|
46 |
|
47 | def main():
|
48 | if len(argv) <= 1:
|
49 | print_help()
|
50 | exit()
|
51 | try:
|
52 | opts, args = getopt.getopt(argv[1:], "h", ["start=", "stop=", "update-password="])
|
53 | except getopt.GetoptError as err:
|
54 | print(str(err))
|
55 | print_help()
|
56 | exit_code = 2
|
57 | else:
|
58 | exit_code = 0
|
59 | for opt, arg in opts:
|
60 | if opt == '-h':
|
61 | print_help()
|
62 | elif opt == "--start":
|
63 | exit_code = neo4j_start(neo4j_home=arg) or 0
|
64 | elif opt == "--stop":
|
65 | exit_code = neo4j_stop(neo4j_home=arg) or 0
|
66 | elif opt == "--update-password":
|
67 | exit_code = neo4j_update_default_password("localhost", 7474, new_password=arg) or 0
|
68 | else:
|
69 | print("Bad option %s" % opt)
|
70 | exit_code = 1
|
71 | if exit_code != 0:
|
72 | break
|
73 | exit(exit_code)
|
74 |
|
75 |
|
76 | def neo4j_start(neo4j_home):
|
77 | if is_windows:
|
78 | return powershell([neo4j_home + '/bin/neo4j.bat install-service;', neo4j_home + '/bin/neo4j.bat start'])
|
79 | else:
|
80 | return callsysshell([neo4j_home + "/bin/neo4j", "start"])
|
81 |
|
82 |
|
83 | def neo4j_stop(neo4j_home):
|
84 | if is_windows:
|
85 | return powershell([neo4j_home + '/bin/neo4j.bat stop;', neo4j_home + '/bin/neo4j.bat uninstall-service'])
|
86 | else:
|
87 | return callsysshell([neo4j_home+"/bin/neo4j", "stop"])
|
88 |
|
89 |
|
90 | def neo4j_update_default_password(host, http_port, new_password):
|
91 | exit_code = 0
|
92 | if new_password == 'neo4j':
|
93 | exit_code = neo4j_update_password(host, http_port, "neo4j", "neo4j", "1234") \
|
94 | or neo4j_update_password(host, http_port, "neo4j", "1234", "neo4j")
|
95 | else:
|
96 | exit_code = neo4j_update_password(host, http_port, "neo4j", "neo4j", new_password)
|
97 | return exit_code
|
98 |
|
99 |
|
100 | def neo4j_update_password(host, http_port, user, password, new_password):
|
101 | print("Changing password...")
|
102 | request = Request("http://%s:%s/user/neo4j/password" % (host, http_port),
|
103 | json_dumps({"password": new_password}, ensure_ascii=True).encode("utf-8"),
|
104 | {"Authorization": "Basic " + b64encode((user + ":" + password).encode("utf-8")).decode("ascii"),
|
105 | "Content-Type": "application/json"})
|
106 | try:
|
107 | f = urlopen(request)
|
108 | f.read()
|
109 | f.close()
|
110 | except HTTPError as error:
|
111 | raise RuntimeError("Cannot update password [%s]" % error)
|
112 |
|
113 |
|
114 | def callsysshell(cmd):
|
115 | p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
116 | out, err = p.communicate()
|
117 | return_code = p.wait()
|
118 | stdout.write(out)
|
119 | stderr.write(err)
|
120 | return return_code
|
121 |
|
122 | def powershell(cmd):
|
123 | cmd = ['powershell.exe'] + cmd
|
124 | return callsysshell(cmd)
|
125 |
|
126 |
|
127 | def print_help():
|
128 | print(__doc__)
|
129 |
|
130 | if __name__ == "__main__":
|
131 | main()
|