Class: OsCtld::User

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, 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

#standaloneObject (readonly)

Returns the value of attribute standalone



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

def standalone
  @standalone
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



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
92
93
# File 'lib/osctld/user.rb', line 50

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

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

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

    add.entry('/etc/passwd', desc: 'System user') do |asset|
      asset.validate_block 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_block 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



158
159
160
# File 'lib/osctld/user.rb', line 158

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

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

Parameters:

  • uid_map (IdMap)
  • gid_map (IdMap)
  • ugid (Integer, nil) (defaults to: nil)
  • standalone (Boolean) (defaults to: true)


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

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

  save_config
end

#containersObject



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

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)


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

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

#homedirObject



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

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



175
176
177
# File 'lib/osctld/user.rb', line 175

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



179
180
181
# File 'lib/osctld/user.rb', line 179

def log_type
  "user=#{ident}"
end

#manipulation_resourceObject



183
184
185
# File 'lib/osctld/user.rb', line 183

def manipulation_resource
  ['user', ident]
end

#registered?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/osctld/user.rb', line 95

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):

  • :standalone (true)
  • :attrs (Hash)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/osctld/user.rb', line 105

def set(opts)
  opts.each do |k, v|
    case k
    when :standalone
      exclusively { @standalone = true }

    when :attrs
      attrs.update(v)

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

  save_config
end

#sysgroupnameObject



146
147
148
# File 'lib/osctld/user.rb', line 146

def sysgroupname
  sysusername
end

#sysusernameObject



142
143
144
# File 'lib/osctld/user.rb', line 142

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

#unset(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :standalone (true)
  • :attrs (Array<String>)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/osctld/user.rb', line 125

def unset(opts)
  opts.each do |k, v|
    case k
    when :standalone
      exclusively { @standalone = false }

    when :attrs
      v.each { |attr| attrs.unset(attr) }

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

  save_config
end

#userdirObject



150
151
152
# File 'lib/osctld/user.rb', line 150

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