Class: CapistranoKyan::CapistranoIntegration

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano-kyan/capistrano_integration.rb

Constant Summary collapse

TASKS =
[
  'deploy:seed',
  'deploy:add_env',
  'kyan:vhost:setup',
  'kyan:vhost:show',
  'nginx:start',
  'nginx:stop',
  'nginx:restart',
  'nginx:reload',
  'foreman:export',
  'foreman:start',
  'foreman:stop',
  'foreman:restart'
]

Class Method Summary collapse

Class Method Details

.load_into(capistrano_config) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/capistrano-kyan/capistrano_integration.rb', line 21

def self.load_into(capistrano_config)
  capistrano_config.load do
    before(CapistranoIntegration::TASKS) do
      _cset(:app_env)             { (fetch(:rails_env) rescue 'staging') }
      _cset(:vhost_env)           { fetch(:app_env) }
      _cset(:vhost_tmpl_path)     { 'config/vhosts' }
      _cset(:vhost_tmpl_name)     { 'vhost.conf.erb' }
      _cset(:vhost_server_path)   { '/etc/nginx/sites-enabled' }
      _cset(:vhost_server_name)   { File.basename(deploy_to) rescue fetch(:app_env) }
    end

    def appize(app, prefix = 'staging')
      "#{app.gsub('.','_')}_#{prefix}"
    end

    def tmpl_server_location
      run "mkdir -p #{shared_path}/config"
      File.join(shared_path, 'config', "#{File.basename(deploy_to)}.conf")
    end

    def symlink(target, link)
      run "ln -nfs #{target} #{link}"
    end

    def parse_template(template)
      ERB.new(File.read(template), nil , '-').result(binding)
    end

    def build_vhost(path, name)
      [
        File.join(path, name),
        File.join(File.dirname(__FILE__),'../../templates/vhost.conf.erb')
      ].each do |template|
        if File.file? template
          return parse_template(template)
        end
      end
    end

    namespace :deploy do
      desc "Load the database with seed data"
      task :seed do
        run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{app_env}"
      end

      task :add_env do
        put "RAILS_ENV=#{app_env}", "#{release_path}/.env"
      end
    end

    after "deploy:finalize_update", "deploy:add_env"

    namespace :nginx do
      task :start, :roles => :app, :except => { :no_release => true } do
        run "sudo /etc/init.d/nginx start"
      end

      task :stop, :roles => :app, :except => { :no_release => true } do
        run "sudo /etc/init.d/nginx stop"
      end

      task :restart, :roles => :app, :except => { :no_release => true } do
        run "sudo /etc/init.d/nginx restart"
      end

      task :reload, :roles => :app, :except => { :no_release => true } do
        run "sudo /etc/init.d/nginx reload"
      end
    end

    namespace :foreman do
      desc "Export the Procfile to Ubuntu's upstart scripts"
      task :export, :roles => :app do
        run "cd #{release_path} && sudo foreman export upstart /etc/init -a #{vhost_server_name} -u #{user} -l #{shared_path}/log"
      end
      desc "Start the application services"
      task :start, :roles => :app do
        run "sudo start #{vhost_server_name}"
      end

      desc "Stop the application services"
      task :stop, :roles => :app do
        run "sudo stop #{vhost_server_name}"
      end

      desc "Restart the application services"
      task :restart, :roles => :app do
        run "sudo start #{vhost_server_name} || sudo restart #{vhost_server_name}"
      end
    end

    namespace :kyan do
      #
      # vhost cap tasks
      #
      namespace :vhost do
        desc <<-DESC
          Creates and symlinks an Nginx virtualhost entry.

          By default, this task uses a builtin template which you
          see the output with rake kyan:vhost:show. If you need to
          customise this, you can create your own erb template and
          update the :vhost_tmpl_path and :vhost_tmpl_name variables.
        DESC
        task :setup, :except => { :no_release => true } do
          if tmpl = build_vhost(vhost_tmpl_path, vhost_tmpl_name)
            put tmpl, tmpl_server_location
            symlink(tmpl_server_location, vhost_server_path)
          else
            puts "Could not find a suitable template."
          end
        end

        desc "Displays the vhost that will be uploaded to server"
        task :show, :except => { :no_release => true } do
          puts build_vhost(vhost_tmpl_path, vhost_tmpl_name)
        end
      end

      #
      # database.yml cap tasks
      #
      namespace :db do
        #
        # Updates the symlink for database.yml file to the just deployed release.
        #
        task :symlink, :except => { :no_release => true } do
          path_to_appl_database_yml = "#{release_path}/config/database.yml"
          path_to_conf_database_yml = "#{shared_path}/config/database.yml"

          run "ln -nfs #{path_to_conf_database_yml} #{path_to_appl_database_yml}"
        end
      end

      after "deploy:finalize_update", "kyan:db:symlink"
    end
  end
end