Class: Manman::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil, opts = nil) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/manman/worker.rb', line 5

def initialize( logger=nil, opts=nil )
  if logger.nil?    
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::INFO
  else
    @logger = logger
  end

  if opts.nil?
    @opts    = Opts.new
  else
    @opts = opts
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/manman/worker.rb', line 20

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



20
21
22
# File 'lib/manman/worker.rb', line 20

def opts
  @opts
end

Instance Method Details

#calc_digest_md5(fn) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/manman/worker.rb', line 174

def calc_digest_md5( fn )
  # digest/hash is a string of 20 hexadecimal 8-bit numbers
  # example:
  #   5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

  ## NB: use b - binary
  #  required for Windows-only (avoids changing of newlines \n to \n\r or similar)

  md5 = Digest::MD5.hexdigest( File.open( fn, 'rb') { |f| f.read } )
  md5
end

#encrypt_callback(text) ⇒ Object



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

def encrypt_callback( text )
  if opts.encrypt_callback.nil?    
    puts "[debug] no encrypt callback configured"
    text   # pass through; identity; do nothing
  else
    opts.encrypt_callback.encrypt_callback( text )
  end
end

#filter_callback(text) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/manman/worker.rb', line 152

def filter_callback( text )
  if opts.filter_callback.nil?
    puts "[debug] no filter callback configured"     
    text
  else
    opts.filter_callback.filter_callback( text )
  end
end

#generate_checksum(text) ⇒ Object



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

def generate_checksum( text )

  # NB: remove possible old checksum in checksum line w/ empty line before md5 calculation
  hash = Digest::MD5.hexdigest( text.gsub( /^CHECKSUM:.*/, '' ) )
  puts "Paket-Hash >#{hash}<"
  encrypted_hash = encrypt_callback( hash )

  ## use strip why? why not?
  ##  was encrypted_hash.strip
 
  text.gsub( /^CHECKSUM:.*/, "CHECKSUM: #{encrypted_hash}" )
end

#generate_manifestObject



33
34
35
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
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
110
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
# File 'lib/manman/worker.rb', line 33

def generate_manifest

  puts "  working folder is: #{Dir.pwd}"
  
  puts "Loading template #{opts.template}..."
  
  old_lines = File.read( opts.template )

  new_lines = []

  header_lines = []

  
  ### todo/fix: move to template  ???
  header_lines << "####################################################\n"
  header_lines << "# generated on  #{Time.now} using  #{Manman.banner}\n"
  header_lines << "# - version: #{opts.release}, env: #{opts.env}\n"

  old_lines.each_line do |line|

    if line =~ /^\s*#/
      # skip komments and do NOT copy to result (keep comments secret!)
      logger.debug 'skipping comment line'
      next
    end
      
    if line =~ /^\s*$/ 
      # kommentar oder leerzeile überspringen 
      logger.debug 'skipping blank line'
      new_lines << line
      next
    end

    # split/process key value pair

    tokens = line.split( ':' )

    key    = tokens[0]
    values = tokens[1]

    ## special keys (NOT files; do NOT calculate md5 digest)
   
    headers_release  = [ 'RELEASE', 'VERSION' ]
    headers_env      = [ 'ENV', 'UMGEBUNG' ]
    headers_valid    = [ 'VALID_UNTIL', 'GUELTIG_BIS' ]
    headers_checksum = [ 'CHECKSUM' ]
   
    headers = headers_release + headers_env + headers_valid + headers_checksum
    headers += opts.headers   # add possible user defined extra headers
   
    if headers.include?( key )
      if headers_release.include?( key )
        new_lines << "#{key}: #{opts.release}\n"
      elsif headers_env.include?( key )
        new_lines << "#{key}: #{opts.env}\n"
      elsif headers_valid.include?( key )
        new_lines << "#{key}: #{opts.valid}\n"
      else    # assume extra headers or checksum
        new_lines << line     ## just pass header line throug; not modified
      end
      next
    end

    ### calculate md5 digests
    
    md5 = ""  
    fn = "#{opts.base}/#{key}"   # filename
    
    if File.exists?( fn ) == false
      md5 = "xxxxxxx"
      msg = "*** ERROR: #{key} missing; using path #{fn}"
      puts msg
      header_lines << "#  #{msg}\n"     # add error to header in manifest
    else
      md5 = calc_digest_md5( fn )
      puts "OK #{key} -> #{md5}"
    end
    
    new_lines << "#{key}: #{md5}, #{values}"
           
  end # oldlines.each


  header_lines << "#####################################\n"
  header_lines << "\n"

  new_lines = header_lines + new_lines

  # lines all in one string (no array/ary of lines)
  new_lines_all_in_one = ""
  new_lines.each do |line|
    new_lines_all_in_one << line
  end

  puts "[debug] new_lines_all_in_one:"
  puts new_lines_all_in_one
  
  ## allow custom filter (lets you add custom headers,checksums,etc.)
  new_lines_all_in_one = filter_callback( new_lines_all_in_one )

  ## last step - calculate checksum
  new_lines_all_in_one = generate_checksum( new_lines_all_in_one )
  
  File.open( opts.output, 'w') do |f|
    f.write( new_lines_all_in_one )
  end
  
end

#runObject



23
24
25
26
27
28
29
30
# File 'lib/manman/worker.rb', line 23

def run
  puts "Using settings:"
  pp opts
  
  generate_manifest()
  
  puts 'Done.'
end