UNPKG

4.27 kBPlain TextView Raw
1#!/usr/bin/env python
2# -*- encoding: utf-8 -*-
3
4# Copyright (c) 2002-2016 "Neo Technology,"
5# Network Engine for Objects in Lund AB [http://neotechnology.com]
6#
7# This file is part of Neo4j.
8#
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20
21"""
22Usage: 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
28Example: neoctl.py --start=./neo4j-enterprise-3.0.0-M01
29 neoctl.py -h
30"""
31
32from __future__ import print_function
33from json import dumps as json_dumps
34from base64 import b64encode
35from sys import argv, exit, stdout, stderr
36from os import name
37import getopt
38from subprocess import Popen, PIPE
39try:
40 from urllib.request import Request, urlopen, HTTPError
41except ImportError:
42 from urllib2 import Request, urlopen, HTTPError
43
44is_windows = (name == 'nt')
45
46
47def 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
76def 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
83def 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
90def 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
100def 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
114def 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
122def powershell(cmd):
123 cmd = ['powershell.exe'] + cmd
124 return callsysshell(cmd)
125
126
127def print_help():
128 print(__doc__)
129
130if __name__ == "__main__":
131 main()