Skip to content

Commit

Permalink
Merge pull request #5 from Selerity/update-code
Browse files Browse the repository at this point in the history
[viya4-home-dir-builder] Add dry run mode
  • Loading branch information
SelerityMichael authored May 16, 2022
2 parents 912c723 + 21b616b commit 372cb12
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion charts/viya4-home-dir-builder/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ maintainers:

type: application

version: 0.1.3
version: 0.2.0

appVersion: "3"
2 changes: 2 additions & 0 deletions charts/viya4-home-dir-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ At a minimum you must provide values for `viya.base_url` and `nfs.server`. All
helm show values selerity/viya4-home-dir-builder
```

The default settings will create a kubernetes Cron Job that must be triggered manually (i.e. suspended), and when triggered will only report on what it will do (i.e. it will perform a `dry run`). To allow the process to create/update home directories add the `--set dry_run=0` option to the command line.

## Install Chart

```
Expand Down
81 changes: 50 additions & 31 deletions charts/viya4-home-dir-builder/home_dir_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'requests'])
import requests
import logging

viya_base_url = os.environ.get('VIYA_BASE_URL')
client_id = os.environ.get('CLIENT_ID')
Expand All @@ -17,6 +18,16 @@
home_dir_path = os.environ.get('HOME_DIR_PATH')
debug_flag = os.environ.get('DEBUG')
user_exceptions = os.environ.get('USER_EXCEPTIONS')
dry_run = os.environ.get('DRY_RUN', '1')

if debug_flag == "1":
loglevel = logging.DEBUG
else:
loglevel = logging.INFO

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', stream = sys.stdout, level=loglevel)
log = logging.getLogger()
log.info("Starting execution.")

def oauth_client_exists(consul_token, viya_base_url, client_id):
# Request OAuth token
Expand Down Expand Up @@ -69,7 +80,7 @@ def get_token(consul_token, viya_base_url, client_id, client_secret, retry=0):
response = requests.request("POST", url, headers=headers, data=payload, auth=(client_id, client_secret))
if response.status_code != 200:
if retry == 0:
print("The OAuth Client has become corrupted. Recreating it...")
log.info("The OAuth Client has become corrupted. Recreating it...")
delete_oauth_client(consul_token, viya_base_url, client_id)
register_oauth_client(consul_token, viya_base_url, client_id, client_secret)
access_token = get_token(consul_token, viya_base_url, client_id, client_secret, retry=1)
Expand All @@ -89,7 +100,7 @@ def get_uids(viya_base_url, access_token):
uid[user['id']] = response.json()["uid"]
return(uid)

def home_dir_builder(consul_token, viya_base_url, client_id, client_secret, home_dir_path, user_exceptions):
def home_dir_builder(consul_token, viya_base_url, client_id, client_secret, home_dir_path, user_exceptions, dry_run):
access_token = get_token(consul_token, viya_base_url, client_id, client_secret)
uids = get_uids(viya_base_url, access_token)
# Get a list of all home dirs
Expand All @@ -102,55 +113,63 @@ def home_dir_builder(consul_token, viya_base_url, client_id, client_secret, home
# Process list of users found in Viya
for uid in uids:
if uid in home_dirs:
print(f"Found a matching home directory for {uid}...")
log.info(f"Found a matching home directory for {uid}...")
if uids[uid] != home_dirs[uid].stat().st_uid:
print(f"uid for {home_dirs[uid]} is different. Directory: {home_dirs[uid].stat().st_uid}, Viya: {uids[uid]}. Changing...")
try:
os.chown(home_dirs[uid], uids[uid], 1001)
except:
print(f" ERROR: Unable to change owner for {home_dirs[uid]}")
log.info(f"uid for {home_dirs[uid]} is different. Directory: {home_dirs[uid].stat().st_uid}, Viya: {uids[uid]}. Changing...")
if dry_run == '0':
try:
os.chown(home_dirs[uid], uids[uid], 1001)
log.info(f"uid changed on {home_dirs[uid]} to {uids[uid]}")
except:
log.error(f"Unable to change owner for {home_dirs[uid]}")
else:
log.info("DRY RUN: No action taken.")
else:
log.info(f"uid of {home_dirs[uid]} is correct ({uids[uid]})")
else:
if uid in user_exceptions:
print(f"Home directory for {uid} doesn't exist, but is being ignored due to exception list")
log.info(f"Home directory for {uid} doesn't exist, but is being ignored due to exception list")
else:
print(f"Home directory for {uid} doesn't appear to exist.")
try:
new_home_dir = Path(home, uid)
new_home_dir.mkdir(mode=750)
print(f" Created home directory for {uid}")
log.info(f"Home directory for {uid} doesn't appear to exist. Creating...")
if dry_run == '0':
try:
os.chown(new_home_dir, uids[uid], 1001)
new_home_dir = Path(home, uid)
new_home_dir.mkdir(mode=750)
log.info(f"Created home directory for {uid}")
try:
os.chown(new_home_dir, uids[uid], 1001)
except:
log.error(f"Unable to change owner for {new_home_dir}")
except:
print(f" ERROR: Unable to change owner for {new_home_dir}")
except:
print(f" ERROR: Unable to create home directory for {uid}")
log.error(f"Unable to create home directory for {uid}")
else:
log.info("DRY RUN: Action not taken.")

def printVariables(viya_base_url, client_id, client_secret, consul_token, home_dir_path, user_exceptions):
print(f"VIYA_BASE_URL : {viya_base_url}")
print(f"CLIENT_ID : {client_id}")
print(f"CLIENT_SECRET : {client_secret}")
print(f"CONSUL_TOKEN : {consul_token}")
print(f"HOME_DIR_PATH : {home_dir_path}")
print(f"USER_EXCEPTIONS: {user_exceptions}")
log.debug(f"VIYA_BASE_URL : {viya_base_url}")
log.debug(f"CLIENT_ID : {client_id}")
log.debug(f"CLIENT_SECRET : {client_secret}")
log.debug(f"CONSUL_TOKEN : {consul_token}")
log.debug(f"HOME_DIR_PATH : {home_dir_path}")
log.debug(f"USER_EXCEPTIONS: {user_exceptions}")

# Main
if user_exceptions == None:
user_exceptions = str()
user_exceptions = ''.join(user_exceptions.split()).split(',')

if debug_flag == "1":
printVariables(viya_base_url, client_id, client_secret, consul_token, home_dir_path, user_exceptions)
printVariables(viya_base_url, client_id, client_secret, consul_token, home_dir_path, user_exceptions)

if consul_token == None or viya_base_url == None or client_id == None or client_secret == None or home_dir_path == None:
print('Environment variables not set correctly.')
log.error('Environment variables not set correctly.')
printVariables(viya_base_url, client_id, client_secret, consul_token, home_dir_path, user_exceptions)
else:
# Check if our OAuth Client exists or not, and create it if not
if oauth_client_exists(consul_token, viya_base_url, client_id) == False:
print(f"OAuth client does not exist. Creating...")
log.info(f"OAuth client does not exist. Creating...")
register_oauth_client(consul_token, viya_base_url, client_id, client_secret)
if oauth_client_exists(consul_token, viya_base_url, client_id) == False:
print(f"Error creating OAuth client {client_id}")
log.error(f"Error creating OAuth client {client_id}")
# Run homedir builder
home_dir_builder(consul_token, viya_base_url, client_id, client_secret, home_dir_path, user_exceptions)
print("Ending execution.")
home_dir_builder(consul_token, viya_base_url, client_id, client_secret, home_dir_path, user_exceptions, dry_run)
log.info("Ending execution.")
3 changes: 2 additions & 1 deletion charts/viya4-home-dir-builder/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data:
CLIENT_ID: "{{ .Values.oauth.client_id }}"
HOME_DIR_PATH: "{{ .Values.viya.home_dir_location }}"
USER_EXCEPTIONS: "{{ .Values.viya.user_exceptions }}"
DEBUG: "{{ .Values.debug }}"
DEBUG: "{{ .Values.debug }}"
DRY_RUN: "{{ .Values.dry_run }}"
3 changes: 2 additions & 1 deletion charts/viya4-home-dir-builder/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ schedule: 0,15,30,45 * * * *
# If suspend is set to true, then you must manually trigger the job
# If suspend is set to false, then the job will run on the schedule above
suspend: true
debug: 1
debug: 0
dry_run: 1

viya:
# The base URL of SAS Viya. e.g. https://viya.company.com
Expand Down

0 comments on commit 372cb12

Please sign in to comment.