Class: CMDB::Commands::Shim

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/commands/shim.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface, command, rewrite:, pretend:, user:) ⇒ Shim

Create a Shim.

Parameters:

  • command (Array)

    collection of string to pass to Kernel#exec; 0th element is the command name



62
63
64
65
66
67
68
# File 'lib/cmdb/commands/shim.rb', line 62

def initialize(interface, command, rewrite:, pretend:, user:)
  @cmdb            = interface
  @command         = command
  @dir             = rewrite
  @pretend         = pretend
  @user            = user
end

Instance Attribute Details

#cmdbCMDB::Interface (readonly)

Returns:



58
59
60
# File 'lib/cmdb/commands/shim.rb', line 58

def cmdb
  @cmdb
end

Class Method Details

.create(interface) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cmdb/commands/shim.rb', line 5

def self.create(interface)
  options = Trollop.options do
    banner <<-EOS
The 'shim' command adapts your applications for use with CMDB without coupling them to
the CMDB RubyGem (or forcing you to write your applications in Ruby). It works by
manipulating the environment or filesystem to make CMDB inputs visible, then invoking
your app.

To use the shim with apps that read configuration from the filesystem, use the --rewrite
option to tell the shim where to rewrite configuration files. It will look for tokens
in JSON or YML that look like <<cmdb.key.name>> and replace them with the value of
the specified key.

The shim populates the environment with all data from every source. The source-
specific prefix of each key is omitted from the environment variable name,
e.g. "common.database.host" is represented as DATABASE_HOST.

Usage:
cmdb shim [options] -- <command_to_exec> [options_for_command]

Where [options] are selected from:
    EOS
    opt :rewrite,
        'Directory to scan for key-replacement tokens in data files',
        type: :string
    opt :pretend,
        'Check for errors, but do not actually launch the app or rewrite files',
        default: false
    opt :user,
        'Switch to named user before executing app',
        type: :string
  end
  options.delete(:help)

  options.delete_if { |k, v| k.to_s =~ /_given$/i }
  new(interface, ARGV, **options)
end

.drop_privileges(login) ⇒ true

Irrevocably change the current user for this Unix process by calling the setresuid system call. This sets both the uid and gid (to the user’s primary group).

Parameters:

  • login (String)

    name of user to switch to

Returns:

  • (true)

Raises:

  • (ArgumentError)

    if the named user does not exist



50
51
52
53
54
55
# File 'lib/cmdb/commands/shim.rb', line 50

def self.drop_privileges()
  pwent = Etc.getpwnam()
  Process::Sys.setresgid(pwent.gid, pwent.gid, pwent.gid)
  Process::Sys.setresuid(pwent.uid, pwent.uid, pwent.uid)
  true
end

Instance Method Details

#runObject

Run the shim.

Raises:

  • (SystemExit)

    if something goes wrong



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cmdb/commands/shim.rb', line 73

def run
  populate_environment
  rewrote = rewrite_files

  if !rewrote && !@pretend && @command.empty?
    CMDB.log.warn 'CMDB: nothing to do; please specify --dir, or -- followed by a command to run'
    exit 7
  end

  launch_app
end