Top Level Namespace

Defined Under Namespace

Modules: Chake

Instance Method Summary collapse

Instance Method Details

#encrypted_for(node) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/chake.rb', line 33

def encrypted_for(node)
  encrypted_files = Array(node.data['encrypted'])
  if encrypted_files.empty?
    encrypted_files = Dir.glob("**/files/{default,host-#{node.hostname}}/*.{asc,gpg}") + Dir.glob('**/files/*.{asc,gpg}')
  end
  encrypted_files.each_with_object({}) do |key, hash|
    hash[key] = key.sub(/\.(asc|gpg)$/, '')
  end
end

#if_files_changed(node, group_name, files) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chake.rb', line 68

def if_files_changed(node, group_name, files)
  return if files.empty?

  hash_io = IO.popen(%w[xargs sha1sum], 'w+')
  hash_io.puts(File.join(Chake.tmpdir, "#{node}.bootstrap"))
  files.sort.each { |f| hash_io.puts(f) }
  hash_io.close_write
  current_hash = hash_io.read

  hash_file = File.join(Chake.tmpdir, "#{node}.#{group_name}.sha1sum")
  hash_on_disk = nil
  hash_on_disk = File.read(hash_file) if File.exist?(hash_file)

  yield if current_hash != hash_on_disk
  FileUtils.mkdir_p(File.dirname(hash_file))
  File.write(hash_file, current_hash)
end

#maybe_decrypt(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chake.rb', line 50

def maybe_decrypt(node)
  if node.needs_upload?
    return yield
  end

  files = encrypted_for(node)
  files.each do |encrypted, target|
    sh "gpg --use-agent --quiet --decrypt --output #{target} #{encrypted}"
  end
  begin
    yield
  ensure
    files.each do |_, target|
      Chake::Wipe.instance.wipe(target)
    end
  end
end

#write_json_file(file, data) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/chake.rb', line 86

def write_json_file(file, data)
  File.chmod(0o600, file) if File.exist?(file)
  File.open(file, 'w', 0o600) do |f|
    f.write(JSON.pretty_generate(data))
    f.write("\n")
  end
end