Skip to content

code_sre

mike fettis edited this page Jan 11, 2024 · 9 revisions

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:
            git@github.com: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" 

python

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()