Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit fc75af4

Browse files
author
Zhen
committed
Moving neokit here to be generally accessable to everyone
0 parents commit fc75af4

File tree

5 files changed

+471
-0
lines changed

5 files changed

+471
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Neo4j Driver Toolkit
2+
3+
Tools for downloading, managing and testing Neo4j servers.
4+
5+
- `neoget` - download and unarchive Neo4j server packages
6+
- `neoctl` - start and stop Neo4j servers and update default server password
7+
- `neorun` - start and stop a Neo4j server with guarantee of server fully started and stopped
8+
9+
10+
## Neoget
11+
Neoget is a download script for fetching Neo4j server packages. To download the latest nightly version of Neo4j, simply use:
12+
```
13+
python neoget.py
14+
```
15+
If successful, the downloaded package would be unarchived immediately following the download.
16+
17+
To install a specific Neo4j package version, use `-v` to specify the version:
18+
```
19+
python neoget.py -v 3.0.1
20+
```
21+
22+
The `-n` option can be used to carry out a download for the latest nightly version of Neo4j.
23+
```
24+
python neoget.py -n 3.0
25+
```
26+
Then the archive with the version `3.0-NIGHTLY` would be downloaded to local disk
27+
28+
Alternatively, a url could also be used with option `-l` to directly download the Neo4j specified by the url
29+
```
30+
python neoget.py -l http://alpha.neohq.net/dist/neo4j-enterprise-3.0-NIGHTLY-unix.tar.gz
31+
```
32+
33+
For a full help page, use `-h`:
34+
```
35+
python neoget.py -h
36+
```
37+
38+
## Neoctl
39+
Neoctl is a controller for start and stop Neo4j packages. It also provides a method to update default neo4j password.
40+
41+
To start a server, use the `start` command:
42+
```
43+
python neoctl.py --start=neo4j
44+
```
45+
46+
Similarly, to stop a server, use the `stop` command:
47+
```
48+
python neoctl.py --stop=neo4j
49+
```
50+
51+
To change the default passowrd of a Neo4j server, simply use
52+
```
53+
python neoctrl.py --update-passowrd=s3cr3tP4ssw0rd
54+
```
55+
56+
57+
## Neorun
58+
Neorun provides commands to start and stop a Neo4j server with the guarantee that the server is fully started and stopped when the script returns.
59+
The start command also exposes a command to download and install a specific Neo4j server if no Neo4j found locally,
60+
as well as an option to change the default server password after the start.
61+
62+
To start a Neo4j server, simply use the following command:
63+
```
64+
python neorun.py --start=neo4j
65+
```
66+
When the script returns, then the server is fully ready for any database tasks.
67+
68+
If no Neo4j server is found in path `./neo4j`, then the default Neo4j server version used in `neoget.py` will be downloaded and installed to `./neo4j`.
69+
To specify other versions to download when a Neo4j is absent, use `-v`, `-n`, `-l` in a similar way as they are defined in `neoget.py`:
70+
```
71+
python neorun.py --start=neo4j -n 3.1 -p TOUFU
72+
```
73+
In the example above, the `-p` option is used to change the default Neo4j password after the server is ready.
74+
75+
For stopping the server, simply use the `stop` command:
76+
```
77+
python neorun.py --stop=neo4j
78+
```
79+

neoctl.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
"""
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
36+
from os import name
37+
import getopt
38+
from subprocess import call, 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_password("localhost", 7474, "neo4j", "neo4j", 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+
call([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+
call([neo4j_home+"/bin/neo4j", "stop"])
88+
89+
90+
def neo4j_update_password(host, http_port, user, password, new_password):
91+
request = Request("http://%s:%s/user/neo4j/password" % (host, http_port),
92+
json_dumps({"password": new_password}, ensure_ascii=True).encode("utf-8"),
93+
{"Authorization": "Basic " + b64encode((user + ":" + password).encode("utf-8")).decode("ascii"),
94+
"Content-Type": "application/json"})
95+
try:
96+
f = urlopen(request)
97+
f.read()
98+
f.close()
99+
except HTTPError as error:
100+
raise RuntimeError("Cannot update password [%s]" % error)
101+
102+
103+
def powershell(cmd):
104+
cmd = ['powershell.exe'] + cmd
105+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
106+
out, err = p.communicate()
107+
return_code = p.wait()
108+
print(out)
109+
print(err)
110+
return return_code
111+
112+
113+
def print_help():
114+
print(__doc__)
115+
116+
if __name__ == "__main__":
117+
main()

neoget.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
"""
22+
Usage: neoget.py <cmd> [arg]
23+
-v neo4j-version: download this specific neo4j enterprise version
24+
-n neo4j-version: download this neo4j enterprise nightly version
25+
-l download-url : download neo4j provided by this url
26+
-h : show this help message
27+
28+
Example: neoget.py -v 2.3.1
29+
neoget.py -h
30+
neoget.py -n 3.0
31+
"""
32+
from __future__ import print_function
33+
from urllib import urlretrieve
34+
from sys import argv, stdout, exit
35+
import getopt
36+
from os import path, name
37+
from zipfile import ZipFile
38+
from tarfile import TarFile
39+
from urlparse import urlparse
40+
41+
DIST = "http://dist.neo4j.org"
42+
NIGHTLY_DIST = "http://alpha.neohq.net/dist"
43+
NIGHTLY30_UNIX_URL = "http://alpha.neohq.net/dist/neo4j-enterprise-3.0-NIGHTLY-unix.tar.gz"
44+
NIGHTLY30_WIN_URL = "http://alpha.neohq.net/dist/neo4j-enterprise-3.0-NIGHTLY-windows.zip"
45+
46+
is_windows = (name == 'nt')
47+
48+
49+
def main():
50+
try:
51+
opts, args = getopt.getopt(argv[1:], "hv:s:l:")
52+
except getopt.GetoptError as err:
53+
print(str(err))
54+
print_help()
55+
exit()
56+
57+
archive_url, archive_name = neo4j_default_archive()
58+
59+
for opt, arg in opts:
60+
if opt == '-h':
61+
print_help()
62+
exit()
63+
elif opt in ('-v', '-n', '-l'):
64+
archive_url, archive_name = neo4j_archive(opt, arg)
65+
try:
66+
download(archive_url, archive_name)
67+
finally:
68+
ret = 0 if path.exists(archive_name) else 1
69+
exit(ret)
70+
71+
72+
def neo4j_default_archive():
73+
archive_url = NIGHTLY30_WIN_URL if is_windows else NIGHTLY30_UNIX_URL
74+
archive_name = path.split(urlparse(archive_url).path)[-1]
75+
return archive_url, archive_name
76+
77+
78+
def neo4j_archive(opt, arg):
79+
archive_url, archive_name = '', ''
80+
81+
if opt == '-v':
82+
if is_windows:
83+
archive_name = "neo4j-enterprise-%s-windows.zip" % arg
84+
else:
85+
archive_name = "neo4j-enterprise-%s-unix.tar.gz" % arg
86+
archive_url = "%s/%s" % (DIST, archive_name)
87+
elif opt == '-n':
88+
if is_windows:
89+
archive_name = "neo4j-enterprise-%s-NIGHTLY-windows.zip" % arg
90+
else:
91+
archive_name = "neo4j-enterprise-%s-NIGHTLY-unix.tar.gz" % arg
92+
archive_url = "%s/%s" % (NIGHTLY_DIST, archive_name)
93+
elif opt == '-l':
94+
archive_url = arg
95+
archive_name = path.split(urlparse(archive_url).path)[-1]
96+
return archive_url, archive_name
97+
98+
99+
def download(archive_url, archive_name, extract_to_path='.'):
100+
stdout.write("Downloading %s...\n" % archive_url)
101+
urlretrieve(archive_url, archive_name)
102+
103+
if archive_name.endswith('.zip'):
104+
stdout.write("Unzipping %s...\n" % archive_name)
105+
zip_ref = ZipFile(archive_name, 'r')
106+
zip_ref.extractall(extract_to_path)
107+
zip_ref.close()
108+
elif archive_name.endswith('.tar.gz'):
109+
stdout.write("Unarchiving %s...\n" % archive_name)
110+
tar_ref = TarFile.open(archive_name)
111+
tar_ref.extractall(extract_to_path)
112+
untar_folder=tar_ref.getnames()[0]
113+
tar_ref.close()
114+
return untar_folder
115+
116+
117+
def print_help():
118+
print(__doc__)
119+
120+
121+
if __name__ == "__main__":
122+
main()

0 commit comments

Comments
 (0)