/
main_callhook.go
120 lines (100 loc) · 2.49 KB
/
main_callhook.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
"github.com/lxc/lxd/client"
)
type cmdCallhook struct {
global *cmdGlobal
}
func (c *cmdCallhook) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "callhook <path> <id> <hook>"
cmd.Short = "Call container lifecycle hook in LXD"
cmd.Long = `Description:
Call container lifecycle hook in LXD
This internal command notifies LXD about a container lifecycle event
(start, stopns, stop, restart) and blocks until LXD has processed it.
`
cmd.RunE = c.Run
cmd.Hidden = true
return cmd
}
func (c *cmdCallhook) Run(cmd *cobra.Command, args []string) error {
// Sanity checks
if len(args) < 2 {
cmd.Help()
if len(args) == 0 {
return nil
}
return fmt.Errorf("Missing required arguments")
}
path := args[0]
id := args[1]
state := args[2]
target := ""
// Only root should run this
if os.Geteuid() != 0 {
return fmt.Errorf("This must be run as root")
}
// Connect to LXD
socket := os.Getenv("LXD_SOCKET")
if socket == "" {
socket = filepath.Join(path, "unix.socket")
}
lxdArgs := lxd.ConnectionArgs{
SkipGetServer: true,
}
d, err := lxd.ConnectLXDUnix(socket, &lxdArgs)
if err != nil {
return err
}
// Prepare the request URL
url := fmt.Sprintf("/internal/containers/%s/on%s", id, state)
if state == "stopns" {
target = os.Getenv("LXC_TARGET")
netns := os.Getenv("LXC_NET_NS")
if target == "" {
target = "unknown"
}
url = fmt.Sprintf("%s?target=%s&netns=%s", url, target, netns)
} else if state == "stop" {
target = os.Getenv("LXC_TARGET")
if target == "" {
target = "unknown"
}
url = fmt.Sprintf("%s?target=%s", url, target)
} else if state == "network-up" {
url = fmt.Sprintf("%s?device=%s&host_name=%s", url, args[3], os.Getenv("LXC_NET_PEER"))
}
// Setup the request
hook := make(chan error, 1)
go func() {
_, _, err := d.RawQuery("GET", url, nil, "")
if err != nil {
hook <- err
return
}
hook <- nil
}()
// Handle the timeout
select {
case err := <-hook:
if err != nil {
return err
}
break
case <-time.After(30 * time.Second):
return fmt.Errorf("Hook didn't finish within 30s")
}
// If the container is rebooting, we purposefully tell LXC that this hook failed so that
// it won't reboot the container, which lets LXD start it again in the OnStop function.
// Other hook types can return without error safely.
if state == "stop" && target == "reboot" {
return fmt.Errorf("Reboot must be handled by LXD")
}
return nil
}