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

TOOL-1398 Add "npmjs.org" caching proxy to "ansible-package-caching-proxy" #5

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ a private PyPI index for uploading of custom Python packages
registry
* [nginx HTTP caching proxy](http://nginx.com/resources/admin-guide/caching/): A
general purpose HTTP caching proxy.
* [sinopia](https://github.com/rlidwka/sinopia): An https://www.npmjs.com
caching proxy.

#### Requirements & Dependencies
* Tested on Ansible 1.8
Expand Down Expand Up @@ -80,6 +82,11 @@ nginx_caching_proxy_server_proxy_cache_valid_codes: 200 301 302
nginx_caching_proxy_server_proxy_cache_valid_time: 3d
nginx_caching_proxy_server_vhost_name: "proxy proxy.yourdomain.local"

npmjs_server_enable: true
npmjs_server_dir: /opt/npmjs-server
npmjs_server_port: 4873
npmjs_server_vhost_name: "npm npm.yourdomain.local"

yum_repo_enable: true
yum_repo_dir: /opt/yum-repo
yum_repo_server_port: 80
Expand Down Expand Up @@ -133,6 +140,10 @@ curl -sD - http://localhost:1080/purge/releases.ubuntu.com/14.04/ubuntu-14.04-se
HTTP/1.1 200 OK
```

##### npmjs.org Caching Proxy

See implementation details at: https://hub.docker.com/r/rnbwd/sinopia/

##### RPM/YUM Repo Server

This role sets up a virtual host with directory indexing to serve a RPM/YUM
Expand Down
5 changes: 5 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ nginx_caching_proxy_server_proxy_cache_valid_codes: 200 301 302
nginx_caching_proxy_server_proxy_cache_valid_time: 3d
nginx_caching_proxy_server_vhost_name: "proxy proxy.yourdomain.local"

npmjs_server_enable: true
npmjs_server_dir: /opt/npmjs-server
npmjs_server_port: 4873
npmjs_server_vhost_name: "npm npm.yourdomain.local"

yum_repo_enable: true
yum_repo_autoindex: "on"
yum_repo_dir: /opt/yum-repo
Expand Down
3 changes: 3 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
- include: docker-registry.yml
when: docker_registry_enable

- include: npmjs-proxy.yml
when: npmjs_server_enable

- include: yum-repo.yml
when: yum_repo_enable
17 changes: 17 additions & 0 deletions tasks/npmjs-proxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2015 by Delphix. All rights reserved.
#

---

- name: npmjs-proxy | Create {{ npmjs_server_dir }}
file:
path="{{ npmjs_server_dir }}"
state=directory

- name: npmjs-proxy | Run the Docker container for the npmjs proxy
docker:
image=rnbwd/sinopia
name=sinopia
ports="{{ npmjs_server_port }}:4873"
volumes="{{ npmjs_server_dir }}:/sinopia/storage"
61 changes: 61 additions & 0 deletions test/integration/default/bats/npmjs-proxy.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bats
#
# Copyright (c) 2015 by Delphix. All rights reserved.
#

#
# Test the npmjs-proxy app, Sinopia.
#

# A node package to attempt to install through our proxy.
NODE_PACKAGE=statsd

setup() {
apt-get install -y curl npm
npm set registry http://localhost:4873/
}

@test "Accessing the npmjs vhost should return HTTP 200" {
run curl -sS -I -H 'Host: npm' http://localhost
[ "$status" -eq 0 ]
[[ "$output" =~ "HTTP/1.1 200 OK" ]]
}

@test "Accessing the npmjs-proxy vhost should reveal the back-end app's name" {
run curl -sS -I -H 'Host: npm' http://localhost
[ "$status" -eq 0 ]
[[ "$output" =~ "X-Powered-By: Sinopia" ]]
}

@test "Downloading & installing a Node package through our npmjs proxy should succeed" {
run npm install $NODE_PACKAGE
[ "$status" -eq 0 ]
echo $output
regex="npm http (200|304) http://localhost:4873/$NODE_PACKAGE"
[[ "$output" =~ $regex ]]
}

#
# Kill the Sinopia container and restart it with its networking broken by
# giving it bad DNS server settings. With npmjs.org unavailable but our
# cache already populated, the download of the previous Node package should
# still succeed.
#

@test "We should be able to install cached Node packages when the Sinopia container's networking backend is broken" {
docker kill sinopia
docker rm sinopia
docker run -d --name sinopia -p 4873:4873 --dns=1.1.1.1 -v /opt/npmjs-server:/sinopia/storage rnbwd/sinopia
# Give the app a few seconds to load
sleep 5

run npm install $NODE_PACKAGE
[ "$status" -eq 0 ]
regex="npm http (200|304) http://localhost:4873/$NODE_PACKAGE"
[[ "$output" =~ $regex ]]

# restart the container with networking un-broken
docker kill sinopia
docker rm sinopia
docker run -d --name sinopia -p 4873:4873 -v /opt/npmjs-server:/sinopia/storage rnbwd/sinopia
}
19 changes: 19 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ nginx_sites:
proxy_cache_purge STATIC $1;
}

npmjs-proxy:
- listen 80
- server_name {{ npmjs_server_vhost_name }}
- access_log "/var/log/nginx/npmjs-proxy-access.log"
- error_log "/var/log/nginx/npmjs-proxy-error.log"
- location / {
client_max_body_size 0;
proxy_pass_header Server;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Protocol http;
proxy_connect_timeout 10;
proxy_read_timeout 1200;
proxy_pass http://localhost:{{ npmjs_server_port }}/;
}

# The yum repo server is simply a vhost serving a directory. The yum repo's
# metadata is produced out-of-band of the web server which serves it out.
yum-repo:
Expand Down