Module: Processor

Included in:
Erb_template, 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

Defined Under Namespace

Classes: Erb_template, Properties, Template

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_filelist(list, target) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/depengine/processor/fileops.rb', line 79

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)}"
      if not File.exists?( File.join(target, file.first) )
        $log.writer.error "Necessary file not found: #{file}"
        exit 1
      end
    end
  end
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/depengine/processor/fileops.rb', line 35

def chmod(file, mode, options={})
  begin
    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 Exception => e
    $log.writer.error "Can not change mode on file #{file}"
    $log.writer.error e.message
    exit 1
  end
end

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



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

def copy(source, target, options={})
  begin
    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 Exception => e
    $log.writer.error "Can not copy #{source} to #{target}"
    $log.writer.error e.message
    exit 1
  end
end

.mkdir(directory) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/depengine/processor/fileops.rb', line 23

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


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/depengine/processor/fileops.rb', line 63

def mklink(source, target)
  if not File.exists? 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 Exception => e
    $log.writer.error "Can not create link #{target}"
    $log.writer.error e.message
    exit 1
  end
end

.remove(file) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/depengine/processor/fileops.rb', line 51

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

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



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

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

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

  begin
    stdout = %x"#{tar} -c -z -C #{source_dir} -f #{tgz_file} #{add_file}"
    $log.writer.debug stdout
  rescue Exception => 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



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

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

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

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

  begin
    stdout = %x"#{tar} -x -z -C #{target_dir} -f #{tgz_file} #{extract_file}"
    $log.writer.debug stdout
  rescue Exception => 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



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

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

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

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

  begin
    stdout = %x"#{unzip} -o \"#{zip_file}\" #{extract_file} -d \"#{target_dir}\""
    $log.writer.debug stdout
  rescue Exception => 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
        end
      end

      # include external file
      c.define_tag 'include' do |tag|
        result   = ''
        ext_file = File.join( ENV['WORKSPACE'], tag.attr['file'] )
        if File.exists?(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 Exception => e
    $log.writer.error "Error in tag definition: #{e.message}"
    $log.writer.error e.backtrace
    exit 1
  end

  context
end