Merge pull request #20 from simar7/master
destroy: Add the ability to destroy a KVM domain.
This commit is contained in:
		
							
								
								
									
										11
									
								
								AUTHORS
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								AUTHORS
									
									
									
									
									
								
							| @@ -4,12 +4,13 @@ DigitalOcean, Inc | ||||
|  | ||||
| Original Authors | ||||
| ---------------- | ||||
| Ben LeMasurier	<blemasurier@digitalocean.com> | ||||
| Matt Layher	<mlayher@digitalocean.com> | ||||
| Ben LeMasurier	 <blemasurier@digitalocean.com> | ||||
| Matt Layher	 <mlayher@digitalocean.com> | ||||
|  | ||||
| Contributors | ||||
| ------------ | ||||
| Justin Kim	<justin@digitalocean.com> | ||||
| Ricky Medina	<rm@do.co> | ||||
| Charlie Drage <charlie@charliedrage.com> | ||||
| Justin Kim	 <justin@digitalocean.com> | ||||
| Ricky Medina	 <rm@do.co> | ||||
| Charlie Drage 	 <charlie@charliedrage.com> | ||||
| Michael Koppmann <me@mkoppmann.at> | ||||
| Simarpreet Singh <simar@linux.com> | ||||
|   | ||||
| @@ -43,6 +43,7 @@ const ( | ||||
| 	ProcConnectGetLibVersion     = 157 | ||||
| 	ProcDomainMigrateSetMaxSpeed = 207 | ||||
| 	ProcDomainUndefineFlags      = 231 | ||||
| 	ProcDomainDestroyFlags       = 234 | ||||
| 	ProcConnectListAllDomains    = 273 | ||||
| 	ProcMigratePerformParams     = 305 | ||||
| ) | ||||
|   | ||||
							
								
								
									
										47
									
								
								libvirt.go
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								libvirt.go
									
									
									
									
									
								
							| @@ -142,6 +142,17 @@ const ( | ||||
| 	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. | ||||
| // The underlying libvirt socket connection must be previously established. | ||||
| func (l *Libvirt) Connect() error { | ||||
| @@ -449,6 +460,42 @@ func (l *Libvirt) Undefine(dom string, flags UndefineFlags) error { | ||||
| 	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. | ||||
| func (l *Libvirt) Version() (string, error) { | ||||
| 	resp, err := l.request(constants.ProcConnectGetLibVersion, constants.ProgramRemote, nil) | ||||
|   | ||||
| @@ -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) { | ||||
| 	conn := libvirttest.New() | ||||
| 	l := New(conn) | ||||
|   | ||||
| @@ -215,6 +215,16 @@ var testUndefineReply = []byte{ | ||||
| 	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{ | ||||
| 	0x00, 0x00, 0x00, 0x24, // length | ||||
| 	0x20, 0x00, 0x80, 0x86, // program | ||||
| @@ -289,6 +299,8 @@ func (m *MockLibvirt) handleRemote(procedure uint32, conn net.Conn) { | ||||
| 		conn.Write(m.reply(testMigrateReply)) | ||||
| 	case constants.ProcDomainUndefineFlags: | ||||
| 		conn.Write(m.reply(testUndefineReply)) | ||||
| 	case constants.ProcDomainDestroyFlags: | ||||
| 		conn.Write(m.reply(testDestroyReply)) | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user