forked from canonical/lxd
/
instance_logs.go
175 lines (141 loc) · 4.6 KB
/
instance_logs.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/gorilla/mux"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/lxd/response"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/version"
)
var instanceLogCmd = APIEndpoint{
Name: "instanceLog",
Path: "instances/{name}/logs/{file}",
Aliases: []APIEndpointAlias{
{Name: "containerLog", Path: "containers/{name}/logs/{file}"},
{Name: "vmLog", Path: "virtual-machines/{name}/logs/{file}"},
},
Delete: APIEndpointAction{Handler: instanceLogDelete, AccessHandler: allowProjectPermission("containers", "operate-containers")},
Get: APIEndpointAction{Handler: instanceLogGet, AccessHandler: allowProjectPermission("containers", "view")},
}
var instanceLogsCmd = APIEndpoint{
Name: "instanceLogs",
Path: "instances/{name}/logs",
Aliases: []APIEndpointAlias{
{Name: "containerLogs", Path: "containers/{name}/logs"},
{Name: "vmLogs", Path: "virtual-machines/{name}/logs"},
},
Get: APIEndpointAction{Handler: instanceLogsGet, AccessHandler: allowProjectPermission("containers", "view")},
}
func instanceLogsGet(d *Daemon, r *http.Request) response.Response {
/* Let's explicitly *not* try to do a containerLoadByName here. In some
* cases (e.g. when container creation failed), the container won't
* exist in the DB but it does have some log files on disk.
*
* However, we should check this name and ensure it's a valid container
* name just so that people can't list arbitrary directories.
*/
instanceType, err := urlInstanceTypeDetect(r)
if err != nil {
return response.SmartError(err)
}
projectName := projectParam(r)
name := mux.Vars(r)["name"]
// Handle requests targeted to a container on a different node
resp, err := forwardedResponseIfInstanceIsRemote(d, r, projectName, name, instanceType)
if err != nil {
return response.SmartError(err)
}
if resp != nil {
return resp
}
err = instance.ValidName(name, false)
if err != nil {
return response.BadRequest(err)
}
result := []string{}
fullName := project.Instance(projectName, name)
dents, err := ioutil.ReadDir(shared.LogPath(fullName))
if err != nil {
return response.SmartError(err)
}
for _, f := range dents {
if !validLogFileName(f.Name()) {
continue
}
result = append(result, fmt.Sprintf("/%s/instances/%s/logs/%s", version.APIVersion, name, f.Name()))
}
return response.SyncResponse(true, result)
}
func validLogFileName(fname string) bool {
/* Let's just require that the paths be relative, so that we don't have
* to deal with any escaping or whatever.
*/
return fname == "lxc.log" ||
fname == "lxc.conf" ||
fname == "qemu.log" ||
strings.HasPrefix(fname, "migration_") ||
strings.HasPrefix(fname, "snapshot_") ||
strings.HasPrefix(fname, "exec_")
}
func instanceLogGet(d *Daemon, r *http.Request) response.Response {
instanceType, err := urlInstanceTypeDetect(r)
if err != nil {
return response.SmartError(err)
}
projectName := projectParam(r)
name := mux.Vars(r)["name"]
// Handle requests targeted to a container on a different node
resp, err := forwardedResponseIfInstanceIsRemote(d, r, projectName, name, instanceType)
if err != nil {
return response.SmartError(err)
}
if resp != nil {
return resp
}
file := mux.Vars(r)["file"]
err = instance.ValidName(name, false)
if err != nil {
return response.BadRequest(err)
}
if !validLogFileName(file) {
return response.BadRequest(fmt.Errorf("log file name %s not valid", file))
}
ent := response.FileResponseEntry{
Path: shared.LogPath(project.Instance(projectName, name), file),
Filename: file,
}
return response.FileResponse(r, []response.FileResponseEntry{ent}, nil, false)
}
func instanceLogDelete(d *Daemon, r *http.Request) response.Response {
instanceType, err := urlInstanceTypeDetect(r)
if err != nil {
return response.SmartError(err)
}
projectName := projectParam(r)
name := mux.Vars(r)["name"]
// Handle requests targeted to a container on a different node
resp, err := forwardedResponseIfInstanceIsRemote(d, r, projectName, name, instanceType)
if err != nil {
return response.SmartError(err)
}
if resp != nil {
return resp
}
file := mux.Vars(r)["file"]
err = instance.ValidName(name, false)
if err != nil {
return response.BadRequest(err)
}
if !validLogFileName(file) {
return response.BadRequest(fmt.Errorf("log file name %s not valid", file))
}
if file == "lxc.log" || file == "lxc.conf" {
return response.BadRequest(fmt.Errorf("lxc.log and lxc.conf may not be deleted"))
}
return response.SmartError(os.Remove(shared.LogPath(project.Instance(projectName, name), file)))
}