Module: Processor

Included in:
ErbTemplate, Template
Defined in:
lib/depengine/processor/zip.rb,
lib/depengine/processor/tags.rb,
lib/depengine/processor/fileops.rb,
lib/depengine/processor/template.rb,
lib/depengine/processor/properties.rb,
lib/depengine/processor/erb_template.rb,
lib/depengine/processor/local_execute.rb

Defined Under Namespace

Classes: ErbTemplate, Properties, Template

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_filelist(list, target) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/depengine/processor/fileops.rb', line 71

def check_filelist(list, target)
  CSV.foreach(list) do |file|
    if not file.nil? and not file.first.nil?
      $log.writer.debug "Checking file #{File.join(target, file.first)}"
      unless File.exist?(File.join(target, file.first))
        $log.writer.error "Necessary file not found: #{file}"
        exit 1
      end
    end
  end
end

.chmod(file, mode, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/depengine/processor/fileops.rb', line 31

def chmod(file, mode, options = {})
  if not options[:recursive].nil? and options[:recursive] == true
    FileUtils.chmod_R(mode, file)
  else
    FileUtils.chmod(mode, file)
  end
  $log.writer.debug "Chmod #{file} to #{mode}"
rescue => e
  $log.writer.error "Can not change mode on file #{file}"
  $log.writer.error e.message
  exit 1
end

.copy(source, target, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/depengine/processor/fileops.rb', line 2

def copy(source, target, options = {})
  if not options[:match].nil? and File.directory? source
    Dir.glob(File.join(source, '*')) do |entry|
      if File.fnmatch(options[:match], entry)
        $log.writer.debug "Copy #{entry} to #{target}"
        FileUtils.cp_r(entry, target)
      end
    end
  else
    $log.writer.debug "Copy #{source} to #{target}"
    FileUtils.cp_r(source, target)
  end
rescue => e
  $log.writer.error "Can not copy #{source} to #{target}"
  $log.writer.error e.message
  exit 1
end

.local_execute(shell_cmds) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/depengine/processor/local_execute.rb', line 2

def local_execute(shell_cmds)
  outputs = []
  shell_cmds.each do |shell_cmd|
    outputs.push shell_cmd
    $log.writer.debug "executing: #{shell_cmd}"
    output = ` /bin/bash --login -c \"#{shell_cmd}\" `
    outputs.push output
    if $CHILD_STATUS.exitstatus == 0
      $log.writer.debug output
    else
      $log.writer.error output
      exit 1
    end
  end
  outputs
end

.mkdir(directory) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/depengine/processor/fileops.rb', line 21

def mkdir(directory)
  FileUtils.mkdir_p(directory)
  $log.writer.debug "Create directory #{directory}"
rescue => e
  $log.writer.error "Can not create directory #{directory}"
  $log.writer.error e.message
  exit 1
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/depengine/processor/fileops.rb', line 55

def mklink(source, target)
  unless File.exist? source
    $log.writer.debug \
      'Create link to nowhere, this deployment is broken by design!'
  end
  begin
    $log.writer.debug "Create link from #{source} to #{target}"
    FileUtils.ln_s(source, target)
  rescue => e
    $log.writer.error "Can not create link #{target}"
    $log.writer.error e.message
    exit 1
  end
end

.remove(file) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/depengine/processor/fileops.rb', line 45

def remove(file)
  FileUtils.rm_rf(file)
  $log.writer.debug "Delete #{file}"
rescue => e
  $log.writer.error "Can not delete file #{file}"
  $log.writer.error e.message
  exit 1
end

.tgz_file(tgz_file, source_dir, add_file = nil, tar_bin = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/depengine/processor/zip.rb', line 26

def tgz_file(tgz_file, source_dir, add_file = nil, tar_bin = nil)
  tar = tar_bin || 'tar'

  unless File.exist? source_dir
    $log.writer.error "Source directory #{source_dir} does not exist!"
    exit 1
  end

  begin
    stdout = `#{tar} -c -z -C #{source_dir} -f #{tgz_file} #{add_file}`
    $log.writer.debug stdout
  rescue => e
    $log.writer.error "Can not add files in #{source_dir} to Tar-Gz file #{tgz_file}"
    $log.writer.error e.message
    exit 1
  end
end

.untgz_file(tgz_file, target_dir, extract_file = nil, tar_bin = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/depengine/processor/zip.rb', line 45

def untgz_file(tgz_file, target_dir, extract_file = nil, tar_bin = nil)
  tar = tar_bin || 'tar'

  unless File.exist? tgz_file
    $log.writer.error "Tar-Gz file #{tgz_file} does not exist!"
    exit 1
  end

  unless File.exist? target_dir
    FileUtils.mkdir(target_dir)
    $log.writer.debug "Create target directory #{target_dir}"
  end

  begin
    stdout = `#{tar} -x -z -C #{target_dir} -f #{tgz_file} #{extract_file}`
    $log.writer.debug stdout
  rescue => e
    $log.writer.error "Can not extract Tar-Gz file #{tgz_file} to #{target_dir}"
    $log.writer.error e.message
    exit 1
  end
end

.unzip_file(zip_file, target_dir, extract_file = nil, unzip_bin = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/depengine/processor/zip.rb', line 2

def unzip_file(zip_file, target_dir, extract_file = nil, unzip_bin = nil)
  unzip = unzip_bin || 'unzip'

  unless File.exist? zip_file
    $log.writer.error "Zip file #{zip_file} does not exist!"
    exit 1
  end

  unless File.exist? target_dir
    FileUtils.mkdir(target_dir)
    $log.writer.debug "Create target directory #{target_dir}"
  end

  begin
    stdout = `#{unzip} -o \"#{zip_file}\" #{extract_file} -d \"#{target_dir}\"`
    $log.writer.debug stdout
  rescue => e
    $log.writer.error "Can not extract Zipfile #{zip_file} to #{target_dir}"
    $log.writer.error e.message
    exit 1
  end
end

Instance Method Details

#load_tags(content, cdb) ⇒ Object



2
3
4
5
6
7
8
9
10
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
53
54
55
56
# File 'lib/depengine/processor/tags.rb', line 2

def load_tags(content, cdb)
  begin
    context = Radius::Context.new do |c|
      # map all parameters from 'content' to a tag with the same name
      content.each do |content_param|
        c.define_tag "#{content_param.first}" do |_tag|
          tag = content_param.last # rubocop:disable Lint/UselessAssignment
        end
      end

      # include external file
      c.define_tag 'include' do |tag|
        result   = ''
        ext_file = File.join(ENV['WORKSPACE'], tag.attr['file'])
        if File.exist?(ext_file)
          result = File.open(ext_file).read
        else
          puts 'Warning: can not read static data ' + ext_file
        end
        result
      end

      c.define_tag 'cdb' do |tag|
        result = ''
        if not cdb.nil? or cdb[tag.attr['key']].nil?
          result = cdb[tag.attr['key']]
        else
          puts "Warning: CDB not set for Template-Engine in tag 'cdb'"
        end
        result
      end

      c.define_tag 'feature_flag' do |tag|
        result = ''
        if not cdb.nil?
          feature = tag.attr['name']
          result  = cdb[cdb[feature]]
        else
          puts "Warning: CDB not set for Template-Engine in tag 'feature_flag'"
        end
        result
      end

      c.define_tag 'echo' do |tag|
        tag.expand
      end
    end
  rescue => e
    $log.writer.error "Error in tag definition: #{e.message}"
    $log.writer.error e.backtrace
    exit 1
  end

  context
end