Class: Rultor::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/rultor/encrypt.rb

Overview

Encrypting command

Instance Method Summary collapse

Constructor Details

#initialize(name, file) ⇒ Encrypt

Returns a new instance of Encrypt.



41
42
43
44
45
# File 'lib/rultor/encrypt.rb', line 41

def initialize(name, file)
  @key = 'rultor-key:' + name
  @dir = File.dirname(file)
  @file = File.basename(file)
end

Instance Method Details

#runObject



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
# File 'lib/rultor/encrypt.rb', line 47

def run
  system(
    "
    set -x
    set -e
    file=#{Shellwords.escape(@file)}
    enc=#{Shellwords.escape(@file + '.enc')}
    if [ -e \"${enc}\" ]; then
      echo \"file already exists: ${enc}\"
      exit -1
    fi
    asc=#{Shellwords.escape(@file + '.asc')}
    if [ -e \"${asc}\" ]; then
      echo \"file already exists: ${asc}\"
      exit -1
    fi
    cd #{Shellwords.escape(@dir)}
    gpg --version
    gpg --symmetric --armor --verbose --batch --no-tty \
      --passphrase #{Shellwords.escape(@key)} \
      -o \"${enc}\" \"${file}\"
    gpg --keyserver hkp://pool.sks-keyservers.net \
      --verbose --recv-keys 9AF0FA4C
    gpg --trust-model always \
      --output \"${asc}\" \
      --batch --no-tty --armor --encrypt --verbose \
      --recipient 9AF0FA4C \"${enc}\"
    rm -f \"${enc}\"
    "
  )
  fail 'Failed to PGP encrypt' unless $CHILD_STATUS.exitstatus == 0
  Rultor.log.info "#{@file} encrypted"
end