Search code examples
capistranocapistrano3

run capistrano 3 custom setup scripts as root once


I've got capistrano 3 running perfectly with passwordless deploy as a non root user.

What I'm trying to do now is setup a install script that install's the upstart service, the sudoers.d file and installs some dependencies on the server.

so that I could install a new server by simply entering the user and host in the production.rb file and run cap production setupserver

the problem is that the setup scripts that I've created need to be run as root.

But since it's a one time thing, I'd simply like to ask the user for the root password and run a couple of tasks on the server.

the as :root command doesn't work since it uses su -c

I could ask for the password as demonstrated here http://capistranorb.com/documentation/faq/how-can-i-get-capistrano-to-prompt-for-a-password/

any suggestions on how to override the user specified in the production.rb file?

and how to pass the asked password?


Solution

  • The way I finally solved it was by adding another role for the same server.

    role :app, 'theappuser@myserver'
    role :install, 'root@myserver', :no_release => true
    

    And then doing something like this

    desc 'Install nginx'
    task :install do
      on roles(:install) do
        require 'erb'
        template = ERB.new(File.new('deploy/templates/nginx.conf.erb').read).result(binding)
        upload! StringIO.new(template), "/etc/nginx/conf.d/#{fetch(:domain)}.conf"
      end
    
      on roles(:app) do
        invoke 'nginx:restart'
      end
    end