Created
June 16, 2022 05:34
-
-
Save vietvudanh/75f46cbad9acaf12f8019f82f7556389 to your computer and use it in GitHub Desktop.
cred
This file contains hidden or 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
# 1. python file | |
sudo mkdir -p /opt/cred | |
cat <<ETO > /opt/cred/creds.py | |
import argparse | |
import sys | |
import yaml | |
parser = argparse.ArgumentParser(description='find creds or execute credential') | |
parser.add_argument('--mode', help='mode to run') | |
parser.add_argument('--name', help='name to find') | |
parser.add_argument('--cli', action='store_true', default=False) | |
parser.add_argument('--db_type', dest='db_type', default='mysql') | |
args = parser.parse_args() | |
data=yaml.load(open('/apps/jupyter/common/production_credentials/credentials', 'r'), Loader=yaml.SafeLoader) | |
if args.mode == 'find': | |
print(list(data.keys())) | |
elif args.mode == 'print': | |
cred=data[args.name] | |
if not args.cli: | |
print(cred) | |
cred['user'] = cred['username'] | |
cred['database'] = cred['db'] | |
print('mysql -u {user} -h {host} --port {port} -D {database} -p{password}'.format(**cred)) | |
elif args.mode == 'dump': | |
print(data) | |
ETO | |
# alias | |
cat <<EOT > /bin/cred_list | |
#!/bin/bash | |
/apps/anaconda2/envs/notebook-kernel-py3.6.10-pyspark3.0.0/bin/python /opt/cred/creds.py --mode find | |
EOT | |
sudo chmod +x /bin/cred_list | |
cat <<EOT > /bin/cred_print | |
#!/bin/bash | |
/apps/anaconda2/envs/notebook-kernel-py3.6.10-pyspark3.0.0/bin/python /opt/cred/creds.py --mode "print" --name \$1 | |
EOT | |
sudo chmod +x /bin/cred_print | |
cat <<EOT > /bin/cred | |
#!/bin/bash | |
OUTPUT=\$(/apps/anaconda2/envs/notebook-kernel-py3.6.10-pyspark3.0.0/bin/python /opt/cred/creds.py --mode "print" --name \$1 --cli) | |
eval $OUTPUT | |
EOT | |
sudo chmod +x /bin/cred | |
sudo chown -R :jupyter /opt/cred | |
sudo chmod -R 775 /opt/cred |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment