Module: Prepper::Tools::Users

Included in:
Package
Defined in:
lib/prepper/tools/users.rb

Overview

user management related helpers

Instance Method Summary collapse

Instance Method Details

#add_user(username, opts = {}) ⇒ Object

add a user to the host

Parameters:

  • username (String)

    name of the user

  • opts (Hash) (defaults to: {})

    options has

Options Hash (opts):

  • :flags (String)

    flags to pass to adduser



9
10
11
12
# File 'lib/prepper/tools/users.rb', line 9

def add_user(username, opts = {})
  opts[:flags] << ' --gecos ,,,'
  @commands << Command.new("adduser #{username} #{opts[:flags]}", sudo: true, verifier: has_user?(username))
end

#has_user?(username, opts = {}) ⇒ Boolean

returns a verifier command to check if a user exists

Parameters:

  • username (String)

    name of the user

  • opts (Hash) (defaults to: {})

    options hash

Options Hash (opts):

  • :in_group (String)

    check if the user is in the given group

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/prepper/tools/users.rb', line 18

def has_user?(username, opts = {})
  if opts[:in_group]
    command = "id -nG #{username} | xargs -n1 echo | grep #{opts[:in_group]}"
  else
    command = "id #{username}"
  end
  Command.new(command, sudo: true)
end