Module: Pulsar::Helpers::Clamp::InstanceMethods
- Includes:
- InstanceAndClassMethods
- Defined in:
- lib/pulsar/helpers/clamp.rb
Instance Method Summary collapse
- #application_path ⇒ Object
- #build_capfile(args) ⇒ Object
- #bundle_install ⇒ Object
- #capfile_path ⇒ Object
- #cd(path, opts, &block) ⇒ Object
- #config_path ⇒ Object
- #create_capfile ⇒ Object
- #fetch_directory_repo(repo) ⇒ Object
- #fetch_git_repo(repo) ⇒ Object
- #fetch_repo ⇒ Object
- #include_app(app, stage = nil) ⇒ Object
- #include_app_recipes(app, stage = nil) ⇒ Object
- #include_base_conf ⇒ Object
- #list_apps ⇒ Object
- #load_configuration ⇒ Object
- #remove_capfile ⇒ Object
- #remove_repo ⇒ Object
- #reset_for_other_app! ⇒ Object
- #rm_rf(path, opts) ⇒ Object
- #run_capistrano(args) ⇒ Object
- #run_cmd(cmd, opts) ⇒ Object
- #set_log_level ⇒ Object
- #time_to_deploy ⇒ Object
- #touch(file, opts) ⇒ Object
Methods included from InstanceAndClassMethods
Instance Method Details
#application_path ⇒ Object
22 23 24 |
# File 'lib/pulsar/helpers/clamp.rb', line 22 def application_path Dir.pwd if from_application_path? end |
#build_capfile(args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pulsar/helpers/clamp.rb', line 26 def build_capfile(args) app, stage = args.split(":") # Variables set_log_level include_base_conf include_app(app, stage) if app # Recipes include_app_recipes(app, stage) if app end |
#bundle_install ⇒ Object
38 39 40 41 42 |
# File 'lib/pulsar/helpers/clamp.rb', line 38 def bundle_install cd(config_path, :verbose => verbose?) do run_cmd("bundle install --quiet", :verbose => verbose?) end end |
#capfile_path ⇒ Object
44 45 46 |
# File 'lib/pulsar/helpers/clamp.rb', line 44 def capfile_path @capfile_name ||= "#{tmp_dir}/capfile-#{time_to_deploy}" end |
#cd(path, opts, &block) ⇒ Object
48 49 50 51 |
# File 'lib/pulsar/helpers/clamp.rb', line 48 def cd(path, opts, &block) puts "Directory: #{path.white}".yellow if opts[:verbose] FileUtils.cd(path) { yield } end |
#config_path ⇒ Object
53 54 55 |
# File 'lib/pulsar/helpers/clamp.rb', line 53 def config_path @configuration_path ||= "#{tmp_dir}/conf-repo-#{time_to_deploy}" end |
#create_capfile ⇒ Object
57 58 59 |
# File 'lib/pulsar/helpers/clamp.rb', line 57 def create_capfile touch(capfile_path, :verbose => verbose?) end |
#fetch_directory_repo(repo) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/pulsar/helpers/clamp.rb', line 69 def fetch_directory_repo(repo) if File.directory?("#{repo}/.git") fetch_git_repo(repo) else run_cmd("cp -rp #{repo} #{config_path}", :verbose => verbose?) end end |
#fetch_git_repo(repo) ⇒ Object
77 78 79 80 |
# File 'lib/pulsar/helpers/clamp.rb', line 77 def fetch_git_repo(repo) = "--quiet --depth=1 --branch #{conf_branch}" run_cmd("git clone #{} #{repo} #{config_path}", :verbose => verbose?) end |
#fetch_repo ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/pulsar/helpers/clamp.rb', line 61 def fetch_repo if conf_repo =~ /\A[a-zA-Z-]+\/[a-zA-Z-]+\Z/ fetch_git_repo("[email protected]:#{conf_repo}.git") else fetch_directory_repo(conf_repo) end end |
#include_app(app, stage = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pulsar/helpers/clamp.rb', line 82 def include_app(app, stage=nil) app_file = "#{config_path}/apps/#{app}/defaults.rb" stage_file = "#{config_path}/apps/#{app}/#{stage}.rb" if File.exists?(app_file) run_cmd("cat #{app_file} >> #{capfile_path}", :verbose => verbose?) end if stage run_cmd("cat #{stage_file} >> #{capfile_path}", :verbose => verbose?) end end |
#include_app_recipes(app, stage = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/pulsar/helpers/clamp.rb', line 95 def include_app_recipes(app, stage=nil) recipes_dir = "#{config_path}/apps/#{app}/recipes" Dir.glob("#{recipes_dir}/*.rb").each do |recipe| run_cmd("cat #{recipe} >> #{capfile_path}", :verbose => verbose?) end if stage Dir.glob("#{recipes_dir}/#{stage}/*.rb").each do |recipe| run_cmd("cat #{recipe} >> #{capfile_path}", :verbose => verbose?) end end end |
#include_base_conf ⇒ Object
109 110 111 |
# File 'lib/pulsar/helpers/clamp.rb', line 109 def include_base_conf run_cmd("cat #{config_path}/apps/base.rb >> #{capfile_path}", :verbose => verbose?) end |
#list_apps ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/pulsar/helpers/clamp.rb', line 113 def list_apps apps = Dir["#{config_path}/apps/*"].each do |app| if File.directory?(app) app_name = File.basename(app) app_envs = [] Dir["#{app}/*"].each do |env| environments = %w(development staging production) env_name = File.basename(env, '.rb') if environments.include?(env_name) app_envs << env_name end end puts "#{app_name.cyan}: #{app_envs.map(&:magenta).join(', ')}" end end end |
#load_configuration ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/pulsar/helpers/clamp.rb', line 133 def load_configuration conf_path = application_path || Dir.home conf_file = File.join(conf_path, ".pulsar") if File.file?(conf_file) File.readlines(conf_file).each do |line| conf, value = line.split("=") ENV[conf] = value.chomp.gsub('"', '') end end end |
#remove_capfile ⇒ Object
146 147 148 |
# File 'lib/pulsar/helpers/clamp.rb', line 146 def remove_capfile rm_rf(capfile_path, :verbose => verbose?) end |
#remove_repo ⇒ Object
150 151 152 |
# File 'lib/pulsar/helpers/clamp.rb', line 150 def remove_repo rm_rf(config_path, :verbose => verbose?) end |
#reset_for_other_app! ⇒ Object
154 155 156 |
# File 'lib/pulsar/helpers/clamp.rb', line 154 def reset_for_other_app! @capfile_name = @configuration_path = @now = nil end |
#rm_rf(path, opts) ⇒ Object
170 171 172 173 |
# File 'lib/pulsar/helpers/clamp.rb', line 170 def rm_rf(path, opts) puts "Remove: #{path.white}".yellow if opts[:verbose] FileUtils.rm_rf(path) end |
#run_capistrano(args) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/pulsar/helpers/clamp.rb', line 158 def run_capistrano(args) cmd = "bundle exec cap" env = "CONFIG_PATH=#{config_path}" opts = "--file #{capfile_path}" env += " APP_PATH=#{application_path}" unless application_path.nil? cd(config_path, :verbose => verbose?) do run_cmd("#{cmd} #{env} #{opts} #{args}", :verbose => verbose?) end end |
#run_cmd(cmd, opts) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/pulsar/helpers/clamp.rb', line 175 def run_cmd(cmd, opts) puts "Command: #{cmd.white}".yellow if opts[:verbose] system(cmd) raise "Command #{cmd} Failed" if $? != 0 end |
#set_log_level ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/pulsar/helpers/clamp.rb', line 182 def set_log_level level = log_level.upcase levels = %w(IMPORTANT INFO DEBUG) level = levels.first unless levels.include?(level) cmd = "echo 'logger.level = logger.level = Capistrano::Logger::#{level}' >> #{capfile_path}" run_cmd(cmd, :verbose => verbose?) end |
#time_to_deploy ⇒ Object
193 194 195 |
# File 'lib/pulsar/helpers/clamp.rb', line 193 def time_to_deploy @now ||= Time.now.strftime("%Y-%m-%d-%H%M%S-s#{rand(9999)}") end |
#touch(file, opts) ⇒ Object
197 198 199 200 |
# File 'lib/pulsar/helpers/clamp.rb', line 197 def touch(file, opts) puts "Touch: #{file.white}".yellow if opts[:verbose] FileUtils.touch(file) end |