How do I get capistrano 3 to prompt for server name?

373 views
Skip to first unread message

Stuart Watson

unread,
Jul 17, 2015, 11:01:16 AM7/17/15
to capis...@googlegroups.com
Here is my config so far:-

Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
#require 'capistrano/passenger'
#require "whenever/capistrano"



# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }


Deploy.rb


# config valid only for current version of Capistrano
lock '3.4.0'

set :application, '#{fetch(:application)}'
set :repo_url, 'git@temp'

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp


set :deploy_user, 'temp'


set :backup_path, "/home/#{fetch(:deploy_user)}/Backup"



# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/home/#{fetch(:deploy_user)}/#{fetch(:application)}"

# Default value for :scm is :git
set :scm, :git
set :scm_username, "temp"


set :rvm_type, :user
set :rvm_ruby_version, '2.0.0-p451'


#set :ssh_options, {
##  config: false,
#  forward_agent: true,
#}

set :ssh_options, {:forward_agent => true}


# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
#set :log_level, :debug

# Default value for :pty is false
set :pty, true

# Default value for :linked_files is []
#set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')


set :linked_files, %w{config/database.yml config/mailer_config.yml config/max_mind.yml}



# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')


# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 5


namespace :deploy do


  desc "Restart app"
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join("tmp/restart.txt")
    end
  end

  after :finishing, "deploy:cleanup"

end


I've written a cap task that configures all the dbs etc. This gets run before I do the cap production deploy.

namespace :config do
  task :setup do
    ask(:application, 'application')
    ask(:web_server, 'web_server')
    ask(:db_user, 'db_user')
    ask(:db_pass, 'db_pass')
    ask(:db_name, 'db_name')
    ask(:db_host, 'db_host')
    setup_config = <<-EOF
#{fetch(:rails_env)}:
  adapter: postgresql
  database: #{fetch(:db_name)}
  username: #{fetch(:db_user)}
  password: #{fetch(:db_pass)}
  host: #{fetch(:db_host)}
    EOF
    on roles(:app) do
      execute "mkdir -p #{shared_path}/config"
      upload! StringIO.new(setup_config), "#{shared_path}/config/database.yml"
    end
  end
end


And i've added this in here, but it fails with an ssh error

@'s password:


How can I resolve this?

Thanks


Lee Hambley

unread,
Jul 17, 2015, 12:36:18 PM7/17/15
to Capistrano
I'm sorry I can't understand what is not working as you haven't included the `server(…)` or `role(…)` lines where `roles(:app)` should be populated from.

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/capistrano/34a7a7cb-add9-4ca7-91b7-a4b2e4487f6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stuart Watson

unread,
Jul 18, 2015, 3:45:02 PM7/18/15
to capis...@googlegroups.com
This is my production.rb file

# server-based syntax
# ======================
# Defines a single server with a list of roles and multiple properties.
# You can define all roles on a single server, or split them:
# server 'example.com', user: 'deploy', roles: %w{app db web}, my_property: :my_value
# server 'example.com', user: 'deploy', roles: %w{app web}, other_property: :other_value
# server 'db.example.com', user: 'deploy', roles: %w{db}
set :stage, :production
set :branch, "capistrano3"
server "#{fetch(:server)}", roles: %w{app web db}
set :application, "#{fetch(:application)}"
set :server_name, "#{fetch(:application)}.#{fetch(:server)}"
set :sudo_user, "user"
set :app_port, "80"


I think it has something to do with the require 'capistrano/rvm' in my Capfile, as if i comment this out, i get a bit further, if i keep this in I get a prompt for a password when its trying to run rvm:check.

Is there a more correct way of doing this?  Ideally we want to have no actual information in the config so they can be easily moved between servers.  Also i'd like to try and avoid having a file which contains all the relevant information which could be committed to github (users change the name and forget to update the git ignore file)


Once this has finished we'll then run cap production deploy - it's designed to replace the deploy:setup.

Hope this helps

Thanks




    ask(:web_server, 'server')
    ask(:db_user, 'db_user')
    ask(:db_pass, 'db_pass')
    ask(:db_name, 'db_name')
    ask(:db_host, 'db_host')
    setup_config = <<-EOF
#{fetch(:rails_env)}:
  adapter: postgresql
  database: #{fetch(:db_name)}
  username: #{fetch(:db_user)}
  password: #{fetch(:db_pass)}
  host: #{fetch(:db_host)}
    EOF
    on roles(:app) do
      execute "mkdir -p #{shared_path}/config"
      upload! StringIO.new(setup_config), "#{shared_path}/config/database.yml"
    end
  end
end

Stuart Watson

unread,
Jul 18, 2015, 3:53:36 PM7/18/15
to capis...@googlegroups.com
I guess I might be better doing

cap --hosts=server1,server2 production deploy

and not having the initial setup file ask for the server name which is probably wrong.

Thanks


On Friday, July 17, 2015 at 4:01:16 PM UTC+1, Stuart Watson wrote:

Lee Hambley

unread,
Jul 19, 2015, 7:53:03 AM7/19/15
to Capistrano
Thats one way, the other would simply be to use `readline` or io/consoel to read the input, i.e http://stackoverflow.com/a/11765329/119669

server STDIN.noecho(&:gets), roles: %w{foo bar baz}

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+...@googlegroups.com.

Stuart Watson

unread,
Jul 19, 2015, 10:45:22 AM7/19/15
to capis...@googlegroups.com
It's using ssh keys so there is no password to input....would this solve this problem?

If I replace the server variable in the production.rb file with the server ip address it works fine.

Thanks

Stuart


On Friday, July 17, 2015 at 4:01:16 PM UTC+1, Stuart Watson wrote:

Lee Hambley

unread,
Jul 19, 2015, 11:42:51 AM7/19/15
to Capistrano
If I replace the server variable in the production.rb file with the server ip address it works fine.


That's what I suggested to do, by prompting for the server name.

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages