Skip to content
This repository was archived by the owner on Apr 14, 2018. It is now read-only.

Commit 3947def

Browse files
committed
Merge pull request #5 from delphix/add-npmjs-proxy-pr
TOOL-1398 Add "npmjs.org" caching proxy to "ansible-package-caching-proxy"
2 parents 143dbbe + 366918e commit 3947def

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ a private PyPI index for uploading of custom Python packages
3636
registry
3737
* [nginx HTTP caching proxy](http://nginx.com/resources/admin-guide/caching/): A
3838
general purpose HTTP caching proxy.
39+
* [sinopia](https://github.com/rlidwka/sinopia): An https://www.npmjs.com
40+
caching proxy.
3941

4042
#### Requirements & Dependencies
4143
* Tested on Ansible 1.8
@@ -80,6 +82,11 @@ nginx_caching_proxy_server_proxy_cache_valid_codes: 200 301 302
8082
nginx_caching_proxy_server_proxy_cache_valid_time: 3d
8183
nginx_caching_proxy_server_vhost_name: "proxy proxy.yourdomain.local"
8284

85+
npmjs_server_enable: true
86+
npmjs_server_dir: /opt/npmjs-server
87+
npmjs_server_port: 4873
88+
npmjs_server_vhost_name: "npm npm.yourdomain.local"
89+
8390
yum_repo_enable: true
8491
yum_repo_dir: /opt/yum-repo
8592
yum_repo_server_port: 80
@@ -133,6 +140,10 @@ curl -sD - http://localhost:1080/purge/releases.ubuntu.com/14.04/ubuntu-14.04-se
133140
HTTP/1.1 200 OK
134141
```
135142
143+
##### npmjs.org Caching Proxy
144+
145+
See implementation details at: https://hub.docker.com/r/rnbwd/sinopia/
146+
136147
##### RPM/YUM Repo Server
137148
138149
This role sets up a virtual host with directory indexing to serve a RPM/YUM

defaults/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ nginx_caching_proxy_server_proxy_cache_valid_codes: 200 301 302
3030
nginx_caching_proxy_server_proxy_cache_valid_time: 3d
3131
nginx_caching_proxy_server_vhost_name: "proxy proxy.yourdomain.local"
3232

33+
npmjs_server_enable: true
34+
npmjs_server_dir: /opt/npmjs-server
35+
npmjs_server_port: 4873
36+
npmjs_server_vhost_name: "npm npm.yourdomain.local"
37+
3338
yum_repo_enable: true
3439
yum_repo_autoindex: "on"
3540
yum_repo_dir: /opt/yum-repo

tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
- include: docker-registry.yml
1414
when: docker_registry_enable
1515

16+
- include: npmjs-proxy.yml
17+
when: npmjs_server_enable
18+
1619
- include: yum-repo.yml
1720
when: yum_repo_enable

tasks/npmjs-proxy.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright (c) 2015 by Delphix. All rights reserved.
3+
#
4+
5+
---
6+
7+
- name: npmjs-proxy | Create {{ npmjs_server_dir }}
8+
file:
9+
path="{{ npmjs_server_dir }}"
10+
state=directory
11+
12+
- name: npmjs-proxy | Run the Docker container for the npmjs proxy
13+
docker:
14+
image=rnbwd/sinopia
15+
name=sinopia
16+
ports="{{ npmjs_server_port }}:4873"
17+
volumes="{{ npmjs_server_dir }}:/sinopia/storage"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bats
2+
#
3+
# Copyright (c) 2015 by Delphix. All rights reserved.
4+
#
5+
6+
#
7+
# Test the npmjs-proxy app, Sinopia.
8+
#
9+
10+
# A node package to attempt to install through our proxy.
11+
NODE_PACKAGE=statsd
12+
13+
setup() {
14+
apt-get install -y curl npm
15+
npm set registry http://localhost:4873/
16+
}
17+
18+
@test "Accessing the npmjs vhost should return HTTP 200" {
19+
run curl -sS -I -H 'Host: npm' http://localhost
20+
[ "$status" -eq 0 ]
21+
[[ "$output" =~ "HTTP/1.1 200 OK" ]]
22+
}
23+
24+
@test "Accessing the npmjs-proxy vhost should reveal the back-end app's name" {
25+
run curl -sS -I -H 'Host: npm' http://localhost
26+
[ "$status" -eq 0 ]
27+
[[ "$output" =~ "X-Powered-By: Sinopia" ]]
28+
}
29+
30+
@test "Downloading & installing a Node package through our npmjs proxy should succeed" {
31+
run npm install $NODE_PACKAGE
32+
[ "$status" -eq 0 ]
33+
echo $output
34+
regex="npm http (200|304) http://localhost:4873/$NODE_PACKAGE"
35+
[[ "$output" =~ $regex ]]
36+
}
37+
38+
#
39+
# Kill the Sinopia container and restart it with its networking broken by
40+
# giving it bad DNS server settings. With npmjs.org unavailable but our
41+
# cache already populated, the download of the previous Node package should
42+
# still succeed.
43+
#
44+
45+
@test "We should be able to install cached Node packages when the Sinopia container's networking backend is broken" {
46+
docker kill sinopia
47+
docker rm sinopia
48+
docker run -d --name sinopia -p 4873:4873 --dns=1.1.1.1 -v /opt/npmjs-server:/sinopia/storage rnbwd/sinopia
49+
# Give the app a few seconds to load
50+
sleep 5
51+
52+
run npm install $NODE_PACKAGE
53+
[ "$status" -eq 0 ]
54+
regex="npm http (200|304) http://localhost:4873/$NODE_PACKAGE"
55+
[[ "$output" =~ $regex ]]
56+
57+
# restart the container with networking un-broken
58+
docker kill sinopia
59+
docker rm sinopia
60+
docker run -d --name sinopia -p 4873:4873 -v /opt/npmjs-server:/sinopia/storage rnbwd/sinopia
61+
}

vars/main.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ nginx_sites:
164164
proxy_cache_purge STATIC $1;
165165
}
166166

167+
npmjs-proxy:
168+
- listen 80
169+
- server_name {{ npmjs_server_vhost_name }}
170+
- access_log "/var/log/nginx/npmjs-proxy-access.log"
171+
- error_log "/var/log/nginx/npmjs-proxy-error.log"
172+
- location / {
173+
client_max_body_size 0;
174+
proxy_pass_header Server;
175+
proxy_redirect off;
176+
proxy_set_header Host $http_host;
177+
proxy_set_header X-Real-IP $remote_addr;
178+
proxy_set_header X-Scheme $scheme;
179+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
180+
proxy_set_header X-Forwarded-Protocol http;
181+
proxy_connect_timeout 10;
182+
proxy_read_timeout 1200;
183+
proxy_pass http://localhost:{{ npmjs_server_port }}/;
184+
}
185+
167186
# The yum repo server is simply a vhost serving a directory. The yum repo's
168187
# metadata is produced out-of-band of the web server which serves it out.
169188
yum-repo:

0 commit comments

Comments
 (0)