blob: b90d5d4c65458837a724cb19aaf44e818bedc4ba [file] [log] [blame]
Denis Egorenkod6dc1082017-01-25 19:29:03 +04001package com.mirantis.mcp
2
3def checkPortIsFree(String localPort) {
4 withEnv(["port=${localPort}"]) {
5 sh '''
6 if netstat -pnat | grep -qw "${port}"; then
7 echo "Port ${port} is already opened."
8 exit 1
9 fi
10 '''
11 }
12}
13
14def deployProxy(String confDir, String proxyPassURL, String localPort) {
15 withEnv(["localPort=${localPort}",
16 "proxyPassURL=${proxyPassURL}",
17 "confDir=${confDir}"]) {
18 sh '''\
19 cat <<EOF > "${confDir}/nginx-${localPort}"
20 worker_processes auto;
21 user jenkins;
22 pid ${confDir}/nginx-${localPort}.pid;
23 error_log /dev/null;
24 #
25 events {
26 worker_connections 768;
27 }
28 #
29 http {
30 sendfile on;
31 tcp_nopush on;
32 tcp_nodelay on;
33 keepalive_timeout 65;
34 types_hash_max_size 2048;
35 #
36 include /etc/nginx/mime.types;
37 default_type application/octet-stream;
38 #
39 access_log off;
40 #
41 server {
42 listen ${localPort} default_server;
43 #
44 server_name _;
45 client_max_body_size 0;
46 # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
47 chunked_transfer_encoding on;
48 #
49 location / {
50 proxy_set_header Host \\$http_host; # required for docker client's sake
51 proxy_set_header X-Real-IP \\$remote_addr; # pass on real client's IP
52 proxy_set_header X-Forwarded-For \\$proxy_add_x_forwarded_for;
53 proxy_set_header X-Forwarded-Proto \\$scheme;
54 proxy_read_timeout 900;
55 proxy_pass ${proxyPassURL} ;
56 }
57 }
58 }'''.stripIndent()
59 }
60 sh "nginx -t -c ${confDir}/nginx-${localPort}"
61 sh "nginx -c ${confDir}/nginx-${localPort}"
62 return "127.0.0.1:${localPort}"
63}
64
65def destroyProxy(String confDir, String localPort) {
66 def proxyPid = sh(script: "cat ${confDir}/nginx-${localPort}.pid",
67 returnStdout: true).trim()
68 if ( proxyPid ) {
Denis Egorenko6e3beae2017-02-02 15:30:26 +040069 sh "kill -QUIT ${proxyPid}"
Denis Egorenkod6dc1082017-01-25 19:29:03 +040070 }
71}