-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxcm.py
155 lines (123 loc) · 4.44 KB
/
xcm.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#Check the dependencies before running the script
try:
import os #To run the xammp commands
import sys #To check params passed to script
import platform #To check the operating system
except ImportError:
print("Error: This script requires : 'os' , 'sys' or 'platform' modules")
print("Please install the dependencies before running the script.")
raise SystemExit(1)
#Functions
#0- App Banner :p
def print_header():
# Website used to create this piece of art :D : https://www.asciiart.eu/text-to-ascii-art
header = r"""
.----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. |
| | ____ ____ | || | ______ | || | ____ ____ | |
| | |_ _||_ _| | || | .' ___ | | || ||_ \ / _|| |
| | \ \ / / | || | / .' \_| | || | | \/ | | |
| | > `' < | || | | | | || | | |\ /| | | |
| | _/ /'`\ \_ | || | \ `.___.'\ | || | _| |_\/_| |_ | |
| | |____||____| | || | `._____.' | || ||_____||_____|| |
| | | || | | || | | |
| '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '----------------'
"""
print(header)
#1-Check if Xammp installed
def check_xampp_installed(directory_path):
if not os.path.isdir(directory_path):
print("xampp is not installed.")
exit(1)
#2- Check OS and return Xammp path
def check_os():
system = platform.system()
if system == 'Windows':
#Later commands will be dynamics ;if windows commands will start without sudo
print("Support for windows will be added later")
exit(1)
#return "C:\\xampp"
elif system == 'Linux':
return "/opt/lampp/"
else:
print("Unknown OS")
exit(1)
#3-Display help menu if no args passed to script
def help_menu():
script_name = os.path.basename(__file__)
msg = f"""
Usage: {script_name} [service] [action]
service:
-ap --apache select Apache
-db --mysql select Database (MariadDB)
-ftp select ProFTPD
all select apache ftp & database
Options:
on turn on the service/services
off turn off the service/services
reload reload the service/services
Examples:
{script_name} -ap on start apache server
{script_name} db off turn off the database
{script_name} -all on start all the services
"""
print(msg)
exit(1)
#4-Action on one or multi services
def oneOrMulti_services(service,argument,allServices=None):
#Check if all services
if allServices is True:
if argument == "on" :
command="sudo ./xampp startapache ; sudo ./xampp startmysql ; sudo ./xampp startftp"
elif argument == "off" :
command="sudo ./xampp stopapache ; sudo ./xampp stopmysql ; sudo ./xampp stopftp"
elif argument == "reload" :
command="sudo ./xampp reloadapache;sudo ./xampp reloadmysql;sudo ./xampp reloadftp"
else :
print(f"Invalide argument : {argument} with : all services ")
sys.exit()
#Execute commands
os.system(command)
exit(1)
#Check if a service checked : apache , mysql or ftp
else :
if argument == "on" :
command="sudo ./xampp start"+service
elif argument == "off" :
command="sudo ./xampp stop"+service
elif argument == "reload" :
command="sudo ./xampp reload"+service
else:
print(f"Invalid parameter name : {argument} ")
help_menu()
os.system(command)
#End Functions Declaration
if len(sys.argv) == 1: #Check params passed to the script
print_header()
help_menu()
exit(2)
Xammp_dir=check_os()
check_xampp_installed(Xammp_dir)
os.chdir(Xammp_dir)
param = (sys.argv[1]).lower()
param2 = (sys.argv[2]).lower()
service=None
if param == "all" :
oneOrMulti_services(service,param2,True)
exit(1)
elif param == "-ap" or param == "--apache":
service='apache'
oneOrMulti_services(service,param2)
exit(1)
elif param == "-db" or param == "--mysql":
service='mysql'
oneOrMulti_services(service,param2)
exit(1)
elif param == "-ftp":
service='ftp'
oneOrMulti_services(service,param2)
exit(1)
else :
print(f"Invalid Service name : {param} ")
exit(2)