summaryrefslogtreecommitdiffstats
path: root/scripts/ci/openresty-setup.sh
blob: eb55d67e392351d1badc34fcedaddf5ecfb74f6d (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
#!/bin/sh -e
#
# ### This is a script to setup an openresty web server for testing rlm_smtp
#

#
# Declare the important path variables
#

# Base Directories
BASEDIR=$(git rev-parse --show-toplevel)
BUILDDIR="${BASEDIR}/build/ci/openresty"
CIDIR="${BASEDIR}/scripts/ci"

# Directories for openresty processes
ROOTDIR="${BUILDDIR}/html"
APIDIR="${BUILDDIR}/api"
LOGDIR="${BUILDDIR}/logs"
CERTDIR="${BUILDDIR}/certs"
CERTSRCDIR="${BASEDIR}/raddb/restcerts"
PASSWORD="whatever"

# Important files for running openresty
CONF="${BUILDDIR}/nginx.conf"

#
# Prepare the directories and files needed for running openresty
#

# Stop any currently running openresty instance
echo "Checking for a running openresty instance"
if [ -e "${LOGDIR}/nginx.pid" ]
then
	echo "Stopping the current openresty instance"
	kill "$(cat ${LOGDIR}/nginx.pid)"
	rm -r "${BUILDDIR}"
fi

# Create the directories
mkdir -p "${BUILDDIR}" "${ROOTDIR}" "${APIDIR}" "${LOGDIR}" "${CERTDIR}"

# Create the certificate
echo "Generating the certificates"
openssl pkcs8 -in ${CERTSRCDIR}/server.key -passin pass:${PASSWORD} -out ${CERTDIR}/server.key
cat ${CERTSRCDIR}/server.pem ${CERTSRCDIR}/ca.pem > ${CERTDIR}/server.pem

# Create nginx.conf file
echo "Generating the openresty configuration file"
touch "${CONF}"

# Build nginx.conf
echo "
#
worker_processes  1;
error_log  ${LOGDIR}/error.log;
pid        ${LOGDIR}/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /usr/local/openresty/nginx/conf/mime.types;
    default_type  text/plain;

    sendfile      on;

    server {
        listen       8080;
	server_name  localhost;

	location / {
	    root   ${ROOTDIR};
	    index  index.html;
	}

	location ~ ^/user(.*)$ {
	    default_type 'application/json';
	    add_header   'Content-Type' 'application/json';
	    content_by_lua_file  ${APIDIR}/json-api.lua;
	}

	location ~ ^/post(.*)$ {
	    content_by_lua_file  ${APIDIR}/post-api.lua;
	}

	location ~ ^/delay(.*)$ {
	    content_by_lua_file ${APIDIR}/delay-api.lua;
	}
    }

    server {
        listen       8443 ssl;
	server_name  localhost;

	ssl_certificate      ${CERTDIR}/server.pem;
	ssl_certificate_key  ${CERTDIR}/server.key;

	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;

	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;

	location / {
	    root   ${ROOTDIR};
	    index  index.html;
	}

        location ~ ^/user(.*)$ {
	    default_type 'application/json';
	    add_header   'Content-Type' 'application/json';
	    content_by_lua_file  ${APIDIR}/json-api.lua;
	}

	location ~ ^/post(.*)$ {
	    content_by_lua_file  ${APIDIR}/post-api.lua;
	}

	location ~ ^/auth(.*)$ {
	    content_by_lua_file   ${APIDIR}/auth-api.lua;
	    auth_basic            'Auth Area';
	    auth_basic_user_file  ${BUILDDIR}/.htpasswd;
	}
    }
}

" >"${CONF}"

echo "Copy lua scripts into place"
cp ${CIDIR}/openresty/*.lua "${APIDIR}"

echo "Copy sample data into place"
cp "${CIDIR}/openresty/test.txt" "${ROOTDIR}"

echo "Copy htpasswd into place"
cp "${CIDIR}/openresty/.htpasswd" "${BUILDDIR}"

#
# Run the openresty instance
#
echo "Starting openresty"
openresty -c ${CONF} -p ${BUILDDIR}
echo "Running openresty on port 8080 and 8443, accepting all local connections"