Refactoring and vmdb2 improvements

This commit is contained in:
Ana Custura 2025-09-30 13:27:57 +01:00
parent e94c68854d
commit f61468fff5
28 changed files with 1257 additions and 0 deletions

View file

@ -0,0 +1,4 @@
interface=wlan0
dhcp-range=10.3.141.50,10.3.141.255,255.255.255.0,12h
dhcp-option=6,10.3.141.1
address=/{{ butter_name }}.lan/10.3.141.1

View file

@ -0,0 +1,95 @@
#!/usr/bin/env bash
#
# butterbox-setup.sh
# Registers a Matrix user and creates a public room. Fails on any non-200 response.
#set -e # exit if any command fails
MATRIX_HOMESERVER="http://localhost:8008"
echo "Registering user {{ butter_name }}-admin..."
USERNAME="{{ butter_name }}-admin"
PASSWORD="{{ butter_name}}-admin"
HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X POST \
"${MATRIX_HOMESERVER}/_matrix/client/v3/register?kind=user" \
-H "Content-Type: application/json" \
-d "{
\"auth\": {
\"type\": \"m.login.dummy\"
},
\"username\": \"$USERNAME\",
\"password\": \"$PASSWORD\",
\"initial_device_display_name\": \"{{ butter_name }}\"
}")
# Separate body and status
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
if [ "$HTTP_STATUS" -ne 200 ]; then
echo "❌ Registration failed with HTTP status $HTTP_STATUS"
echo "Response from server:"
echo "$HTTP_BODY"
fi
# Extract access_token using grep/sed
ACCESS_TOKEN=$(echo "$HTTP_BODY" | grep -o '"access_token"[^,}]*' | sed 's/.*: *"//; s/"$//')
if [ -z "$ACCESS_TOKEN" ]; then
echo "❌ No access token found in registration response."
echo "Response:"
echo "$HTTP_BODY"
exit 1
fi
echo "✅ Registration successful!"
echo "Access Token: $ACCESS_TOKEN"
# --- Create a room ---
ROOM_ALIAS_NAME="public"
ROOM_NAME="public"
LANG_ARG={{ butter_language }}
ROOM_TOPIC="This is a public, unencrypted room. To have a private conversation, create a new room."
if [ "$LANG_ARG" = "es" ]; then
ROOM_TOPIC="Esta es una sala de chat pública y sin cifrar. Para tener una conversación privada, crea una nueva sala."
fi
echo "Creating public room '${ROOM_NAME}'..."
ROOM_HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X POST \
"${MATRIX_HOMESERVER}/_matrix/client/v3/createRoom" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"visibility\": \"public\",
\"preset\": \"public_chat\",
\"room_alias_name\": \"${ROOM_ALIAS_NAME}\",
\"name\": \"${ROOM_NAME}\",
\"topic\": \"${ROOM_TOPIC}\"
}")
ROOM_HTTP_BODY=$(echo "$ROOM_HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
ROOM_HTTP_STATUS=$(echo "$ROOM_HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
if [ "$ROOM_HTTP_STATUS" -ne 200 ]; then
SEARCH_STRING="already exists"
if echo "$HTTP_BODY" | grep -q "$SEARCH_STRING"; then
echo "Response from server:"
echo "$HTTP_BODY"
exit 0
fi
echo "❌ Room creation failed with HTTP status $ROOM_HTTP_STATUS"
echo "Response from server:"
echo "$ROOM_HTTP_BODY"
exit 1
fi
echo "✅ Public room created successfully!"
echo "Response:"
echo "$ROOM_HTTP_BODY"

View file

@ -0,0 +1,16 @@
driver=nl80211
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
auth_algs=1
wpa_key_mgmt=WPA-PSK
beacon_int=100
ssid={{ butter_name }}
channel=1
hw_mode=g
ieee80211n=0
interface=wlan0
wpa=none
wpa_pairwise=CCMP
wpa_passphrase=ThisIsInertAndSatisfiesValidation
country_code=US
ignore_broadcast_ssid=0

View file

@ -0,0 +1,11 @@
[Unit]
Description=Set Hostapd Interface at Boot
After=network.target systemd-udev-settle.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/set_hostapd_iface.py
RemainAfterExit=true
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,56 @@
#!/usr/bin/env python3
import subprocess
import re
CONFIG_FILE = "/etc/hostapd/hostapd.conf"
def find_ap_interface():
try:
iw_dev_output = subprocess.check_output(["iw", "dev"], text=True)
except Exception as e:
print(f"Error running iw dev: {e}")
return None
interfaces = re.findall(r"Interface\s+(\w+)", iw_dev_output)
for iface in interfaces:
try:
modes_output = subprocess.check_output(["iw", "dev", iface, "info"], text=True)
# Check for AP mode support
phy_output = subprocess.check_output(["iw", iface, "info"], text=True, stderr=subprocess.DEVNULL)
except Exception:
continue
try:
phy_name = re.search(r"wiphy (\d+)", modes_output)
if not phy_name:
continue
phy_info = subprocess.check_output(["iw", "phy", f"phy{phy_name.group(1)}", "info"], text=True)
if "AP" in phy_info:
return iface
except Exception:
continue
return None
def update_config(interface_name):
try:
with open(CONFIG_FILE, "r") as f:
lines = f.readlines()
with open(CONFIG_FILE, "w") as f:
for line in lines:
if line.strip().startswith("interface="):
f.write(f"interface={interface_name}\n")
else:
f.write(line)
print(f"Updated {CONFIG_FILE} to use interface: {interface_name}")
except Exception as e:
print(f"Error updating config: {e}")
if __name__ == "__main__":
iface = find_ap_interface()
if iface:
update_config(iface)
else:
print("No AP-capable interface found.")