Class: Docman::Deployers::Deployer

Inherits:
Command
  • Object
show all
Defined in:
lib/docman/deployers/deployer.rb

Direct Known Subclasses

CommonDeployer, GitDeployer

Constant Summary collapse

@@deployers =
{}

Instance Attribute Summary

Attributes inherited from Command

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#add_action, #add_actions, #perform, #prefix, register_command, #replace_placeholder, #run_actions, #run_with_hooks

Methods included from Logging

#log, logger, #logger, #prefix, #properties_info, #with_logging

Constructor Details

#initialize(params, context = nil, caller = nil, type = nil) ⇒ Deployer

Returns a new instance of Deployer.



28
29
30
31
32
33
34
# File 'lib/docman/deployers/deployer.rb', line 28

def initialize(params, context = nil, caller = nil, type = nil)
  super(params, context, caller, type)
  @docroot_config = caller.docroot_config
  @builded = []
  @build_results = {}
  @versions = {}
end

Class Method Details

.create(params, context = nil, caller = nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/docman/deployers/deployer.rb', line 15

def self.create(params, context = nil, caller = nil)
  c = @@deployers[params['handler']]
  if c
    c.new(params, context, caller, 'deployer')
  else
    raise "Bad deployer type: #{type}"
  end
end

.register_deployer(name) ⇒ Object



24
25
26
# File 'lib/docman/deployers/deployer.rb', line 24

def self.register_deployer(name)
  @@deployers[name] = self
end

Instance Method Details

#buildObject



132
133
134
135
136
137
138
# File 'lib/docman/deployers/deployer.rb', line 132

def build
  if self['name'].nil?
    build_recursive
  else
    build_dir_chain(@docroot_config.info_by(self['name']))
  end
end

#build_dir(info) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/docman/deployers/deployer.rb', line 161

def build_dir(info)
  return if @builded.include? info['name']
  info.state_name = self['state']
  builder = Docman::Builders::Builder.create(self['builders'][info['type']], info, self)
  build_result = builder.perform
  logger.info '-------------------------------------------------------'
  @changed = true if build_result
  @build_results[info['name']] = build_result ? build_result : 'Not builded'
  @versions[info['name']] = builder.version
  @builded << info['name']
end

#build_dir_chain(info) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/docman/deployers/deployer.rb', line 140

def build_dir_chain(info)
  @docroot_config.chain(info).values.each do |item|
    item.state_name = self['state']
    if item.need_rebuild?
      build_recursive(item)
      return
    elsif
    build_dir(item)
    end
  end
end

#build_recursive(info = nil) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/docman/deployers/deployer.rb', line 152

def build_recursive(info = nil)
  info = info ? info : @docroot_config.structure
  build_dir(info)
  info['children'].sort_by!{|a| a['order']}
  info['children'].each do |child|
    build_recursive(child)
  end
end

#configObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/docman/deployers/deployer.rb', line 36

def config
  unless self['name'].nil?
    @docroot_config.chain(@docroot_config.info_by(self['name'])).values.each do |info|
      add_actions(info, info)
    end
  else
    # add_actions()
  end

  path = @docroot_config.root['full_build_path']
  if File.directory?(path) and GitUtil.repo?(path)
    Dir.chdir path
    if GitUtil.repo_changed? path
      GitUtil.reset_repo path
    end
  end

  stored_config_hash = read_version_file_param('config_hash')
  @config_hash = Docman::Application.instance.config.config_hash
  @config_yaml = Docman::Application.instance.config.unmutable_config.to_yaml

  #TODO: need to refactor
  #TODO: need to check changes in config repo too
  stored_docroot_config_hash = read_version_file_param('docroot_config_hash')
  @docroot_config_hash = @docroot_config.config_hash
  @docroot_config_yaml = @docroot_config.raw_infos.to_yaml
  if stored_config_hash != @config_hash
    log 'Forced rebuild as configuration was changed', 'info'
    filename = File.join(@docroot_config.root['full_build_path'], 'config.yaml')
    log Diffy::Diff.new(read_file(filename), @config_yaml) if File.file? filename
    Docman::Application.instance.force = true
  end
  if stored_docroot_config_hash != @docroot_config_hash
    log 'Forced rebuild as docroot configuration was changed', 'info'
    filename = File.join(@docroot_config.root['full_build_path'], 'docroot_config.yaml')
    log Diffy::Diff.new(read_file(filename), @docroot_config_yaml) if File.file? filename
    Docman::Application.instance.force = true
  end
end

#deployObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/docman/deployers/deployer.rb', line 86

def deploy
  filename = 'version.yaml'
  path = File.join(@docroot_config.root['full_build_path'], filename)
  version = SecureRandom.hex
  write_version_file version, path
  write_config_file @config_yaml, File.join(@docroot_config.root['full_build_path'], 'config.yaml')
  write_config_file @docroot_config_yaml, File.join(@docroot_config.root['full_build_path'], 'docroot_config.yaml')
  run_with_hooks('push')
  raise 'Files are not deployed' unless files_deployed? version, filename
end

#describe(type = 'short') ⇒ Object

TODO: need to refactor.



174
175
176
# File 'lib/docman/deployers/deployer.rb', line 174

def describe(type = 'short')
  properties_info(['handler'])
end

#executeObject



76
77
78
79
80
81
82
83
84
# File 'lib/docman/deployers/deployer.rb', line 76

def execute
  run_with_hooks('build')
  if @changed
    run_with_hooks('deploy')
  else
    log 'No changes in docroot', 'info'
  end
  log "Deploy results:\n" + @build_results.to_yaml
end

#files_deployed?(version, filename) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# File 'lib/docman/deployers/deployer.rb', line 97

def files_deployed?(version, filename)
  return true unless self['environment'].has_key? 'target_checker'
  params = self['environment']['target_checker']
  params['version'] = version
  params['filename'] = filename
  Docman::TargetChecker.create(params, self).perform
end

#read_file(path) ⇒ Object



105
106
107
108
109
110
# File 'lib/docman/deployers/deployer.rb', line 105

def read_file(path)
  YAML::load_file(path)
rescue
  log "Error in config file #{path}"
  return false
end

#read_version_file_param(param) ⇒ Object



112
113
114
115
116
117
# File 'lib/docman/deployers/deployer.rb', line 112

def read_version_file_param(param)
  path = File.join(@docroot_config.root['full_build_path'], 'version.yaml')
  return false unless File.file?(path)
  content = read_file(path)
  content[param] if content.has_key? param
end

#write_config_file(config, path) ⇒ Object



128
129
130
# File 'lib/docman/deployers/deployer.rb', line 128

def write_config_file(config, path)
  File.open(path, 'w') {|f| f.write config}
end

#write_version_file(version, path) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/docman/deployers/deployer.rb', line 119

def write_version_file(version, path)
  to_write = Hash.new
  to_write['random'] = version
  to_write['config_hash'] = @config_hash
  to_write['docroot_config_hash'] = @docroot_config_hash
  to_write.deep_merge! @versions
  File.open(path, 'w') {|f| f.write to_write.to_yaml}
end