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
- .user_app_env_underscore_short_char_safe ⇒ Object
Class Method Details
.ask(message, default = true) ⇒ Object
Prompts the user for a message to agree/decline
83 84 85 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 83 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 52 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 96 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
24 25 26 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 24 def get_capistrano_instance @@capistrano_instance end |
.parse_config(file) ⇒ Object
parse a erb template and return the result
73 74 75 76 77 78 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 73 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
125 126 127 128 129 130 131 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 125 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
137 138 139 140 141 142 143 144 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 137 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
114 115 116 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 114 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
20 21 22 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 20 def set_capistrano_instance(cap_instance) @@capistrano_instance = cap_instance end |
.user_app_env_path ⇒ Object
40 41 42 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 40 def user_app_env_path File.join(get_capistrano_instance.fetch(:user), "#{get_capistrano_instance.fetch(:application)}_#{environment}") end |
.user_app_env_underscore ⇒ Object
28 29 30 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 28 def user_app_env_underscore "#{@@capistrano_instance.fetch(:user)}_#{@@capistrano_instance.fetch(:application)}_#{environment}" end |
.user_app_env_underscore_short ⇒ Object
32 33 34 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 32 def user_app_env_underscore_short "#{@@capistrano_instance.fetch(:user)[0...1]}_#{environment[0...1]}_#{@@capistrano_instance.fetch(:application)}" end |
.user_app_env_underscore_short_char_safe ⇒ Object
36 37 38 |
# File 'lib/capistrano/base_helper/base_helper.rb', line 36 def user_app_env_underscore_short_char_safe user_app_env_underscore_short.gsub!("-","_") end |