Skip to content

Commit

Permalink
add class user type, fix some bugs with puppet dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
camillescottatwork committed Sep 18, 2024
1 parent 362a4aa commit 273a50e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cheeto/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.rc5'
__version__ = '1.0.rc6'
26 changes: 20 additions & 6 deletions cheeto/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import argparse
from collections import defaultdict
from collections.abc import Iterable
from enum import member
import logging
from pathlib import Path
from re import A, L
Expand Down Expand Up @@ -975,6 +976,14 @@ def query_user_slurmership(sitename: str, user: site_user_t):
yield group.groupname


def query_user_sudogroups(sitename: str, user: site_user_t):
if type(user) is str:
user = SiteUser.objects.get(sitename=sitename, username=user)
qs = SiteGroup.objects(_sudoers=user, sitename=sitename).only('groupname')
for group in qs:
yield group.groupname


def query_user_slurm(sitename: str, user: site_user_t):
if type(user) is str:
user = SiteUser.objects.get(sitename=sitename, username=user)
Expand Down Expand Up @@ -1443,7 +1452,9 @@ def user_to_puppet(user: SiteUser):
logger = logging.getLogger(__name__)
memberships = set(query_user_groups(user.sitename, user))
memberships.remove(user.username)
slurmerships = list(query_user_slurmership(user.sitename, user))
memberships = sorted(memberships)
slurmerships = sorted(list(query_user_slurmership(user.sitename, user)))
group_sudo = sorted(list(query_user_sudogroups(user.sitename, user)))
slurm = {'account': slurmerships} if slurmerships else None

tags = set()
Expand All @@ -1469,6 +1480,7 @@ def user_to_puppet(user: SiteUser):
uid=user.uid,
gid=user.gid,
groups=memberships,
group_sudo=group_sudo,
shell=user.shell,
tag=tags,
home=user.home_directory,
Expand Down Expand Up @@ -1534,8 +1546,10 @@ def site_to_puppet(sitename: str):

groups = {}
for group in SiteGroup.objects(sitename=sitename).order_by('groupname'):
if query_user_exists(group.groupname, sitename):
continue
if group.parent.type == 'user':
user = SiteUser.objects.get(username=group.groupname, sitename=sitename)
if user.uid == group.gid:
continue
groups[group.groupname] = group_to_puppet(group)

shares = {}
Expand Down Expand Up @@ -1630,7 +1644,6 @@ def site_sync_old_puppet(args: argparse.Namespace):
clean=True,
push_merge=args.push_merge) as add:
puppet_map.save_yaml(yaml_path)
add(yaml_path)

for user in SiteUser.objects(sitename=args.site).only('parent'):
if not user.parent.ssh_key:
Expand All @@ -1639,7 +1652,7 @@ def site_sync_old_puppet(args: argparse.Namespace):
with keyfile.open('w') as fp:
for key in user.parent.ssh_key:
print(key, file=fp)
add(keyfile)
add(args.repo)


@subcommand('to-ldap',
Expand Down Expand Up @@ -2663,7 +2676,8 @@ def process_createaccount_event(event: QueuedEventDataModel,
global_user = GlobalUser.from_hippo(hippo_account)
global_user.save()
global_group = GlobalGroup(groupname=username,
gid=global_user.gid)
gid=global_user.gid,
type='user')
global_group.save()
else:
logger.info(f'GlobalUser for {username} exists, checking status.')
Expand Down
15 changes: 7 additions & 8 deletions cheeto/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
USER_TYPES = {
'user',
'admin',
'system'
'system',
'class'
}

GROUP_TYPES = {
Expand Down Expand Up @@ -197,6 +198,10 @@ class _BaseModel:
SKIP_VALUES = [None, {}, []]
Schema: ClassVar[Type[_Schema]] = _Schema # For the type checker

class Meta:
ordered = True
render_module = yaml

def items(self):
return dataclasses.asdict(self).items() #type: ignore

Expand Down Expand Up @@ -224,10 +229,9 @@ def _sortable(data):
@staticmethod
def _sort(data):
if BaseModel._sortable(data):
if isinstance(data, Sequence) and not isinstance(data, (str, bytes, bytearray)):
if is_listlike(data):
return sorted(data)
elif isinstance(data, (dict, OrderedDict)):

return OrderedDict(sorted(data.items(), key=lambda t: t[0]))
else:
return data
Expand All @@ -238,11 +242,6 @@ def _sort(data):
def sort_listlikes(self, data, **kwargs):
return BaseModel._sort(data)


class Meta:
ordered = True
render_module = yaml

@classmethod
def load(cls, data: dict, **kwargs) -> Self:
return cls.Schema().load(data, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cheeto"
version = "1.0.rc5"
version = "1.0.rc6"
description = "Utilities for the UC Davis HPC Core Facility."
license = "Proprietary"
authors = [
Expand Down

0 comments on commit 273a50e

Please sign in to comment.