Sha256: 93723d91a3a07fda1441ab2cce1a974303e9f51d25738e1ea914eb868ad051f9

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

require 'shellwords'

module NexusSW
  module LXD
    class Transport
      module Mixins
        module Helpers
          module UsersMixin
            def user(user_nameorid, options = {})
              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]

              # 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

            attr_accessor :file_mode, :gid
            attr_reader :uid, :username

            private

            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
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lxd-common-0.9.1 lib/nexussw/lxd/transport/mixins/helpers/users.rb
lxd-common-0.9.0 lib/nexussw/lxd/transport/mixins/helpers/users.rb
lxd-common-0.8.1 lib/nexussw/lxd/transport/mixins/helpers/users.rb
lxd-common-0.8.0 lib/nexussw/lxd/transport/mixins/helpers/users.rb