-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filtered WrapSpawner #15
Open
Luke035
wants to merge
7
commits into
jupyterhub:master
Choose a base branch
from
icteam-spa:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b1db794
Dummy user-based sudospawner
Luke035 dc34c53
User group based spawning
Luke035 e71bc9d
Definition of FilteredSpawner class and use of system default encodin…
6b28f8e
Tabs consistency
Luke035 fe9ae2b
Added wildcard support for global profiles
Luke035 5bb2704
Update README.md
Luke035 e96a87e
Merge pull request #1 from icteam-spa/user_based_spawning
Luke035 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,7 +145,6 @@ class ProfilesSpawner(WrapSpawner): | |
help = """List of profiles to offer for selection. Signature is: | ||
List(Tuple( Unicode, Unicode, Type(Spawner), Dict )) corresponding to | ||
profile display name, unique key, Spawner class, dictionary of spawner config options. | ||
|
||
The first three values will be exposed in the input_template as {display}, {key}, and {type}""" | ||
) | ||
|
||
|
@@ -220,6 +219,56 @@ def clear_state(self): | |
super().clear_state() | ||
self.child_profile = '' | ||
|
||
class FilteredSpawner(ProfilesSpawner): | ||
|
||
"""ProfilesSpawner - leverages the Spawner options form feature to allow user-driven | ||
configuration of Spawner classes while permitting: | ||
1) configuration of Spawner classes that don't natively implement options_form | ||
2) administrator control of allowed configuration changes | ||
3) runtime choice of which Spawner backend to launch | ||
""" | ||
default_profiles = List( | ||
trait = Tuple( Unicode(), Unicode(), Type(Spawner), Dict(), Unicode() ), | ||
default_value = [], | ||
config = True, | ||
help = """List of profiles to offer in addition to docker images for selection. Signature is: | ||
List(Tuple( Unicode, Unicode, Type(Spawner), Dict , Unicode)) corresponding to | ||
profile display name, unique key, Spawner class, dictionary of spawner config options, comma separated list of authorized groups. | ||
The first three values will be exposed in the input_template as {display}, {key}, and {type}""" | ||
) | ||
|
||
def get_user_groups(self): | ||
import subprocess, sys | ||
user = self.user.name | ||
cmd_result = subprocess.Popen("groups "+user, shell=True, stdout=subprocess.PIPE).stdout.read() | ||
#Get system default encoding | ||
sys_encoding = sys.getdefaultencoding() | ||
groups_list = cmd_result.decode(sys_encoding).split(':')[1].replace('\n', '').strip().split(' ') | ||
return groups_list | ||
|
||
def _user_profiles(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than implementing logic specific to your use case (filtering based on OS groups), maybe this filter should itself be a hook and then your configuration would register the appropriate filter function for your use case? |
||
#Parse default_profiles and return a valid list | ||
valid_profiles = [] | ||
for p in self.default_profiles: | ||
#Parse authorized groups | ||
#If authorized_groups contained the all wildcard, pass it without groups check | ||
if p[4] == '*': | ||
valid_profiles.append((p[0], p[1], p[2], p[3])) | ||
#Check if profile is enabled for the user | ||
authorized_groups = p[4].split(',') | ||
user_groups = self.get_user_groups() | ||
#Interesection between groups | ||
groups_intersection = list(set(authorized_groups).intersection(user_groups)) | ||
#If groups_intersection is not empty, profile is authorized for user | ||
if groups_intersection is not None and len(groups_intersection)>0: | ||
valid_profiles.append((p[0], p[1], p[2], p[3])) | ||
return valid_profiles | ||
|
||
@property | ||
def profiles(self): | ||
#New profiles are default profiles in addition to user_based profiles | ||
return self._user_profiles() | ||
|
||
class DockerProfilesSpawner(ProfilesSpawner): | ||
|
||
"""DockerProfilesSpawner - leverages ProfilesSpawner to dynamically create DockerSpawner | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a safe or generic way to get groups. It assumes users are local on the Hub system, which is not generally true.