Skip to content

Instantly share code, notes, and snippets.

View DavesCodeMusings's full-sized avatar

Dave Horton DavesCodeMusings

  • Planet Earth
View GitHub Profile
@DavesCodeMusings
DavesCodeMusings / docker-exec-api.sh
Created January 2, 2022 23:16
Execute a Docker command inside a container using API calls with curl
#!/bin/bash
# Docker API Exec proof of concept. Run a `date` command in a container with curl.
echo "Choose a container ID (or CTRL+C to exit)"
CONTAINER_ID_LIST=$(curl --unix-socket /var/run/docker.sock http://localhost/containers/json 2>/dev/null | jq -r .[].Id)
select CONTAINER_ID in $CONTAINER_ID_LIST; do
# Create an exec instance and retain the ID that was returned.
@DavesCodeMusings
DavesCodeMusings / html_template.js
Created January 3, 2022 00:37
Dynamic HTML from JavaScript without a bunch of `html += . . .` madness.
#!/usr/bin/env node
/*
* Dynamic HTML from JavaScript without a bunch of `html += ...` madness.
*/
// Define the HTML with {{ }} placeholders for values to be filled in later.
let template = `
<details>
<summary><img alt="{{state}}" src="icons/{{stateIcon}}">{{name}}</summary>
#!/usr/bin/env node
import { request } from 'http';
// Replace containerID below.
const containerID = 'abcdefg1234567890hijklmnop1234567890qrstuv1234567890wxyz12345678';
function startExec(execID) {
let options = {
socketPath: '/var/run/docker.sock',
@DavesCodeMusings
DavesCodeMusings / docker-info-api.js
Created January 9, 2022 23:37
Call the Docker API using http.request wrapped in a promise
#!/usr/bin/env node
import { request } from 'http';
function callDockerAPI(path) {
return new Promise ((resolve, reject) => {
let options = {
socketPath: '/var/run/docker.sock',
method: 'GET',
path: path
@DavesCodeMusings
DavesCodeMusings / auth.js
Last active August 20, 2022 03:03
Example of LDAP user authentication using ldapjs
#!/usr/bin/env node
/**
* Example of LDAP user authentication using ldapjs.
*
* Run like this:
* auth.js ; echo $?
*
* A successful authentication will show output like this along with a return code of 0:
* Connecting to: [ 'ldap://127.0.0.1:389' ]
@DavesCodeMusings
DavesCodeMusings / create-mailboxes.yml
Created January 26, 2022 03:33
Ansible playbook to create mailboxes for users on Raspberry Pi OS
---
- name: Configure mailboxes for users
hosts: localhost
connection: local
become: true
vars:
accounts:
- pi
@DavesCodeMusings
DavesCodeMusings / dance_the_rainbow.py
Created March 27, 2022 22:54
Micropython NeoPixel LED strip DJ lighting effect
# LED chase that pulses to the beat of the music and features a color changing background.
import machine, neopixel
from time import ticks_ms, sleep_ms
bpm = 128 # Tempo of your mix in beats per minute
num_leds = 42 # Number of NeoPixel LEDs in the strip
data_pin = 16 # Data pin for the NeoPixel strip
bg_colors = [ # Series of changing background colors
(16, 0, 0), # red
@DavesCodeMusings
DavesCodeMusings / beat_drop.py
Last active March 28, 2022 02:07
Micropython NeoPixel LED strip lighting effect for beat drops
# A short sequence of LED flashes that doubles in frequency with each repetition, ending with a strobe.
import machine, neopixel
from time import ticks_ms, sleep_ms
bpm = 134 # Tempo of your mix in beats per minute
num_leds = 42 # Number of NeoPixel LEDs in the strip
data_pin = 16 # Data pin for the NeoPixel strip
np = neopixel.NeoPixel(machine.Pin(data_pin), num_leds)
@DavesCodeMusings
DavesCodeMusings / list_pools.py
Created May 1, 2022 19:24
List storage pools using python-libvirt
#!/usr/bin/env python3
import sys
import libvirt
conn = None
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
@DavesCodeMusings
DavesCodeMusings / define_pool.py
Last active May 1, 2022 23:54
Create a new storage pool with libvirt-python api
#!/usr/bin/env python3
# Define a storage pool using the libvirt API.
# virsh pool-destroy ; virsh pool-undefine can be used to remove.
import libvirt
import sys
import uuid
import os