Class: WhiskeyDisk::Config

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

Class Method Summary collapse

Class Method Details

.add_environment_scoping(data) ⇒ Object



116
117
118
119
# File 'lib/whiskey_disk/config.rb', line 116

def add_environment_scoping(data)
  return data unless needs_environment_scoping?(data)
  { environment_name => data }
end

.add_project_scoping(data) ⇒ Object



121
122
123
124
125
# File 'lib/whiskey_disk/config.rb', line 121

def add_project_scoping(data)
  return data unless needs_project_scoping?(data)
  override_project_name!(data)
  { project_name => data }
end

.base_pathObject



56
57
58
59
# File 'lib/whiskey_disk/config.rb', line 56

def base_path
  return path if path
  find_rakefile_from_current_path
end

.check_duplicates(project, target, domain_list) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/whiskey_disk/config.rb', line 151

def check_duplicates(project, target, domain_list)
  seen = {}
  domain_list.each do |domain|
    raise "duplicate domain [#{domain[:name]}] in configuration file for project [#{project}], target [#{target}]" if seen[domain[:name]]
    seen[domain[:name]] = true
  end
end

.check_staleness?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/whiskey_disk/config.rb', line 28

def check_staleness?
  !!(ENV['check'] && ENV['check'] =~ /^(?:t(?:rue)?|y(?:es)?|1)$/)
end

.compact_list(list) ⇒ Object



131
132
133
# File 'lib/whiskey_disk/config.rb', line 131

def compact_list(list)
  [ list ].flatten.delete_if { |d| d.nil? or d == '' }
end

.configuration_dataObject



92
93
94
# File 'lib/whiskey_disk/config.rb', line 92

def configuration_data
  open(configuration_file) {|f| f.read }
end

.configuration_fileObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/whiskey_disk/config.rb', line 71

def configuration_file
  return path if valid_path?(path)
  
  files = []

  files += [
    File.join(base_path, 'deploy', specified_project_name, "#{environment_name}.yml"),  # /deploy/foo/staging.yml
    File.join(base_path, 'deploy', "#{specified_project_name}.yml") # /deploy/foo.yml
  ] if specified_project_name

  files += [
    File.join(base_path, 'deploy', "#{environment_name}.yml"),  # /deploy/staging.yml
    File.join(base_path, "#{environment_name}.yml"), # /staging.yml
    File.join(base_path, 'deploy.yml') # /deploy.yml
  ]

  files.each { |file|  return file if File.exists?(file) }

  raise "Could not locate configuration file in path [#{base_path}]"
end

.contains_rakefile?(path) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/whiskey_disk/config.rb', line 41

def contains_rakefile?(path)
  File.exists?(File.expand_path(File.join(path, 'Rakefile')))
end

.debug?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/whiskey_disk/config.rb', line 32

def debug?
  !!(ENV['debug'] && ENV['debug'] =~ /^(?:t(?:rue)?|y(?:es)?|1)$/)
end

.domain_limitObject



36
37
38
39
# File 'lib/whiskey_disk/config.rb', line 36

def domain_limit
  return false unless ENV['only'] and ENV['only'] != ''
  ENV['only']
end

.environment_nameObject



8
9
10
11
12
# File 'lib/whiskey_disk/config.rb', line 8

def environment_name
  return false unless (ENV['to'] && ENV['to'] != '')
  return ENV['to'] unless ENV['to'] =~ /:/
  ENV['to'].split(/:/)[1]
end

.fetchObject



190
191
192
193
# File 'lib/whiskey_disk/config.rb', line 190

def fetch
  raise "Cannot determine current environment -- try rake ... to=staging, for example." unless environment_name
  filter_data(load_data)
end

.filter_data(data) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/whiskey_disk/config.rb', line 177

def filter_data(data)
  current = data[project_name][environment_name] rescue nil
  raise "No configuration file defined data for project `#{project_name}`, environment `#{environment_name}`" unless current

  current.merge!({
    'environment' => environment_name,
    'project' => project_name,
  })
  
  current['config_target'] ||= environment_name
  current
end

.find_rakefile_from_current_pathObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/whiskey_disk/config.rb', line 45

def find_rakefile_from_current_path
  original_path = Dir.pwd
  while (!contains_rakefile?(Dir.pwd))
    return File.join(original_path, 'config') if Dir.pwd == '/'
    Dir.chdir('..')
  end
  File.join(Dir.pwd, 'config')
ensure
  Dir.chdir(original_path)
end

.load_dataObject



171
172
173
174
175
# File 'lib/whiskey_disk/config.rb', line 171

def load_data
  normalize_data(YAML.load(configuration_data))
rescue Exception => e
  raise %Q{Error reading configuration file [#{configuration_file}]: "#{e}"}
end

.localize_domain_list(list) ⇒ Object



127
128
129
# File 'lib/whiskey_disk/config.rb', line 127

def localize_domain_list(list)
  [ list ].flatten.collect { |d| (d.nil? or d == '') ? 'local' : d }
end

.needs_environment_scoping?(data) ⇒ Boolean

is this data hash a bottom-level data hash without an environment name?

Returns:

  • (Boolean)


107
108
109
# File 'lib/whiskey_disk/config.rb', line 107

def needs_environment_scoping?(data)
  repository_depth(data) == 0
end

.needs_project_scoping?(data) ⇒ Boolean

is this data hash an environment data hash without a project name?

Returns:

  • (Boolean)


112
113
114
# File 'lib/whiskey_disk/config.rb', line 112

def needs_project_scoping?(data)
  repository_depth(data) == 1
end

.normalize_data(data) ⇒ Object



167
168
169
# File 'lib/whiskey_disk/config.rb', line 167

def normalize_data(data)
  normalize_domains(add_project_scoping(add_environment_scoping(data.clone)))
end

.normalize_domain(data) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/whiskey_disk/config.rb', line 135

def normalize_domain(data)
  compacted = localize_domain_list(data)
  compacted = [ 'local' ] if compacted.empty?
  
  compacted.collect do |d|
    if d.respond_to?(:keys)
      row = { :name => (d['name'] || d[:name]) }
      roles = compact_list(d['roles'] || d[:roles])
      row[:roles] = roles unless roles.empty?
      row
    else
      { :name => d }
    end
  end
end

.normalize_domains(data) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/whiskey_disk/config.rb', line 159

def normalize_domains(data)
  data.each_pair do |project, project_data|
    project_data.each_pair do |target, target_data|
      target_data['domain'] = check_duplicates(project, target, normalize_domain(target_data['domain']))
    end
  end
end

.override_project_name!(data) ⇒ Object



19
20
21
22
# File 'lib/whiskey_disk/config.rb', line 19

def override_project_name!(data)
  return if ENV['to'] && ENV['to'] =~ /:/
  ENV['to'] = data[environment_name]['project'] + ':' + ENV['to'] if data[environment_name]['project']
end

.pathObject



24
25
26
# File 'lib/whiskey_disk/config.rb', line 24

def path
  (ENV['path'] && ENV['path'] != '') ? ENV['path'] : false
end

.project_nameObject



96
97
98
# File 'lib/whiskey_disk/config.rb', line 96

def project_name
  specified_project_name || 'unnamed_project'
end

.repository_depth(data, depth = 0) ⇒ Object



100
101
102
103
104
# File 'lib/whiskey_disk/config.rb', line 100

def repository_depth(data, depth = 0)
  raise 'no repository found' unless data.respond_to?(:has_key?)
  return depth if data.has_key?('repository')
  repository_depth(data.values.first, depth + 1)
end

.specified_project_nameObject



14
15
16
17
# File 'lib/whiskey_disk/config.rb', line 14

def specified_project_name
  return false unless (ENV['to'] && ENV['to'] =~ /:/)
  ENV['to'].split(/:/).first
end

.valid_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/whiskey_disk/config.rb', line 61

def valid_path?(path)
  if path
    uri = URI.parse(path)
    return path if uri.scheme
    return path if File.file?(path)
  end
  
  false
end