The lazy programmer scripts
scripts that work

Leader election (PowerShell)

This is a small script useful when multiple Jenkins parallel jobs try to acess the same shared resources, in order to decide for a leader. It is the simplest leader election possible, and it works as long as the sleep wait time is long enough for all the parallel jobs to converge to the same point.

The most important part is that the parallel workers do not need to establish quorum and communicate with each other.

The function is the most naive implementation of the consensus in a proof of work blockchain.

Written by kami on Friday June 9, 2023
Permalink -

Deploy Polygon (Matic) private RPC

Polygon (former Matic) network is a Layer 2 scaling solution for Ethereum, with high throughput and security based on periodic snapshots saved on Ethereum. The documentation to have a private RPC node is awfully bad (@0xpolygon, please fix it).

Requirements for deploying a private RPC:

* 4 core or more VM/baremetal

* 16 GB or more of RAM

* 1 TB of SSD or NVME. An SSD is *REQUIRED*, do not try this on an HDD, you will just waste your time

This script has been tested on Ubuntu Server 20.04, deployed on a Xeon 4 core / 8 threads, 32 GB RAM DDR3, 940 GB ENTERPRISE SSD.

Written by kami on Tuesday September 7, 2021
Permalink -

Lazy speed up apt install on baremetal

On Ubuntu (Debian) installed on baremetal servers, the kernel upgrade might take tens of minutes or even hours to install.

The apt install might appear to be stuck, but in fact, it performs fsync() calls after each file.

Warning!: Make sure that when you perform the apt install, the server has a reliable power source, otherwise some files might not be persisted (synced) and you end up with a broken installation/upgrade.

This script has been tested on Ubuntu Server 18.04.

Written by kami on Thursday November 5, 2020
Permalink -

Lazy upgrade GitLab

Upgrading or installing GitLab can be a very simple or very complicated endeavor, depending on how you perform it and what are your requirements.

In my setup, I had GitLab version 12.6.3-ee.0 installed using Omnibus on an Ubuntu 18.04. At the moment, the latest version of GitLab is 13.3, but I chose to upgrade to the latest version of 13.2.x, as I considered it more stable. I will upgrade to 13.3.x when 13.4.x appears.

These are the raw information I used when choosing the upgrade path:

I chose to perform the upgrade in multiple stages, 7 to be more exact:

  • 12.6.3 -> 12.6.8 -> 12.7.9 -> 12.8.10 -> 12.9.10 -> 12.10.14 -> 13.0.14 ->13.2.9

I have chosen the path as I wanted to perform upgrades between minor version to minor version. In addition, upgrade 12.6.3 -> 12.6.8 was added for a safer patch to patch version upgrade (according to semantic versioning MAJOR.MINOR.PATCH).

Besides the apt upgrade, two manual interventions were required:

  • update unicorn settings when upgrading from 12.6.3-ee.0 to 12.6.8-ee.0
  • upgrade postgres version to v11 after installing GitLab version 12.8.10-ee.0

The following script should not be run automatically, as things can go wrong between every upgrade. Perform functional tests between every upgrade to make sure GitLab works as expected.

The commands should be run with administrative privileges (sudo).

Written by kami on Friday September 11, 2020
Permalink - Tags: linux, bash, gitlab, ubuntu, git, apt

Lazy optimize running Linux from USB stick

Installing a Linux distro on a USB stick is as easy as installing it on a normal HDD / SDD.

Making that Linux work well on that USB stick was quite hard, as there is little or no information on the Internet.

Note: booting and running Linux from a USB stick will be slow and it will most likely shorten the life of your USB stick in the long run.

My setup consisted of a i7 Lenovo laptop and a 128GB Kingston HyperX Savage USB 3.0, which supposedly has 350MB / 250MB sequential read / write speed.

I chose Ubuntu 19.10 ISO netboot, which I flashed using Rufus on another USB 2.0 stick.

The following bash commands were run under root user after I successfully installed Ubuntu 19.10 on the 128GB Kinston HyperX Savage USB 3.0 stick.

Written by kami on Saturday October 5, 2019
Permalink - Tags: linux, usb, ubuntu, ubuntu19.10, optimization, kernel, sysctl

Lazy build qemu-img.exe statically from source

qemu-img is a great tool to convert and create disk images on the fly on Windows or Linux. On Windows, I use it to convert VHD(X) to qcow2 or any disk formats you can imagine.

If you want the qemu binaries prebuilt for Windows, you can easily download them from https://qemu.weilnetz.de/. The installer contains the qemu-img.exe binary too.

The documentation to build it on *nix systems is pretty good, but it is lacking parts of it on how to build it on Windows, especially how to build the static version of it. The official documentation can be found at https://wiki.qemu.org/Hosts/W32.

Static build means that I only want a qemu-img.exe binary which works without any open source dependency .dll. Just a small binary, preferably as small as possible (less than 5MB), which works on any Windows version > Windows Vista. The only requirement for the box where you run it is the MSVC (Microsoft Visual C++) runtime.

So, let's get the party started! The scripts below should be run in the mingw64 console after you install msys64.

Written by kami on Friday September 27, 2019
Permalink - Tags: windows, qemu, gnu, linux, c, static




Lazy remove cached favicons in Chrome

It is very cumbersome sometimes to find that you cannot update the favicons on your favorite website when using Chrome.

This script removes the file where Chrome caches the favicons on Windows. It needs to be run in PowerShell as a normal user.

Tested on Window 10 PRO x64.

Written by kami on Tuesday March 5, 2019
Permalink - Tags: powershell, windows, chrome, favicon