Class: DatalackeyProcess

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exe, directory, permissions, memory) ⇒ DatalackeyProcess

Returns a new instance of DatalackeyProcess.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datalackeylib.rb', line 12

def initialize(exe, directory, permissions, memory)
  @exit_code = 0
  if exe.nil?
    exe = DatalackeyProcess.locate_executable(
      'datalackey', [ '/usr/local/libexec', '/usr/libexec' ])
    raise ArgumentError, 'datalackey not found' if exe.nil?
  elsif !File.exist?(exe) || !File.executable?(exe)
    raise ArgumentError, "Executable not found or not executable: #{exe}"
  end
  @executable = exe
  args = [ exe,
      '--command-in', 'stdin', 'JSON', '--command-out', 'stdout', 'JSON' ]
  args.push('--memory') unless memory.nil?
  args.concat([ '--directory', directory ]) unless directory.nil?
  args.concat([ '--permissions', permissions ]) unless permissions.nil?
  @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(*args)
end

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



10
11
12
# File 'lib/datalackeylib.rb', line 10

def executable
  @executable
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



10
11
12
# File 'lib/datalackeylib.rb', line 10

def exit_code
  @exit_code
end

#stderrObject (readonly)

Returns the value of attribute stderr.



10
11
12
# File 'lib/datalackeylib.rb', line 10

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



10
11
12
# File 'lib/datalackeylib.rb', line 10

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



10
11
12
# File 'lib/datalackeylib.rb', line 10

def stdout
  @stdout
end

Class Method Details

.locate_executable(exe_name, dirs_outside_path = []) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/datalackeylib.rb', line 72

def DatalackeyProcess.locate_executable(exe_name, dirs_outside_path = [])
  # Absolute file name or found in current working directory.
  return exe_name if File.exist?(exe_name) && File.executable?(exe_name)
  dirs = []
  dirs_outside_path = [ dirs_outside_path ] unless dirs_outside_path.is_a? Array
  dirs.concat dirs_outside_path
  dirs.concat ENV['PATH'].split(File::PATH_SEPARATOR)
  dirs.each do |d|
    exe = File.join(d, exe_name)
    return exe if File.exist?(exe) && File.executable?(exe)
  end
  nil
end

.options_for_OptionParser(parser, separator, exe_callable, mem_callable, dir_callable, perm_callable, echo_callable) ⇒ Object



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
# File 'lib/datalackeylib.rb', line 37

def DatalackeyProcess.options_for_OptionParser(parser, separator,
    exe_callable, mem_callable, dir_callable, perm_callable, echo_callable)
  unless separator.nil?
    unless separator.is_a? String
      separator = 'Options for case where this process runs datalackey:'
    end
    parser.separator separator
  end
  unless exe_callable.nil?
    parser.on('-l', '--lackey PROGRAM', 'Use specified datalackey executable.') do |e|
      exe_callable.call(e)
    end
  end
  unless mem_callable.nil?
    parser.on('-m', '--memory', 'Store data in memory.') do
      mem_callable.call(true)
    end
  end
  unless dir_callable.nil?
    parser.on('-d', '--directory [DIR]', 'Store data under (working) directory.') do |d|
      dir_callable.call(d || Dir.pwd)
    end
  end
  unless perm_callable.nil?
    parser.on('-p', '--permissions MODE', i[user group other], 'File permissions cover (user, group, other).') do |p|
      perm_callable.call({ user: '600', group: '660', other: '666' }[p])
    end
  end
  unless echo_callable.nil?
    parser.on('--echo', 'Echo communication with datalackey.') do
      echo_callable.call(true)
    end
  end
end

.verify_directory_permissions_memory(directory, permissions, memory) ⇒ Object



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
# File 'lib/datalackeylib.rb', line 86

def DatalackeyProcess.verify_directory_permissions_memory(
    directory, permissions, memory)
  if !memory.nil? && !(directory.nil? && permissions.nil?)
    raise ArgumentError, 'Cannot use both memory and directory/permissions.'
  end
  if memory.nil?
    if directory.nil?
      directory = Dir.pwd
    elsif !Dir.exist? directory
      raise ArgumentError, "Given directory does not exist: #{directory}"
    end
    if permissions.nil?
      if (File.umask & 0o77).zero?
        permissions = '666'
      elsif (File.umask & 0o70).zero?
        permissions = '660'
      else
        permissions = '600'
      end
    elsif permissions != '600' && permissions != '660' && permissions != '666'
      raise ArgumentError, 'Permissions not in {600, 660, 666}.'
    end
  end
  [ directory, permissions, memory ]
end

Instance Method Details

#finishObject



30
31
32
33
34
# File 'lib/datalackeylib.rb', line 30

def finish
  @stdin.close
  @wait_thread.join
  @exit_code = @wait_thread.value.exitstatus
end