Snippets

These are code snippets for one-off task.

ghc

compare core output

# Bump with core output
cabal clean && cabal build --ghc-options="-ddump-simpl -ddump-to-file -dsuppress-uniques" lib:simple-dsp
# Save output
cp ./dist-newstyle/build/x86_64-linux/ghc-9.6.1/simple-dsp-0.1/build/src/SimpleDSP/IIR.dump-simpl .
# Make change, rebuild and compare diff
...

runtime profile

With `cabal.project.local`:

ignore-project: False
library-profiling: True
executable-profiling: True
tests: True
benchmarks: True
profiling: True

Run:

# Build/run the project with profiling options
cabal run -O2 --enable-library-profiling --enable-profiling --ghc-options="-fprof-late-inline" exe:my-exe -- +RTS -p -hy -l-au -RTS
# Render report
nix shell nixpkgs#haskellPackages.eventlog2html eventlog2html my-exe.eventlog

perf

create a flamegraph

# Record perf data
sudo perf record -a -g -- ma-command
# Generate flame graph
sudo perf script | ~/src/github.com/brendangregg/FlameGraph/stackcollapse-perf.pl --all | ~/src/github.com/brendangregg/FlameGraph/flamegraph.pl > perfjuicer.svg

git

delete merged branch

git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d

ffmpeg

Encode mp3 with pochette for youtube

ffmpeg -loop 1 -r 1 -i opFreak-album.jpg -i opFreak-album.mp3 -c:a copy -shortest -c:v libx264 opFreak-album.mp4

gnome

Show wifi passwords

nmcli -s connection show $SSID | grep -i psk

rpm

List installed by size

# rpm -qia|awk '$1=="Name" {n=$3} $1=="Size" {s=$3} $1=="Description" {print s " " n }' | sort -n
rpm -qa --queryformat '%{SIZE} %{NAME}\n'

wayland

Disable input

sudo libinput list-devices
# grab the input path and run:
sudo nix run nixpkgs#evtest --grab /dev/input/event3

x11

mouse speeds

xinputs -> get the mouse id
xinputs list-props -> get the matrix prop id
xinput set-prop $MID $PID 3.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 1.0

gpg

Update expired keys

$ gpg --edit-key EB103DE8B5E69E631C6FF17922B9A05C925CC5D8
gpg> expire
...
gpg> key 1
gpg> expire
...
gpg> save
$ gpg  --send-keys  EB103DE8B5E69E631C6FF17922B9A05C925CC5D8

Add ssh key

From https://opensource.com/article/19/4/gpg-subkeys-ssh

$ gpg2 --expert --edit-key <KEY ID>
gpg> addkey
   (8) RSA (set your own capabilities)
Your selection? 8

Possible actions for a RSA key: Sign Encrypt Authenticate
Current allowed actions: Authenticate

   (Q) Finished

Your selection? q
Is this correct? (y/N) y
Really create? (y/N) y
gpg> quit
Save changes? (y/N) y

In gpg.conf

enable-ssh-support

Auto add key to agent

gpg2 -K --with-keygrip
echo ID >> ~/.gnupg/sshcontrol

Then in .xinitrc

gpg-agent --daemon --keep-display
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)

image magic

Compare test

// Use imagemagick to test that your image is what you want
'compare -metric RMSE actual.png expected.png',

pdf

split in half a double-page scanned PDF in a single pass:

mutool poster -r -x 2 in.pdf out.pdf

gnuplot

nice looking charts from CSV

From https://raymii.org/s/tutorials/GNUplot_tips_for_nice_looking_charts_from_a_CSV_file.html

set datafile separator ','
set xdata time
set timefmt "%Y-%m-%dT%H:%M:%S"
set key autotitle columnhead
set ylabel "First Y Units"
set xlabel 'Time'
set y2tics
set ytics nomirror
set y2label "Second Y Axis Value"
set style line 100 lt 1 lc rgb "grey" lw 0.5
set grid ls 100
set ytics 0.5
set xtics 1
set style line 101 lw 3 lt rgb "#f62aa0"
set style line 102 lw 3 lt rgb "#26dfd0"
set style line 103 lw 4 lt rgb "#b8ee30"

set xtics rotate # rotate labels on the x axis
set key right center # legend placement

plot filename using 1:2 with lines ls 101, '' using 1:3 with lines ls 102, '' using 1:4 with lines axis x1y2 ls 103

google calendar

Export to org mode:

Download archive from https://calendar.google.com/calendar/r/settings/export, then

TZ=Asia/Seoul ical2orgpy cal.ics gcal.org

notmuch

Delete mails matching query

notmuch search --output=files --exclude=false tag:monit | xargs -L1000 rm

ZooKeeper

Connect with kazoo

import json
import kazoo.client
client = kazoo.client.KazooClient(hosts="zookeeper")
client.start()
def load_node(path, node, x):
    d = {}
    try:
      d = json.loads(node[0].decode('utf-8'))
      d['node_path'] = path + x
    except:
      print("error: ", x, node)
    return d


nodes = list(map(lambda x: load_node(client.get("/nodepool/nodes/" + x), x), client.get_children("/nodepool/nodes")))

# delete provider node
for node in nodes:
    if node.get('cloud') == 'rdo-cloud':
        print("deleting ", node.get('node_path'))
        client.delete(node['node_path'], recursive=True)

node = json.loads(client.get("/nodepool/nodes/" + nodes[0])[0].decode('utf-8'))

OpenStack

Deploy local instance

yum install openstack-nova openstack-neutron libvirt openstack-glance openstack-cinder openstack-heat-api openstack-heat-common openstack-heat-engine openstack-keystone openstack-nova-scheduler qemu-img-ev rabbitmq-server libvirt-daemon-kvm galera mariadb-server-galera memcached container-selinux openstack-selinux sos openstack-neutron-ml2 targetcli openvswitch
# resize hdd
fdisk /dev/vda
resize2fs /dev/vda

packstack --answer-file=basic

# Create cirros
curl -OL https://trunk.rdoproject.org/cirros-0.3.4-x86_64-disk.img
openstack image create --file cirros-0.3.4-x86_64-disk.img --disk-format qcow2 cirros2

# Remove router1
neutron router-gateway-clear router1
neutron router-port-list router1
neutron router-interface-delete router1 03916f57-a7a5-417d-b7d9-528b2fb4f993
neutron router-delete router1

# Remove public_subnet
neutron subnet-delete public_subnet

# Set br-ex network
/etc/sysconfig/network-scripts/ifcfg-br-ex
DEVICE=br-ex
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=static
IPADDR=192.168.100.2
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=192.168.42.1
ONBOOT=yes

/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=OVSPort
DEVICETYPE=ovs
OVS_BRIDGE=br-ex
ONBOOT=yes

systemctl restart network

# Create public_subnet
neutron subnet-create --name public_subnet --enable_dhcp=True --allocation-pool=start=192.168.100.200,end=192.168.100.250  --gateway=192.168.100.1 public 192.168.100.0/24
# Create router1
neutron router-create router1
neutron router-gateway-set router1 public
neutron router-interface-add router1 private_subnet

# Create keypair
openstack keypair create --public-key kp id_rsa

openstack image set --public centos-7

# Enable ingress
openstack security group rule create --ingress default
openstack security group rule create --protocol icmp --ingress default