-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycloudd
executable file
·137 lines (114 loc) · 3.74 KB
/
pycloudd
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
# Copyright 2016 Year4000.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
### BEGIN INIT INFO
# Provides: pycloudd
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 4 5 6
# Default-Stop: 0 1 6
# Short-Description: Manages the PyCloud Daemon
# Description: Manages the PyCloud daemon by providing
# a wrapper for common PyCloud tasks.
### END INIT INFO
import sys
import os
import time
import signal
from pycloud.constants import PID_FILE
USAGE = 'Usage: {0} {{start|stop|restart|force-reload|status}}'
class PyCloudError(BaseException):
""" A basic error for PyCloud """
def __init__(self, message, *args, **kwargs):
self.__message = str(message).format(*args, **kwargs)
def get_message(self):
""" Get the formatted message of the error """
return self.__message
def get_pid(throw=True):
""" Get the pid or raise an exception """
if os.path.exists(PID_FILE):
with open(PID_FILE) as pid_file:
return int(pid_file.read())
else:
if throw:
raise PyCloudError('No process id file found: {0}', PID_FILE)
else:
return False
def is_pid_running(process_id):
""" Check if the id is a running process """
try:
os.kill(process_id, 0)
except OSError:
return False
else:
return True
def stop():
""" Stop the PyCloud daemon """
pid = -1
try:
pid = get_pid()
os.kill(pid, signal.SIGABRT)
print('PyCloud has been stopped...')
except OSError:
raise PyCloudError('Found process id {0} and PyCloud not running...', pid)
finally:
while pid > 0 and is_pid_running(pid):
time.sleep(0.25)
if os.path.exists(PID_FILE):
os.remove(PID_FILE)
def start():
""" Start the PyCloud daemon """
if get_pid(throw=False):
raise PyCloudError('PyCloud is currently running...')
else:
os.system('sudo -u year4000 python3 -m pycloud.app --daemon')
print('PyCloud has been started...')
def status():
""" Get the status of the PyCloud daemon """
if get_pid(throw=False):
print('PyCloud is running...')
return True
else:
print('PyCloud is not running...')
return False
try:
if os.getuid() != 0:
raise PyCloudError('PyCloud must be ran as root user')
if __name__ == "__main__" and len(sys.argv) > 1:
argument = sys.argv[1]
if argument == 'start':
start()
elif argument == 'stop':
stop()
elif argument == 'status':
status()
elif argument == 'restart':
stop()
start()
elif argument == 'force-reload':
try:
stop()
except PyCloudError as error:
print('An error has been suppressed: ' + error.get_message())
pass
finally:
start()
else:
raise PyCloudError(USAGE, sys.argv[0])
else:
raise PyCloudError(USAGE, sys.argv[0])
except PyCloudError as error:
print(error.get_message())
sys.exit(1)