Class: Docman::DocrootConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/docman/docroot_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(docroot_dir, deploy_target_name = nil, options = nil, state = nil) ⇒ DocrootConfig

Returns a new instance of DocrootConfig.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/docman/docroot_config.rb', line 11

def initialize(docroot_dir, deploy_target_name = nil, options = nil, state = nil)
  @state = state

  @override = {}
  if options && options['config']
    @override = JSON.parse(options['config'])
  end
  @docroot_dir = docroot_dir
  #@deploy_target = deploy_target
  @docroot_config_dir = File.join(docroot_dir, 'config')

  Dir.chdir @docroot_config_dir
  unless options.key? :config_dir
    update('origin')
  end
  config_files = Docman::Application.instance.config_dirs(options).collect{|item|
    File.join(@docroot_config_dir, item, 'config.{yaml,yml}')
  }
  config_file_path = Dir.glob(config_files).first

  raise "Configuration file config.{yaml,yml} not found." if config_file_path.nil?

  @config_dir = File.dirname(config_file_path)
  @config_file = File.basename(config_file_path)

  Docman::Application.instance.config.merge_config_from_file(docroot_dir, @config_dir, @config_file, options)

  if deploy_target_name
    @deploy_target = Application.instance.config['deploy_targets'][deploy_target_name]
    raise "Wrong deploy target: #{deploy_target_name}" if @deploy_target.nil?
    @deploy_target['name'] = deploy_target_name
  end

  @names = {}
  @raw_infos = {}
  master_file = File.join(@docroot_config_dir, 'master')
  if File.directory? master_file
    @structure = structure_build(File.join(@docroot_config_dir, 'master'))
  else
    @structure = structure_build_from_config_file(Docman::Application.instance.config)
  end
end

Instance Attribute Details

#config_dirObject (readonly)

Returns the value of attribute config_dir.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def config_dir
  @config_dir
end

#deploy_targetObject (readonly)

Returns the value of attribute deploy_target.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def deploy_target
  @deploy_target
end

#docroot_config_dirObject (readonly)

Returns the value of attribute docroot_config_dir.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def docroot_config_dir
  @docroot_config_dir
end

#docroot_dirObject (readonly)

Returns the value of attribute docroot_dir.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def docroot_dir
  @docroot_dir
end

#raw_infosObject (readonly)

Returns the value of attribute raw_infos.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def raw_infos
  @raw_infos
end

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def root
  @root
end

#structureObject (readonly)

Returns the value of attribute structure.



9
10
11
# File 'lib/docman/docroot_config.rb', line 9

def structure
  @structure
end

Instance Method Details

#chain(info) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/docman/docroot_config.rb', line 206

def chain(info)
  chain = {}
  chain[info['name']] = info
  while info['parent'] do
    chain[info['parent']['name']] = info['parent']
    info = info['parent']
  end
  Hash[chain.to_a.reverse!]
end

#config_hashObject



241
242
243
# File 'lib/docman/docroot_config.rb', line 241

def config_hash
  Digest::MD5.hexdigest(Marshal::dump(@raw_infos))
end

#deploy_target_nameObject



245
246
247
# File 'lib/docman/docroot_config.rb', line 245

def deploy_target_name
  @deploy_target.name
end

#info_by(name) ⇒ Object



216
217
218
# File 'lib/docman/docroot_config.rb', line 216

def info_by(name)
  @names[name]
end

#override_from_environment(info) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/docman/docroot_config.rb', line 158

def override_from_environment(info)

  docman_config_var = "DOCMAN_CONFIG"
  if ENV.has_key? docman_config_var and ENV[docman_config_var].length > 0
    puts "Variable #{docman_config_var} => #{ENV[docman_config_var]}"
    docman_config_overrides = JSON.parse(ENV[docman_config_var])
    if docman_config_overrides['projects'] && docman_config_overrides['projects'].key?(info['name'])
      info.merge! docman_config_overrides['projects'][info['name']]
    end
  else
    puts "Variable #{docman_config_var} not found."
  end

  docman_repo_var = "COMPONENT_#{info['name'].upcase}_REPO"
  if ENV.has_key? docman_repo_var and ENV[docman_repo_var].length > 0
    puts "Variable #{docman_repo_var} => #{ENV[docman_repo_var]}"
    info['repo'] = ENV[docman_repo_var]
  else
    puts "Variable #{docman_repo_var} not found."
  end

  docman_order_var = "COMPONENT_#{info['name'].upcase}_ORDER"
  if ENV.has_key? docman_order_var and ENV[docman_tag_var].length > 0
    puts "Variable #{docman_order_var} => #{ENV[docman_order_var]}"
    info['order'] = ENV[docman_order_var]
  else
    puts "Variable #{docman_order_var} not found."
  end

  docman_version_var = "COMPONENT_#{info['name'].upcase}_VERSION"
  if ENV.has_key? docman_version_var and ENV[docman_version_var].length > 0
    puts "Variable #{docman_version_var} => #{ENV[docman_version_var]}"
    if @state.nil?
      info['states'].each do |k,v|
        info['states'][k]['type'] = "branch"
        info['states'][k]['version'] = ENV[docman_version_var]
      end
    else
      info['states'][@state]['type'] = "branch"
      info['states'][@state]['version'] = ENV[docman_version_var]
    end
  else
    puts "Variable #{docman_version_var} not found."
  end

  info
end

#project(name) ⇒ Object



220
221
222
223
# File 'lib/docman/docroot_config.rb', line 220

def project(name)
  raise "There is no project with name '#{name}' exists in config" unless @names.has_key? name
  @names[name]
end

#states_dependin_on(name, version) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/docman/docroot_config.rb', line 225

def states_dependin_on(name, version)
  states = {}
  master_project = project(name)
  if master_project.has_key?('states_project')
    states_project = project(master_project['states_project'])
    states_project.states.each do |state, info|
      states[state] = info if info['version'] == version
    end
  else
    master_project.states.each do |state, info|
      states[state] = info if info['version'] == version
    end
  end
  states
end

#structure_build(path, prefix = '', parent = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/docman/docroot_config.rb', line 111

def structure_build(path, prefix = '', parent = nil)
  return unless File.file? File.join(path, 'info.yaml')

  children = []
  info = YAML::load_file(File.join(path, 'info.yaml'))
  @raw_infos[File.basename path] = YAML::load_file(File.join(path, 'info.yaml'))
  unless info['status'].nil?
    return if info['status'] == 'disabled'
  end
  name = File.basename path
  prefix = prefix.size > 0 ? File.join(prefix, name) : name
  info['full_path'] = path
  info['docroot_config'] = self
  info['build_path'] = prefix
  info['full_build_path'] = File.join(@docroot_dir, prefix)
  info['temp_path'] = File.join(@docroot_dir, '.docman/tmp', info['build_path'])
  info['states_path'] = File.join(@docroot_dir, '.docman/states', info['build_path'])
  info['name'] = name
  info['parent'] = parent
  info['order'] = info.has_key?('order') ? info['order'] : 10
  info['children'] = children

  if @override['projects'] && @override['projects'].key?(info['name'])
    info.merge! @override['projects'][info['name']]
  end

  info = override_from_environment(info)

  i = Docman::Info.new(info)
  @root = i if parent.nil?
  i['root'] = @root

  @names[name.to_s] = i

  Dir.foreach(path) do |entry|
    next if (entry == '..' || entry == '.')
    full_path = File.join(path, entry)
    if File.directory?(full_path)
      dir_hash = structure_build(full_path, prefix, i)
      unless dir_hash == nil
        children << dir_hash
      end
    end
  end
  i
end

#structure_build_from_config_file(config, prefix = '', parent = nil, parent_key = 'master') ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/docman/docroot_config.rb', line 61

def structure_build_from_config_file(config, prefix = '', parent = nil, parent_key = 'master')
  return if config['components'][parent_key].nil?
  children = []

  info = config['components'][parent_key]

  children_components_config = nil
  unless info['components'].nil?
    children_components_config = {'components' => info.delete('components')}
  end

  @raw_infos[parent_key] = info

  unless info['status'].nil?
    return if info['status'] == 'disabled'
  end

  name = parent_key
  prefix = prefix.size > 0 ? File.join(prefix, name) : name
  info['full_path'] = @docroot_config_dir
  info['docroot_config'] = self
  info['build_path'] = prefix
  info['full_build_path'] = File.join(@docroot_dir, prefix)
  info['temp_path'] = File.join(@docroot_dir, '.docman/tmp', info['build_path'])
  info['states_path'] = File.join(@docroot_dir, '.docman/states', info['build_path'])
  info['name'] = name
  info['parent'] = parent
  info['order'] = info.has_key?('order') ? info['order'] : 10
  info['children'] = children

  if @override['projects'] && @override['projects'].key?(info['name'])
    info.merge! @override['projects'][info['name']]
  end

  info = override_from_environment(info)

  i = Docman::Info.new(info)
  @root = i if parent.nil?
  i['root'] = @root

  @names[name.to_s] = i

  unless children_components_config.nil?
    children_components_config['components'].each {|child_name, child_config|
      children << structure_build_from_config_file(children_components_config, prefix, i, child_name)
    }
  end
  i
end

#update(options = '') ⇒ Object



54
55
56
57
58
59
# File 'lib/docman/docroot_config.rb', line 54

def update(options = '')
  Dir.chdir @docroot_config_dir
  GitUtil.exec("reset --hard", false)
  branch = GitUtil.branch
  GitUtil.update @docroot_config_dir, "#{options} #{branch.strip}"
end