Skip to content

Instantly share code, notes, and snippets.

View kkc's full-sized avatar
🌴
On vacation

Kakashi Liu kkc

🌴
On vacation
View GitHub Profile
@kkc
kkc / get_or_create.py
Created September 8, 2020 07:40 — forked from jleclanche/get_or_create.py
SQLAlchemy get or create
def get_or_create(session, model, defaults=None, **kwargs):
"""
Get or create a model instance while preserving integrity.
"""
try:
return session.query(model).filter_by(**kwargs).one(), False
except NoResultFound:
if defaults is not None:
kwargs.update(defaults)
@kkc
kkc / slides I read
Last active July 2, 2020 08:25
Slides I read
[Linux Container Internals 2.0](https://docs.google.com/presentation/d/1S-JqLQ4jatHwEBRUQRiA5WOuCwpTUnxl2d1qRUoTz5g/edit#slide=id.g3e1a17e39e_2_41)
@kkc
kkc / AWS CLI VPC-Network
Created April 28, 2020 03:29
AWS CLI commands to build a sample VPC
aws> ec2 describe-vpcs
aws> ec2 create-vpc --cidr-block 10.0.0.0/16
aws> ec2 create-tags --resources vpc-f0bff594 --tags Key=Name,Value=sample-vpc
aws> ec2 describe-vpcs
aws> ec2 describe-internet-gateways
aws> ec2 create-internet-gateway
aws> ec2 create-tags --resources igw-6992410d --tags Key=Name,Value=sample-vpc-igw
aws> ec2 attach-internet-gateway --vpc-id vpc-f0bff594 --internet-gateway-id igw-6992410d
aws> ec2 describe-internet-gateways
aws> ec2 describe-subnets --query 'Subnets[?VpcId==`vpc-f0bff594`]'
ifconfig (or ip link, ip addr) - for obtaining information about network interfaces
ping
for validating, if target host is accessible from my machine.
ping is also could be used for basic DNS diagnostics - we could ping host by IP-address or by its hostname and then decide if DNS works at all. And then traceroute or tracepath or mtr to look what's going on on the way there.
dig
@kkc
kkc / gist:73daa92c15e0dfd9ec316298d5fc438f
Created March 13, 2020 13:25
Analyze pcap storing on S3
You can pull a capture from S3 into #Wireshark using the AWS CLI:
aws s3 cp s3://my-bucket/odd-http.pcap - | wireshark -k -i -
@kkc
kkc / gist:a82e4fdd5197ad9cf29f98bc649ccc94
Created October 15, 2018 12:48
Using environment variables in Kubernetes deployment spec
https://serverfault.com/questions/791715/using-environment-variables-in-kubernetes-deployment-spec
A much easier/cleaner solution: envsubst
In deploy.yml:
LoadbalancerIP: $LBIP
Then just create your env var and run kubectl like this:
export LBIP="1.2.3.4"
@kkc
kkc / S3-Static-Sites.md
Created July 25, 2018 07:48 — forked from bradwestfall/S3-Static-Sites.md
Use S3 and CloudFront to host Static Single Page Apps (SPAs) with HTTPs and www-redirects. Also covers deployments.

S3 Static Sites

What this will cover

  • Host a static website at S3
  • Redirect www.website.com to website.com
  • Website can be an SPA (requiring all requests to return index.html)
  • Free AWS SSL certs
  • Deployment with CDN invalidation

Resources

@kkc
kkc / AMI_finder
Created December 28, 2017 08:50
Find the most recent Ubuntu AMI 16.04 using aws-cli (or any other AMI for that matter)
#!/bin/sh
# Use AWS CLI to get the most recent version of an AMI that
# matches certain criteria. Has obvious uses. Made possible via
# --query, --output text, and the fact that RFC3339 datetime
# fields are easily sortable.
export AWS_DEFAULT_REGION=us-east-1
aws ec2 describe-images \
@kkc
kkc / readContent.js
Created October 16, 2017 16:01
promise and singleton
var fs = require('fs');
function Reader() {
this.content = null;
}
Reader.prototype.getContent = function() {
if (this.content === null) {
return new Promise((resolve, reject) => {
console.log('new promise');
@kkc
kkc / push_gpu_metrics.py
Last active July 11, 2017 07:27
Azure insight gpu metrics
#!/usr/bin/env python
from applicationinsights import TelemetryClient
import subprocess
import sys
import socket
hostname = socket.gethostname()
telemetry_id = sys.argv[1]
tc = TelemetryClient(telemetry_id)