Skip to content

Instantly share code, notes, and snippets.

@efim-a-efim
efim-a-efim / options_parse_simple
Last active August 29, 2015 13:58
Parses script/function options. Simple parsing, just for reference
#!/bin/bash
local _optstring=':f:'
while getopts "${_optstring}" _opt; do
case "${_opt}" in
f)
_f="${OPTARG}"
;;
*)
return 255
;;
@efim-a-efim
efim-a-efim / is_declared
Last active August 29, 2015 13:58
Check if variable/function/array value is declared.
#!/bin/bash
is_declared() {
# options
local _opts=''
while getopts ':aAfirtx' _opt; do
case "${_opt}" in
[aAfirtx])
_opts="${_opts}${_opt}"
;;
*)
@efim-a-efim
efim-a-efim / log
Created April 2, 2014 09:51
Complex Bash log routine. May log to files and stdout. Supports messages stream from STDIN.
#!/bin/sh
log() {
local _opt=''
local _date="`date -R`"
local _log_tag="`basename $0`"
local _level='${LOG_LEVEL:-debug}'
local _print=0
local _log_path="${LOG_PATH:-/tmp}"
local _log_facility="${LOG_FACILITY:-local1}"
@efim-a-efim
efim-a-efim / pid
Created April 2, 2014 09:55
PID file management.
#!/bin/bash
pid() {
local _pidfile="${PID_FILE:-/tmp/backup.pid}"
local _pid="$$"
while getopts ':f:p:' _opt; do
case "${_opt}" in
f)
_pidfile="${OPTARG}"
;;
@efim-a-efim
efim-a-efim / lock
Created April 2, 2014 09:56
Complex lock management with one command.
#!/bin/bash
########################################################
# Locking library
########################################################
__LOCK_DIR="${_LOCK_DIR:-/var/lock/backup}"
__clear_locks() {
local _lock_dir="${1:-${__LOCK_DIR}}"
@efim-a-efim
efim-a-efim / gist:11372066
Last active August 29, 2015 14:00
Bash XML simple parser
#!/bin/bash
read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
local RET=$?
TAG_NAME=${ENTITY%% *}
ATTRIBUTES=${ENTITY#* }
return $RET
}
@efim-a-efim
efim-a-efim / JS-build.csproj
Last active April 26, 2016 10:49
MSBuild transforms
<Target Name="BuildJS" BeforeTargets="TransformAllFiles" >
<Exec Command="npm install" WorkingDirectory="$(MSBuildProjectDirectory)\Content" />
<Exec Command=".\node_modules\.bin\mimosa build --errorout -P dist-config" WorkingDirectory="$(MSBuildProjectDirectory)\Content" />
<ItemGroup>
<_JSContent Include="Content\public\**\*.*" />
</ItemGroup>
<ItemGroup>
<_JSContent Condition="$([System.String]::Copy('%(Filename)').EndsWith('.$(Configuration)'))">
<DependentUpon>%(RelativeDir)$([System.String]::Copy('%(Filename)').Replace('.$(Configuration)',''))%(Extension)</DependentUpon>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
@efim-a-efim
efim-a-efim / user-data.yml
Last active January 11, 2017 04:13
RancherOS + AWS AutoScaling + Swarm
#cloud-config
rancher:
services:
swarm:
image: swarm
command: join --advertise $private_ipv4:2375 --heartbeat "10s" --delay "10s" consul://consul.example.internal:8500
net: host
privileged: true
labels:
io.rancher.os.scope: system
@efim-a-efim
efim-a-efim / mock_http.py
Last active December 16, 2016 16:03
[python] [test] Mock HTTP response with pre-defined string
import urllib2
from StringIO import StringIO
SERVER_LIST="""test1.pvt
test2.pvt
test5.pvt
test8.pvt
test7.pvt
"""
ADD_HEADERS="""X-Header-Mock-1: asd"""
@efim-a-efim
efim-a-efim / README.md
Created June 22, 2018 13:18
Python dict data Key-Value serializer and deserializer

Input structure:

{ 
  'data': { 'backup': 'oqwpnruvq3r89q24hgq0fgy183ogb76w25v94g16fg981qo3f',
            'mysql': { 'asd': [ 'staff1',
                                'staff2',
                                { 'arr': [ 'service',
                                           'host',
                                           { 'asd': 'fff'}],