Skip to content

Instantly share code, notes, and snippets.

View bsnux's full-sized avatar
💭
👨‍💻

Arturo Fernandez bsnux

💭
👨‍💻
View GitHub Profile
@bsnux
bsnux / pvc-inspector.sh
Created August 30, 2023 23:41
k8s-pvc-inspector
#!/usr/bin/env bash
set -euo pipefail
claim=$1
ns=$2
pod_name="pvc-inspector"
kubectl get po "$pod_name" -n "$ns" && kubectl delete po "$pod_name" -n "$ns"
cat <<EOF | kubectl apply -f -
@bsnux
bsnux / py-one-liners.sh
Last active March 3, 2023 00:42
Useful Python one-liners
# Reading file lines from stdin
cat /tmp/test.txt | python -c 'import fileinput; [print(line, end="" for line in fileinput.input()]
python -c 'import fileinput; [print(line, end="") for line in fileinput.input()]' < /tmp/test.txt
# Printing the 3 lines of the file
python -c "import sys; sys.stdout.write(''.join(sys.stdin.readlines()[:3]))" < /tmp/test.txt
# Checking if file exists
python -c 'from pathlib import Path; print("yes") if Path("/tmp/metrics.txt").is_file() else print("no")'
@bsnux
bsnux / new-ts-project.sh
Last active February 17, 2023 05:59
Creates a new TypeScript project ready to use with Node.js
#!/bin/bash
#
# Creates a new TypeScript project ready to use with Node.js
set -euo pipefail
if [[ ! -f $(which npm) ]] || [[ ! -f $(which yarn) ]]; then
echo "Please, install npm and yarn before continuing"
echo "See https://docs.npmjs.com/downloading-and-installing-node-js-and-npm"
echo "See https://yarnpkg.com/"
exit
@bsnux
bsnux / onliner-perl.sh
Created November 21, 2022 17:51
Perl one-liners
# Starting REPL
perl -del
# Search and replace in place
perl -p -i -e "s/test/testing/g" /tmp/testing.txt
# Search/replace in specific files in a directory
perl -p -i -e 's/oldstring/newstring/g' `find ./ -name *.html`
# Similar to awk
@bsnux
bsnux / deleteOldBuildsJob.groovy
Created March 9, 2022 06:52
Deletes old builds leaving only the ones given by MAX_TO_KEEP variable
import java.util.ArrayList
def MAX_TO_KEEP = 10
def JOB_NAME = "job_name_here"
def hi = hudson.model.Hudson.instance
def item = hi.getItemByFullName(JOB_NAME)
def jobs = item.getAllJobs()
Iterator<?> iterator = jobs.iterator()
@bsnux
bsnux / get-cpu-processors.md
Last active November 8, 2021 21:46
Getting number or CPU processors

Getting number of CPU processors in different languages

ruby -retc -e 'puts Etc.nprocessors'
python -c "import multiprocessing; print(multiprocessing.cpu_count())"
@bsnux
bsnux / download-release-github.sh
Last active November 5, 2021 20:44
Download a release file from a private GitHub repo
#!/bin/bash
# How to download a release file from a private GitHub repo
set -eou pipefail
TOKEN="<your_github_token>"
API_URL="https://api.github.com/repos/<your_repo_here>"
asset_id=$(curl -s -H "Authorization: token $TOKEN" -H "Accept: application/vnd.github.v3.raw" $API_URL/releases \
| jq '.[0].assets[0].id')
@bsnux
bsnux / set-zsh.sh
Created October 27, 2021 17:07
How to modify shell when auth is done by LDAP
#!/bin/bash
#
# Set zsh as default shell when auth is done by LDAP
#
set -eou pipefail
getent passwd afernandez | perl -pne 's/bash/zsh/' | sudo tee -a /etc/passwd
@bsnux
bsnux / create-python-standalone.md
Created September 10, 2021 19:03
Creating Python Standalone Applications with zipapp

Creating Python Standalone Applications with zipapp

zipapp module allows us to create self-contained Python standalone programs that can be distributed to users who have Python already installed on their system.

Python packages not using C extensions can be bundled as well. shiv is recommended for those applications using those kind of extensions. In general, if your dependencies don't require a C compiler, you should be good to go using zipapp module.

Simple aplication listing current directory content

@bsnux
bsnux / run-cmd-pod.sh
Last active October 8, 2021 00:30
Runs commands in K8s pods when those are not installed there but they're in the worker node
#!/bin/bash
#------------------------------------------------------------------------
# run-cmd-pod.sh
#
# Runs commands in K8s pods when those are not installed there
# but they're in the worker node.
#
# Motivation: You want to run a command in a pod without
# installing anything in the pod. If that command exists
# in the worker node running the pod, then you can use