module NexusSW::LXD::Transport::Mixins::Helpers::UsersMixin

Attributes

file_mode[RW]
gid[RW]
uid[R]
username[R]

Public Instance Methods

reset_user() click to toggle source
# File lib/nexussw/lxd/transport/mixins/helpers/users.rb, line 26
def reset_user
  @username = @uid = @gid = nil
end
user(user_nameorid, options = {}) click to toggle source
# File lib/nexussw/lxd/transport/mixins/helpers/users.rb, line 9
def user(user_nameorid, options = {})
  return unless user_nameorid
  passwd = read_file options[:passwd_file] || "/etc/passwd"

  # rework into .split(':') if this gets more complicated
  @uid = user_nameorid.is_a?(String) ? passwd[/^#{user_nameorid}:[^:]*:([^:]*):/, 1] : user_nameorid
  @username = user_nameorid.is_a?(String) ? user_nameorid : passwd[/^([^:]*):[^:]*:#{user_nameorid}:/, 1]
  raise "User not found (#{user_nameorid}) while attempting to set transport identity" unless @uid && @username

  # gotcha: we're always setting the default group here, but it's changeable by the user, afterwards
  # so if `user` gets called again, and the caller wants an alternative gid, the caller will need to re-set the gid
  @gid = passwd[/^[^:]*:[^:]*:#{uid}:([^:]*):/, 1]
end

Private Instance Methods

runas_command(command, options = {}) click to toggle source
# File lib/nexussw/lxd/transport/mixins/helpers/users.rb, line 32
def runas_command(command, options = {})
  uname = options[:runas] || username
  return command unless uname
  command = command.shelljoin if command.is_a? Array
  ["su", uname, "-c", command]
end