Skip to content

Instantly share code, notes, and snippets.

@sathed
sathed / conditional-dockerfile
Last active May 16, 2017 20:00
Conditional Dockerfile
FROM busybox
VAR USERNAME \
PASSWORD
RUN if [[ -n $username ]]; then echo $password; fi
#!/bin/bash
# Create array
array=(foo, bar)
echo ${array[@]} # => foo, bar
# Remove first index of array.
array=("${array[@]:1}")
echo ${array[@]} # => bar
@sathed
sathed / watch-docker-api.sh
Last active May 17, 2017 18:30
Use this to see the details of Docker API calls. Requires 'socat' which is a utility that creates two bidirectional byte streams and watches data transfers between them.
#!/bin/bash
mv /var/run/docker.sock /var/run/docker.sock.orig && socat -v UNIX-LISTEN:/var/run/docker.sock,group=docker,perm=0660,reuseaddr,fork UNIX-CONNECT:/var/run/docker.sock.orig; mv /var/run/docker.sock.orig /var/run/docker.sock
@sathed
sathed / gist:b7c15fb427225c825c442aaece2896e1
Created August 10, 2017 19:21
Commands to set up Docker without root on Linux (Fedora, SUSE, Ubuntu, etc.)
sudo groupadd docker && sudo gpasswd -a ${USER} docker && sudo systemctl restart docker
newgrp docker
@sathed
sathed / mount_format.sh
Created August 10, 2017 19:28
Format new volume on Linux
#!/bin/bash
# Swap 'ext4' for the file system type you'd like
# Find partition name by running $ cat proc/partitions
mkfs.ext4 /dev/[partition_name]
# Mount with:
mount /dev/[partition_name] /path/to/directory/
# Always mount on boot:
@sathed
sathed / export_actions.sh
Created August 10, 2017 19:34
AWS Search IAM Policies for Specific Action
#!/bin/bash
# Get the ARNs of the policies and store in the policies_arn array.
mapfile -t policies_arn < <(aws iam list-policies --query 'Policies[*].[Arn]' --output text)
# Get the VersionIds for each policy and store in the policies_ver array.
mapfile -t policies_ver < <(aws iam list-policies --query 'Policies[*].[DefaultVersionId]' --output text)
# Use a for loop to loop through each policy and store the policy document in ~/policies.txt
for (( i=0; i<${#policies_arn[@]}; i++ )); do echo ${policies_arn[i]} >> policies.txt && aws iam get-policy-version --policy-arn ${policies_arn[i]} --version-id ${policies_ver[i]} --output json >> ~/policies.txt; done
@sathed
sathed / gist:9d18e70a01394c8a0333d1f8d403aa51
Created August 15, 2017 16:41
[Warning] IPv4 forwarding is disabled. Networking will not work.
#!/bin/bash
# This is usually caused because of a network outage/restart while the Docker daemon was running.
# Restart the Docker daemon to fix it.
service docker restart
@sathed
sathed / bleep_bleep_bleeping_oracle.sql
Last active October 26, 2017 16:19
Converting a relatively simple call to Oracle's decode() to PostgreSQL...
/*
I recently took on a project at work to convert a very large Oracle database to PostgreSQL as part of our transition to AWS.
Now, I hate Oracle. When I say hate, I'm saying hate because it's unprofessional for me to use any stronger terminology.
Oracle likes to make things fairly easy (all things considered). They put a LOT of effort into creating a lot of little
features that, for the most part, are nice! But... I firmly believe that they do this because they don't want you to leave.
They WANT you to design yourself into a corner to the point where it's too much work for you to leave.
Below is a perfect example...
The oracle decode function basically makes for a one line case statement.
@sathed
sathed / postgres_fdw.sql
Created October 25, 2017 21:48
postgres_fdw example
/***** "Remote" server first *****/
-- Note: Unless the object you are trying to gain access to is in the same DATABASE, it's a remote datebase. Even if it's
-- on the same node!
-- 1. create the role and assign it a password. Note: CREATE USER is an alias for CREATE ROLE. Either one is fine
CREATE ROLE new_user WITH PASSWORD 'somepassword';
-- 2. Grant the required permissions. This grants select, insert, update, and delete on all tables in the public schema.
-- I also gave execute to all functions in the public schema as well.
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO new_user;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO new_user;
@sathed
sathed / process_status.ps1
Last active September 6, 2018 19:19
Get the state of a Windows process.
#Using Notepad as an example.
$target = "notepad"
#Get the process
$process = Get-Process -Name $target -ErrorAction SilentlyContinue
#Get the execution state of the thread.
$threads=$process.Threads
$threads | select Id,ThreadState,WaitReason