Class: Kikubari::Deploy::Deployer

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

Direct Known Subclasses

GitDeployer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Deployer

Returns a new instance of Deployer.



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

def initialize(config)
  @config = config
  @logger = Logger.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



17
18
19
# File 'lib/deployer/deployer.rb', line 17

def config
  @config
end

Instance Method Details

#after_deploy_runObject

Run comand line script after deploy



120
121
122
123
124
125
126
127
128
# File 'lib/deployer/deployer.rb', line 120

def after_deploy_run
  return unless has_after_deploy_run_commands
  out = Array.new
  @logger.head "Executing After Deploy"
  @config.after.run.each do |run_task|
    out.push(execute_shell(run_task) )
  end
  out
end

#before_deploy_runObject



134
135
136
137
138
139
140
141
142
# File 'lib/deployer/deployer.rb', line 134

def before_deploy_run
  return unless has_before_deploy_run_commands
  out = Array.new
  @logger.head "Executing Before Deploy"
  @config.before.run.each do |run_task|
    out.push(execute_shell(run_task) )
  end
  out
end

#capture_stderr(command) ⇒ Object



153
154
155
156
# File 'lib/deployer/deployer.rb', line 153

def capture_stderr ( command )
 stdin, stdout, stderr = Open3.popen3( command )
 ({ :stdout => stdout.readlines.join(""), :stderr => stderr.readlines.join("") })
end

Create the current symlink to the deploy version folder



48
49
50
51
52
# File 'lib/deployer/deployer.rb', line 48

def create_current_symlink_folder
  FileUtils.rm_f(@config.current_deploy_folder) if File.symlink?(@config.current_deploy_folder)
  FileUtils.ln_s @config.env_time_folder, @config.current_deploy_folder
  self
end

#create_deploy_structureObject

Create deployment structure



100
101
102
103
# File 'lib/deployer/deployer.rb', line 100

def create_deploy_structure
  create_structure if @config.do.folder_structure
  self
end

#create_release_folderObject

Create the folder where you will deploy the actual version of the code based on the configuration



40
41
42
43
# File 'lib/deployer/deployer.rb', line 40

def create_release_folder
  FileUtils.mkdir_p(@config.env_time_folder) unless File.directory? @config.env_time_folder
  self
end

#create_structureObject

Create te the folder structure as is in the YAML



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

def create_structure
  @config.do.folder_structure.each_pair do |folder, target_folder|
    unless !target_folder.empty? && File.directory?(@config.deploy_folder.join(folder.to_s))
      @logger.print "Creating Structure folder: #{folder}"
      FileUtils.mkdir_p @config.deploy_folder.join(folder.to_s)
    end
  end
  self
end

#create_sylinked_foldersObject

Create the Symlinked folders



70
71
72
73
74
75
76
77
78
# File 'lib/deployer/deployer.rb', line 70

def create_sylinked_folders
  @config.do.folder_structure.each_pair do |folder, target_folder|
    next if target_folder.empty?
    @logger.print "- linking: #{@config.env_time_folder}/#{target_folder}"
    raise "Folder: #{@config.env_time_folder.join(target_folder.to_s)} already exists and the symlink can't be created" if File.directory?(@config.env_time_folder.join(target_folder.to_s))
    create_symlink( @config.deploy_folder.join(folder.to_s), @config.env_time_folder.join(target_folder.to_s) )
  end
  self
end

Execute creation of symlinked folder

Raises:

  • (ArgumentError)


83
84
85
86
# File 'lib/deployer/deployer.rb', line 83

def create_symlink( folder, target_folder )
  raise ArgumentError , "Origin folder #{folder} is not a valid folder" unless File.directory?(folder)
  FileUtils.ln_s folder, target_folder
end

#create_symlinked_filesObject



89
90
91
92
93
94
# File 'lib/deployer/deployer.rb', line 89

def create_symlinked_files
  @config.do.link_files.each_pair do |source, target|
    @logger.print "- linking: #{@config.env_time_folder.join(target.to_s)}"
    FileUtils.ln_s(@config.deploy_folder.join(source.to_s), @config.env_time_folder.join(target.to_s))
  end
end

#deployObject

Execute the deploy



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

def deploy
    before_deploy_run
  @logger.head "Executing Deploy"
  test_files  if @config.do.test_files
    do_deploy
  create_sylinked_folders
  create_symlinked_files if @config.do.link_files
  create_current_symlink_folder
  rotate_folders
    after_deploy_run
  self
end

#execute_shell(command) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/deployer/deployer.rb', line 144

def execute_shell(command)
  @logger.run(command, @config.env_time_folder)
  temp = capture_stderr "cd #{@config.env_time_folder.to_s.strip} && #{command} "
  @logger.result(temp[:stdout]) if temp[:stdout] != ""
  @logger.error(temp[:stderr]) if temp[:stderr] != ""
  temp
end

#has_after_deploy_run_commandsObject



113
114
115
# File 'lib/deployer/deployer.rb', line 113

def has_after_deploy_run_commands
  @config.after && @config.after.run && !@config.after.run.empty?
end

#has_before_deploy_run_commandsObject



130
131
132
# File 'lib/deployer/deployer.rb', line 130

def has_before_deploy_run_commands
  @config.before && @config.before.run && !@config.before.run.empty?
end

#rotate_foldersObject

Rotate old version folders



108
109
110
# File 'lib/deployer/deployer.rb', line 108

def rotate_folders
  DeployDir.rotate_folders( @config.environment_folder , @config.config.history_limit )
end

#test_filesObject

Test if selected file already exist



58
59
60
61
62
63
64
65
# File 'lib/deployer/deployer.rb', line 58

def test_files
  @config.do.link_files.each_pair do |source, target|
    unless File.exist? @config.deploy_folder.join(source.to_s)
      raise ArgumentError, "Please verify this file exist: #{@config.deploy_folder.join(source.to_s)}"
    end
  end
  self
end