Module: Pulsar::Helpers::Clamp::InstanceMethods
- Includes:
- InstanceAndClassMethods
- Defined in:
- lib/pulsar/helpers/clamp.rb
Instance Method Summary collapse
- #application_path ⇒ Object
- #build_capfile(app, stage) ⇒ Object
- #bundle_install ⇒ Object
- #create_capfile ⇒ Object
- #create_tmp_dir ⇒ Object
- #each_app ⇒ Object
- #expand_applications ⇒ Object
- #fetch_directory_repo(repo) ⇒ Object
- #fetch_git_repo(repo, local = false) ⇒ Object
- #fetch_repo ⇒ Object
- #find_apps_from_pattern(glob) ⇒ Object
- #include_app(app, stage) ⇒ Object
- #include_app_recipes(app, stage) ⇒ Object
- #include_base_conf ⇒ Object
- #list_apps ⇒ Object
- #load_configuration ⇒ Object
- #pulsar_configuration ⇒ Object
- #remove_capfile ⇒ Object
- #remove_repo ⇒ Object
- #run_capistrano(args) ⇒ Object
- #set_log_level ⇒ Object
- #stages_for(app) ⇒ Object
- #supported_env_vars ⇒ Object
- #validate(app, stage) ⇒ Object
- #with_clean_env_and_supported_vars ⇒ Object
Methods included from InstanceAndClassMethods
Instance Method Details
#application_path ⇒ Object
21 22 23 |
# File 'lib/pulsar/helpers/clamp.rb', line 21 def application_path Dir.pwd if from_application_path? end |
#build_capfile(app, stage) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/pulsar/helpers/clamp.rb', line 25 def build_capfile(app, stage) # 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
35 36 37 38 39 |
# File 'lib/pulsar/helpers/clamp.rb', line 35 def bundle_install cd(config_path, :verbose => verbose?) do run_cmd("bundle install --quiet", :verbose => verbose?) end end |
#create_capfile ⇒ Object
41 42 43 |
# File 'lib/pulsar/helpers/clamp.rb', line 41 def create_capfile touch(capfile_path, :verbose => verbose?) end |
#create_tmp_dir ⇒ Object
45 46 47 |
# File 'lib/pulsar/helpers/clamp.rb', line 45 def create_tmp_dir run_cmd("mkdir -p #{tmp_dir}", :verbose => verbose?) end |
#each_app ⇒ Object
49 50 51 52 53 |
# File 'lib/pulsar/helpers/clamp.rb', line 49 def each_app Dir["#{config_apps_path}/*"].each do |path| yield(File.basename(path)) if File.directory?(path) end end |
#expand_applications ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pulsar/helpers/clamp.rb', line 55 def if from_application_path? [ ENV['PULSAR_APP_NAME'] || File.basename(application_path) ] else # # Given following applications: # pulsar_repo/ # apps/ # app1 # app2 # app3-web # app3-worker # app4 # it turns app1,app2,app3* into # # [ app1, app2, app3-web, app3-worker ] # applications.split(',').flat_map { |glob| find_apps_from_pattern(glob) }.uniq end end |
#fetch_directory_repo(repo) ⇒ Object
88 89 90 |
# File 'lib/pulsar/helpers/clamp.rb', line 88 def fetch_directory_repo(repo) run_cmd("cp -rp #{repo} #{config_path}", :verbose => verbose?) end |
#fetch_git_repo(repo, local = false) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/pulsar/helpers/clamp.rb', line 92 def fetch_git_repo(repo, local=false) = "--quiet --branch #{conf_branch}" = "#{} --depth=1" unless local run_cmd("git clone #{} #{repo} #{config_path}", :verbose => verbose?) end |
#fetch_repo ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pulsar/helpers/clamp.rb', line 76 def fetch_repo if File.directory?(conf_repo) fetch_directory_repo(conf_repo) else if conf_repo =~ /\A[a-zA-Z-]+\/[a-zA-Z-]+\Z/ fetch_git_repo("[email protected]:#{conf_repo}.git") else fetch_git_repo(conf_repo) end end end |
#find_apps_from_pattern(glob) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pulsar/helpers/clamp.rb', line 99 def find_apps_from_pattern(glob) found_apps = [] Dir.glob(config_app_path("#{glob}/")).each do |path| found_apps << File.basename(path) end found_apps << glob if found_apps.empty? found_apps end |
#include_app(app, stage) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/pulsar/helpers/clamp.rb', line 110 def include_app(app, stage) app_file = config_app_defaults_path(app) stage_file = config_stage_path(app, stage) if File.exists?(app_file) run_cmd("cat #{app_file} >> #{capfile_path}", :verbose => verbose?) end if File.exists?(stage_file) run_cmd("cat #{stage_file} >> #{capfile_path}", :verbose => verbose?) end end |
#include_app_recipes(app, stage) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/pulsar/helpers/clamp.rb', line 123 def include_app_recipes(app, stage) recipes_dir = config_app_recipes_path(app) stage_recipes_dir = config_app_stage_recipes_path(app, stage) Dir["#{recipes_dir}/*.rb", "#{stage_recipes_dir}/*.rb"].each do |recipe| run_cmd("cat #{recipe} >> #{capfile_path}", :verbose => verbose?) end end |
#include_base_conf ⇒ Object
132 133 134 |
# File 'lib/pulsar/helpers/clamp.rb', line 132 def include_base_conf run_cmd("cat #{config_base_path} >> #{capfile_path}", :verbose => verbose?) end |
#list_apps ⇒ Object
136 137 138 139 140 |
# File 'lib/pulsar/helpers/clamp.rb', line 136 def list_apps each_app do |name| puts "#{name.cyan}: #{stages_for(name).map(&:magenta).join(', ')}" end end |
#load_configuration ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/pulsar/helpers/clamp.rb', line 142 def load_configuration unless pulsar_configuration.nil? File.readlines(pulsar_configuration).each do |line| conf, value = line.split("=") ENV[conf] = value.chomp.gsub('"', '') if !conf.nil? && !value.nil? end end end |
#pulsar_configuration ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/pulsar/helpers/clamp.rb', line 152 def pulsar_configuration conf_file = ".pulsar" inside_app = File.join(application_path, conf_file) rescue nil inside_home = File.join(Dir.home, conf_file) rescue nil return inside_app if inside_app && File.file?(inside_app) return inside_home if inside_home && File.file?(inside_home) end |
#remove_capfile ⇒ Object
161 162 163 |
# File 'lib/pulsar/helpers/clamp.rb', line 161 def remove_capfile rm_rf(capfile_path, :verbose => verbose?) end |
#remove_repo ⇒ Object
165 166 167 |
# File 'lib/pulsar/helpers/clamp.rb', line 165 def remove_repo rm_rf(config_path, :verbose => verbose?) end |
#run_capistrano(args) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/pulsar/helpers/clamp.rb', line 169 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 |
#set_log_level ⇒ Object
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pulsar/helpers/clamp.rb', line 181 def set_log_level level = log_level.upcase levels = %w(IMPORTANT INFO DEBUG TRACE) level = levels.first unless levels.include?(level) cmd = "echo 'logger.level = logger.level = Capistrano::Logger::#{level}' >> #{capfile_path}" run_cmd(cmd, :verbose => verbose?) end |
#stages_for(app) ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/pulsar/helpers/clamp.rb', line 196 def stages_for(app) exclude = %w(defaults recipes) Dir["#{config_app_path(app)}/*"].sort.map do |env| env_name = File.basename(env, '.rb') env_name unless exclude.include?(env_name) end.compact end |
#supported_env_vars ⇒ Object
192 193 194 |
# File 'lib/pulsar/helpers/clamp.rb', line 192 def supported_env_vars %w(PULSAR_APP_NAME PULSAR_CONF_REPO PULSAR_DEFAULT_TASK) end |
#validate(app, stage) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/pulsar/helpers/clamp.rb', line 206 def validate(app, stage) app_path = config_app_path(app) stage_path = config_stage_path(app, stage) valid_paths = File.exists?(app_path) && File.exists?(stage_path) raise(ArgumentError, "no pulsar config available for app=#{app}, stage=#{stage}") unless valid_paths end |
#with_clean_env_and_supported_vars ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/pulsar/helpers/clamp.rb', line 213 def with_clean_env_and_supported_vars vars_from_original_env = ENV.select { |var| supported_env_vars.include?(var) } Bundler.with_clean_env do vars_from_original_env.each { |var, val| ENV[var] = val } yield end end |