Skip to content

Instantly share code, notes, and snippets.

View jordiclariana's full-sized avatar

Jordi Clariana jordiclariana

View GitHub Profile
BEGIN {
STARTBLOCK="{"
CONDITION="REMOVEME"
ENDBLOCK="}"
}
{
if ($0 ~ STARTBLOCK) {
block[nf=1] = $0
flag=1
} else if (flag==1) {
@jordiclariana
jordiclariana / yaml_syntax_check.py
Created April 9, 2015 09:29
Simple yaml syntax checker
#!/usr/bin/env python
import sys
from yaml import load, dump, error
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
@jordiclariana
jordiclariana / print_block.awk
Created July 31, 2015 10:17
Print only a block of text
awk -v BINIT="START PATTERN" -v BEND="END PATTERN" '
{
if ($0 ~ BINIT) {
F=1
}
if ($0 ~ BEND) {
F=0
}
if (F==1) {
print $0}
#!/bin/bash
YAML="${1}"
[ -f "${YAML}" ] || { echo "No such file as '${YAML}'"; exit 1; }
eyaml decrypt -e "${YAML}" | awk '
{
l=$0;
getline nl;
@jordiclariana
jordiclariana / cmd_execute
Created October 22, 2015 08:13
Python execute comand
import subprocess, shlex
def cmd_execute(command):
cmd = shlex.split(command, posix = False)
try:
p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
preturn = p.wait()
stdout = ''
tmp_stdout = p.stdout.readline()
while tmp_stdout:
@jordiclariana
jordiclariana / terraform.sh
Last active April 11, 2022 12:02
Terraform bash completion
_terraform() {
local cmds cur colonprefixes;
COMPREPLY=();
if [[ "${COMP_WORDS[$COMP_CWORD]}" =~ ^-target= ]]; then
cmds=$(grep -hE "^ *(module|resource)" *.tf | tr -d '"'| awk '
$1 ~ /module/ {print "-target="$1"."$2};
$1 ~ /resource/ { print "-target="$2"."$3}
')
else
cmds=$(terraform --help ${COMP_WORDS[1]} | awk '
@jordiclariana
jordiclariana / pre-commit.sh
Last active January 22, 2016 11:53
Pre commit for scala projects (sergigp review)
#!/bin/bash
declare -A COLORS
COLORS[RED]='\033[0;31m'
COLORS[GREEN]='\033[0;32m'
COLORS[NC]='\033[0m'
COLORS[WHITE]='\033[1m'
# Print a string using colors
# $1 The color
@jordiclariana
jordiclariana / push_terraform.sh
Created March 24, 2016 14:37
Sync terraform state from/to s3 bucket
#!/bin/bash
TERRAFORM_DIR="TERRAFORM_DIRECTORY"
find ${TERRAFORM_DIR} -name "*.tfstate" | while read lfile; do
rfile="$(jq '.remote.config | select (.key != null) | "s3://" + .bucket + "/" + .key' ${lfile} -r)"
if [ -n "${rfile}" ]; then
if ! diff -q ${lfile} <(s3cmd --no-progress get ${rfile} -) > /dev/null; then
echo "${lfile} differs, syncing ..."
cd "$(dirname "${lfile}")/.."
terraform remote pull && \
@jordiclariana
jordiclariana / authorized_keys
Created April 19, 2016 19:20
Enable transparent sudo through authorized_keys, also for scp, rsync, etc.
command="if [ -n \"$SSH_ORIGINAL_COMMAND\" ]; then sudo /bin/bash -c \"$SSH_ORIGINAL_COMMAND\"; else sudo -i; fi" ssh-rsa AAAA...WXYZ myname
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<END
RUBY_VERSION="2.2.5"
sudo apt-get -y update
sudo apt-get -y install git nodejs
# Install ruby environment
if ! type rvm >/dev/null 2>&1; then
curl -sSL https://rvm.io/mpapis.asc | gpg --import -