This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
registration.go
203 lines (170 loc) · 4.71 KB
/
registration.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2014 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stage0
import (
"crypto/rand"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"syscall"
"time"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/common"
)
const (
retryCount = 3
mdsRegisteredFile = "./mds-registered"
)
var retryPause = time.Second
var errUnreachable = errors.New(`could not reach the metadata service.
Make sure metadata service is currently running or use
"rkt run --mds-register=false" to skip pod registration.
For more information on running metadata service,
see https://github.com/rkt/rkt/blob/master/Documentation/subcommands/metadata-service.md`)
// registerPod registers pod with metadata service.
// Returns authentication token to be passed in the URL
func registerPod(root string, uuid *types.UUID, apps schema.AppList) (token string, rerr error) {
u := uuid.String()
var err error
token, err = generateMDSToken()
if err != nil {
rerr = errwrap.Wrap(errors.New("failed to generate MDS token"), err)
return
}
pmfPath := common.PodManifestPath(root)
pmf, err := os.Open(pmfPath)
if err != nil {
rerr = errwrap.Wrap(fmt.Errorf("failed to open runtime manifest (%v)", pmfPath), err)
return
}
pth := fmt.Sprintf("/pods/%v?token=%v", u, token)
err = httpRequest("PUT", pth, pmf)
pmf.Close()
if err != nil {
rerr = errwrap.Wrap(errors.New("failed to register pod with metadata svc"), err)
return
}
defer func() {
if rerr != nil {
unregisterPod(root, uuid)
}
}()
rf, err := os.Create(filepath.Join(root, mdsRegisteredFile))
if err != nil {
rerr = errwrap.Wrap(errors.New("failed to create mds-register file"), err)
return
}
rf.Close()
for _, app := range apps {
ampath := common.ImageManifestPath(root, app.Name)
amf, err := os.Open(ampath)
if err != nil {
rerr = errwrap.Wrap(fmt.Errorf("failed reading app manifest %q", ampath), err)
return
}
err = registerApp(u, app.Name.String(), amf)
amf.Close()
if err != nil {
rerr = errwrap.Wrap(errors.New("failed to register app with metadata svc"), err)
return
}
}
return
}
// unregisterPod unregisters pod with the metadata service.
func unregisterPod(root string, uuid *types.UUID) error {
_, err := os.Stat(filepath.Join(root, mdsRegisteredFile))
switch {
case err == nil:
pth := path.Join("/pods", uuid.String())
return httpRequest("DELETE", pth, nil)
case os.IsNotExist(err):
return nil
default:
return err
}
}
func generateMDSToken() (string, error) {
bytes := make([]byte, 16)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", bytes), nil
}
func registerApp(uuid, app string, r io.Reader) error {
pth := path.Join("/pods", uuid, app)
return httpRequest("PUT", pth, r)
}
func httpRequest(method, pth string, body io.Reader) error {
uri := "http://unixsock" + pth
t := &http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", common.MetadataServiceRegSock)
},
}
var err error
for i := 0; i < retryCount; i++ {
var req *http.Request
req, err = http.NewRequest(method, uri, body)
if err != nil {
return err
}
cli := http.Client{Transport: t}
var resp *http.Response
resp, err = cli.Do(req)
switch {
case err == nil:
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("%v %v returned %v", method, pth, resp.StatusCode)
}
return nil
default:
log.Error(err)
time.Sleep(retryPause)
}
}
if urlErr, ok := err.(*url.Error); ok {
if opErr, ok := urlErr.Err.(*net.OpError); ok {
errno := opErr.Err
// in go1.5 syscall errors in OpError.Err are of type
// os.SyscallError instead of directly syscall.Errno
if sysErr, ok := opErr.Err.(*os.SyscallError); ok {
errno = sysErr.Err
}
if errno == syscall.ENOENT || errno == syscall.ENOTSOCK {
return errUnreachable
}
}
}
return err
}
// CheckMdsAvailability checks whether a local metadata service can be reached.
func CheckMdsAvailability() error {
if conn, err := net.Dial("unix", common.MetadataServiceRegSock); err != nil {
return errUnreachable
} else {
conn.Close()
return nil
}
}