Skip to content

Commit

Permalink
main: Add -version
Browse files Browse the repository at this point in the history
Give callers an easy way to extract both the plugin version and the
dependency versions.  With this commit:

  $ ./terraform-provider-libvirt -version
  ./terraform-provider-libvirt 48adac3-dirty
  Compiled against library: libvirt 3.9.0
  Using library: libvirt 3.9.0
  Running hypervisor: QEMU 2.9.0
  Running against daemon: 3.9.0

The details are very similar to:

  $ virsh version --daemon
  Compiled against library: libvirt 3.9.0
  Using library: libvirt 3.9.0
  Using API: QEMU 3.9.0
  Running hypervisor: QEMU 2.9.0
  Running against daemon: 3.9.0

except the Go bindings do not currently expose the API version [1].
I've submitted a patch to libvirt-go adding ParseVersion there.  But
until then, carrying our own parseVersion shouldn't be too much
trouble.

I hunted around for a way to expose this in Terraform.  Currently:

  $ cat main.tf
  provider "libvirt" {
    uri = "qemu:///system"
  }
  trking@trking /tmp/test $ terraform version
  Terraform v0.11.8
  + provider.libvirt (unversioned)

but the RPC API is not particularly clear to me, and the versions
there may be extracted from plugin filenames and not from RPC calls.

[1]: https://libvirt.org/git/?p=libvirt-go.git;a=blob;f=connect.go;h=8cc7cc771f7d258400175df47fcc8b3779ef4930;hb=9c5bdce3c18faad94cb383e7ec42f5122a8c7fa1#l313
  • Loading branch information
wking committed Oct 12, 2018
1 parent 48adac3 commit e6dbca1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
17 changes: 12 additions & 5 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
``` openSUSE 42.2/ Centos7/ Ubuntu.. ```

### Terraform Version Report
(Provided by running `terraform -v`.)

### Libvirt version
```sh
terraform -v
```

```virsh --version```
### Provider and libvirt versions

### terraform-provider-libvirt plugin version (git-hash)
```sh
terraform-provider-libvirt -version
```

``` git log```
If that gives you "was not built correctly", get the Git commit hash from your local provider repository:

```sh
git describe --always --abbrev=40 --dirty
```
___
# Description of Issue/Question

Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
LDFLAGS += -X main.version=$$(git describe --always --abbrev=40 --dirty)

default: build

build: gofmtcheck golint vet
go build
go build -ldflags "${LDFLAGS}"

install:
go install
Expand Down
66 changes: 64 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,82 @@
package main

import (
"github.com/dmacvicar/terraform-provider-libvirt/libvirt"
"github.com/hashicorp/terraform/plugin"
"flag"
"fmt"
"log"
"math/rand"
"os"
"time"

"github.com/dmacvicar/terraform-provider-libvirt/libvirt"
"github.com/hashicorp/terraform/plugin"
libvirtgo "github.com/libvirt/libvirt-go"
)

var version = "was not built correctly" // set via the Makefile

func main() {
versionFlag := flag.Bool("version", false, "print version information and exit")
flag.Parse()
if *versionFlag {
err := printVersion()
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}

defer libvirt.CleanupLibvirtConnections()

plugin.Serve(&plugin.ServeOpts{
ProviderFunc: libvirt.Provider,
})
}

func printVersion() error {
fmt.Printf("%s %s\n", os.Args[0], version)

fmt.Printf("Compiled against library: libvirt %s\n", parseVersion(libvirtgo.VERSION_NUMBER))

libvirtVersion, err := libvirtgo.GetVersion()
if err != nil {
return err
}
fmt.Printf("Using library: libvirt %s\n", parseVersion(libvirtVersion))

conn, err := libvirtgo.NewConnect("qemu:///system")
if err != nil {
return err
}
defer conn.Close()

hvType, err := conn.GetType()
if err != nil {
return err
}
libvirtVersion, err = conn.GetVersion()
if err != nil {
return err
}
fmt.Printf("Running hypervisor: %s %s\n", hvType, parseVersion(libvirtVersion))

libvirtVersion, err = conn.GetLibVersion()
if err != nil {
return err
}
fmt.Printf("Running against daemon: %s\n", parseVersion(libvirtVersion))

return nil
}

func parseVersion(version uint32) string {
release := version % 1000
version /= 1000
minor := version % 1000
major := version / 1000
return fmt.Sprintf("%d.%d.%d", major, minor, release)
}

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

0 comments on commit e6dbca1

Please sign in to comment.