Top Level Namespace

Extended by:
Rake::DSL

Defined Under Namespace

Modules: Greenletters, Rake Classes: Conf, ConfStack, ConfigurationError, Construct, Cook, CookDecryptionError, CookEncryptionError, ResourceNotFoundError

Instance Method Summary collapse

Methods included from Rake::DSL

add_cookbook, config, each_resource, find_resource, load_recipe, local_run, local_sh, mesg, mesg_pp, read_resource, remote_run, remote_ssh, rsync_from_remote, rsync_to_remote, scp_from_remote, scp_to_remote, set_command_options

Instance Method Details

#applyTemplate(templateFileName, resultFileName, mode = nil, owner = nil, group = nil) ⇒ Object

Apply templates and/or copy resources



254
255
256
257
258
259
260
261
# File 'lib/rake/cookUtils.rb', line 254

def applyTemplate(templateFileName, resultFileName, mode = nil, owner = nil, group = nil)
  require 'erubis'
  mesg "Applying erubis template: [#{templateFileName}]\nto produce: [#{resultFileName}]";
  eruby = Erubis::Eruby.new(IO.read(templateFileName));
  File.open(resultFileName, 'w') do | io |
    io.write(eruby.result(binding()));        
  end
end

#areYouSure?(prompt, defaultAnswer = false, askUser = true) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rake/cookUtils.rb', line 9

def areYouSure?(prompt, defaultAnswer = false, askUser = true)
  if askUser then
    defaultPrompt = "yN";
    defaultRegExp = /^[yY]/;
    if defaultAnswer then
      defaultPrompt = "Yn" 
      defaultRegExp = /^[nN]/;
    end
    puts "\n*************************************************************\n#{prompt}\n\tAre you sure you want to do this? [#{defaultPrompt}]";
    return !defaultAnswer if STDIN.gets =~ defaultRegExp;
  end
  defaultAnswer;
end

#copyOverrideOrBinary(binaryPath, resultPath) ⇒ Object



272
273
274
275
276
277
278
279
# File 'lib/rake/cookUtils.rb', line 272

def copyOverrideOrBinary(binaryPath, resultPath)
  overridePath = resultPath.sub(/^upload/, 'override');
  if File.exists?(overridePath) then
    local_sh("cp #{overridePath} #{resultPath}");
  else
    local_sh("cp #{binaryPath} #{resultPath}");
  end
end

#ensureDirExists(aDirectory) ⇒ Object



23
24
25
26
27
# File 'lib/rake/cookUtils.rb', line 23

def ensureDirExists(aDirectory)
  unless File.directory?(aDirectory)
    Rake::Task.local_sh("mkdir -p #{aDirectory}");
  end
end

#fileMatches(fileName, aRegExp) ⇒ Object



281
282
283
284
# File 'lib/rake/cookUtils.rb', line 281

def fileMatches(fileName, aRegExp)
  fileContents = File.open(fileName, "r").read;
  fileContents =~ aRegExp;
end

#gpgDecryptFile2Data(encryptedFilePath) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rake/cookUtils.rb', line 125

def gpgDecryptFile2Data(encryptedFilePath)

  if !Conf.gpg.has_key?(:recipientUID) then
    raise CookDecryptionError, "No GnuPG recipient UID (Conf.gpg.recipientUID) specified in configuration";
  end

  if !ENV.has_key?("GPG_AGENT_INFO") then
    raise CookDecryptionError, "Could not find GnuPG agent (GPG_AGENT_INFO environment variable), is it running?";
  end

  begin 
    require 'gpgme';
  rescue LoadError
    raise CookDecryptionError, "The required gpgme (GnuPG Made Easy) ruby gem could not be loaded";
  end

  # check to ensure the file name conforms to our naming conventions...
  #
  if encryptedFilePath !~ /\.enc$/ then
    raise CookDecryptionError, "The encrypted file (#{encryptedFilePath}) MUST have the file extension '.enc'";
  end

  # check to make sure the file exists...
  #
  if !File.exists?(encryptedFilePath) then
    raise CookDecryptionError, "The encrypted file (#{encryptedFilePath}) does not exist";
  end

  cypherFile = File.open(encryptedFilePath);
  header = cypherFile.gets;
  if header !~ /^--gpgRecipientUID-cypherText--$/ then
    raise CookDecryptionError, "Attempting to decrypt a file which was not encrypted using cookUtils gpgEncryptData2File (header: [#{header}]).";
  end
  recipientUID = cypherFile.gets.chomp;
  if recipientUID != Conf.gpg.recipientUID then
    require 'pp';
    pp recipientUID;
    pp Conf.gpg.recipientUID;
    raise CookDecryptionError, "Configured recipient UID (#{Conf.gpg.recipientUID}) does not match the recipient UID (#{recipientUID}) used to encrypt the file: [#{encryptedFilePath}]";
  end
  cypherText = Base64.decode64(cypherFile.read);

  crypto = GPGME::Crypto.new
  decrypted = crypto.decrypt(cypherText, :recipients => Conf.gpg.recipientUID).read;

  return decrypted;  
end

#gpgEncryptData2File(plainText, encryptedFilePath) ⇒ Object

gpgEncryptData2File and gpgDecryptFile2Data are both based on the ruby gpgme bindings documentation. (See:



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
# File 'lib/rake/cookUtils.rb', line 99

def gpgEncryptData2File(plainText, encryptedFilePath)

  if !Conf.gpg.has_key?(:recipientUID) then
    raise CookEncryptionError, "No GnuPG recipient UID specified in configuration";
  end

  if !ENV.has_key?("GPG_AGENT_INFO") then
    raise CookEncryptionError, "Could not find GnuPG agent (GPG_AGENT_INFO environment variable), is it running?";
  end

  begin 
    require 'gpgme';
  rescue LoadError
    raise CookEncryptionError, "The required gpgme (GnuPG Made Easy) ruby gem could not be loaded";
  end

  crypto = GPGME::Crypto.new
  encrypted = crypto.encrypt(plainText, { :recipients => Conf.gpg.recipientUID, :always_trust => true } ).read;

  File.open(encryptedFilePath, 'w') do | cypherFile |
    cypherFile.puts "--gpgRecipientUID-cypherText--";
    cypherFile.puts Conf.gpg.recipientUID;
    cypherFile.puts Base64.encode64(encrypted);
  end
end

#openSslDecryptFile2Data(encryptedFilePath) ⇒ Object



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
# File 'lib/rake/cookUtils.rb', line 61

def openSslDecryptFile2Data(encryptedFilePath)

  # check to ensure the file name conforms to our naming conventions...
  #
  if encryptedFilePath !~ /\.enc$/ then
    raise CookDecryptionError, "The encrypted file (#{encryptedFilePath}) MUST have the file extension '.enc' ";
  end

  # check to make sure the file exists...
  #
  if !File.exists?(encryptedFilePath) then
    raise CookDecryptionError, "The encrypted file (#{encryptedFilePath}) does not exist";
  end

  cypherFile = File.open(encryptedFilePath);
  headerLine = cypherFile.gets;
  if headerLine !~ /^--salt32bytes-iv-cypherText--$/ then
    raise CookDecryptionError, "Attempting to decrypt a file which was not encrypted using cookUtils openSslEncryptData2File (header line: [#{headerLine}]).";
  end
  salt = Base64.decode64(cypherFile.gets);
  iv = Base64.decode64(cypherFile.gets);
  cypherText = Base64.decode64(cypherFile.read);

  decrypter = OpenSSL::Cipher.new('AES-256-CBC');
  decrypter.decrypt;
  decrypter.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(Conf.get_pass_phrase,
                                                  salt, 2000, 256);
  decrypter.iv = iv;

  decrypted = decrypter.update cypherText;
  decrypted << decrypter.final

  return decrypted;
end

#openSslEncryptData2File(plainText, encryptedFilePath) ⇒ Object

encryptData2File and decryptFile2Data are both based on: “Encrypting and decrypting some data” at: www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html see also: www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/PKCS5.html



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
# File 'lib/rake/cookUtils.rb', line 35

def openSslEncryptData2File(plainText, encryptedFilePath)
  # check to ensure the file name conforms to our naming conventions...
  #
  if encryptedFilePath !~ /\.enc$/ then
    raise CookEncryptionError, "The file in which to store the encrypted data (#{encryptedFilePath}) MUST have the file extension '.enc'";
  end

  salt = SecureRandom.random_bytes(32);

  encrypter = OpenSSL::Cipher.new('AES-256-CBC');
  encrypter.encrypt;
  encrypter.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(Conf.get_pass_phrase(true),
                                                  salt, 2000, 256);
  iv = encrypter.random_iv;

  encrypted = encrypter.update plainText;
  encrypted << encrypter.final

  File.open(encryptedFilePath, 'w') do | cypherFile |
    cypherFile.puts "--salt32bytes-iv-cypherText--";
    cypherFile.puts Base64.encode64(salt);
    cypherFile.puts Base64.encode64(iv);
    cypherFile.puts Base64.encode64(encrypted);
  end
end

#replaceLines(fileName, replaceLinesHash) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rake/cookUtils.rb', line 286

def replaceLines(fileName, replaceLinesHash)
  tmpFileName = fileName + '.tmp';
  inFile = File.open(fileName, 'r');
  outFile = File.open(tmpFileName, 'w');
  inFile.each_line() do | aLine |
    replaceLinesHash.keys.each() do | aRegExp |
      if aRegExp.match(aLine) then
        aLine = replaceLinesHash[aRegExp];
      end
    end
    outFile.puts aLine;
  end
  outFile.close();
  inFile.close();
  File.rename(tmpFileName, fileName);
end

#toSymbolHash(aHash) ⇒ Object



315
316
317
318
319
320
321
322
# File 'lib/rake/cookUtils.rb', line 315

def toSymbolHash(aHash)
  aHash.keys.each do | anOldKey |
    next if anOldKey.is_a?(Symbol);
    if anOldKey.is_a?(String) then
      aHash[anOldKey.to_sym] = aHash.delete(anOldKey);
    end
  end
end

#useOverrideOrApplyTemplate(templatePath, resultPath) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/rake/cookUtils.rb', line 263

def useOverrideOrApplyTemplate(templatePath, resultPath)
  overridePath = resultPath.sub(/^upload/, 'override');
  if File.exists?(overridePath) then
    local_sh("cp #{overridePath} #{resultPath}");
  else
    applyTemplate(templatePath, resultPath);
  end
end

#walkResourceBinaries(templateType, serverType, targetServer, targetMachine, &aBlock) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/rake/cookUtils.rb', line 233

def walkResourceBinaries(templateType, serverType, targetServer, targetMachine, &aBlock)
  mesg "";
  targetDir = 'upload/' + targetMachine.to_s + '/' + targetServer.to_s + '/' + templateType.to_s + '/resources';
  ensureDirExists(targetDir);
  resources = Hash.new();
  each_resource(templateType.to_s + '/' + serverType.to_s + '/resourceTemplates') do | aResourcePath |
    mesg "walking binaries in [#{aResourcePath}]";
    Dir.glob(aResourcePath + '/*.bin').sort.each do | aResourceBIN |
      baseName = File.basename(aResourceBIN, '.bin');
      resources[baseName] = aResourceBIN;
    end
  end
  resources.keys.sort.each do | aResourceBaseName |
    aResourceName = targetDir + '/' + aResourceBaseName;
    aBlock.call(resources[aResourceBaseName], aResourceName);
  end
end

#walkResourceScriptTemplates(templateType, serverType, targetServer, targetMachine, &aBlock) ⇒ Object

Walk resources, scripts and binaries



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rake/cookUtils.rb', line 176

def walkResourceScriptTemplates(templateType, serverType, targetServer, targetMachine, &aBlock)
  mesg "";
  targetResourceScriptsDir = 'upload/' + targetMachine.to_s + '/' + targetServer.to_s + '/' + templateType.to_s + '/resourceScripts';
  targetDir = 'upload/' + targetMachine.to_s + '/' + targetServer.to_s + '/' + templateType.to_s + '/resources';
  ensureDirExists(targetResourceScriptsDir);
  ensureDirExists(targetDir);
  resourceScripts = Hash.new();
  each_resource(templateType + '/' + serverType.to_s + '/resourceScriptTemplates') do | aResourceScriptPath |
    mesg "walking resource scripts in [#{aResourceScriptPath}]";
    Dir.glob(aResourceScriptPath + '/*.erb').sort.each do | aResourceScriptERB |
      baseName = File.basename(aResourceScriptERB, '.erb');
      resourceScripts[baseName] = aResourceScriptERB;
    end
  end
  resourceScripts.keys.sort.each do | aResourceScriptBaseName |
    aResourceScriptName = targetResourceScriptsDir + '/' + aResourceScriptBaseName;
    aResourceName = targetDir + '/' + aResourceScriptBaseName;
    aBlock.call(resourceScripts[aResourceScriptBaseName], aResourceScriptName, aResourceName);
  end
end

#walkResourceTemplates(templateType, serverType, targetServer, targetMachine, &aBlock) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rake/cookUtils.rb', line 215

def walkResourceTemplates(templateType, serverType, targetServer, targetMachine, &aBlock)
  mesg "";
  targetDir = 'upload/' + targetMachine.to_s + '/' + targetServer.to_s + '/' + templateType.to_s + '/resources';
  ensureDirExists(targetDir);
  resources = Hash.new();
  each_resource(templateType.to_s + '/' + serverType.to_s + '/resourceTemplates') do | aResourcePath |
    mesg "walking resources in [#{aResourcePath}]";
    Dir.glob(aResourcePath + '/*.erb').sort.each do | aResourceERB |
      baseName = File.basename(aResourceERB, '.erb');
      resources[baseName] = aResourceERB;
    end
  end
  resources.keys.sort.each do | aResourceBaseName |
    aResourceName = targetDir + '/' + aResourceBaseName;
    aBlock.call(resources[aResourceBaseName], aResourceName);
  end
end

#walkScriptTemplates(templateType, serverType, targetServer, targetMachine, &aBlock) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rake/cookUtils.rb', line 197

def walkScriptTemplates(templateType, serverType, targetServer, targetMachine, &aBlock)
  mesg "";
  targetDir = 'upload/' + targetMachine.to_s + '/' + targetServer.to_s + '/' + templateType.to_s + '/scripts';
  ensureDirExists(targetDir);
  scripts = Hash.new();
  each_resource(templateType.to_s + '/' + serverType.to_s + '/scriptTemplates') do | aResourcePath |
    mesg "walking scripts in [#{aResourcePath}]";
    Dir.glob(aResourcePath + '/*.erb').sort.each do | aScriptERB |
      baseName = File.basename(aScriptERB, '.erb');
      scripts[serverType+baseName] = aScriptERB;
    end
  end
  scripts.keys.sort.each do | aScriptBaseName |
    aScriptName = targetDir + '/' + aScriptBaseName;
    aBlock.call(scripts[aScriptBaseName], aScriptName);
  end
end

#walkThroughDirectoriesDoing(curDir, &aBlock) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rake/cookUtils.rb', line 303

def walkThroughDirectoriesDoing(curDir, &aBlock)
  Dir.entries(curDir).sort.each do | aFile |
    next if aFile =~ /^\.$/;
    next if aFile =~ /^\.\.$/;
    fullPath = curDir + '/' + aFile;
    aBlock.call(fullPath);
    if File.directory?(fullPath) then
      walkThroughDirectoriesDoing(fullPath, &aBlock);
    end
  end
end