-
Notifications
You must be signed in to change notification settings - Fork 2
/
down.go
46 lines (39 loc) · 883 Bytes
/
down.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package vm
import (
"fmt"
"strings"
"github.com/libvirt/libvirt-go"
)
// Down looks up an active domain by name and stops it. If force is true, the
// domain is stopped without prompting for confirmation. If graceful is true,
// the domain is shutdown gracefully.
func Down(uri, name string, force bool, graceful bool) error {
conn, err := libvirt.NewConnect(uri)
if err != nil {
return err
}
dom, err := conn.LookupDomainByName(name)
if err != nil {
return nil
}
defer dom.Free()
if !force {
fmt.Printf("Are you sure you wish to stop %v? (y/N) ", name)
var response string
fmt.Scan(&response)
response = strings.ToLower(strings.TrimSpace(response))
if response != "y" {
return nil
}
}
if graceful {
if err := dom.Shutdown(); err != nil {
return err
}
} else {
if err := dom.Destroy(); err != nil {
return err
}
}
return nil
}