nginxinator 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b829943382a3708dfe4e28c665d8fe93d65168cd
4
+ data.tar.gz: c055ef06028e3bf7bcdca0ccd2c77d7745816ef4
5
+ SHA512:
6
+ metadata.gz: 1ce2ff7f57db5da8c751ed6756ac3cab26598b5285763793e86d6d25139e73abb1bb637a6ee15e744a0a8d342fa16ae370b32d5ebaf3bd1d2303c5612773e3b9
7
+ data.tar.gz: 15184a3635cb2ffaf163002b9da4567ea6957f6e14fb139c77195c2f0f7fb12cfa4eb7bea967db1d266f2e011c2b9fa8a623906df4311add5df002f63ce4bd4f
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'sshkit'
3
+ require 'sshkit/dsl'
4
+
5
+ load 'nginxinator/nginx.rb'
6
+ load 'nginxinator/config.rb'
@@ -0,0 +1,30 @@
1
+ namespace :nginx do
2
+
3
+ task :ensure_setup do |t, args|
4
+ @settings = NginxInstance.new
5
+ # use 'rake pg:COMMAND debug=true' for debugging (you can also add --trace if you like)
6
+ SSHKit.config.output_verbosity = Logger::DEBUG if ENV['debug'] == "true"
7
+ end
8
+
9
+ desc 'Write example config files'
10
+ task :write_example_configs do
11
+ run_locally do
12
+ execute "mkdir -p templates/nginx/sites-enabled"
13
+ {
14
+ 'examples/Dockerfile' => 'Dockerfile_example',
15
+ 'examples/nginxinator_example.rb' => 'nginxinator_example.rb',
16
+ 'examples/nginx_example.conf.erb' => 'templates/nginx/nginx_example.conf.erb',
17
+ 'examples/site-enabled_example.erb' => 'templates/nginx/sites-enabled/client-app_example.erb',
18
+ 'examples/ssl.crt_example.erb' => 'templates/nginx/ssl.crt_example.erb',
19
+ 'examples/ssl.key_example.erb' => 'templates/nginx/ssl.key_example.erb',
20
+ 'examples/mime.types_example.erb' => 'templates/nginx/mime.types_example.erb'
21
+ }.each do |source, destination|
22
+ config = File.read(File.dirname(__FILE__) + "/#{source}")
23
+ File.open("./#{destination}", 'w') { |f| f.write(config) }
24
+ info "Wrote '#{destination}'"
25
+ end
26
+ info "Now remove the '_example' portion of their names or diff with existing files and add the needed lines."
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,10 @@
1
+ # vi: ft=config
2
+ FROM ubuntu:12.04
3
+ MAINTAINER david amick <docker@davidamick.com>
4
+
5
+ ENV DEBIAN_FRONTEND noninteractive
6
+
7
+ RUN /bin/bash -l -c "apt-get update -qq && apt-get install -qy nginx"
8
+
9
+ ENTRYPOINT ["/usr/sbin/nginx"]
10
+ CMD ["-c", "/etc/nginx/nginx.conf"]
@@ -0,0 +1,79 @@
1
+ types {
2
+ text/html html htm shtml;
3
+ text/css css;
4
+ text/xml xml rss;
5
+ image/gif gif;
6
+ image/jpeg jpeg jpg;
7
+ application/x-javascript js;
8
+ application/atom+xml atom;
9
+
10
+ text/mathml mml;
11
+ text/plain txt;
12
+ text/vnd.sun.j2me.app-descriptor jad;
13
+ text/vnd.wap.wml wml;
14
+ text/x-component htc;
15
+
16
+ image/png png;
17
+ image/tiff tif tiff;
18
+ image/vnd.wap.wbmp wbmp;
19
+ image/x-icon ico;
20
+ image/x-jng jng;
21
+ image/x-ms-bmp bmp;
22
+ image/svg+xml svg svgz;
23
+
24
+ application/java-archive jar war ear;
25
+ application/json json;
26
+ application/mac-binhex40 hqx;
27
+ application/msword doc;
28
+ application/pdf pdf;
29
+ application/postscript ps eps ai;
30
+ application/rtf rtf;
31
+ application/vnd.ms-excel xls;
32
+ application/vnd.ms-powerpoint ppt;
33
+ application/vnd.wap.wmlc wmlc;
34
+ application/vnd.google-earth.kml+xml kml;
35
+ application/vnd.google-earth.kmz kmz;
36
+ application/x-7z-compressed 7z;
37
+ application/x-cocoa cco;
38
+ application/x-java-archive-diff jardiff;
39
+ application/x-java-jnlp-file jnlp;
40
+ application/x-makeself run;
41
+ application/x-perl pl pm;
42
+ application/x-pilot prc pdb;
43
+ application/x-rar-compressed rar;
44
+ application/x-redhat-package-manager rpm;
45
+ application/x-sea sea;
46
+ application/x-shockwave-flash swf;
47
+ application/x-stuffit sit;
48
+ application/x-tcl tcl tk;
49
+ application/x-x509-ca-cert der pem crt;
50
+ application/x-xpinstall xpi;
51
+ application/xhtml+xml xhtml;
52
+ application/zip zip;
53
+
54
+ application/octet-stream bin exe dll;
55
+ application/octet-stream deb;
56
+ application/octet-stream dmg;
57
+ application/octet-stream eot;
58
+ application/octet-stream iso img;
59
+ application/octet-stream msi msp msm;
60
+ application/ogg ogx;
61
+
62
+ audio/midi mid midi kar;
63
+ audio/mpeg mpga mpega mp2 mp3 m4a;
64
+ audio/ogg oga ogg spx;
65
+ audio/x-realaudio ra;
66
+ audio/webm weba;
67
+
68
+ video/3gpp 3gpp 3gp;
69
+ video/mp4 mp4;
70
+ video/mpeg mpeg mpg mpe;
71
+ video/ogg ogv;
72
+ video/quicktime mov;
73
+ video/webm webm;
74
+ video/x-flv flv;
75
+ video/x-mng mng;
76
+ video/x-ms-asf asx asf;
77
+ video/x-ms-wmv wmv;
78
+ video/x-msvideo avi;
79
+ }
@@ -0,0 +1,39 @@
1
+ # deamon off is imporant to keep the docker container running
2
+ daemon off;
3
+
4
+ user www-data;
5
+ worker_processes 4;
6
+
7
+ error_log <%= @settings.internal_logs_path %>/error.log warn;
8
+ pid /var/run/nginx.pid;
9
+
10
+ events {
11
+ worker_connections 2048;
12
+ }
13
+
14
+ http {
15
+ include <%= @settings.internal_conf_path %>/mime.types;
16
+ default_type application/octet-stream;
17
+
18
+ access_log <%= @settings.internal_logs_path %>/access.log;
19
+
20
+ sendfile on;
21
+ tcp_nopush on;
22
+ tcp_nodelay on;
23
+
24
+ keepalive_timeout 65;
25
+
26
+ gzip on;
27
+ gzip_http_version 1.0;
28
+ gzip_comp_level 2;
29
+ gzip_proxied any;
30
+ gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
31
+
32
+ server_names_hash_bucket_size 64;
33
+ types_hash_max_size 2048;
34
+ types_hash_bucket_size 64;
35
+
36
+
37
+ include <%= @settings.internal_conf_path %>/conf.d/*.conf;
38
+ include <%= @settings.internal_sites_enabled_path %>/*;
39
+ }
@@ -0,0 +1,115 @@
1
+ class NginxInstance
2
+
3
+ ## For a standard Ubuntu 12.04 Nginx Docker image you should only
4
+ ## need to change the following values to get started:
5
+ def domain
6
+ "client.example.com"
7
+ end
8
+
9
+ def sites_enabled
10
+ ['client-app']
11
+ end
12
+
13
+ def publish_ports
14
+ [
15
+ {
16
+ "external" => "80",
17
+ "internal" => "80"
18
+ },
19
+ {
20
+ "external" => "443",
21
+ "internal" => "443"
22
+ }
23
+ ]
24
+ end
25
+
26
+ def image_name
27
+ "snarlysodboxer/nginx:0.0.0"
28
+ end
29
+
30
+ def external_data_path
31
+ "/var/www/current"
32
+ end
33
+
34
+ def external_logs_path
35
+ "/var/log/nginx"
36
+ end
37
+
38
+
39
+
40
+ ## The values below may be commonly changed to match specifics
41
+ ## relating to a particular Docker image or setup:
42
+ def config_files
43
+ ["nginx.conf", "ssl.crt", "ssl.key", "mime.types"]
44
+ end
45
+
46
+ def internal_data_path
47
+ "/var/www/current"
48
+ end
49
+
50
+ def internal_conf_path
51
+ "/etc/nginx"
52
+ end
53
+
54
+ def internal_sites_enabled_path
55
+ "/etc/nginx/sites-enabled"
56
+ end
57
+
58
+ def internal_logs_path
59
+ "/var/log/nginx"
60
+ end
61
+
62
+ def internal_sock_path
63
+ "/var/run/unicorn"
64
+ end
65
+
66
+ def ssh_user
67
+ ENV["USER"]
68
+ end
69
+
70
+
71
+
72
+ ## The values below are not meant to be changed and shouldn't
73
+ ## need to be under the majority of circumstances:
74
+
75
+ def external_conf_path
76
+ "/#{container_name}-conf"
77
+ end
78
+
79
+ def external_sites_enabled_path
80
+ "#{external_conf_path}/sites-enabled"
81
+ end
82
+
83
+ def external_sock_path
84
+ "#{external_conf_path}/run"
85
+ end
86
+
87
+ def container_name
88
+ "#{domain}-nginx-#{publish_ports.collect { |p| p['external'] }.join('-')}"
89
+ end
90
+
91
+ def docker_run_command
92
+ ports_options = []
93
+ publish_ports.each do |port_set|
94
+ ports_options += ["--publish", "0.0.0.0:#{port_set['external']}:#{port_set['internal']}"]
95
+ end
96
+ [ "--detach", "--tty",
97
+ "--name", container_name,
98
+ "--volume", "#{external_data_path}:#{internal_data_path}:rw",
99
+ "--volume", "#{external_conf_path}:#{internal_conf_path}:rw",
100
+ "--volume", "#{external_sock_path}:#{internal_sock_path}:rw",
101
+ "--volume", "#{external_logs_path}:#{internal_logs_path}:rw",
102
+ ports_options,
103
+ image_name
104
+ ].flatten
105
+ end
106
+
107
+ def local_templates_path
108
+ "templates/nginx"
109
+ end
110
+
111
+ def local_site_templates_path
112
+ "#{local_templates_path}/sites-enabled"
113
+ end
114
+
115
+ end
@@ -0,0 +1,65 @@
1
+ upstream unicorn {
2
+ server unix:<%= @settings.internal_sock_path %>/unicorn.socket fail_timeout=0;
3
+ }
4
+
5
+
6
+ server {
7
+ listen 80;
8
+ location / {
9
+ rewrite ^/(.*)$ https://<%= @settings.domain %>/$1 redirect;
10
+ }
11
+ }
12
+
13
+ server {
14
+ listen 443;
15
+ set $public_root <%= @settings.internal_data_path %>/public;
16
+ set $rails_root <%= @settings.internal_data_path %>;
17
+
18
+ root $public_root;
19
+
20
+ ssl on;
21
+ ssl_certificate <%= @settings.internal_conf_path %>/ssl.crt;
22
+ ssl_certificate_key <%= @settings.internal_conf_path %>/ssl.key;
23
+ ssl_ciphers HIGH;
24
+ ssl_protocols SSLv3 TLSv1;
25
+ ssl_prefer_server_ciphers on;
26
+
27
+ if (-f $document_root/system/maintenance.html) {
28
+ rewrite ^(.*)$ /system/maintenance.html last;
29
+ break;
30
+ }
31
+
32
+ location / {
33
+ client_max_body_size 4096M;
34
+ proxy_read_timeout 900;
35
+ proxy_set_header X-Real-IP $remote_addr;
36
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
37
+ proxy_set_header X-Forwarded-Proto https;
38
+ proxy_set_header Host $http_host;
39
+ proxy_redirect off;
40
+ proxy_max_temp_file_size 0;
41
+ proxy_set_header X-Sendfile-Type X-Accel-Redirect;
42
+ proxy_set_header X-Accel-Remote-Mapping webdav_redirect;
43
+ proxy_set_header X-Accel-Mapping $rails_root=$rails_root;
44
+
45
+ if (-f $request_filename) {
46
+ expires max;
47
+ break;
48
+ }
49
+
50
+ if (!-f $request_filename) {
51
+ proxy_pass http://unicorn;
52
+ break;
53
+ }
54
+ }
55
+
56
+ location ~ /files/(.*) {
57
+ alias $rails_root/$1;
58
+ internal;
59
+ }
60
+
61
+ error_page 500 502 503 504 /500.html;
62
+ location = /500.html {
63
+ root $public_root;
64
+ }
65
+ }
@@ -0,0 +1 @@
1
+ # Remove this line and add your cert here
@@ -0,0 +1 @@
1
+ # Remove this line and add your key here
@@ -0,0 +1,185 @@
1
+ require 'erb'
2
+
3
+ require './nginxinator.rb' if File.exists?('./nginxinator.rb')
4
+
5
+ ## NOTES:
6
+ # tasks without 'desc' description lines are for manual debugging of this
7
+ # deployment code.
8
+ #
9
+ # we've choosen to only pass strings (if anything) to tasks. this allows tasks to be
10
+ # debugged individually. only private methods take ruby objects.
11
+
12
+ namespace :nginx do
13
+
14
+ desc "Idempotently setup one or more Nginx instances using values in ./nginxinator.rb"
15
+ task :setup => :ensure_setup do
16
+ # instance variables are lost inside SSHKit's 'on' block, so
17
+ # at the beginning of each task we assign 'settings' to @settings.
18
+ settings = @settings
19
+ Rake::Task['nginx:ensure_access_docker'].invoke
20
+ Rake::Task['nginx:open_firewall'].invoke
21
+ # 'on', 'run_locally', 'as', 'execute', 'info', 'warn', and 'fatal' are from SSHKit
22
+ on "#{settings.ssh_user}@#{settings.domain}" do
23
+ config_file_changed = false
24
+ settings.config_files.each do |config_file|
25
+ if config_file_differs?(settings, settings.local_templates_path, settings.external_conf_path, config_file)
26
+ warn "Config file #{config_file} on #{settings.domain} is being updated."
27
+ Rake::Task['nginx:install_config_file'].invoke(settings.local_templates_path, settings.external_conf_path, config_file)
28
+ Rake::Task['nginx:install_config_file'].reenable
29
+ config_file_changed = true
30
+ end
31
+ end
32
+ settings.sites_enabled.each do |config_file|
33
+ if config_file_differs?(settings, settings.local_site_templates_path, settings.external_sites_enabled_path, config_file)
34
+ warn "Config file #{config_file} on #{settings.domain} is being updated."
35
+ Rake::Task['nginx:install_config_file'].invoke(settings.local_site_templates_path, settings.external_sites_enabled_path, config_file)
36
+ Rake::Task['nginx:install_config_file'].reenable
37
+ config_file_changed = true
38
+ end
39
+ end
40
+ unless container_exists?(settings.container_name)
41
+ Rake::Task['nginx:create_container'].invoke
42
+ else
43
+ unless container_is_running?(settings.container_name)
44
+ Rake::Task['nginx:start_container'].invoke
45
+ else
46
+ if config_file_changed
47
+ Rake::Task['nginx:restart_container'].invoke
48
+ else
49
+ info "No config file changes for #{settings.container_name} and it is already running; we're setup!"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ desc "Check the status of the Nginx instance."
57
+ task :status => :ensure_setup do |t, args|
58
+ settings = @settings
59
+ on "#{settings.ssh_user}@#{settings.domain}" do
60
+ info ""
61
+ if container_exists?(settings.container_name)
62
+ info "#{settings.container_name} exists on #{settings.domain}"
63
+ info ""
64
+ if container_is_running?(settings.container_name)
65
+ info "#{settings.container_name} is running on #{settings.domain}"
66
+ info ""
67
+ else
68
+ info "#{settings.container_name} is not running on #{settings.domain}"
69
+ info ""
70
+ end
71
+ else
72
+ info "#{settings.container_name} does not exist on #{settings.domain}"
73
+ info ""
74
+ end
75
+ end
76
+ end
77
+
78
+ task :create_container => :ensure_setup do |t, args|
79
+ settings = @settings
80
+ on "#{settings.ssh_user}@#{settings.domain}" do
81
+ warn "Starting a new container named #{settings.container_name} on #{settings.domain}"
82
+ execute("docker", "run", settings.docker_run_command)
83
+ sleep 2
84
+ fatal stay_running_message(settings) and raise unless container_is_running?(settings.container_name)
85
+ end
86
+ end
87
+
88
+ task :start_container => :ensure_setup do |t, args|
89
+ settings = @settings
90
+ on "#{settings.ssh_user}@#{settings.domain}" do
91
+ warn "Starting an existing but non-running container named #{settings.container_name}"
92
+ execute("docker", "start", settings.container_name)
93
+ sleep 2
94
+ fatal stay_running_message(settings) and raise unless container_is_running?(settings.container_name)
95
+ end
96
+ end
97
+
98
+ task :restart_container => :ensure_setup do |t, args|
99
+ settings = @settings
100
+ on "#{settings.ssh_user}@#{settings.domain}" do
101
+ warn "Restarting a running container named #{settings.container_name}"
102
+ execute("docker", "restart", settings.container_name)
103
+ sleep 2
104
+ fatal stay_running_message(settings) and raise unless container_is_running?(settings.container_name)
105
+ end
106
+ end
107
+
108
+ task :ensure_access_docker => :ensure_setup do |t, args|
109
+ settings = @settings
110
+ on "#{settings.ssh_user}@#{settings.domain}" do
111
+ as settings.ssh_user do
112
+ unless test("bash", "-c", "\"docker", "ps", "&>", "/dev/null\"")
113
+ execute("sudo", "usermod", "-a", "-G", "docker", settings.ssh_user)
114
+ fatal "Newly added to docker group, this run will fail, next run will succeed. Simply try again."
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ task :install_config_file, [:template_path, :config_path, :config_file] => :ensure_setup do |t, args|
121
+ settings = @settings
122
+ on "#{settings.ssh_user}@#{settings.domain}" do
123
+ as 'root' do
124
+ execute("mkdir", "-p", args.config_path) unless test("test", "-d", args.config_path)
125
+ generated_config_file = generate_config_file(settings, "#{args.template_path}/#{args.config_file}.erb")
126
+ upload! StringIO.new(generated_config_file), "/tmp/#{args.config_file}"
127
+ execute("mv", "/tmp/#{args.config_file}", "#{args.config_path}/#{args.config_file}")
128
+ execute("chown", "-R", "root:root", args.config_path)
129
+ execute("chmod", "-R", "700", args.config_path)
130
+ end
131
+ end
132
+ end
133
+
134
+ task :open_firewall => :ensure_setup do |t, args|
135
+ settings = @settings
136
+ on "#{settings.ssh_user}@#{settings.domain}" do
137
+ as "root" do
138
+ if test "ufw", "status"
139
+ settings.publish_ports.collect { |port_set| port_set['external'] }.each do |port|
140
+ raise "Error during opening UFW firewall" unless test("ufw", "allow", "#{port}/tcp")
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+
147
+ private
148
+
149
+ def stay_running_message(settings)
150
+ "Container #{settings.container_name} on #{settings.domain} did not stay running more than 2 seconds"
151
+ end
152
+
153
+ def config_file_differs?(settings, local_templates_path, external_config_path, config_file)
154
+ generated_config_file = generate_config_file(settings, "#{local_templates_path}/#{config_file}.erb")
155
+ as 'root' do
156
+ config_file_path = "#{external_config_path}/#{config_file}"
157
+ if file_exists?(config_file_path)
158
+ capture("cat", config_file_path).chomp != generated_config_file.chomp
159
+ else
160
+ true
161
+ end
162
+ end
163
+ end
164
+
165
+ def generate_config_file(settings, template_file_path)
166
+ @settings = settings # needed for ERB
167
+ template_path = File.expand_path(template_file_path)
168
+ ERB.new(File.new(template_path).read).result(binding)
169
+ end
170
+
171
+ def container_exists?(container_name)
172
+ test "docker", "inspect", container_name, ">", "/dev/null"
173
+ end
174
+
175
+ def container_is_running?(container_name)
176
+ (capture "docker", "inspect",
177
+ "--format='{{.State.Running}}'",
178
+ container_name).strip == "true"
179
+ end
180
+
181
+ def file_exists?(file_name_path)
182
+ test "[", "-f", file_name_path, "]"
183
+ end
184
+
185
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nginxinator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - david amick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 10.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 10.3.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: sshkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.1
41
+ description: An Opinionated Nginx Deployment gem
42
+ email: davidamick@ctisolutionsinc.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/nginxinator.rb
48
+ - lib/nginxinator/nginx.rb
49
+ - lib/nginxinator/config.rb
50
+ - lib/nginxinator/examples/nginxinator_example.rb
51
+ - lib/nginxinator/examples/nginx_example.conf.erb
52
+ - lib/nginxinator/examples/site-enabled_example.erb
53
+ - lib/nginxinator/examples/ssl.crt_example.erb
54
+ - lib/nginxinator/examples/ssl.key_example.erb
55
+ - lib/nginxinator/examples/mime.types_example.erb
56
+ - lib/nginxinator/examples/Dockerfile
57
+ homepage: https://github.com/snarlysodboxer/nginxinator
58
+ licenses:
59
+ - GNU
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.9.3
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Deploy Nginx
81
+ test_files: []