Posted on 12-07-2021 10:02 AM
I have a Mac lab running Catalina and Python 3.x (multiple versions) from python.org, not Apple's built-in Python.
We would like to deploy Python virtual environments as follows— the first 2 are easy enough but I'm stuck on #3:
Additionally, we'll need a script to return the user space to clean state at login, but that shouldn't be hard.
Posted on 12-20-2021 09:22 AM
not surprisingly, the hard part was easy and the easy part is hard.
I’ve got the Python virtualenvs under control. Now I’m fighting with a bash script to reset the user home to a clean state on login.
Here’s the script:
#!/bin/bash
## variable setup
## set the userList
userList=/Library/CS_lab_items/users
## who's logging in?
theUser=$(whoami)
## create the courses group if it doesn't already exist
groupExists=$(dscl . list /Groups | grep cs_courses) ;
if test -z "$groupExists"
then
echo "group does not exist; creating group"
/usr/bin/dscl . -create /Groups/cs_courses
/usr/bin/dscl . -append /Groups/cs_courses RealName 'CS_course_accounts'
else
echo "group already exists"
fi
## get all CS Course Account usernames & dump to text file
/usr/bin/dscl . -list /Users | grep cs_ > "$userList" ;
## add all CS Course Account users to group
while read thisUser; do
/usr/bin/dscl . -append /Groups/cs_courses GroupMembership "$thisUser" ;
done < "$userList" ;
# who's logging in and are they in 'courses' group?
if grep -q "$theUser" "$userList" ; then
## nuke the userspace & create clean from user template
/usr/local/bin/rsync -rltvh --delete --force --ignore-errors '/Library/User Template/Non_localized'/* /Users/"$theUser"/
## copy any specific per-user items
/usr/local/bin/rsync -rltvh /Library/CS_lab_items/"$theUser"/* /Users/"$theUser"/
fi