initial work on dns resolver + openwrt mgmt
This commit is contained in:
1 parent
b51257e5e1
commit
0614d7ff04
22 files changed
+505
-2
No files matched your search
@@ -0,0 +1,66 @@
|
||||
{ pkgs, lib }:
|
||||
|
||||
let
|
||||
generator = pkgs.rustPlatform.buildRustPackage {
|
||||
pname = "openwrt-gen";
|
||||
version = "0.1.0";
|
||||
src = ./generator;
|
||||
cargoLock.lockFile = ./generator/Cargo.lock;
|
||||
};
|
||||
in
|
||||
config:
|
||||
|
||||
let
|
||||
# Config JSON is read from stdin at deploy time — caller decrypts the age secret.
|
||||
# Usage: age -d secrets/glmain.json.age | openwrt-deploy
|
||||
deployScript = pkgs.writeShellScriptBin "openwrt-deploy" ''
|
||||
set -euo pipefail
|
||||
ROUTER="${config.router.ip}"
|
||||
ROUTER_USER="${config.router.user}"
|
||||
SERVER="$ROUTER_USER@$ROUTER"
|
||||
|
||||
CONFIG=$(cat)
|
||||
|
||||
DNSMASQ=$(echo "$CONFIG" | ${generator}/bin/openwrt-gen dnsmasq)
|
||||
ETHERS=$(echo "$CONFIG" | ${generator}/bin/openwrt-gen ethers)
|
||||
|
||||
echo "Fetching current config from $SERVER..."
|
||||
CURRENT_DNSMASQ=$(ssh "$SERVER" 'cat /etc/dnsmasq.conf' 2>/dev/null || echo "")
|
||||
CURRENT_ETHERS=$(ssh "$SERVER" 'cat /etc/ethers' 2>/dev/null || echo "")
|
||||
|
||||
echo ""
|
||||
echo "$DNSMASQ"
|
||||
echo "=== dnsmasq.conf diff ==="
|
||||
${pkgs.diffutils}/bin/diff --color=always <(echo "$CURRENT_DNSMASQ") <(echo "$DNSMASQ") || true
|
||||
echo ""
|
||||
read -p "Looks good? [y/N] " -n 1 -r < /dev/tty; echo
|
||||
[[ $REPLY =~ ^[Yy]$ ]] || exit 1
|
||||
|
||||
echo "$ETHERS"
|
||||
echo "=== ethers diff ==="
|
||||
${pkgs.diffutils}/bin/diff --color=always <(echo "$CURRENT_ETHERS") <(echo "$ETHERS") || true
|
||||
echo ""
|
||||
|
||||
read -p "Deploy to $SERVER? [y/N] " -n 1 -r < /dev/tty; echo
|
||||
[[ $REPLY =~ ^[Yy]$ ]] || exit 1
|
||||
|
||||
echo "$DNSMASQ" | ssh "$SERVER" 'cat > /tmp/dnsmasq.new.conf'
|
||||
echo "$ETHERS" | ssh "$SERVER" 'cat > /etc/ethers'
|
||||
|
||||
ssh "$SERVER" '
|
||||
cp /etc/dnsmasq.conf /tmp/dnsmasq.old.conf
|
||||
cp /tmp/dnsmasq.new.conf /etc/dnsmasq.conf
|
||||
if dnsmasq --test; then
|
||||
/etc/init.d/dnsmasq restart
|
||||
echo "Done"
|
||||
else
|
||||
echo "Config invalid, reverting..."
|
||||
cp /tmp/dnsmasq.old.conf /etc/dnsmasq.conf
|
||||
exit 1
|
||||
fi
|
||||
'
|
||||
'';
|
||||
in
|
||||
{
|
||||
inherit deployScript;
|
||||
}
|
||||
Reference in new issue
Block a user