-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrestart.go
48 lines (41 loc) · 986 Bytes
/
restart.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
47
48
package vm
import (
"fmt"
"strings"
"github.com/libvirt/libvirt-go"
)
// Restart looks up a domain by name and restarts it. If force is true, the
// domain is restarted without prompting for user confirmation first. If
// graceful is false, the dom is reset forcibly rather than being sent a
// restart signal.
func Restart(uri, name string, force bool, graceful bool) error {
conn, err := libvirt.NewConnect(uri)
if err != nil {
return err
}
var dom *libvirt.Domain
dom, err = conn.LookupDomainByName(name)
if err != nil {
return nil
}
defer dom.Free()
if !force {
fmt.Printf("Are you sure you wish to restart %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.Reboot(libvirt.DOMAIN_REBOOT_DEFAULT); err != nil {
return err
}
} else {
if err := dom.Reset(0); err != nil {
return err
}
}
return nil
}