Module: Capistrano::BaseHelper
- Defined in:
- lib/capistrano/base_helper/base_helper.rb
the group to chown the path collapse
-
.prepare_path(path, user, group, use_sudo = false) ⇒ Object
Prepare a path with the given user and group name.
-
.remote_file_exists?(path) ⇒ Boolean
Check for file existance See stackoverflow.com/questions/1661586/how-can-you-check-to-see-if-a-file-exists-on-the-remote-server-in-capistrano Credits: Patrick Reagen / Knocte.
Class Method Summary collapse
-
.ask(message, default = true) ⇒ Object
Prompts the user for a message to agree/decline.
-
.environment ⇒ Object
Automatically sets the environment based on presence of :stage (multistage) :rails_env RAILS_ENV variable; .
-
.generate_and_upload_config(local_file, remote_file, use_sudo = false) ⇒ Object
Generate a config file by parsing an ERB template and uploading the file Fetches local file and uploads it to remote_file Make sure your user has the right permissions.
- .get_capistrano_instance ⇒ Object
-
.parse_config(file) ⇒ Object
parse a erb template and return the result.
-
.run_rake(task) ⇒ Object
Execute a rake taske using bundle and the proper env.
- .set_capistrano_instance(cap_instance) ⇒ Object
- .user_app_env_path ⇒ Object
- .user_app_env_underscore ⇒ Object
- .user_app_env_underscore_short ⇒ Object
Class Method Details
.ask(message, default = true) ⇒ Object
Prompts the user for a message to agree/decline
78 79 80 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 78 def ask(, default=true) Capistrano::CLI.ui.agree() end |
.environment ⇒ Object
Automatically sets the environment based on presence of :stage (multistage) :rails_env RAILS_ENV variable;
Defaults to “production” if not found
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 47 def environment if @@capistrano_instance.exists?(:rails_env) @@capistrano_instance.fetch(:rails_env) elsif @@capistrano_instance.exists?(:rack_env) @@capistrano_instance.fetch(:rack_env) elsif @@capistrano_instance.exists?(:stage) @@capistrano_instance.fetch(:stage) elsif(ENV['RAILS_ENV']) ENV['RAILS_ENV'] else puts "------------------------------------------------------------------" puts "- Stage, rack or rails environment isn't set in -" puts "- :stage or :rails_env or :rack_env, defaulting to 'production' -" puts "------------------------------------------------------------------" "production" end end |
.generate_and_upload_config(local_file, remote_file, use_sudo = false) ⇒ Object
Generate a config file by parsing an ERB template and uploading the file Fetches local file and uploads it to remote_file Make sure your user has the right permissions.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 91 def generate_and_upload_config(local_file, remote_file, use_sudo=false) temp_file = '/tmp/' + File.basename(local_file) erb_buffer = Capistrano::BaseHelper::parse_config(local_file) # write temp file File.open(temp_file, 'w+') { |f| f << erb_buffer } # upload temp file @@capistrano_instance.upload temp_file, temp_file, :via => :scp # create any folders required, # move temporary file to remote file @@capistrano_instance.run "#{use_sudo ? @@capistrano_instance.sudo : ""} mkdir -p #{Pathname.new(remote_file).dirname}; #{use_sudo ? "sudo" : ""} mv #{temp_file} #{remote_file}" # remove temp file `rm #{temp_file}` end |
.get_capistrano_instance ⇒ Object
23 24 25 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 23 def get_capistrano_instance @@capistrano_instance end |
.parse_config(file) ⇒ Object
parse a erb template and return the result
68 69 70 71 72 73 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 68 def parse_config(file) require 'erb' #render not available in Capistrano 2 template = File.read(file) # read it returnval = ERB.new(template).result(binding) # parse it return returnval end |
.prepare_path(path, user, group, use_sudo = false) ⇒ Object
Prepare a path with the given user and group name
120 121 122 123 124 125 126 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 120 def prepare_path(path, user, group, use_sudo = false) commands = [] commands << "#{use_sudo ? @@capistrano_instance.sudo : ""} mkdir -p #{path}" commands << "#{use_sudo ? @@capistrano_instance.sudo : ""} chown #{user}:#{group} #{path} -R" commands << "#{use_sudo ? @@capistrano_instance.sudo : ""} chmod +rw #{path}" @@capistrano_instance.run commands.join(" &&") end |
.remote_file_exists?(path) ⇒ Boolean
Check for file existance See stackoverflow.com/questions/1661586/how-can-you-check-to-see-if-a-file-exists-on-the-remote-server-in-capistrano Credits: Patrick Reagen / Knocte
132 133 134 135 136 137 138 139 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 132 def remote_file_exists?(path) results = [] @@capistrano_instance.invoke_command("if [ -e '#{path}' ]; then echo -n 'true'; fi") do |ch, stream, out| results << (out == 'true') end results == [true] end |
.run_rake(task) ⇒ Object
Execute a rake taske using bundle and the proper env. run_rake db:migrate
109 110 111 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 109 def run_rake(task) @@capistrano_instance.run "cd #{@@capistrano_instance.current_path} && RAILS_ENV=#{Capistrano::BaseHelper.environment} bundle exec rake #{task}" end |
.set_capistrano_instance(cap_instance) ⇒ Object
19 20 21 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 19 def set_capistrano_instance(cap_instance) @@capistrano_instance = cap_instance end |
.user_app_env_path ⇒ Object
35 36 37 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 35 def user_app_env_path File.join(get_capistrano_instance.fetch(:user), "#{get_capistrano_instance.fetch(:application)}_#{environment}") end |
.user_app_env_underscore ⇒ Object
27 28 29 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 27 def user_app_env_underscore "#{@@capistrano_instance.fetch(:user)}_#{@@capistrano_instance.fetch(:application)}_#{environment}" end |
.user_app_env_underscore_short ⇒ Object
31 32 33 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 31 def user_app_env_underscore_short "#{@@capistrano_instance.fetch(:user)[0...1]}_#{environment[0...1]}_#{@@capistrano_instance.fetch(:application)}" end |