Skip to content

Commit

Permalink
Exclude hidden directories from the snapshot
Browse files Browse the repository at this point in the history
Flag include_hidden is added to enable including hidden directories in
the snapshot
  • Loading branch information
xcompass committed Feb 15, 2024
1 parent 5754e36 commit bc8937e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 81 deletions.
91 changes: 16 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,11 @@ Creates a Snapshot of the all students home directories with a common name, with

#### Required Headers & Post Variables:

<table>
<tr>
<td><strong>X-Api-Key</strong></td>
<td>Header Variable</td>
<td>The API Key is Provided by UBC IT</td>
</tr>
<tr></tr>
<tr>
<td><strong>SNAPSHOT_NAME</strong></td>
<td>Post Variable</td>
<td>The Name of the Snapshot</td>
</tr>
</table>
| | | |
|----------------|-----------------|---------------------------------------|
| X-Api-Key | Header Variable | The API Key is Provided by UBC IT |
| SNAPSHOT_NAME | Post Variable | The Name of the Snapshot |
| INCLUDE_HIDDEN | Post Variable | Whether to include hidden directories |

#### Curl Command Call Examples:

Expand All @@ -397,68 +389,17 @@ user@host:~$

## Environment Variables

<div>
<table>
<thead>
<tr>
<th>Environment Variable</th>
<th>Required</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>DEBUG</td>
<td align="center"></td>
<td>TRUE</td>
<td>Enables Debug output within the API code</td>
</tr>
<tr>
<td>JUPYTER_API_PORT</td>
<td align="center"></td>
<td>5000</td>
<td>The Port Number of the API</td>
</tr>
<tr>
<td>JUPYTER_API_HOST</td>
<td align="center"></td>
<td>0.0.0.0</td>
<td>The IP the API is being served on</td>
</tr>
<tr>
<td>JUPYTER_API_KEY</td>
<td align="center">&check; </td>
<td>12345</td>
<td>The API Key</td>
</tr>
<tr>
<td>JNOTE_HOME</td>
<td align="center">&check; </td>
<td>{No Default Value}</td>
<td>The location of Jupyter Notebooks' user Home directory</td>
</tr>
<tr>
<td>JNOTE_SNAP</td>
<td align="center">&check; </td>
<td>{No Default Value}</td>
<td>The location of Jupyter Notebooks final Snapshot directory</td>
</tr>
<tr>
<td>JNOTE_INTSNAP</td>
<td align="center">&check; </td>
<td>{No Default Value}</td>
<td>The location of Jupyter Notebooks internal Snapshot directory</td>
</tr>
<tr>
<td>JNOTE_COURSE_CODE</td>
<td align="center">&check; </td>
<td>{No Default Value}</td>
<td>The Course Code</td>
</tr>
</tbody>
</table>
</div>
| Environment Variable | Required | Default Value | Description |
|----------------------|----------|------------------------------------------|---------------------------------------------------------------|
| DEBUG | TRUE | Enables Debug output within the API code |
| JUPYTER_API_PORT | 5000 | The Port Number of the API |
| JUPYTER_API_HOST | 0.0.0.0 | The IP the API is being served on |
| JUPYTER_API_KEY | &check; | 12345 | The API Key |
| JNOTE_HOME | &check; | {No Default Value} | The location of Jupyter Notebooks' user Home directory |
| JNOTE_SNAP | &check; | {No Default Value} | The location of Jupyter Notebooks final Snapshot directory |
| JNOTE_INTSNAP | &check; | {No Default Value} | The location of Jupyter Notebooks internal Snapshot directory |
| JNOTE_COURSE_CODE | &check; | {No Default Value} | The Course Code |



## Repo Files
Expand Down
29 changes: 23 additions & 6 deletions usr/share/jupyter-canvas-api/api-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
PORT = int(os.getenv('JUPYTER_API_PORT', '5000')) # API TCP Port Number
HOST = str(os.getenv('JUPYTER_API_HOST', '0.0.0.0')) # API TCP Address
APIKEY = str(os.getenv('JUPYTER_API_KEY', '12345')) # API Key Value
HOMEDIR = str(os.getenv('JNOTE_HOME', '/mnt/efs/stat-100a-home/')) # Home Directory Root
SNAPSHOT_DIR = str(os.getenv('JNOTE_SNAP', '/mnt/efs/stat-100a-snap/')) # Instructor Snapshot Directory
INTERMEDIARY_DIR = str(os.getenv('JNOTE_INTSNAP', '/mnt/efs/stat-100a-internal/')) # Intermediary Snapshot Directory
HOMEDIR = os.path.join(str(os.getenv('JNOTE_HOME', '/mnt/efs/stat-100a-home/')), '') # Home Directory Root
SNAPSHOT_DIR = os.path.join(str(os.getenv('JNOTE_SNAP', '/mnt/efs/stat-100a-snap/')), '') # Instructor Snapshot Directory
INTERMEDIARY_DIR = os.path.join(str(os.getenv('JNOTE_INTSNAP', '/mnt/efs/stat-100a-internal/')), '') # Intermediary Snapshot Directory
all_directories = [HOMEDIR, SNAPSHOT_DIR, INTERMEDIARY_DIR]
COURSE_CODE = str(os.getenv('JNOTE_COURSE_CODE', 'STAT100a')) # The API Course Code

Expand Down Expand Up @@ -589,6 +589,8 @@ def snapshot():

student_id = request.form.get('STUDENT_ID') # StudentID Post Variable
snapshot_name = request.form.get('SNAPSHOT_NAME') # SNAPSHOT_NAME Post Variable
# whether to include hidden directories
include_hidden = request.form.get('INCLUDE_HIDDEN', "false").lower() == 'true'

date = datetime.datetime.now() # Get Current Date
date = date.isoformat() # Convert to ISO Format Date
Expand Down Expand Up @@ -654,11 +656,16 @@ def snapshot():
# Create Student Home Directory Structure to Final Snapshot Directory If Missing
Path(snap_student_path).mkdir(parents=True, exist_ok=True)

options = ['-a', '-v', '-h', '-W']
if include_hidden:
exclusions = None
else:
exclusions = ['.*']
# RSYNC Student Home to Intermediate Snapshot Directory
sysrsync.run(source=student_path,
destination=intsnap_student_path,
sync_source_contents=True,
options=['-a', '-v', '-h', '-W', '--no-compress'])
options=options, exclusions=exclusions)

# Move Int Snap to Final Snap Location with New Name
shutil.move(intsnap_student_path, snap_name_path)
Expand All @@ -675,12 +682,15 @@ def snapshot():
#
# Curl Usage Command Examples For '/snapshot_all' API Call
# Required Post Variables: SNAPSHOT_NAME
# Optional Post Variables: INCLUDE_HIDDEN
# Required Header Variables: X-Api-Key
# Example Response:
#
# curl -X POST -H "X-Api-Key: 12345" -F "STUDENT_NAME=assignment-1-snap-all" http://localhost:5000/snapshot_all
# curl -X POST -H "X-Api-Key: 12345" -d "STUDENT_NAME=assignment-1-snap-all" http://localhost:5000/snapshot_all
# curl -X POST -H "X-Api-Key: 12345" -data "STUDENT_NAME=assignment-1-snap-all" http://localhost:5000/snapshot_all
# curl -X POST -H "X-Api-Key: 12345" -d "STUDENT_NAME=assignment-1-snap-all" -d "INCLUDE_HIDDEN=true"
# http://localhost:5000/snapshot_all
#
@app.route('/snapshot_all', methods=['POST'])
@requires_apikey
Expand All @@ -689,6 +699,8 @@ def snapshot_all():
""" Create a Snapshot of tll the Student's Home Directories with the Specified Snapshot Name. """

snapshot_name = request.form.get('SNAPSHOT_NAME') # SNAPSHOT_NAME Post Variable
# whether to include hidden directories
include_hidden = request.form.get('INCLUDE_HIDDEN', "false").lower() == 'true'

date = datetime.datetime.now() # Get Current Date
date = date.isoformat() # Convert to ISO Format Date
Expand Down Expand Up @@ -744,17 +756,22 @@ def snapshot_all():
lockfile_obj = open(lockfile, 'w+') # Open Lock File, Create if Does Not Exist
fcntl.flock(lockfile_obj, fcntl.LOCK_EX | fcntl.LOCK_NB) # Create Non Blocking Exclusive Flock
break # Break Out of While Loop if no Errors
except:
except Exception as e:
time.sleep(2)

# Create Student Home Directory Structure to Final Snapshot Directory If Missing
Path(snap_student_path).mkdir(parents=True, exist_ok=True)

# RSYNC Student Home to Intermediate Snapshot Directory
options = ['-a', '-v', '-h', '-W']
if include_hidden:
exclusions = None
else:
exclusions = ['.*']
sysrsync.run(source=student_path,
destination=intsnap_student_path,
sync_source_contents=True,
options=['-a', '-v', '-h', '-W', '--no-compress'])
options=options, verbose=True, exclusions=exclusions)

# Move Int Snap to Final Snap Location with New Name
shutil.move(intsnap_student_path, snap_name_path)
Expand Down

0 comments on commit bc8937e

Please sign in to comment.