destroy: Adding the ability to destroy a domain.
Signed-off-by: Simarpreet Singh <simar@linux.com>
This commit is contained in:
		
				
					committed by
					
						 Simarpreet Singh
						Simarpreet Singh
					
				
			
			
				
	
			
			
			
						parent
						
							e7178e25dc
						
					
				
				
					commit
					693bffa997
				
			
							
								
								
									
										1
									
								
								AUTHORS
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								AUTHORS
									
									
									
									
									
								
							| @@ -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> | ||||||
|   | |||||||
| @@ -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 | ||||||
| ) | ) | ||||||
|   | |||||||
							
								
								
									
										47
									
								
								libvirt.go
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								libvirt.go
									
									
									
									
									
								
							| @@ -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) | ||||||
|   | |||||||
| @@ -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) | ||||||
|   | |||||||
| @@ -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)) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user