File: group.rb

package info (click to toggle)
ruby-specinfra 2.89.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 2,412 kB
  • sloc: ruby: 10,338; sh: 4; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download | duplicates (4)
1
2
3
4
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
class Specinfra::Command::Base::Group < Specinfra::Command::Base
  class << self
    def check_exists(group)
      "getent group #{escape(group)}"
    end

    def check_has_gid(group, gid)
      "getent group #{escape(group)} | cut -f 3 -d ':' | grep -w -- #{escape(gid)}"
    end

    def check_is_system_group(group)
      exists = "getent group #{escape(group)} > /dev/null 2>&1"
      gid = "getent group #{escape(group)} | cut -f 3 -d ':'"
      sys_gid_min = "awk 'BEGIN{sys_gid_min=101} {if($1~/^SYS_GID_MIN/){sys_gid_min=$2}} END{print sys_gid_min}' /etc/login.defs"
      sys_gid_max = "awk 'BEGIN{sys_gid_max=0;gid_min=1000} {if($1~/^SYS_GID_MAX/){sys_gid_max=$2}if($1~/^GID_MIN/){gid_min=$2}} END{if(sys_gid_max!=0){print sys_gid_max}else{print gid_min-1}}' /etc/login.defs"
      %Q|#{exists} && test "$(#{gid})" -ge "$(#{sys_gid_min})" && test "$(#{gid})" -le "$(#{sys_gid_max})"|
    end

    def get_gid(group)
      "getent group #{escape(group)} | cut -f 3 -d ':'"
    end

    def update_gid(group, gid)
      "groupmod -g #{escape(gid)} #{escape(group)}"
    end

    def add(group, options)
      command = ['groupadd']
      command << '-g' << escape(options[:gid])  if options[:gid]
      command << '-r' if options[:system_group]
      command << escape(group)
      command.join(' ')
    end
  end
end