Skip to content

Commit

Permalink
Merge pull request #377 from MalloZup/net-qemu-agentvar
Browse files Browse the repository at this point in the history
use qemu-agent variable as terraform variable for domain
  • Loading branch information
MalloZup authored Aug 27, 2018
2 parents 9d83963 + ea30155 commit c1221a7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.4.4

HIGHLIGHTS:

* qemu-agent variable is not anymore env. variable, instead it is
domain terraform variable
## 0.4.3 (August 14, 2018)

HIGHLIGHTS:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ From its documentation, [qemu-agent](https://wiki.libvirt.org/page/Qemu_guest_ag
Until terraform-provider-libvirt 0.4.2, qemu-agent was used by default to get network configuration. However, if qemu-agent is not running, this creates a delay until connecting to it times-out.

In current versions, we default to not to attempt connecting to it, and attempting to retrieve network interface information from the agent needs to be enabled explicitly with `TF_USE_QEMU_AGENT`. Note that you still need to make sure the agent is running in the OS, and that is unrelated to this option.
In current versions, we default to not to attempt connecting to it, and attempting to retrieve network interface information from the agent needs to be enabled explicitly with `qemu_agent = true`, further details [here](https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/domain.html.markdown). Note that you still need to make sure the agent is running in the OS, and that is unrelated to this option.

`TF_SKIP_QEMU_AGENT` is deprecated and has no effect (except for a warning).
Note: when using bridge network configurations youneed to enable the `qemu_agent = true`. otherwise you will not retrieve the ip adresses of domains.

Be aware that this variables may be subject to change again in future versions.

Expand Down
24 changes: 9 additions & 15 deletions libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
libvirt "github.com/libvirt/libvirt-go"
"github.com/libvirt/libvirt-go-xml"
)
Expand All @@ -26,7 +26,7 @@ const domWaitLeaseDone = "all-addresses-obtained"
var errDomainInvalidState = errors.New("invalid state for domain")

func domainWaitForLeases(domain *libvirt.Domain, waitForLeases []*libvirtxml.DomainInterface,
timeout time.Duration, domainDef libvirtxml.Domain, virConn *libvirt.Connect) error {
timeout time.Duration, domainDef libvirtxml.Domain, virConn *libvirt.Connect, rd *schema.ResourceData) error {
waitFunc := func() (interface{}, string, error) {

state, err := domainGetState(*domain)
Expand All @@ -46,7 +46,7 @@ func domainWaitForLeases(domain *libvirt.Domain, waitForLeases []*libvirtxml.Dom

// check we have IPs for all the interfaces we are waiting for
for _, iface := range waitForLeases {
found, ignore, err := domainIfaceHasAddress(*domain, *iface, domainDef, virConn)
found, ignore, err := domainIfaceHasAddress(*domain, *iface, domainDef, virConn, rd)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func domainWaitForLeases(domain *libvirt.Domain, waitForLeases []*libvirtxml.Dom
}

func domainIfaceHasAddress(domain libvirt.Domain, iface libvirtxml.DomainInterface,
domainDef libvirtxml.Domain, virConn *libvirt.Connect) (found bool, ignore bool, err error) {
domainDef libvirtxml.Domain, virConn *libvirt.Connect, rd *schema.ResourceData) (found bool, ignore bool, err error) {

mac := strings.ToUpper(iface.MAC.Address)
if mac == "" {
Expand All @@ -89,7 +89,7 @@ func domainIfaceHasAddress(domain libvirt.Domain, iface libvirtxml.DomainInterfa
}

log.Printf("[DEBUG] waiting for network address for iface=%s\n", mac)
ifacesWithAddr, err := domainGetIfacesInfo(domain, domainDef, virConn)
ifacesWithAddr, err := domainGetIfacesInfo(domain, domainDef, virConn, rd)
if err != nil {
return false, false, fmt.Errorf("Error retrieving interface addresses: %s", err)
}
Expand Down Expand Up @@ -148,10 +148,9 @@ func domainIsRunning(domain libvirt.Domain) (bool, error) {
}

func domainGetIfacesInfo(domain libvirt.Domain, domainDef libvirtxml.Domain,
virConn *libvirt.Connect) ([]libvirt.DomainInterface, error) {

_, found := os.LookupEnv(useQemuAgentEnvVar)
if found {
virConn *libvirt.Connect, rd *schema.ResourceData) ([]libvirt.DomainInterface, error) {
qemuAgentEnabled := rd.Get("qemu_agent").(bool)
if qemuAgentEnabled {
// get all the interfaces using the qemu-agent, this includes also
// interfaces that are not attached to networks managed by libvirt
// (eg. bridges, macvtap,...)
Expand All @@ -164,12 +163,7 @@ func domainGetIfacesInfo(domain libvirt.Domain, domainDef libvirtxml.Domain,
return interfaces, nil
}
} else {
_, found = os.LookupEnv(skipQemuAgentEnvVar)
if found {
log.Printf("[DEBUG] %s is deprecated and qemu-agent is not used by default.", skipQemuAgentEnvVar)
}
log.Printf("[INFO] Set %s if you want to get network information from qemu-agent", useQemuAgentEnvVar)

log.Printf("[DEBUG] qemu-agent is not used")
}

log.Print("[DEBUG] getting domain addresses from networks")
Expand Down
11 changes: 9 additions & 2 deletions libvirt/resource_libvirt_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ func resourceLibvirtDomain() *schema.Resource {
Type: schema.TypeMap,
},
},
"qemu_agent": {
Type: schema.TypeBool,
Optional: true,
Required: false,
Default: false,
ForceNew: false,
},
},
}
}
Expand Down Expand Up @@ -494,7 +501,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error

if len(waitForLeases) > 0 {
err = domainWaitForLeases(domain, waitForLeases, d.Timeout(schema.TimeoutCreate),
domainDef, virConn)
domainDef, virConn, d)
if err != nil {
return err
}
Expand Down Expand Up @@ -757,7 +764,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error {
d.Set("filesystems", filesystems)

// lookup interfaces with addresses
ifacesWithAddr, err := domainGetIfacesInfo(*domain, domainDef, virConn)
ifacesWithAddr, err := domainGetIfacesInfo(*domain, domainDef, virConn, d)
if err != nil {
return fmt.Errorf("Error retrieving interface addresses: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The following arguments are supported:
* `boot_device` - (Optional) A list of devices (dev) which defines boot order. Example
[below](#define-boot-device-order).
* `emulator` - (Optional) The path of the emulator to use

* `qemu_agent` (Optional) By default is disabled, set to true for enabling it. More info [qemu-agent](https://wiki.libvirt.org/page/Qemu_guest_agent).
### Kernel and boot arguments

* `kernel` - (Optional) The path of the kernel to boot
Expand Down

0 comments on commit c1221a7

Please sign in to comment.