-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb-service.py
123 lines (107 loc) · 3.02 KB
/
mongodb-service.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
#!/usr/bin/env python3
import ctypes
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
import argparse
import subprocess as sp
import sys
from dataclasses import dataclass
from os import makedirs, path
SERVICE_NAME = "MongoDB"
DB_PATH = "C:\\mongodb\\db"
LOG_PATH = "C:\\mongodb\\log.txt"
@dataclass
class CliArgs:
install: bool
remove: bool
start: bool
stop: bool
def get_args() -> CliArgs:
parser = argparse.ArgumentParser()
if "-h" in sys.argv or "--help" in sys.argv:
parser._print_message(
f"{path.basename(__file__)}\n\nSERVICE_NAME:\t{SERVICE_NAME}\nDB_PATH:\t{DB_PATH}\nLOG_PATH:\t{LOG_PATH}\n\n"
)
action = parser.add_argument_group("Action").add_mutually_exclusive_group(
required=True
)
action.add_argument(
"--install",
dest="install",
action="store_true",
default=False,
help=f"install a service for {SERVICE_NAME}",
)
action.add_argument(
"--remove",
dest="remove",
action="store_true",
default=False,
help=f"remove {SERVICE_NAME} service",
)
action.add_argument(
"--start",
dest="start",
action="store_true",
default=False,
help=f"start {SERVICE_NAME} service",
)
action.add_argument(
"--stop",
dest="stop",
action="store_true",
default=False,
help=f"stop {SERVICE_NAME} service",
)
return CliArgs(**vars(parser.parse_args()))
def run_as_admin(argv=None):
shell32 = ctypes.windll.shell32
if argv is None and shell32.IsUserAnAdmin():
return True
if argv is None:
argv = sys.argv
else:
arguments = argv
argument_line = " ".join(arguments)
executable = sys.executable
ret = shell32.ShellExecuteW(None, "runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
def run():
args = get_args()
if is_admin:
if args.install:
if not path.isdir(DB_PATH):
makedirs(DB_PATH)
result = sp.run(
[
"mongod",
f"--dbpath={DB_PATH}",
f"--logpath={LOG_PATH}",
"--install",
],
shell=True,
)
print(
f"Service {SERVICE_NAME} installed"
) if result.returncode == 0 else exit(result.returncode)
elif args.remove:
result = sp.run(
["mongod", "--remove"],
shell=True,
)
print(
f"Service {SERVICE_NAME} removed"
) if result.returncode == 0 else exit(result.returncode)
else:
cmd = ["net"]
if args.start:
cmd.append("start")
elif args.stop:
cmd.append("stop")
cmd.append(SERVICE_NAME)
sp.run(cmd, shell=True)
else:
run_as_admin(sys.argv)
if __name__ == "__main__":
run()