Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejeda, Engelbert committed May 6, 2022
0 parents commit efb0831
Show file tree
Hide file tree
Showing 18 changed files with 1,430 additions and 0 deletions.
106 changes: 106 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
.dist
dist/
eggs/
.eggs/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
## ZipApp Releases
release

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# vagrant
.vagrant
30 changes: 30 additions & 0 deletions aws/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import argparse
import boto3

def parse_args():
parser = argparse.ArgumentParser(description="List ec2 Instances by naming pattern")
parser.add_argument('--aws-profile-name','-p', required=True)
parser.add_argument('--aws-region','-r', default='us-east-1', required=False)
parser.add_argument('--ec2-naming-pattern','-n', required=True)
parser.add_argument('--verbose','-v', action='store_true', default=False)
return parser.parse_known_args()

# CLI Args
args, unknown= parse_args()

ec2_instance_name_values = args.ec2_naming_pattern.split('|')
aws_profile_name = args.aws_profile_name
aws_region = args.aws_region

boto3.setup_default_session(profile_name=aws_profile_name)
ec2 = boto3.resource('ec2', region_name=aws_region)
filters = [{'Name':'tag:Name', 'Values':ec2_instance_name_values}]
instances = ec2.instances.filter(Filters=filters)

for instance in instances:
instance_name = [t.get('Value') for t in instance.tags if t.get('Key') == 'Name'][0]
print(f"Name: {instance_name}, Id: {instance.id}, State: {instance.state['Name']}")

ec2_snapshots = ec2.describe_snapshots()['Snapshots']
for snap in ec2_snapshots:
print(snap)
68 changes: 68 additions & 0 deletions confluence/search.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

script_dir=${0%/*}
script_name=${0##*/}
script_base_name=${script_name%%.*}
script_dir_name=${script_dir##*/}
command_name="${script_dir_name}.${script_base_name}"
__docstring__="Search Confluence Wiki using CQL"
numargs=$#
search_term=$*


EXAMPLES="""
e.g.
* Search for pages with a parent id of 123456, modified after March 20th, 2021
${command_name} 'type=page and parent=123456 and lastmodified>2021-03-20'
"""

USAGE="""
Usage: ${script_dir_name}.${script_base_name}
Script to search Confluence wiki using Confluence Query Language (CQL)
Read More: https://developer.atlassian.com/server/confluence/advanced-searching-using-cql/
param: [--username|-u <Confluence Username>]
param: [--password|-p <Confluence Password>]
param: [--dry-run|--dry] # Do nothing except echo the commands
param: [--use-cred-mgr] # See --help usage
param: [--verbose] # Show informational output
<search term>
"""

# CLI
while (( "$#" )); do
if [[ "$1" =~ ^--username$|^-u$ ]]; then user_name="${2}";shift;fi
if [[ "$1" =~ ^--password$|^-p$ ]]; then password="${2}";shift;fi
if [[ "$1" =~ ^--base-url$|^-b$ ]]; then baseurl="${2}";shift;fi
if [[ "$1" =~ ^--dry-run$|^--dry$ ]]; then action=echo;fi
if [[ "$1" =~ ^--help$|^--help$ ]]; then help=true;fi
if [[ "$1" =~ ^--help-extended$ ]]; then help_extended=true;fi
if [[ "$1" =~ ^--verbose$ ]]; then verbose=true;fi
shift
done

if [[ (-z $username) && (-z $password) && (-z $help) && (-z $help_extended) ]];then
echo "Warning: Values for Username and password are empty!"
echo -e "${USAGE}"
exit 0
elif [[ ((-z $username) || (-z $password)) && (-z $help) && (-z $help_extended) ]];then
echo "Warning: Values for either of username or password is empty!"
echo -e "${USAGE}"
exit 0
elif [[ (-n $help) ]];then
echo -e "${USAGE}"
exit 0
elif [[ (-n $help_extended) ]];then
echo -e """${USAGE}
Examples:
${EXAMPLES}
"""
exit 0
fi

if [[ -n $verbose ]];then
echo "Search Term is: ${search_term}"
fi

echo "Searching against ${baseurl?Must provide baseurl (-b)}"
curl -L -s -u "${username}:${password}" -G "${baseurl}/rest/api/content/search" \
--data-urlencode "cql=(${search_term})"
46 changes: 46 additions & 0 deletions git/create-issue-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

script_dir=${0%/*}
script_name=${0##*/}
script_base_name=${script_name%%.*}
script_dir_name=${script_dir##*/}
__docstring__="Create a git issue branch"

USAGE="""
Usage: ${script_dir_name}.${script_base_name}
param: [--branch-name|-b <Name of the Git Branch>]
param: --change-context|-c <Name of the change context>
param: --branch-type|-t <Branch Type, e.g. feature, bugfix, hotfix>
param: [--dry-run|--dry # Do nothing except echo the commands]
param: [--use-cred-mgr This implicit option
populates the username/password variables from
the Operating System's Credential Manager
It expects an entry in the credential manager
for '${script_dir_name}.${script_base_name}'
Note: This must be a Generic Credential for Windows Hosts]
"""

numargs=$#

# CLI
while (( "$#" )); do
if [[ "$1" =~ ^--branch-name$|^-b$ ]]; then branch_name="${2}";shift;fi
if [[ "$1" =~ ^--branch-type$|^-t$ ]]; then branch_type="${2}";shift;fi
if [[ "$1" =~ ^--branch-context$|^-c$ ]]; then branch_context="${2}";shift;fi
if [[ "$1" =~ ^--dry-run$|^--dry$ ]]; then action=echo;fi
if [[ "$1" =~ ^--help$|^--help$ ]]; then help=true;fi
shift
done

if [[ (numargs -lt 1) || (-n $help) ]];then
echo -e "${USAGE}"
exit 0
fi

dtm=$(date +%Y%m%d/%H%M);
if [[ -z $branch_name ]]; then
final_branch_name=${username-$USERNAME}/$(git rev-parse --abbrev-ref HEAD)/${branch_type}/${dtm}/${branch_context};
else
final_branch_name=${username-$USERNAME}/${branch_name-$default_branch_name}/${branch_type}/${dtm}/${branch_context};
fi;
${action-eval} git checkout -b ${final_branch_name}
76 changes: 76 additions & 0 deletions harbor/add-latest-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash

script_dir=${0%/*}
script_name=${0##*/}
script_base_name=${script_name%%.*}
script_dir_name=${script_dir##*/}
environment_name=${script_dir##*/}

__docstring__="Add 'latest' tag to specified application matching specified tag "

USAGE="""
Usage: ${script_dir_name}.${script_base_name}
param: [--username|-u <Harbor Username>]
param: [--password|-p <Harbor Password>]
param: [--harbor-url|-h <Harbor URL>]
param: [--harbor-project|-r <Harbor Project Name>]
param: [--application|-a <Application Name>]
param: [--dry-run|--dry # Do nothing except echo the underlying commands]
param: [--use-cred-mgr This implicit option
populates the username/password variables from
the Operating System's Credential Manager
It expects an entry in the credential manager
for '${script_dir_name}.${script_base_name}'
Note: This must be a Generic Credential for Windows Hosts]
"""

numargs=$#

# CLI
while (( "$#" )); do
if [[ "$1" =~ ^--dry-run$|^--dry$ ]]; then action=echo;fi
if [[ "$1" =~ ^--help$|^--help$ ]]; then help=true;fi
if [[ "$1" =~ ^--username$|^-u$ ]]; then user="${2}";shift;fi
if [[ "$1" =~ ^--password$|^-p$ ]]; then pass="${2}";shift;fi
if [[ "$1" =~ ^--harbor-url$|^-H$ ]]; then harbor_url="${2}";shift;fi
if [[ "$1" =~ ^--harbor-project$|^-r$ ]]; then harbor_project="${2}";shift;fi
if [[ "$1" =~ ^--application$|^-a$ ]]; then application="${2}";shift;fi
if [[ "$1" =~ ^--image-tag$|^-t$ ]]; then tag_search="${2}";shift;fi
shift
done

if [[ (numargs -lt 1) || (-n $help) ]];then
echo -e "${USAGE}"
exit 0
fi

echo "Querying Harbor URL: ${harbor_url?Must specify harbor URL (-H)}"

app_ref="${harbor_project?Must specify harbor project name}/${application?Must specify application name}"

# Get the digest for the 'latest' Tag for a given repository
echo "Retrieving 'latest' tag digest for ${app_ref} ..."
sha=$(curl -s "http://${harbor_url}/api/v2.0/projects/${harbor_project}/repositories/${application}/artifacts?q=tags=latest" | jq -r '.[].digest')
if [[ -n $sha ]];then
# Delete 'latest' tag
echo "The digest for tag 'latest' for ${app_ref} is ${sha} ..."
echo -n "Deleting 'latest' tag digest for ${app_ref} ... "
${action} curl -s -u "${username}:${password}" -X DELETE "http://${harbor_url}/api/v2.0/projects/${harbor_project}/repositories/${application}/artifacts/${sha}/tags/latest"
echo "Done"
fi

# Add a tag 'latest' to specified image
if [[ -n $tag_search ]];then
newest_artifact_name=$(curl -s "http://${harbor_url}/api/v2.0/projects/${harbor_project}/repositories/${application}/artifacts?q=tags=${tag_search}&page_size=100" | jq -r ''' .[].tags | select( . != null ) | sort_by(.push_time) | .[].name ''' | head -1)
else
newest_artifact_name=$(curl -s "http://${harbor_url}/api/v2.0/projects/${harbor_project}/repositories/${application}/artifacts?q=tags!=latest&page_size=100" | jq -r ''' .[].tags | select( . != null ) | sort_by(.push_time) | .[].name ''' | head -1)
fi

if [[ -n $newest_artifact_name ]];then
echo -n "Adding 'latest' tag to ${app_ref} with tag ${newest_artifact_name} ... "
sha=$(curl -s "http://${harbor_url}/api/v2.0/projects/${harbor_project}/repositories/${application}/artifacts?q=tags=${newest_artifact_name}" | jq -r '.[].digest')
${action} curl -s -H 'Content-Type: application/json' -u "${username}:${password}" -X POST "http://${harbor_url}/api/v2.0/projects/${harbor_project}/repositories/${application}/artifacts/${sha}/tags" --data-raw '{"name":"latest"}'
echo "Done"
else
echo "Couldn't determine target"
fi
51 changes: 51 additions & 0 deletions harbor/check-storage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

script_dir=${0%/*}
script_name=${0##*/}
script_base_name=${script_name%%.*}
script_dir_name=${script_dir##*/}
environment_name=${script_dir##*/}
__docstring__="Check project storage quotas for specified Harbor instance"

USAGE="""
Usage: ${script_dir_name}.${script_base_name}
param: [--username|-u <Harbor Username>]
param: [--password|-p <Harbor Password>]
param: [--dry-run|--dry # Do nothing except echo the underlying commands]
param: [--use-cred-mgr This implicit option
populates the username/password variables from
the Operating System's Credential Manager
It expects an entry in the credential manager
for '${script_dir_name}.${script_base_name}'
Note: This must be a Generic Credential for Windows Hosts]
"""

numargs=$#

# CLI
while (( "$#" )); do
if [[ "$1" =~ ^--dry-run$|^--dry$ ]]; then action=echo;fi
if [[ "$1" =~ ^--help$|^--help$ ]]; then help=true;fi
if [[ "$1" =~ ^--username$|^-u$ ]]; then user="${2}";shift;fi
if [[ "$1" =~ ^--password$|^-p$ ]]; then pass="${2}";shift;fi
if [[ "$1" =~ ^--harbor-url$|^-H$ ]]; then harbor_url="${2}";shift;fi
shift
done
if [[ (numargs -lt 1) || (-n $help) ]];then
echo -e "${USAGE}"
exit 0
fi

echo "Querying Harbor at ${harbor_url-?Must specify harbor URL (-H)} for specified tag pattern ..."

mapfile -t used <<< $(curl -u "$user":"$pass" -i -k -X GET "${harbor_url}/api/v2.0/quotas" | grep -A 2 used | grep storage | awk '//{print $2 }')

for (( i=1; i<${#used[@]}; i++ ))
do
count=$(echo "${total[$i]} - ${used[$i]}"|bc)
if [[ $count -lt 4000000000 ]] && [[ "${total[$i]}" -ne -1 ]]
then
echo "The ${names[$i]} Harbor project is low on storage"
fi
done

Loading

0 comments on commit efb0831

Please sign in to comment.