summarylogtreecommitdiffstats
path: root/nginx-site
blob: 4a346ba030aff1fa1bafb0cfbdfc91a3b7cb9981 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash - 
#===============================================================================
#
#          FILE: nginx-site
# 
#   DESCRIPTION: Script for adding/disabling sites on nginx, based
#               on script from http://catap.ru/debian-catap/
# 
#        AUTHOR: Piotr Rogoża (piecia), rogoza dot piotr at gmail dot com
#  ORGANIZATION: dracoRP
#===============================================================================

CONFIGFILE='/etc/conf.d/nginx-site.conf'
#{{{ ENV
SYSCONFDIR='/etc/nginx'
EXTSITE='.conf'
SITESAVAILABLE="sites-available"
SITESENABLED="sites-enabled"
RELOADSERVICE='no'
if [ -r "$CONFIGFILE" ]; then
    source $CONFIGFILE
fi
# else use default options
#}}}
### Don't modify below the line.
PROGRAMNAME=$(basename $0)
PROGENABLE=nginx-ensite
PROGDISABLE=nginx-dissite
# Help, Version
OPTIONS='hv'
VERSION=0.1
#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------
enable_site (){ #{{{
#---  FUNCTION  ----------------------------------------------------------------
#          NAME:  enable_site
#   DESCRIPTION:  enable site
#    PARAMETERS:  site priority
#       RETURNS:  -
#-------------------------------------------------------------------------------
    if [ -z "$1" ]; then
        echo "Which site would you like to enable?"
        echo -n "Your choices are: "
        ls $SYSCONFDIR/$SITESAVAILABLE/*$EXTSITE 2>/dev/null | \
        sed -e "s,$SYSCONFDIR/$SITESAVAILABLE/,,g" -e "s,$EXTSITE,,g" | xargs echo
        echo -n "Site name? "
        read SITENAME
    else
        SITENAME=$1
    fi

    if [ -z "$SITENAME" ]; then
        echo "No site has been selected."
        exit
    fi

#    if [ "$SITENAME" = "default" ]; then
#        PRIORITY="000"
#    fi
    
#    if [ -z "$2" ]; then
#        echo -n "Enter the priority for the site:"
#        read PRIORITY
#    fi
    if [ -n "$2" ]; then
        PRIORITY="$2"
    fi

    if [ -e "$SYSCONFDIR/$SITESENABLED/$SITENAME$EXTSITE" -o \
        -e "$SYSCONFDIR/$SITESENABLED/$PRIORITY-$SITENAME" ]; then
        echo "This site is already enabled!"
        exit 0
    fi

    if ! [ -e "$SYSCONFDIR/$SITESAVAILABLE/$SITENAME$EXTSITE" ]; then
        echo "This site does not exist!" >&2
        exit 1
    fi

    if [ "$SITENAME" = "default" ]; then
        ln -sf $SYSCONFDIR/$SITESAVAILABLE/$SITENAME$EXTSITE \
            $SYSCONFDIR/$SITESENABLED/${PRIORITY:+$PRIORITY-}$SITENAME$EXTSITE
        RETVAL=$?
    else
        ln -sf $SYSCONFDIR/$SITESAVAILABLE/$SITENAME$EXTSITE \
            $SYSCONFDIR/$SITESENABLED/$SITENAME$EXTSITE
        RETVAL=$?
    fi

    if [ $RETVAL -eq 0 ]; then
        echo "Site '${PRIORITY:+$PRIORITY-}$SITENAME' installed; run 'systemctl reload nginx' to enable."
        return 0
    else
        echo "Site '${PRIORITY:+$PRIORITY-}$SITENAME' is not installed."
        return 1
    fi
} # ----------  end of function enable_site  ----------}}}
disable_site (){ #{{{
#---  FUNCTION  ----------------------------------------------------------------
#          NAME:  disable_site
#   DESCRIPTION:  disable site
#    PARAMETERS:  site
#       RETURNS:  -
#-------------------------------------------------------------------------------
    if [ -z "$1" ]; then
        echo "Which site would you like to disable?"
        echo -n "Your choices are: "
        ls $SYSCONFDIR/$SITESENABLED/*$EXTSITE 2>/dev/null| \
        sed -e "s,$SYSCONFDIR/$SITESENABLED/,,g" -e "s,$EXTSITE,,g" | xargs echo
        echo -n "Site name? "
        read SITENAME
    else
        SITENAME=$1
    fi

    if [ -z "$SITENAME" ]; then
        echo "No site has been selected."
        exit
    fi

#    if [ -z "$2" ]; then
#        echo -n "Enter the priority for the site:"
#        read PRIORITY
#    fi
    if [ -n "$2" ]; then
        PRIORITY="$2"
    fi

    if ! [ -e "$SYSCONFDIR/$SITESENABLED/$SITENAME$EXTSITE" -o \
        -e "$SYSCONFDIR/$SITESENABLED/$PRIORITY-$SITENAME$EXTSITE" ]; then
        if [ -e "$SYSCONFDIR/$SITESAVAILABLE/$SITENAME$EXTSITE" ]; then
            echo "Site $SITENAME is already disabled"
            exit 0
        fi
        echo "Site $SITENAME does not exist!" >&2
        exit 1
    fi

    rm "$SYSCONFDIR/$SITESENABLED/$SITENAME$EXTSITE" 2>/dev/null
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        rm -f "$SYSCONFDIR/$SITESENABLED/${PRIORITY:+$PRIORITY-}$SITENAME$EXTSITE"
        RETVAL=$?
    fi
    if [ $RETVAL -eq 0 ]; then
        echo "Site '$SITENAME' disabled; run 'systemctl reload nginx' to fully disable."
    else
        echo "Site '$SITENAME' is not disabled."
        return 1
    fi
} # ----------  end of function disable_site  ----------}}}
long_help (){ #{{{
    echo "Run '$PROGDISABLE [site]' to disable a site"
    echo "Run '$PROGENABLE [site] [priority]' to enable a site"
    echo "If you do not specify the name of a site you will be asked to do so."
} # ----------  end of function long_help  ----------}}}
short_help (){ #{{{
    echo "Run '$PROGRAMNAME -h' to see help"
} # ----------  end of function short_help  ----------}}}
show_version (){ #{{{
#---  FUNCTION  ----------------------------------------------------------------
#          NAME:  show_version
#   DESCRIPTION:  show version of this script
#    PARAMETERS:  -
#       RETURNS:  -
#-------------------------------------------------------------------------------
    echo "$PROGRAMNAME version $VERSION"
} # ----------  end of function show_version  ----------}}}
_check_permission (){ #{{{
#---  FUNCTION  ----------------------------------------------------------------
#          NAME:  _check_permission
#   DESCRIPTION:  check permission for write to the $SITESENABLED directory
#    PARAMETERS:  -
#       RETURNS:  1 if don't have permission
#-------------------------------------------------------------------------------
    if [ ! -w "$SYSCONFDIR/$SITESENABLED" ]; then
        echo "You don't have permission to write or delete files from the directory: $SYSCONFDIR/$SITESENABLED"
        return 1
    fi
} # ----------  end of function _check_permission  ----------}}}
reload_service (){ #{{{
#---  FUNCTION  ----------------------------------------------------------------
#          NAME:  reload_service
#   DESCRIPTION:  
#    PARAMETERS:  
#       RETURNS:  
#-------------------------------------------------------------------------------
    if [ "$RELOADSERVICE" = 'yes' ]; then
        systemctl reload nginx
    fi
} # ----------  end of function reload_service  ----------}}}
### Main program
_check_permission || exit 1
while getopts "$OPTIONS" OPT; do
    case $OPT in
        h)
            long_help
            exit 0
            ;;
        v)
            show_version
            exit 0
           ;;
        ?)
            short_help
            exit 1
            ;;
    esac
done
case $PROGRAMNAME in
    $PROGENABLE)
        enable_site $* && reload_service
        ;;
    $PROGDISABLE)
        disable_site $* && reload_service
        ;;
    *)
        short_help
        exit 1
        ;;
esac