nginx.plugin.zsh 4.59 KB
Newer Older
mikka's avatar
mikka committed
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
: ${NGINX_DIR:=/etc/nginx}
: ${NGINX_VHOST_TEMPLATE:=$ZSH/plugins/nginx/templates/symfony2}

if [ $use_sudo -eq 1 ]; then
    sudo="sudo"
else
    sudo=""
fi

# nginx basic completition

_nginx_get_en_command_list () {
    ls -a $NGINX_DIR/sites-available | awk '/^[a-z][a-z.-]+$/ { print $1 }'
}

_nginx_get_dis_command_list () {
    ls -a $NGINX_DIR/sites-enabled | awk '/^[a-z][a-z.-]+$/ { print $1 }'
}

_nginx_en () {
   compadd `_nginx_get_en_command_list`
}

_nginx_dis () {
   compadd `_nginx_get_dis_command_list`
}

# Enabling a site
en () {
    if [ ! $1 ]; then
        echo "\033[337;41m\nThe name of the vhost is required!\n\033[0m";
        return
    fi
    
    if [ ! -e $NGINX_DIR/sites-available/$1 ]; then
        echo "\033[31m$1\033[0m doesn't exist";
        return
    fi

    if [ ! -e $NGINX_DIR/sites-enabled/$1 ]; then
  	    $sudo ln -s $NGINX_DIR/sites-available/$1 $NGINX_DIR/sites-enabled/$1;
	    if [ -e /etc/nginx/sites-enabled/$1 ]; then
        	echo "\033[32m$1\033[0m successfully enabled";
        else
            echo "An error occured during the enabling of \033[31m$1\033[0m";
        fi
    else
        echo "\033[31m$1\033[0m is already enabled";
    fi
}
compdef _nginx_en en

# Disabling a site
dis () {
    if [ ! $1 ]; then
        echo "\033[337;41m\nThe name of the vhost is required!\n\033[0m";
        return
    fi

    if [ ! -e $NGINX_DIR/sites-enabled/$1 ]; then
        echo "\033[31m$1\033[0m doesn't exist";
    else
	    $sudo rm -f $NGINX_DIR/sites-enabled/$1;
	    if [ ! -e $NGINX_DIR/sites-enabled/$1 ]; then
        	echo "\033[32m$1\033[0m successfully disabled";
        else
            echo "An error occured during the disabling of \033[31m$1\033[0m";
        fi
    fi
}
compdef _nginx_dis dis

# Completition of vhost
_nginx_get_possible_vhost_list () {
    ls -a $HOME/www | awk '/^[^.][a-z0-9._]+$/ { print $1 }'
}

_nginx_vhost () {
   compadd `_nginx_get_possible_vhost_list`
}

# Parsing arguments
vhost () {
84
85
86
87
88
    local user=$USER;
    local template=$NGINX_VHOST_TEMPLATE
    local tpl="non_existing_template"
    local enable=1
    local write_hosts=0
mikka's avatar
mikka committed
89
90
91
92
93

    while getopts ":lu:t:nwh" option
    do
      case $option in
        l ) ls $NGINX_DIR/sites-enabled; return ;;
94
95
96
97
        u ) user=$OPTARG ;;
        t ) tpl=$OPTARG ;;
        n ) enable=0 ;;
        w ) write_hosts=1 ;;
mikka's avatar
mikka committed
98
99
100
101
        h ) _vhost_usage; return ;;
      esac
    done
    
102
103
104
105
    shift $[ $OPTIND - 1 ]
    local vhost=$1
  
    if [ -z "$vhost" ]; then
mikka's avatar
mikka committed
106
107
108
109
110
        echo "\033[337;41m\nThe name of the vhost is required!\n\033[0m"
        return
    fi
    
    if [ -e $ZSH/plugins/nginx/templates/$tpl ]; then
111
        local template=$ZSH/plugins/nginx/templates/$tpl
mikka's avatar
mikka committed
112
    elif [ -e $tpl ]; then
113
        local template=$tpl
mikka's avatar
mikka committed
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
    fi
        
    _vhost_generate $vhost $user
    
    if [ $enable -eq 1 ]; then
        en $vhost
    fi
    
    if [ $write_hosts -eq 1 ]; then
        _write_hosts $vhost
    fi
}
compdef _nginx_vhost vhost

_vhost_usage () {
    echo "Usage: vhost [options] [vhost_name]"
    echo
    echo "Options"
    echo "  -l   Lists enabled vhosts"
    echo "  -u   Sets the user - defaults to the current user ($USER)"
    echo "  -t   Sets the template"
    echo "  -n   Does not enable the generated vhost"
    echo "  -w   Write the vhost to the /etc/hosts file pointing to 127.0.0.1 (writes it at the end of the first line actually)"
    echo "  -h   Get this help message"
    return
}

# Generate config file
_vhost_generate () {
143
    local user=$(cat /etc/passwd | grep $2 | awk -F : '{print $1 }')
mikka's avatar
mikka committed
144
145
146
147
148
149
150
151
    
    if [ ! $user ]; then
      echo "User \033[31m$2\033[0m doesn't have an account on \033[33m$HOST\033[0m"
      return
    fi

    echo "Generating \033[32m$1\033[0m vhost for \033[33m$user\033[0m user"
        
152
153
    local user_id=$(cat /etc/passwd | grep $2 | awk -F : '{print $3 }')
    local pool_port=1$user_id
mikka's avatar
mikka committed
154
    
155
    local conf=$(sed -e 's/{vhost}/'$1'/g' -e 's/{user}/'$user'/g' -e 's/{pool_port}/'$pool_port'/g' $template )
mikka's avatar
mikka committed
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
    
    echo $conf > $1.tmp
    $sudo mv $1.tmp $NGINX_DIR/sites-available/$1
    
    if [ -e $NGINX_DIR/sites-available/$1 ]; then
        echo "\033[32m$1\033[0m vhost has been successfully created"
    else
        echo "An error occured during the creating of \033[31m$1\033[0m vhost"
    fi
}

# Write the /etc/hosts file
_write_hosts () {
    temp=$HOME/hosts.temp
    exec < /etc/hosts
	while read line
	do
		if [ -e $temp ]; then
			echo "$line" >> $temp;
		else
			echo "$line $1" > $temp;		
		fi
	done
	
	$sudo mv $temp /etc/hosts;
	
182
	echo "\033[32m$1\033[0m vhost has been successfully written in /etc/hosts"
mikka's avatar
mikka committed
183
184
185
186
}

alias ngt="$sudo nginx -t"
alias ngr="$sudo service nginx restart"