Class: OsCtld::User

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, Assets::Definition, Lockable, Manipulable
Defined in:
lib/osctld/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assets::Definition

#define_assets

Methods included from Manipulable

#acquire_manipulation_lock, #init_manipulable, #is_being_manipulated?, #manipulate, #manipulated_by, #release_manipulation_lock

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(pool, name, load: true, config: nil) ⇒ User

Returns a new instance of User



18
19
20
21
22
23
24
25
# File 'lib/osctld/user.rb', line 18

def initialize(pool, name, load: true, config: nil)
  init_lock
  init_manipulable
  @pool = pool
  @name = name
  @attrs = Attributes.new
  load_config(config) if load
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs



15
16
17
# File 'lib/osctld/user.rb', line 15

def attrs
  @attrs
end

#gid_mapObject (readonly)

Returns the value of attribute gid_map



15
16
17
# File 'lib/osctld/user.rb', line 15

def gid_map
  @gid_map
end

#nameObject (readonly)

Returns the value of attribute name



15
16
17
# File 'lib/osctld/user.rb', line 15

def name
  @name
end

#poolObject (readonly)

Returns the value of attribute pool



15
16
17
# File 'lib/osctld/user.rb', line 15

def pool
  @pool
end

#registered=(value) ⇒ Object

Sets the attribute registered

Parameters:

  • value

    the value to set the attribute registered to.



16
17
18
# File 'lib/osctld/user.rb', line 16

def registered=(value)
  @registered = value
end

#ugidObject (readonly)

Returns the value of attribute ugid



15
16
17
# File 'lib/osctld/user.rb', line 15

def ugid
  @ugid
end

#uid_mapObject (readonly)

Returns the value of attribute uid_map



15
16
17
# File 'lib/osctld/user.rb', line 15

def uid_map
  @uid_map
end

Instance Method Details

#assetsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/osctld/user.rb', line 48

def assets
  define_assets do |add|
    # Directories and files
    add.directory(
      userdir,
      desc: 'User directory',
      user: 0,
      group: ugid,
      mode: 0751
    )

    add.directory(
      homedir,
      desc: 'Home directory',
      user: ugid,
      group: ugid,
      mode: 0751
    )

    add.file(
      config_path,
      desc: "osctld's user config",
      user: 0,
      group: 0,
      mode: 0400
    )

    add.entry('/etc/passwd', desc: 'System user') do |asset|
      asset.validate do
        if /^#{Regexp.escape(sysusername)}:x:#{ugid}:#{ugid}:/ !~ File.read(asset.path)
          asset.add_error('entry missing or invalid')
        end
      end
    end

    add.entry('/etc/group', desc: 'System group') do |asset|
      asset.validate do
        if /^#{Regexp.escape(sysgroupname)}:x:#{ugid}:$/ !~ File.read(asset.path)
          asset.add_error('entry missing or invalid')
        end
      end
    end
  end
end

#config_pathObject



148
149
150
# File 'lib/osctld/user.rb', line 148

def config_path
  inclusively { File.join(pool.conf_path, 'user', "#{name}.yml") }
end

#configure(uid_map, gid_map, ugid: nil) ⇒ Object

Parameters:

  • uid_map (IdMap)
  • gid_map (IdMap)
  • ugid (Integer, nil)


38
39
40
41
42
43
44
45
46
# File 'lib/osctld/user.rb', line 38

def configure(uid_map, gid_map, ugid: nil)
  exclusively do
    @ugid = ugid || UGidRegistry.get
    @uid_map = uid_map
    @gid_map = gid_map
  end

  save_config
end

#containersObject



159
160
161
162
163
# File 'lib/osctld/user.rb', line 159

def containers
  DB::Containers.get do |cts|
    cts.select { |ct| ct.user == self && ct.pool.name == pool.name }
  end
end

#has_containers?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
# File 'lib/osctld/user.rb', line 152

def has_containers?
  ct = DB::Containers.get.detect do |ct|
    ct.user.name == name && ct.pool.name == pool.name
  end
  ct ? true : false
end

#homedirObject



144
145
146
# File 'lib/osctld/user.rb', line 144

def homedir
  File.join(userdir, '.home')
end

#idObject



27
28
29
# File 'lib/osctld/user.rb', line 27

def id
  name
end

#id_range_allocation_ownerObject



165
166
167
# File 'lib/osctld/user.rb', line 165

def id_range_allocation_owner
  "user:#{name}"
end

#identObject



31
32
33
# File 'lib/osctld/user.rb', line 31

def ident
  inclusively { "#{pool.name}:#{name}" }
end

#log_typeObject



169
170
171
# File 'lib/osctld/user.rb', line 169

def log_type
  "user=#{ident}"
end

#manipulation_resourceObject



173
174
175
# File 'lib/osctld/user.rb', line 173

def manipulation_resource
  ['user', ident]
end

#registered?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/osctld/user.rb', line 93

def registered?
  inclusively { return registered unless registered.nil? }
  v = SystemUsers.include?(sysusername)
  exclusively { self.registered = v }
  v
end

#set(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Hash)


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/osctld/user.rb', line 102

def set(opts)
  opts.each do |k, v|
    case k
    when :attrs
      attrs.update(v)

    else
      fail "unsupported option '#{k}'"
    end
  end

  save_config
end

#sysgroupnameObject



136
137
138
# File 'lib/osctld/user.rb', line 136

def sysgroupname
  sysusername
end

#sysusernameObject



132
133
134
# File 'lib/osctld/user.rb', line 132

def sysusername
  "#{pool.name}-#{name}"
end

#unset(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Array<String>)


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/osctld/user.rb', line 118

def unset(opts)
  opts.each do |k, v|
    case k
    when :attrs
      v.each { |attr| attrs.unset(attr) }

    else
      fail "unsupported option '#{k}'"
    end
  end

  save_config
end

#userdirObject



140
141
142
# File 'lib/osctld/user.rb', line 140

def userdir
  inclusively { File.join(pool.user_dir, name) }
end