nginxinator 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +1,21 @@
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
1
+ namespace :nginxinator do
8
2
 
9
3
  desc 'Write example config files'
10
4
  task :write_example_configs do
11
5
  run_locally do
12
- execute "mkdir -p templates/nginx/sites-enabled"
6
+ execute "mkdir -p config/deploy templates/nginx/sites-enabled"
13
7
  {
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'
8
+ 'examples/Capfile' => 'Capfile_example',
9
+ 'examples/config/deploy.rb' => 'config/deploy_example.rb',
10
+ 'examples/config/deploy_nginxinator.rb' => 'config/deploy_nginxinator_example.rb',
11
+ 'examples/config/deploy/staging.rb' => 'config/deploy/staging_example.rb',
12
+ 'examples/config/deploy/staging_nginxinator.rb' => 'config/deploy/staging_nginxinator_example.rb',
13
+ 'examples/Dockerfile' => 'templates/nginx/Dockerfile_example',
14
+ 'examples/nginx.conf.erb' => 'templates/nginx/nginx_example.conf.erb',
15
+ 'examples/site-enabled.erb' => 'templates/nginx/sites-enabled/client-app_example.erb',
16
+ 'examples/ssl.crt.erb' => 'templates/nginx/ssl.crt_example.erb',
17
+ 'examples/ssl.key.erb' => 'templates/nginx/ssl.key_example.erb',
18
+ 'examples/mime.types.erb' => 'templates/nginx/mime.types_example.erb'
21
19
  }.each do |source, destination|
22
20
  config = File.read(File.dirname(__FILE__) + "/#{source}")
23
21
  File.open("./#{destination}", 'w') { |f| f.write(config) }
@@ -0,0 +1,27 @@
1
+ # Load DSL and Setup Up Stages
2
+ require 'capistrano/setup'
3
+
4
+ # Includes default deployment tasks
5
+ require 'capistrano/deploy'
6
+
7
+ # Includes tasks from other gems included in your Gemfile
8
+ #
9
+ # For documentation on these, see for example:
10
+ #
11
+ # https://github.com/capistrano/rvm
12
+ # https://github.com/capistrano/rbenv
13
+ # https://github.com/capistrano/chruby
14
+ # https://github.com/capistrano/bundler
15
+ # https://github.com/capistrano/rails
16
+ #
17
+ # require 'capistrano/rvm'
18
+ # require 'capistrano/rbenv'
19
+ # require 'capistrano/chruby'
20
+ # require 'capistrano/bundler'
21
+ # require 'capistrano/rails/assets'
22
+ # require 'capistrano/rails/migrations'
23
+
24
+ require 'nginxinator'
25
+
26
+ # Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
27
+ Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
@@ -0,0 +1,50 @@
1
+ # Simple Role Syntax
2
+ # ==================
3
+ # Supports bulk-adding hosts to roles, the primary server in each group
4
+ # is considered to be the first unless any hosts have the primary
5
+ # property set. Don't declare `role :all`, it's a meta role.
6
+
7
+ set :domain, "my-app.example.com"
8
+ set :deploy_username, "deployer"
9
+ set :user_host, "#{fetch(:deploy_username)}@#{fetch(:domain)}"
10
+
11
+ role :app, fetch(:user_host)
12
+ role :web, fetch(:user_host)
13
+ role :db, fetch(:user_host)
14
+
15
+ # Extended Server Syntax
16
+ # ======================
17
+ # This can be used to drop a more detailed server definition into the
18
+ # server list. The second argument is a, or duck-types, Hash and is
19
+ # used to set extended properties on the server.
20
+
21
+ #server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value
22
+
23
+
24
+ # Custom SSH Options
25
+ # ==================
26
+ # You may pass any option but keep in mind that net/ssh understands a
27
+ # limited set of options, consult[net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start).
28
+ #
29
+ # Global options
30
+ # --------------
31
+ # set :ssh_options, {
32
+ # keys: %w(/home/rlisowski/.ssh/id_rsa),
33
+ # forward_agent: false,
34
+ # auth_methods: %w(password)
35
+ # }
36
+ #
37
+ # And/or per server (overrides global)
38
+ # ------------------------------------
39
+ # server 'example.com',
40
+ # user: 'user_name',
41
+ # roles: %w{web app},
42
+ # ssh_options: {
43
+ # user: 'user_name', # overrides user setting above
44
+ # keys: %w(/home/user_name/.ssh/id_rsa),
45
+ # forward_agent: false,
46
+ # auth_methods: %w(publickey password)
47
+ # # password: 'please use keys'
48
+ # }
49
+
50
+ require './config/deploy/staging_nginxinator.rb'
@@ -0,0 +1,58 @@
1
+ ## For a standard Ubuntu 12.04 Nginx Docker image you should only
2
+ ## need to change the following values to get started:
3
+ set :domain, "client.example.com"
4
+ set :sites_enabled, ['client-app']
5
+ set :publish_ports, [
6
+ {
7
+ "external" => "80",
8
+ "internal" => "80"
9
+ },
10
+ {
11
+ "external" => "443",
12
+ "internal" => "443"
13
+ }
14
+ ]
15
+ set :image_name, "snarlysodboxer/nginx:0.0.0"
16
+ set :external_data_path, "/var/www/client-app/current"
17
+ set :external_logs_path, "/var/www/client-app/shared/log/nginx"
18
+
19
+
20
+
21
+ ## The values below may be commonly changed to match specifics
22
+ ## relating to a particular Docker image or setup:
23
+ set :config_files, ["nginx.conf", "ssl.crt", "ssl.key", "mime.types"]
24
+ set :internal_data_path, -> { fetch(:external_data_path) }
25
+ set :internal_conf_path, "/etc/nginx"
26
+ set :internal_sites_enabled_path, "/etc/nginx/sites-enabled"
27
+ set :internal_logs_path, "/var/log/nginx"
28
+ set :internal_sock_path, "/var/run/unicorn"
29
+ set :ssh_user, ENV["USER"]
30
+
31
+
32
+
33
+ ## The values below are not meant to be changed and shouldn't
34
+ ## need to be under the majority of circumstances:
35
+ set :nginx_container_name, "#{fetch(:domain)}-nginx-#{fetch(:publish_ports).collect { |p| p['external'] }.join('-')}"
36
+ set :external_conf_path, "/#{fetch(:nginx_container_name)}-conf"
37
+ set :external_sites_enabled_path, "#{fetch(:external_conf_path)}/sites-enabled"
38
+ set :external_sock_path, "#{fetch(:external_conf_path)}/run"
39
+ set :ports_options, -> {
40
+ options = []
41
+ fetch(:publish_ports).each do |port_set|
42
+ options += ["--publish", "0.0.0.0:#{port_set['external']}:#{port_set['internal']}"]
43
+ end
44
+ options
45
+ }
46
+ set :docker_run_command, -> { [
47
+ "--detach", "--tty",
48
+ "--name", fetch(:nginx_container_name),
49
+ "--volume", "#{fetch(:external_data_path)}:#{fetch(:internal_data_path)}:rw",
50
+ "--volume", "#{fetch(:external_conf_path)}:#{fetch(:internal_conf_path)}:rw",
51
+ "--volume", "#{fetch(:external_sock_path)}:#{fetch(:internal_sock_path)}:rw",
52
+ "--volume", "#{fetch(:external_logs_path)}:#{fetch(:internal_logs_path)}:rw",
53
+ "--restart", "always",
54
+ fetch(:ports_options),
55
+ fetch(:image_name)
56
+ ].flatten }
57
+ set :local_templates_path, "templates/nginx"
58
+ set :local_site_templates_path, -> { "#{fetch(:local_templates_path)}/sites-enabled" }
@@ -0,0 +1,60 @@
1
+ # config valid only for Capistrano 3.1
2
+ lock '3.2.1'
3
+
4
+ set :application, 'my_app_name'
5
+ set :repo_url, 'git@example.com:me/my_repo.git'
6
+
7
+ # Default branch is :master
8
+ # ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
9
+
10
+ # Default deploy_to directory is /var/www/my_app
11
+ # set :deploy_to, '/var/www/my_app'
12
+
13
+ # Default value for :scm is :git
14
+ # set :scm, :git
15
+
16
+ # Default value for :format is :pretty
17
+ # set :format, :pretty
18
+
19
+ # Default value for :log_level is :debug
20
+ # set :log_level, :debug
21
+
22
+ # Default value for :pty is false
23
+ # set :pty, true
24
+
25
+ # Default value for :linked_files is []
26
+ # set :linked_files, %w{config/database.yml}
27
+
28
+ # Default value for linked_dirs is []
29
+ # set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
30
+
31
+ # Default value for default_env is {}
32
+ # set :default_env, { path: "/opt/ruby/bin:$PATH" }
33
+
34
+ # Default value for keep_releases is 5
35
+ # set :keep_releases, 5
36
+
37
+ namespace :deploy do
38
+
39
+ desc 'Restart application'
40
+ task :restart do
41
+ on roles(:app), in: :sequence, wait: 5 do
42
+ # Your restart mechanism here, for example:
43
+ # execute :touch, release_path.join('tmp/restart.txt')
44
+ end
45
+ end
46
+
47
+ after :publishing, :restart
48
+
49
+ after :restart, :clear_cache do
50
+ on roles(:web), in: :groups, limit: 3, wait: 10 do
51
+ # Here we can do anything such as:
52
+ # within release_path do
53
+ # execute :rake, 'cache:clear'
54
+ # end
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ require './config/deploy_nginxinator.rb'
@@ -0,0 +1 @@
1
+ # Only stage-specific nginxinator config options are needed.
@@ -4,7 +4,7 @@ daemon off;
4
4
  user www-data;
5
5
  worker_processes 4;
6
6
 
7
- error_log <%= @settings.internal_logs_path %>/error.log warn;
7
+ error_log <%= @internal_logs_path %>/error.log warn;
8
8
  pid /var/run/nginx.pid;
9
9
 
10
10
  events {
@@ -12,10 +12,10 @@ events {
12
12
  }
13
13
 
14
14
  http {
15
- include <%= @settings.internal_conf_path %>/mime.types;
15
+ include <%= @internal_conf_path %>/mime.types;
16
16
  default_type application/octet-stream;
17
17
 
18
- access_log <%= @settings.internal_logs_path %>/access.log;
18
+ access_log <%= @internal_logs_path %>/access.log;
19
19
 
20
20
  sendfile on;
21
21
  tcp_nopush on;
@@ -34,6 +34,6 @@ http {
34
34
  types_hash_bucket_size 64;
35
35
 
36
36
 
37
- include <%= @settings.internal_conf_path %>/conf.d/*.conf;
38
- include <%= @settings.internal_sites_enabled_path %>/*;
37
+ include <%= @internal_conf_path %>/conf.d/*.conf;
38
+ include <%= @internal_sites_enabled_path %>/*;
39
39
  }
@@ -1,25 +1,25 @@
1
1
  upstream unicorn {
2
- server unix:<%= @settings.internal_sock_path %>/unicorn.socket fail_timeout=0;
2
+ server unix:<%= @internal_sock_path %>/unicorn.socket fail_timeout=0;
3
3
  }
4
4
 
5
5
 
6
6
  server {
7
7
  listen 80;
8
8
  location / {
9
- rewrite ^/(.*)$ https://<%= @settings.domain %>/$1 redirect;
9
+ rewrite ^/(.*)$ https://<%= @domain %>/$1 redirect;
10
10
  }
11
11
  }
12
12
 
13
13
  server {
14
14
  listen 443;
15
- set $public_root <%= @settings.internal_data_path %>/public;
16
- set $rails_root <%= @settings.internal_data_path %>;
15
+ set $public_root <%= @internal_data_path %>/public;
16
+ set $rails_root <%= @internal_data_path %>;
17
17
 
18
18
  root $public_root;
19
19
 
20
20
  ssl on;
21
- ssl_certificate <%= @settings.internal_conf_path %>/ssl.crt;
22
- ssl_certificate_key <%= @settings.internal_conf_path %>/ssl.key;
21
+ ssl_certificate <%= @internal_conf_path %>/ssl.crt;
22
+ ssl_certificate_key <%= @internal_conf_path %>/ssl.key;
23
23
  ssl_ciphers HIGH;
24
24
  ssl_protocols SSLv3 TLSv1;
25
25
  ssl_prefer_server_ciphers on;
@@ -1,7 +1,5 @@
1
1
  require 'erb'
2
2
 
3
- require './nginxinator.rb' if File.exists?('./nginxinator.rb')
4
-
5
3
  ## NOTES:
6
4
  # tasks without 'desc' description lines are for manual debugging of this
7
5
  # deployment code.
@@ -11,42 +9,41 @@ require './nginxinator.rb' if File.exists?('./nginxinator.rb')
11
9
 
12
10
  namespace :nginx do
13
11
 
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
12
+ desc "Idempotently setup an Nginx instance using values in ./config/deploy/<stage>_nginxinator.rb"
13
+ task :setup do
19
14
  Rake::Task['nginx:ensure_access_docker'].invoke
20
15
  Rake::Task['nginx:open_firewall'].invoke
21
16
  # 'on', 'run_locally', 'as', 'execute', 'info', 'warn', and 'fatal' are from SSHKit
22
- on "#{settings.ssh_user}@#{settings.domain}" do
17
+ on roles(:app) do
23
18
  config_file_changed = false
24
- settings.config_files.each do |config_file|
25
- if nginx_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)
19
+ fetch(:config_files).each do |config_file|
20
+ if nginx_config_file_differs?(fetch(:local_templates_path), fetch(:external_conf_path), config_file)
21
+ warn "Config file #{config_file} on #{fetch(:domain)} is being updated."
22
+ Rake::Task['nginx:install_config_file'].invoke(fetch(:local_templates_path), fetch(:external_conf_path), config_file)
28
23
  Rake::Task['nginx:install_config_file'].reenable
29
24
  config_file_changed = true
30
25
  end
31
26
  end
32
- settings.sites_enabled.each do |config_file|
33
- if nginx_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)
27
+ fetch(:sites_enabled).each do |config_file|
28
+ if nginx_config_file_differs?(fetch(:local_site_templates_path), fetch(:external_sites_enabled_path), config_file)
29
+ warn "Config file #{config_file} on #{fetch(:domain)} is being updated."
30
+ Rake::Task['nginx:install_config_file'].invoke(fetch(:local_site_templates_path), fetch(:external_sites_enabled_path), config_file)
36
31
  Rake::Task['nginx:install_config_file'].reenable
37
32
  config_file_changed = true
38
33
  end
39
34
  end
40
- unless nginx_container_exists?(settings.container_name)
35
+ execute "sudo", "mkdir", "-p", fetch(:external_sock_path)
36
+ execute "sudo", "chown", "-R", "www-data:www-data", fetch(:external_sock_path)
37
+ unless nginx_container_exists?
41
38
  Rake::Task['nginx:create_container'].invoke
42
39
  else
43
- unless nginx_container_is_running?(settings.container_name)
40
+ unless nginx_container_is_running?
44
41
  Rake::Task['nginx:start_container'].invoke
45
42
  else
46
43
  if config_file_changed
47
44
  Rake::Task['nginx:restart_container'].invoke
48
45
  else
49
- info "No config file changes for #{settings.container_name} and it is already running; we're setup!"
46
+ info "No config file changes for #{fetch(:nginx_container_name)} and it is already running; we're setup!"
50
47
  end
51
48
  end
52
49
  end
@@ -54,75 +51,69 @@ namespace :nginx do
54
51
  end
55
52
 
56
53
  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
54
+ task :status do
55
+ on roles(:app) do
60
56
  info ""
61
- if nginx_container_exists?(settings.container_name)
62
- info "#{settings.container_name} exists on #{settings.domain}"
57
+ if nginx_container_exists?
58
+ info "#{fetch(:nginx_container_name)} exists on #{fetch(:domain)}"
63
59
  info ""
64
- if nginx_container_is_running?(settings.container_name)
65
- info "#{settings.container_name} is running on #{settings.domain}"
60
+ if nginx_container_is_running?
61
+ info "#{fetch(:nginx_container_name)} is running on #{fetch(:domain)}"
66
62
  info ""
67
63
  else
68
- info "#{settings.container_name} is not running on #{settings.domain}"
64
+ info "#{fetch(:nginx_container_name)} is not running on #{fetch(:domain)}"
69
65
  info ""
70
66
  end
71
67
  else
72
- info "#{settings.container_name} does not exist on #{settings.domain}"
68
+ info "#{fetch(:nginx_container_name)} does not exist on #{fetch(:domain)}"
73
69
  info ""
74
70
  end
75
71
  end
76
72
  end
77
73
 
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)
74
+ task :create_container do
75
+ on roles(:app) do
76
+ warn "Starting a new container named #{fetch(:nginx_container_name)} on #{fetch(:domain)}"
77
+ execute("docker", "run", fetch(:docker_run_command))
83
78
  sleep 2
84
- fatal nginx_stay_running_message(settings) and raise unless nginx_container_is_running?(settings.container_name)
79
+ fatal nginx_stay_running_message and raise unless nginx_container_is_running?
85
80
  end
86
81
  end
87
82
 
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)
83
+ task :start_container do
84
+ on roles(:app) do
85
+ warn "Starting an existing but non-running container named #{fetch(:nginx_container_name)}"
86
+ execute("docker", "start", fetch(:nginx_container_name))
93
87
  sleep 2
94
- fatal nginx_stay_running_message(settings) and raise unless nginx_container_is_running?(settings.container_name)
88
+ fatal nginx_stay_running_message and raise unless nginx_container_is_running?
95
89
  end
96
90
  end
97
91
 
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)
92
+ task :restart_container do
93
+ on roles(:app) do
94
+ warn "Restarting a running container named #{fetch(:nginx_container_name)}"
95
+ execute("docker", "restart", fetch(:nginx_container_name))
103
96
  sleep 2
104
- fatal nginx_stay_running_message(settings) and raise unless nginx_container_is_running?(settings.container_name)
97
+ fatal nginx_stay_running_message and raise unless nginx_container_is_running?
105
98
  end
106
99
  end
107
100
 
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
101
+ task :ensure_access_docker do
102
+ on roles(:app) do
103
+ as fetch(:ssh_user) do
112
104
  unless test("bash", "-c", "\"docker", "ps", "&>", "/dev/null\"")
113
- execute("sudo", "usermod", "-a", "-G", "docker", settings.ssh_user)
105
+ execute("sudo", "usermod", "-a", "-G", "docker", fetch(:ssh_user))
114
106
  fatal "Newly added to docker group, this run will fail, next run will succeed. Simply try again."
115
107
  end
116
108
  end
117
109
  end
118
110
  end
119
111
 
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
112
+ task :install_config_file, [:template_path, :config_path, :config_file] do |t, args|
113
+ on roles(:app) do
123
114
  as 'root' do
124
115
  execute("mkdir", "-p", args.config_path) unless test("test", "-d", args.config_path)
125
- generated_config_file = nginx_generate_config_file(settings, "#{args.template_path}/#{args.config_file}.erb")
116
+ generated_config_file = nginx_generate_config_file("#{args.template_path}/#{args.config_file}.erb")
126
117
  upload! StringIO.new(generated_config_file), "/tmp/#{args.config_file}"
127
118
  execute("mv", "/tmp/#{args.config_file}", "#{args.config_path}/#{args.config_file}")
128
119
  execute("chown", "-R", "root:root", args.config_path)
@@ -131,12 +122,11 @@ namespace :nginx do
131
122
  end
132
123
  end
133
124
 
134
- task :open_firewall => :ensure_setup do |t, args|
135
- settings = @settings
136
- on "#{settings.ssh_user}@#{settings.domain}" do
125
+ task :open_firewall do
126
+ on roles(:app) do
137
127
  as "root" do
138
128
  if test "ufw", "status"
139
- settings.publish_ports.collect { |port_set| port_set['external'] }.each do |port|
129
+ fetch(:publish_ports).collect { |port_set| port_set['external'] }.each do |port|
140
130
  raise "Error during opening UFW firewall" unless test("ufw", "allow", "#{port}/tcp")
141
131
  end
142
132
  end
@@ -149,12 +139,12 @@ namespace :nginx do
149
139
  # Temporarily added 'nginx_' to the beginning of each of these methods to avoid
150
140
  # getting them overwritten by other gems with methods with the same names, (E.G. postgresinator.)
151
141
  ## TODO Figure out how to do this the right or better way.
152
- def nginx_stay_running_message(settings)
153
- "Container #{settings.container_name} on #{settings.domain} did not stay running more than 2 seconds"
142
+ def nginx_stay_running_message
143
+ "Container #{fetch(:nginx_container_name)} on #{fetch(:domain)} did not stay running more than 2 seconds"
154
144
  end
155
145
 
156
- def nginx_config_file_differs?(settings, local_templates_path, external_config_path, config_file)
157
- generated_config_file = nginx_generate_config_file(settings, "#{local_templates_path}/#{config_file}.erb")
146
+ def nginx_config_file_differs?(local_templates_path, external_config_path, config_file)
147
+ generated_config_file = nginx_generate_config_file("#{local_templates_path}/#{config_file}.erb")
158
148
  as 'root' do
159
149
  config_file_path = "#{external_config_path}/#{config_file}"
160
150
  if nginx_file_exists?(config_file_path)
@@ -165,20 +155,31 @@ namespace :nginx do
165
155
  end
166
156
  end
167
157
 
168
- def nginx_generate_config_file(settings, template_file_path)
169
- @settings = settings # needed for ERB
158
+ def nginx_generate_config_file(template_file_path)
159
+ set :logs_path, -> { fetch(:internal_logs_path) }
160
+ set :conf_path, -> { fetch(:internal_conf_path) }
161
+ set :sock_path, -> { fetch(:internal_sock_path) }
162
+ set :data_path, -> { fetch(:internal_data_path) }
163
+ set :sites_path, -> { fetch(:internal_sites_enabled_path) }
164
+ set :cdomain, -> { fetch(:domain) }
165
+ @internal_logs_path = fetch(:logs_path)
166
+ @internal_conf_path = fetch(:conf_path)
167
+ @internal_sock_path = fetch(:sock_path)
168
+ @internal_data_path = fetch(:data_path)
169
+ @internal_sites_enabled_path = fetch(:sites_path)
170
+ @domain = fetch(:cdomain)
170
171
  template_path = File.expand_path(template_file_path)
171
172
  ERB.new(File.new(template_path).read).result(binding)
172
173
  end
173
174
 
174
- def nginx_container_exists?(container_name)
175
- test "docker", "inspect", container_name, ">", "/dev/null"
175
+ def nginx_container_exists?
176
+ test "docker", "inspect", fetch(:nginx_container_name), ">", "/dev/null"
176
177
  end
177
178
 
178
- def nginx_container_is_running?(container_name)
179
+ def nginx_container_is_running?
179
180
  (capture "docker", "inspect",
180
181
  "--format='{{.State.Running}}'",
181
- container_name).strip == "true"
182
+ fetch(:nginx_container_name)).strip == "true"
182
183
  end
183
184
 
184
185
  def nginx_file_exists?(file_name_path)
data/lib/nginxinator.rb CHANGED
@@ -1,6 +1,5 @@
1
- require 'rake'
2
- require 'sshkit'
3
- require 'sshkit/dsl'
1
+ require 'capistrano/setup'
2
+ require 'capistrano/deploy'
4
3
 
5
4
  load 'nginxinator/nginx.rb'
6
5
  load 'nginxinator/config.rb'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nginxinator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-05 00:00:00.000000000 Z
12
+ date: 2014-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capistrano
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.1
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: rake
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -52,12 +68,16 @@ files:
52
68
  - lib/nginxinator.rb
53
69
  - lib/nginxinator/nginx.rb
54
70
  - lib/nginxinator/config.rb
55
- - lib/nginxinator/examples/nginxinator_example.rb
56
- - lib/nginxinator/examples/nginx_example.conf.erb
57
- - lib/nginxinator/examples/site-enabled_example.erb
58
- - lib/nginxinator/examples/ssl.crt_example.erb
59
- - lib/nginxinator/examples/ssl.key_example.erb
60
- - lib/nginxinator/examples/mime.types_example.erb
71
+ - lib/nginxinator/examples/Capfile
72
+ - lib/nginxinator/examples/config/deploy.rb
73
+ - lib/nginxinator/examples/config/deploy_nginxinator.rb
74
+ - lib/nginxinator/examples/config/deploy/staging.rb
75
+ - lib/nginxinator/examples/config/deploy/staging_nginxinator.rb
76
+ - lib/nginxinator/examples/nginx.conf.erb
77
+ - lib/nginxinator/examples/site-enabled.erb
78
+ - lib/nginxinator/examples/ssl.crt.erb
79
+ - lib/nginxinator/examples/ssl.key.erb
80
+ - lib/nginxinator/examples/mime.types.erb
61
81
  - lib/nginxinator/examples/Dockerfile
62
82
  homepage: https://github.com/snarlysodboxer/nginxinator
63
83
  licenses:
@@ -1,116 +0,0 @@
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
- "--restart", "always",
103
- ports_options,
104
- image_name
105
- ].flatten
106
- end
107
-
108
- def local_templates_path
109
- "templates/nginx"
110
- end
111
-
112
- def local_site_templates_path
113
- "#{local_templates_path}/sites-enabled"
114
- end
115
-
116
- end