-
Notifications
You must be signed in to change notification settings - Fork 6
/
nginxserver.py
66 lines (55 loc) · 1.84 KB
/
nginxserver.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
from jumpscale.loader import j
from jumpscale.core.base import Base, fields
import shutil
class NginxServer(Base):
server_name = fields.String(default="main")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.config_path = j.sals.fs.join_paths(j.core.dirs.CFGDIR, "nginx", self.server_name, "nginx.conf")
@property
def check_command_string(self):
return r"nginx.* \-c {CONFIG_PATH}".format(CONFIG_PATH=j.sals.fs.expanduser(self.config_path))
@property
def installed(self) -> bool:
"""check if nginx is installed
Returns:
bool: True if nginx is installed
"""
return shutil.which("nginx")
def start(self):
"""
start nginx server using your config path
"""
nginx = j.sals.nginx.get(self.server_name)
nginx.configure()
cmd = j.tools.startupcmd.get(f"nginx_{self.server_name}")
cmd.start_cmd = f"nginx -c {self.config_path}"
cmd.stop_cmd = f"nginx -c {self.config_path} -s stop"
cmd.path = nginx.cfg_dir
cmd.timeout = 10
cmd.process_strings_regex = [self.check_command_string]
cmd.save()
if not cmd.is_running():
cmd.start()
def stop(self):
"""
stop nginx server
"""
cmd = j.tools.startupcmd.get(f"nginx_{self.server_name}")
cmd.stop()
def is_running(self):
"""
Check if nginxserver is running
"""
return j.tools.startupcmd.get(f"nginx_{self.server_name}").is_running()
def reload(self):
"""
reload nginx server using your config path
"""
j.sals.process.execute(f"nginx -c {self.config_path} -s reload")
def restart(self):
"""
restart nginx server
"""
self.stop()
self.start()