Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 30 #129

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1b2d460
started libvirt-go monitor
Oct 9, 2016
f2b7da5
libvirtgo: added new, connect, run, and disconnect
robertojrojas Oct 9, 2016
8bc24ba
Merge remote-tracking branch 'upstream/master' into issue_30
Oct 10, 2016
84266ae
Merge remote-tracking branch 'upstream/master' into issue_30
Oct 11, 2016
316181f
qmp/libvirt-go: start build tag support
Oct 12, 2016
f8e85b6
Merge remote-tracking branch 'upstream/master' into issue_30
Oct 12, 2016
f675e2d
qmp/libvirtgo: better code distribution
Oct 12, 2016
e0a60fe
qmp/libvirtgo: move exported methods to implementation
Oct 12, 2016
f5d43e3
qmp/libvirtgo: move domain, uri to implementation
robertojrojas Oct 12, 2016
b737dda
qmp/libvirtgo: fix pointer bug on Connect, Run methods
robertojrojas Oct 13, 2016
c4e662b
qmp/libvirtgo: add command example
robertojrojas Oct 13, 2016
bfb4bef
qmp/libvirtgo: start events notificaiton
Oct 15, 2016
e4d22a6
qmp/libvirtgo: compiles on linux
robertojrojas Oct 15, 2016
f92839f
qmp/libvirtgo: remove doneChan. change log messages.
Oct 16, 2016
85c6ceb
qmp/libvirtgo: fixed return val issue.
robertojrojas Oct 16, 2016
d550391
qmp/libvirtgo: fix bugs. add more comments.
robertojrojas Oct 17, 2016
bea75d9
qmp/libvirtgo: change libvirtgo_events example.
robertojrojas Oct 17, 2016
b35ebff
qmp/libvirtgo: remove fmt package. add events interval env variable
robertojrojas Oct 17, 2016
0a7d859
qmp/libvirtgo: construct event changes.
Oct 17, 2016
50f507f
qmp/libvirtgo: fix bug
robertojrojas Oct 17, 2016
06bd4ef
Merge remote-tracking branch 'upstream/master' into issue_30
Oct 17, 2016
a4e3825
qmp/libvirtgo: prepare for tests.
Oct 18, 2016
7acfe55
qmp/libvirtgo: fix typos and bugs.
robertojrojas Oct 18, 2016
417cec6
qmp/libvirtgo: start unit tests
robertojrojas Oct 18, 2016
daac6ad
qmp/libvirtgo: add more test
robertojrojas Oct 18, 2016
4ef4ca4
qmp/libvirtgo: add getPollInterval tests
robertojrojas Oct 18, 2016
c3c598c
qmp/libvirtgo: add events OK test
robertojrojas Oct 18, 2016
f6749c9
qmp/libvirtgo: add libvirtgo_run_command to README section
Oct 18, 2016
4838752
qmp/libvirtgo: add libvirtgo_events example to README section
Oct 18, 2016
64e311b
qmp/libvirtgo: changes PR review
Oct 21, 2016
bab3bd7
qmp/libvirtgo: use interfaces for internal implmentation
robertojrojas Oct 21, 2016
20740c8
qmp/libvirtgo: fix events test
robertojrojas Oct 21, 2016
42ad391
qmp/libvirtgo: add events loop interval parameter
robertojrojas Oct 24, 2016
aa14b77
qmp/libvirtgo: add libvirtgo to README
robertojrojas Oct 24, 2016
98ca2de
Merge remote-tracking branch 'upstream/master' into issue_30
Oct 24, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,49 @@ You should have an output similar to this:
```{r, engine='bash', count_lines}
Connecting to unix:///var/run/libvirt/libvirt-sock
Domain should be shut off now
```

#### libvirtgo_run_command

[libvirtgo_run_command](./libvirtgo_run_command) demonstrates how to use
the [libvirtGoMonitorLinux](https://godoc.org/github.com/digitalocean/go-qemu/libvirtGoMonitorLinux)
package to send a QMP command to the specified domain.

To run:
```{r, engine='bash', count_lines}
$ go get github.com/digitalocean/go-qemu
$ go run examples/libvirtgo_run_command/main.go -uri="qemu:///system" -domainName="centos7"
```


You should see an output similar to this:
```{r, engine='bash', count_lines}
query-cpus: {"return":[{"current":true,"CPU":0,"qom_path":"/machine/unattached/device[0]","pc":-2130342250,"halted":true,"thread_id":2462}],"id":"libvirt-36"}
```

#### libvirtgo_events

[libvirtgo_events](./libvirtgo_events) demonstrates how to use
the [libvirtGoMonitorLinux](https://godoc.org/github.com/digitalocean/go-qemu/libvirtGoMonitorLinux)
package to wait for lifecycle events from the specified domain.

To run:
```{r, engine='bash', count_lines}
$ go get github.com/digitalocean/go-qemu

Terminal 1:
$ go run examples/libvirtgo_events/main.go -uri="qemu:///system" -domainName="ubuntu14.04"

Terminal 2:
virsh -c qemu:///system
virsh # start ubuntu14.04
```


You should see an output similar to this on Terminal 1:
```{r, engine='bash', count_lines}
Waiting for Domain events...
Press the Enter key to stop
Event: qmp.Event{Event:"Domain event=\"resumed\" detail=\"unpaused\"", Data:map[string]interface {}{"details":libvirt.DomainLifecycleEvent{Event:4, Detail:0}}, Timestamp:struct { Seconds int64 "json:\"seconds\""; Microseconds int64 "json:\"microseconds\"" }{Seconds:11, Microseconds:0}}
Event: qmp.Event{Event:"Domain event=\"started\" detail=\"booted\"", Data:map[string]interface {}{"details":libvirt.DomainLifecycleEvent{Event:2, Detail:0}}, Timestamp:struct { Seconds int64 "json:\"seconds\""; Microseconds int64 "json:\"microseconds\"" }{Seconds:12, Microseconds:0}}
```
55 changes: 55 additions & 0 deletions examples/libvirtgo_events/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2016 The go-qemu Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"fmt"
"log"

"github.com/digitalocean/go-qemu/qmp"
)

var (
uri = flag.String("uri", "qemu:///system", "URI to connect to the libvirtd host.")
domainName = flag.String("domainName", "mydomain", "The domain to run commands against.")
)

func main() {
flag.Parse()

mon := qmp.NewLibvirtGoMonitor(*uri, *domainName)

err := mon.Connect()
if err != nil {
log.Fatalf("Unable to connect: %v\n", err)
}

events, err := mon.Events()
if err != nil {
log.Fatalf("Unable to register for events: %v\n", err)
}

fmt.Println("Waiting for Domain events...")
go func() {
for event := range events {
fmt.Printf("Event: %#v\n", event)
}
}()

fmt.Println("Press the Enter key to stop")
fmt.Scanln()
mon.Disconnect()
}
57 changes: 57 additions & 0 deletions examples/libvirtgo_run_command/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2016 The go-qemu Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"fmt"
"log"

qemu "github.com/digitalocean/go-qemu"
"github.com/digitalocean/go-qemu/qmp"
)

var (
uri = flag.String("uri", "qemu:///system", "URI to connect to the libvirtd host.")
domainName = flag.String("domainName", "mydomain", "The domain to run commands against.")
)

func main() {
flag.Parse()

mon := qmp.NewLibvirtGoMonitor(*uri, *domainName)

if err := mon.Connect(); err != nil {
log.Fatalf("failed to connect: %v", err)
}

domain, err := qemu.NewDomain(mon, *domainName)
if err != nil {
log.Fatalf("failed to create domain object: %v", err)
}
defer domain.Close()

// domain.CPUs will forward QMP commands to the monitor
cpus, err := domain.CPUs()
if err != nil {
log.Fatalf("failed to get cpus: %v", err)
}

fmt.Printf("CPUs: %v\n", cpus)

if err = mon.Disconnect(); err != nil {
log.Fatalf("Unable to disconnect: %v\n", err)
}
}
11 changes: 10 additions & 1 deletion qmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Package `qmp` enables interaction with QEMU instances via the QEMU Machine Proto

### Libvirt

If your environment is managed by Libvirt, QMP interaction must be proxied through the Libvirt daemon. This can be be done through two available drivers:
If your environment is managed by Libvirt, QMP interaction must be proxied through the Libvirt daemon. This can be done through three available drivers:

#### RPC

Expand All @@ -19,6 +19,15 @@ conn, err := net.DialTimeout("tcp", "192.168.1.1:16509", 2*time.Second)
monitor := libvirtrpc.New("stage-lb-1", conn)
```

### Libvirt-go

This monitor provides communication with the Libvirt daemon using the [libvirt-go](https://github.com/rgbkrk/libvirt-go) package as its internal implementation.
At the moment, libvirt-go only supports `Linux`.

```go
monitor, err := qmp.NewLibvirtGoMonitor("qemu:///system", "centos7")
```

#### virsh

A connection to the monitor socket is provided by proxing requests through the `virsh` executable.
Expand Down
23 changes: 23 additions & 0 deletions qmp/libvirtgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The go-qemu Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package qmp

// LibvirtGoMonitor is a Monitor that wraps the libvirt-go package to
// communicate with a QEMU Machine Protocol (QMP) socket.
// Communication is proxied via the libvirtd daemon. Multiple
// connections to the same hypervisor and domain are permitted.
type LibvirtGoMonitor struct {
Monitor
}
Loading