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.



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

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



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

def attrs
  @attrs
end

#gid_mapObject (readonly)

Returns the value of attribute gid_map



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

def gid_map
  @gid_map
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#poolObject (readonly)

Returns the value of attribute pool



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

def pool
  @pool
end

#registered=(value) ⇒ Object

Sets the attribute registered

Parameters:

  • value

    the value to set the attribute registered to.



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

def registered=(value)
  @registered = value
end

#standaloneObject (readonly)

Returns the value of attribute standalone



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

def standalone
  @standalone
end

#ugidObject (readonly)

Returns the value of attribute ugid



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

def ugid
  @ugid
end

#uid_mapObject (readonly)

Returns the value of attribute uid_map



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

def uid_map
  @uid_map
end

Instance Method Details

#assetsObject



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

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



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

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)


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

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



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

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)


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

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



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

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

#idObject



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

def id
  name
end

#id_range_allocation_ownerObject



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

def id_range_allocation_owner
  "user:#{name}"
end

#identObject



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

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

#log_typeObject



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

def log_type
  "user=#{ident}"
end

#manipulation_resourceObject



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

def manipulation_resource
  ['user', ident]
end

#registered?Boolean

Returns:

  • (Boolean)


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

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)


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

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



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

def sysgroupname
  sysusername
end

#sysusernameObject



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

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

#unset(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

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


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

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



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

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