Module: Pulsar::Helpers::Clamp::InstanceMethods

Includes:
InstanceAndClassMethods
Defined in:
lib/pulsar/helpers/clamp.rb

Instance Method Summary collapse

Methods included from InstanceAndClassMethods

#from_application_path?

Instance Method Details

#application_pathObject



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_installObject



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_capfileObject



41
42
43
# File 'lib/pulsar/helpers/clamp.rb', line 41

def create_capfile
  touch(capfile_path, :verbose => verbose?)
end

#each_appObject



45
46
47
48
49
# File 'lib/pulsar/helpers/clamp.rb', line 45

def each_app
  Dir["#{config_apps_path}/*"].each do |path|
    yield(File.basename(path)) if File.directory?(path)
  end
end

#fetch_directory_repo(repo) ⇒ Object



63
64
65
# File 'lib/pulsar/helpers/clamp.rb', line 63

def fetch_directory_repo(repo)
  run_cmd("cp -rp #{repo} #{config_path}", :verbose => verbose?)
end

#fetch_git_repo(repo, local = false) ⇒ Object



67
68
69
70
71
72
# File 'lib/pulsar/helpers/clamp.rb', line 67

def fetch_git_repo(repo, local=false)
  git_options = "--quiet --branch #{conf_branch}"
  git_options = "#{git_options} --depth=1" unless local

  run_cmd("git clone #{git_options} #{repo} #{config_path}", :verbose => verbose?)
end

#fetch_repoObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pulsar/helpers/clamp.rb', line 51

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

#include_app(app, stage) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pulsar/helpers/clamp.rb', line 74

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



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pulsar/helpers/clamp.rb', line 87

def include_app_recipes(app, stage)
  recipes_dir = config_app_recipes_path(app)
  stage_recipes_dir = config_app_stage_recipes_path(app, stage)

  Dir.glob("#{recipes_dir}/*.rb").each do |recipe|
    run_cmd("cat #{recipe} >> #{capfile_path}", :verbose => verbose?)
  end

  Dir.glob("#{stage_recipes_dir}/*.rb").each do |recipe|
    run_cmd("cat #{recipe} >> #{capfile_path}", :verbose => verbose?)
  end
end

#include_base_confObject



100
101
102
# File 'lib/pulsar/helpers/clamp.rb', line 100

def include_base_conf
  run_cmd("cat #{config_base_path} >> #{capfile_path}", :verbose => verbose?)
end

#list_appsObject



104
105
106
107
108
# File 'lib/pulsar/helpers/clamp.rb', line 104

def list_apps
  each_app do |name|
    puts "#{name.cyan}: #{stages_for(name).map(&:magenta).join(', ')}"
  end
end

#load_configurationObject



110
111
112
113
114
115
116
117
118
# File 'lib/pulsar/helpers/clamp.rb', line 110

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_configurationObject



120
121
122
123
124
125
126
127
# File 'lib/pulsar/helpers/clamp.rb', line 120

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_capfileObject



129
130
131
# File 'lib/pulsar/helpers/clamp.rb', line 129

def remove_capfile
  rm_rf(capfile_path, :verbose => verbose?)
end

#remove_repoObject



133
134
135
# File 'lib/pulsar/helpers/clamp.rb', line 133

def remove_repo
  rm_rf(config_path, :verbose => verbose?)
end

#reset_for_other_app!Object



137
138
139
# File 'lib/pulsar/helpers/clamp.rb', line 137

def reset_for_other_app!
  @capfile_name = @configuration_path = @now = nil
end

#run_capistrano(args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pulsar/helpers/clamp.rb', line 141

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_levelObject



153
154
155
156
157
158
159
160
161
162
# File 'lib/pulsar/helpers/clamp.rb', line 153

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



164
165
166
167
168
169
170
171
172
# File 'lib/pulsar/helpers/clamp.rb', line 164

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

#validate(app, stage) ⇒ Object

Raises:

  • (ArgumentError)


174
175
176
177
178
179
# File 'lib/pulsar/helpers/clamp.rb', line 174

def validate(app, stage)
  app_path = config_app_path(app)
  stage_path = config_stage_path(app, stage)

  raise(ArgumentError) unless File.exists?(app_path) && File.exists?(stage_path)
end