Skip to content

Commit

Permalink
feat: extend hpcuser with login_shell and home_directory (#173) (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo authored Feb 14, 2024
1 parent 784fd2f commit 80ab1b0
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.2.10 on 2024-02-14 15:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("usersec", "0017_remove_hpcuser_uid_remove_hpcuserversion_uid"),
]

operations = [
migrations.AddField(
model_name="hpcuser",
name="home_directory",
field=models.CharField(
default="/data/cephfs-1/home/users/{user.username}",
help_text="Path to the user home directory on the cluster",
max_length=64,
),
preserve_default=False,
),
migrations.AddField(
model_name="hpcuser",
name="login_shell",
field=models.CharField(
choices=[
("/usr/bin/bash", "/usr/bin/bash"),
("/usr/bin/fish", "/usr/bin/fish"),
("/usr/bin/zsh", "/usr/bin/zsh"),
("/usr/bin/sh", "/usr/bin/sh"),
],
default="/usr/bin/bash",
help_text="Login shell of the user on the cluster",
max_length=32,
),
),
migrations.AddField(
model_name="hpcuserversion",
name="home_directory",
field=models.CharField(
default="/data/cephfs-1/home/users/{user.username}",
help_text="Path to the user home directory on the cluster",
max_length=64,
),
preserve_default=False,
),
migrations.AddField(
model_name="hpcuserversion",
name="login_shell",
field=models.CharField(
choices=[
("/usr/bin/bash", "/usr/bin/bash"),
("/usr/bin/fish", "/usr/bin/fish"),
("/usr/bin/zsh", "/usr/bin/zsh"),
("/usr/bin/sh", "/usr/bin/sh"),
],
default="/usr/bin/bash",
help_text="Login shell of the user on the cluster",
max_length=32,
),
),
]
34 changes: 34 additions & 0 deletions usersec/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@
(INVITATION_STATUS_EXPIRED, INVITATION_STATUS_EXPIRED),
)

#: Login shell bash.
LOGIN_SHELL_BASH = "/usr/bin/bash"

#: Login shell fish.
LOGIN_SHELL_FISH = "/usr/bin/fish"

#: Login shell zsh.
LOGIN_SHELL_ZSH = "/usr/bin/zsh"

#: Login shell sh.
LOGIN_SHELL_SH = "/usr/bin/sh"

#: Login shell choices.
LOGIN_SHELL_CHOICES = [
(LOGIN_SHELL_BASH, LOGIN_SHELL_BASH),
(LOGIN_SHELL_FISH, LOGIN_SHELL_FISH),
(LOGIN_SHELL_ZSH, LOGIN_SHELL_ZSH),
(LOGIN_SHELL_SH, LOGIN_SHELL_SH),
]


# ------------------------------------------------------------------------------
# Mixins
Expand Down Expand Up @@ -331,6 +351,20 @@ class Meta:
#: Expiration date of the user account
expiration = models.DateTimeField(help_text="Expiration date of the user account")

#: Home directory of the user on the cluster.
home_directory = models.CharField(
max_length=64,
help_text="Path to the user home directory on the cluster",
)

#: Login shell of the user on the cluster.
login_shell = models.CharField(
max_length=32,
help_text="Login shell of the user on the cluster",
choices=LOGIN_SHELL_CHOICES,
default=LOGIN_SHELL_BASH,
)


class HpcUser(VersionManagerMixin, HpcUserAbstract):
"""HpcUser model"""
Expand Down
4 changes: 4 additions & 0 deletions usersec/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class HpcUserAbstractSerializer(HpcObjectAbstractSerializer):
first_name = serializers.SerializerMethodField()
last_name = serializers.SerializerMethodField()
phone_number = serializers.SerializerMethodField()
home_directory = serializers.CharField()
login_shell = serializers.CharField()

def get_full_name(self, obj) -> str:
return obj.user.name
Expand Down Expand Up @@ -65,6 +67,8 @@ class Meta:
"uid",
"username",
"expiration",
"home_directory",
"login_shell",
]


Expand Down
1 change: 1 addition & 0 deletions usersec/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class Meta:
description = "this is a user"
username = factory.Sequence(lambda n: f"user{n}_" + settings.INSTITUTE_USERNAME_SUFFIX)
expiration = datetime(2050, 1, 1, tzinfo=utc)
home_directory = factory.LazyAttribute(lambda o: f"/data/cephfs-1/home/users/{o.username}")


class HpcUserRequestFactoryBase(HpcRequestFactoryBase):
Expand Down
2 changes: 2 additions & 0 deletions usersec/tests/snapshots/snap_test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
"expiration": "2050-01-01T00:00:00Z",
"first_name": None,
"full_name": "name_placeholder",
"home_directory": "/data/cephfs-1/home/users/user0_c",
"last_name": None,
"login_shell": "/usr/bin/bash",
"phone_number": "phone_number_placeholder",
"primary_group": "primary_group_uuid_placeholder",
"resources_requested": {"null": "null"},
Expand Down
4 changes: 2 additions & 2 deletions usersec/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.urls import reverse
from factory import SubFactory
from factory import LazyAttribute, SubFactory
from test_plus.test import TestCase

from usersec.models import (
Expand Down Expand Up @@ -318,7 +318,7 @@ def _test_save_with_version_new(self, **supplementaries):
data.update(supplementaries)

for k, v in data.items():
if not isinstance(v, SubFactory):
if not isinstance(v, SubFactory) and not isinstance(v, LazyAttribute):
setattr(obj, k, v)

obj.save_with_version()
Expand Down
11 changes: 10 additions & 1 deletion usersec/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
COMMENT_REACTIVATED = "Request re-activated"
COMMENT_RETRACTED = "Request retracted"

# -----------------------------------------------------------------------------
# Other
# -----------------------------------------------------------------------------

DEFAULT_HOME_DIRECTORY = "/data/cephfs-1/home/users/{username}"


class HpcPermissionMixin(LoginRequiredMixin, PermissionRequiredMixin):
"""Customized required login and permission mixin."""
Expand Down Expand Up @@ -1496,14 +1502,17 @@ def get(self, request, *args, **kwargs):
try:
from adminsec.views import django_to_hpc_username

username = django_to_hpc_username(obj.username)

hpcuser = HpcUser.objects.create_with_version(
user=request.user,
primary_group=obj.hpcusercreaterequest.group,
resources_requested=obj.hpcusercreaterequest.resources_requested,
creator=obj.hpcusercreaterequest.editor,
username=django_to_hpc_username(obj.username),
username=username,
status=OBJECT_STATUS_ACTIVE,
expiration=obj.hpcusercreaterequest.expiration,
home_directory=DEFAULT_HOME_DIRECTORY.format(username=username),
)

except Exception as e:
Expand Down

0 comments on commit 80ab1b0

Please sign in to comment.