summaryrefslogtreecommitdiff
path: root/doc/forum/WIP_adding_dhcp_records_to_libvirt.mdwn
blob: 118f01ab8db6533ff172d41d773ee4f520448147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
I'm working on adding static (predictable) dhcp records to libvirt guests (code at the end). It seems like I might need to either do the equivalent of
[[!format bash """
virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:f0:62:01'/>"  --config --live || /bin/true
virsh net-update default add ip-dhcp-host "<host mac='52:54:00:f0:62:01' ip='192.168.122.32'/>" --config --live
"""]]
or parse the xml output of "virsh net-dumpxml". Is there some simple lightweight xml parsing option? Last time I tried something like this was a decade ago using HXT.Arrow, which didn't really end well.

[[!format haskell """
staticDHCP :: Host -> IPAddr -> Maybe Network.Gateway -> Property UnixLike
staticDHCP h ip gw = property "assign ip to host via dhcp" $ do
                mac <- liftIO $ macAddress
                case mac of
                  Nothing -> errorMessage "no interface"
                  Just addr -> makeChange $ unlessM (updateIt addr) $
                                errorMessage "failed to update network"
  where
    updateIt mac = boolSystem "virsh" [ Param "net-update"
                                  , Param "default"
                                  , Param "add-last"
                                  , Param "ip-dhcp-host"
                                  , Param $ "<host mac=\""++mac++"\" ip=\""++(ifaceToString ip)++"\"/>"
                                  , Param "--config"
                                  , Param "--live"]
    ifaceToString (IPv6 ipstr) = ipstr
    ifaceToString (IPv4 ipstr) = ipstr
    macAddress = do
      ifaces <- virshGetColumns ["domiflist", hostName h]
      case ifaces of
        [] -> return Nothing
        (i:_) -> return $ Just $ Propellor.Base.last i
"""]]