Merge pull request #20 from simar7/master

destroy: Add the ability to destroy a KVM domain.
This commit is contained in:
Ben LeMasurier 2016-11-03 08:27:33 -06:00 committed by GitHub
commit 850adbae3f
5 changed files with 76 additions and 5 deletions

View File

@ -13,3 +13,4 @@ Justin Kim <justin@digitalocean.com>
Ricky Medina <rm@do.co> Ricky Medina <rm@do.co>
Charlie Drage <charlie@charliedrage.com> Charlie Drage <charlie@charliedrage.com>
Michael Koppmann <me@mkoppmann.at> Michael Koppmann <me@mkoppmann.at>
Simarpreet Singh <simar@linux.com>

View File

@ -43,6 +43,7 @@ const (
ProcConnectGetLibVersion = 157 ProcConnectGetLibVersion = 157
ProcDomainMigrateSetMaxSpeed = 207 ProcDomainMigrateSetMaxSpeed = 207
ProcDomainUndefineFlags = 231 ProcDomainUndefineFlags = 231
ProcDomainDestroyFlags = 234
ProcConnectListAllDomains = 273 ProcConnectListAllDomains = 273
ProcMigratePerformParams = 305 ProcMigratePerformParams = 305
) )

View File

@ -142,6 +142,17 @@ const (
UndefineFlagNVRAM UndefineFlagNVRAM
) )
// DestroyFlags specifies options available when destroying a domain.
type DestroyFlags uint32
const (
// DestroyFlagDefault default behavior, forcefully terminate the domain.
DestroyFlagDefault DestroyFlags = 1 << iota
// DestroyFlagGraceful only sends a SIGTERM no SIGKILL.
DestroyFlagGraceful
)
// Connect establishes communication with the libvirt server. // Connect establishes communication with the libvirt server.
// The underlying libvirt socket connection must be previously established. // The underlying libvirt socket connection must be previously established.
func (l *Libvirt) Connect() error { func (l *Libvirt) Connect() error {
@ -449,6 +460,42 @@ func (l *Libvirt) Undefine(dom string, flags UndefineFlags) error {
return nil return nil
} }
// Destroy destroys the domain specified by dom, e.g., 'prod-lb-01'.
// The flags argument allows additional options to be specified such as
// allowing a graceful shutdown with SIGTERM than SIGKILL.
// For more information on available flags, see DestroyFlag*.
func (l *Libvirt) Destroy(dom string, flags DestroyFlags) error {
d, err := l.lookup(dom)
if err != nil {
return err
}
payload := struct {
Domain Domain
Flags DestroyFlags
}{
Domain: *d,
Flags: flags,
}
buf, err := encode(&payload)
if err != nil {
return err
}
resp, err := l.request(constants.ProcDomainDestroyFlags, constants.ProgramRemote, &buf)
if err != nil {
return err
}
r := <-resp
if r.Status != StatusOK {
return decodeError(r.Payload)
}
return nil
}
// Version returns the version of the libvirt daemon. // Version returns the version of the libvirt daemon.
func (l *Libvirt) Version() (string, error) { func (l *Libvirt) Version() (string, error) {
resp, err := l.request(constants.ProcConnectGetLibVersion, constants.ProgramRemote, nil) resp, err := l.request(constants.ProcConnectGetLibVersion, constants.ProgramRemote, nil)

View File

@ -215,6 +215,16 @@ func TestUndefine(t *testing.T) {
} }
} }
func TestDestroy(t *testing.T) {
conn := libvirttest.New()
l := New(conn)
var flags DestroyFlags
if err := l.Destroy("test", flags); err != nil {
t.Fatalf("unexpected destroy error: %v", err)
}
}
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
conn := libvirttest.New() conn := libvirttest.New()
l := New(conn) l := New(conn)

View File

@ -215,6 +215,16 @@ var testUndefineReply = []byte{
0x00, 0x00, 0x00, 0x00, // status 0x00, 0x00, 0x00, 0x00, // status
} }
var testDestroyReply = []byte{
0x00, 0x00, 0x00, 0x1c, // length
0x20, 0x00, 0x80, 0x86, // program
0x00, 0x00, 0x00, 0x01, // version
0x00, 0x00, 0x00, 0xea, // procedure
0x00, 0x00, 0x00, 0x01, // type
0x00, 0x00, 0x00, 0x00, // serial
0x00, 0x00, 0x00, 0x00, // status
}
var testVersionReply = []byte{ var testVersionReply = []byte{
0x00, 0x00, 0x00, 0x24, // length 0x00, 0x00, 0x00, 0x24, // length
0x20, 0x00, 0x80, 0x86, // program 0x20, 0x00, 0x80, 0x86, // program
@ -289,6 +299,8 @@ func (m *MockLibvirt) handleRemote(procedure uint32, conn net.Conn) {
conn.Write(m.reply(testMigrateReply)) conn.Write(m.reply(testMigrateReply))
case constants.ProcDomainUndefineFlags: case constants.ProcDomainUndefineFlags:
conn.Write(m.reply(testUndefineReply)) conn.Write(m.reply(testUndefineReply))
case constants.ProcDomainDestroyFlags:
conn.Write(m.reply(testDestroyReply))
} }
} }