code_sre - ridingintraffic/ridingintraffic.github.com GitHub Wiki
bash
timestamps because time sucks
want a date/timestamp that is in the past?
utc 4 hours ago
date -u -v-4H '+%Y-%m-%dT%H:%M:%S'
local time 4 hours ago
date -v-4H '+%Y-%m-%dT%H:%M:%S
utc 1 day ago
date -u -v-1d '+%Y-%m-%dT%H:%M:%S'
local time 1 day ago
date -v-1d '+%Y-%m-%dT%H:%M:%S'
utc 1 month ago
date -u -v-1m '+%Y-%m-%dT%H:%M:%S'
local time 1 month ago
date -v-1m '+%Y-%m-%dT%H:%M:%S'
Then there are different formats too
date -u -v-2H '+%Y-%m-%dT%H:%M:%S%z' UTC minus 2 hours (+0000) 2025-02-11T12:30:45+0000
date -v-2H '+%Y-%m-%dT%H:%M:%S%z' Local time minus 2 hours with local offset 2025-02-11T08:30:45-0600
git
rebase feature branch
while you are on the feature branch
$ git rebase main <feature_branch>
$ git pull --rebase
$ git push origin <feature_branch>
done
github
git repo mirroring
I needed a way to mirror a repo so that every time i pushed a new commit to one, it would go to the other. Pretty simple and straight forward. A little google later and I have a github action that will do it for me and then ignore the job once it lands. Otherwise the mirror workflow firees on the destination again.
name: Mirroring
on: [push, delete]
jobs:
to_github:
runs-on: ubuntu-latest
if: ${{ github.actor == 'ridingintraffic' }} # <-- this is to make sure that the mirror only occurs on the source remote
steps: # <-- must use actions/checkout before mirroring!
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: pixta-dev/repository-mirroring-action@v1
with:
target_repo_url:
[email protected]:other-user/other-repo-wiki.git
ssh_private_key: # <-- use 'secrets' to pass credential information.
${{ secrets.YOUR_SSH_PRIVATE_KEY }}
resolving merge conflicts
there are a million ways to do this.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line
terraform
terraform targeting
Yes you are supposed to never use a targeted plan and apply in terraform becuase it is the violation of the Infra as code creed. But you know what sometimes you just gotta do a thing and get it done. Then you never want to remember your shame and as a result you go and forget how to do that thing. so here it is
plan
terraform plan -target="aws_appautoscaling_scheduled_action.app_scheduled_down" -target="aws_appautoscaling_scheduled_action.app_scheduled_up" input=false -compact-warnings -out plan-dev.tfplan
apply
terraform apply -target="aws_appautoscaling_scheduled_action.app_scheduled_down" -target="aws_appautoscaling_scheduled_action.app_scheduled_up"
AWS account info
# The attribute `${data.aws_caller_identity.current.account_id}` will be current account number.
data "aws_caller_identity" "current" {}
# The attribue `${data.aws_iam_account_alias.current.account_alias}` will be current account alias
data "aws_iam_account_alias" "current" {}
# The attribute `${data.aws_region.current.name}` will be current region
data "aws_region" "current" {}
# The attribute `${data.aws_partition.current.partition}` will be current partition
data "aws_partition" "current" {}
# Set as [local values](https://www.terraform.io/docs/configuration/locals.html)
locals {
account_id = data.aws_caller_identity.current.account_id
account_alias = data.aws_iam_account_alias.current.account_alias
region = data.aws_region.current.name
partition = data.aws_partition.current.partition
default_tags = data.aws_default_tags.current.tags
}
python
python virtual env
python3 -m venv path/to/venv
source path/to/venv/bin/activate
python3 -m pip install xyz
python3 -m venv $PWD
source $PWD/bin/activate
python3 -m pip install
URL encoding strings
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
Accessing Dictionaries
Getting dictionaries back from a http endpoint usually results in a mess of objects. Dealing with this in python can be a challenge of ValueError exceptions. so what is the easiest way to handle this? a nested get. if the object looks like
message={"detail":{"operation":"create"}}
then you can access it like this
>>> message.get('detail', {}).get('operation')
create
then if you access something that doesn't exist you will get a nice None back
>>> print(message.get('detail', {}).get('notoperation'))
None
hurray safe handling hey
List of dictionaries
removing a dictionary for a list
iterate through a list and then find that list as a value to a given key.
remove that dictionary entry from the list.
EXCLUDED_VENDOR=["this", "that", "the other"]
all_tenants=[
{"slug":"this"},
{"slug":"something"},
{"slug":"something_else"}
]
for EXCLUDED_VENDOR in EXCLUDED_VENDORS:
index = None
for i, tenant in enumerate(all_tenants):
if tenant.get("slug") == EXCLUDED_VENDOR:
index = i
break
if index is not None:
all_tenants.pop(index)
for tenant in all_tenants:
print(tenant.get("slug"))
base64 user auth in requests
use the base64 for the first header and then reuse the bearer token that you get back.
import requests
import json
from requests.auth import HTTPBasicAuth
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
base_url = 'https://www.something.com'
token_url = "/tauth/1.0/token/"
url=base_url + token_url
response = requests.post(url, headers=headers, auth=HTTPBasicAuth(USER, PASSWORD))
data = response.json()
#reusing that bearer token from the repsonse
headers = {
'Authorization': f"Bearer {data['token']}",
'Content-Type': 'application/json',
'Accept': 'application/json'
}
url="/something/"
response=requests.get( url, headers=headers)
data = response.json()