Skip to content

Instantly share code, notes, and snippets.

View kaugm's full-sized avatar
💭
Learning GCP & Kubernetes

Karl Martin kaugm

💭
Learning GCP & Kubernetes
View GitHub Profile
@kaugm
kaugm / ip_address.py
Last active April 18, 2022 11:35
IP Address class for use in Python scripts
import re
'''IP Address Class'''
class Address:
# Clean up user input and split into ip and mask
# Accepted input format -> '192.168.1.4/24'
def __init__(self, address):
regex = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+$"
@kaugm
kaugm / contrast.py
Created September 30, 2021 17:23
Create contrasting color for foreground text and elements
import re
def contrast(basecolor, format='HEX'):
'''
Create a contrasting color for foreground text and elements
Depending on the color [argument], return either black or white hex code
Useful for automatically coloring text which overlays a background
Output formats: HEX (hexidecimal) and RGB (as tuple)
'''
@kaugm
kaugm / filter.py
Last active July 20, 2022 13:28
Filter and pretty-print labels of Kubernetes Pods and Nodes for better reading and debugging
#!/opt/homebrew/bin/python3
''' Help: Ensure filter.py is executable -> 'chmod u+x filter.py'
Help: Ensure path to python executable is correct on first line -> 'which python3'
Help: filter.py -h '''
import re, sys, argparse
class KObject:
'''Kubernetes node or pod object'''
@kaugm
kaugm / policy.py
Last active June 2, 2022 14:04
Simple script to compare a CloudFormation template against a master template and output any missing permissions. Intended use for checking Eco policy.
#!/opt/homebrew/bin/python3
''' Help: Ensure policy.py is executable -> 'chmod u+x policy.py'
Help: Ensure path to python executable is correct on first line -> 'which python3'
'''
import re, sys, urllib.request, os
permissions_regex = '"([a-zA-z2-3]+:[a-zA-z2-3\*]+)"'
eco_policy_url = ''
eco_policy_url_old = ''
spot_policy_url = ''
@kaugm
kaugm / mysql_connection.py
Last active April 11, 2023 14:32
MySQL Connection Class Object
class MySQL_Connection:
"""Connection to MySQL database
Arguments:
HOST: <class 'str'> Full hostname to connect to
DATABASE: <class 'str'> Database within host
"""
# Get Database Credentials From Environment Variables
DB_USERNAME = os.environ.get('SPOT_DB_USERNAME')
@kaugm
kaugm / ocean_costs.py
Last active January 10, 2023 21:25
Spot.io Ocean - Get Cluster Costs - Python SDK Example
#!/opt/homebrew/bin/python3
import os
from datetime import datetime
try:
from spotinst_sdk2 import SpotinstSession
from spotinst_sdk2.models.ocean.aws import *
except ModuleNotFoundError:
print('Spotinst SDK Library not installed. Exiting..')
@kaugm
kaugm / spotinst_api.py
Last active May 4, 2023 18:34
Spot.io API Calls via Python
#!/opt/homebrew/bin/python3
"""
Perform Spot.io API calls with Python
Ensure body JSON is correct when performing API calls, otherwise the response will be 'bad request'.
"""
try:
import requests
import os
import json
@kaugm
kaugm / export_rightsizing_recommendations.py
Created March 2, 2023 20:44
Export Spot.io Ocean Rightsizing Recommendations to CSV
#!/opt/homebrew/bin/python3
try:
import requests
import os
import json
import re
import csv
from datetime import datetime
except ModuleNotFoundError:
@kaugm
kaugm / spot_api.sh
Created March 23, 2023 21:26
Call the Spot.io API with a shell script
#!/usr/bin/env bash
# This script shows how to call the Spot.io API via a shell script and contains 4 examples for HTTP methods: GET, POST, PUT, and DELETE
# Requires jq
# Written by Karl Martin
# [Optional] Set Spot token and account manually. Currently set as environment variables
# SPOTINST_TOKEN=''
# SPOTINST_ACCOUNT_AWS=''
@kaugm
kaugm / elastigroup_cfn.yaml
Last active April 3, 2023 20:57
Create an Elastigroup with CloudFormation (Example)
AWSTemplateFormatVersion: "2010-09-09"
Description: This template creates an Elastigroup. This is not meant to be run as a standalone CloudFormation template, but rather to have the Parameters and Resource added to existing ASG CloudFormation templates to create and manage both simultaneously. All API parameters in Spot documentation are valid for the Elastigroup resource https://docs.spot.io/api/#tag/Elastigroup-AWS/operation/elastigroupAwsCreate
Parameters:
ServiceToken:
Type: String
Description: ARN of Lambda function in Spot's AWS account that will handle creation of Elastigroup. DO NOT CHANGE THIS.
Default: arn:aws:lambda:us-west-2:178579023202:function:spotinst-cloudformation
accessToken: