Class: Chap::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/chap/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



4
5
6
7
8
# File 'lib/chap/config.rb', line 4

def initialize(options={})
  @options = options
  # preload all config files so validatation happens before deploys
  _ = yaml, chap, node
end

Instance Attribute Details

#chapObject (readonly)

Returns the value of attribute chap.



3
4
5
# File 'lib/chap/config.rb', line 3

def chap
  @chap
end

#nodeObject (readonly)

Returns the value of attribute node.



3
4
5
# File 'lib/chap/config.rb', line 3

def node
  @node
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/chap/config.rb', line 3

def options
  @options
end

Instance Method Details

#cached_pathObject



76
77
78
79
80
# File 'lib/chap/config.rb', line 76

def cached_path
  return @cached_path if @cached_path
  path = chap[:repo].split(':').last.sub('.git','')
  @cached_path = "#{shared_path}/cache/#{strategy}/#{path}"
end

#chap_log_pathObject



99
100
101
102
103
104
105
106
# File 'lib/chap/config.rb', line 99

def chap_log_path
  return @chap_log_path if @chap_log_path
  dir = "#{shared_path}/chap"
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  @chap_log_path = "#{dir}/chap.log"
  system("cat /dev/null > #{@chap_log_path}")
  @chap_log_path
end

#current_pathObject



68
69
70
# File 'lib/chap/config.rb', line 68

def current_path
  "#{deploy_to}/current"
end

#deploy_toObject



59
60
61
# File 'lib/chap/config.rb', line 59

def deploy_to
  chap[:deploy_to]
end

#latest_releaseObject



82
83
84
85
86
87
88
# File 'lib/chap/config.rb', line 82

def latest_release
  return @latest_release if @latest_release
  @latest_release = Dir.glob("#{deploy_to}/releases/*").
                      select {|p| File.directory?(p) }.
                      sort.
                      last
end

#load_json(key) ⇒ Object

the chap.json and node.json is assumed to be in th same folder as chap.yml if a relative path is given



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chap/config.rb', line 35

def load_json(key)
  path = if yaml[key] =~ %r{^/} # root path given
           yaml[key]
         else # relative path
           dirname = File.dirname(options[:config])
           "#{dirname}/#{yaml[key]}"
         end
  if File.exist?(path)
    Mash.from_hash(JSON.parse(IO.read(path)))
  else
    puts "ERROR: #{key}.json config does not exist at: #{path}"
    exit 1
  end
end

#log(msg) ⇒ Object



94
95
96
97
# File 'lib/chap/config.rb', line 94

def log(msg)
  puts msg unless options[:quiet]
  logger.info(msg)
end

#loggerObject



108
109
110
111
112
113
# File 'lib/chap/config.rb', line 108

def logger
  return @logger if @logger
  @logger = Logger.new(chap_log_path)
  # @logger.level = Logger::WARN
  @logger
end

#override_timestamp(timestamp) ⇒ Object

useful so we can test hooks



55
56
57
# File 'lib/chap/config.rb', line 55

def override_timestamp(timestamp)
  @timestamp = timestamp
end

#release_pathObject

special attributes added to chap



64
65
66
# File 'lib/chap/config.rb', line 64

def release_path
  "#{deploy_to}/releases/#{timestamp}"
end

#run(cmd) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/chap/config.rb', line 115

def run(cmd)
  log "Running: #{cmd}"
  cmd = "#{cmd} 2>&1" unless cmd.include?(" > ")
  out = `#{cmd}`
  log out
  raise "DeployError" if $?.exitstatus > 0
end

#shared_pathObject



72
73
74
# File 'lib/chap/config.rb', line 72

def shared_path
  "#{deploy_to}/shared"
end

#strategyObject



90
91
92
# File 'lib/chap/config.rb', line 90

def strategy
  chap[:strategy] || 'checkout'
end

#timestampObject



50
51
52
# File 'lib/chap/config.rb', line 50

def timestamp
  @timestamp ||= Time.now.strftime("%Y%m%d%H%M%S")
end

#yamlObject



10
11
12
13
14
15
16
17
18
# File 'lib/chap/config.rb', line 10

def yaml
  path = options[:config]
  if File.exist?(path)
    @yaml ||= Mash.from_hash(YAML.load(IO.read(path)))
  else
    puts "ERROR: chap.yaml config does not exist at: #{path}"
    exit 1
  end
end