-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
87 lines (76 loc) · 2.61 KB
/
core.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
#!/usr/bin/python3
import os;
from sys import exit;
class Site:
def ensite( self, site ):
site = str( site );
# Append .conf automatically to the end if not specified
site += '.conf' if not site.endswith( '.conf' ) else '';
abs_site = site;
abs_enabled = os.path.join( self.sites_enabled, site );
abs_site = os.path.join( self.sites_available, abs_site.strip() );
# Check if site to be enabled exists
if not os.path.exists( abs_site ):
print( 'Unable to enable site: {0} does not exist'.format( abs_site ) );
exit( 1 );
elif( os.path.exists( abs_enabled ) ):
print( '{0} already enabled\nSkipping...Run\n\tsystemctl reload httpd'.format( site ) );
exit();
else:
# Seems to exist, proceed with enabling
print( 'Enabling \'{0}\' '.format( site ) );
try:
# Create a link from sites-available to sites-enabled. That's the whole trick
os.symlink( abs_site, abs_enabled );
print( "{0} enabled successfully\nReload httpd \n \t systemctl reload httpd".format( abs_site ) );
exit();
except IOError as e:
# Oops! We have a problem.
print( 'Unable to enable site:\n{0}'.format( e ) );
exit( 1 );
def dissite( self, site ):
site = str( site );
# Append .conf automatically to the end if not specified
site += '.conf' if not site.endswith( '.conf' ) else '';
# Create absolute path
abs_enabled = os.path.join( self.sites_enabled, site );
abs_available = os.path.join( self.sites_available, site );
# Check if its enabled so to be disabled
if not os.path.exists( abs_enabled ):
print( '{0} is not enabled\nSkipping...Run:\n\tsystemctl reload httpd'.format( site ) );
exit();
else:
print( 'Disabling \'{0}\''.format( site ) );
try:
# Unlink to disable
os.unlink( abs_enabled );
print( '{0} disabled successfully\nReload httpd\n\tsystemctl reload httpd'.format( site ) );
exit( 1 );
except IOError as e:
print( 'Unable to disable site:\n{0}'.format( e ) );
exit( 1 );
def enabled_sites( self ):
try:
# Get list of files, iterate and print
sites = os.listdir( self.sites_enabled );
if len( sites ) > 0:
for site in sites:
print( site.strip( '.conf' ) );
exit();
else:
print( 'No enabled sites' );
exit();
except IOError as e:
print( e );
exit( 1 );
def available_sites( self ):
try:
# Get list of files, iterate and print
available = os.listdir( self.sites_available );
enabled = os.listdir( self.sites_enabled );
disabled = list( set( available ).difference( enabled ) );
for site in disabled:
print( site.strip( '.conf' ) );
except IOError as e:
print( e );
exit( 1 );