This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
sandbox.go
123 lines (102 loc) · 3.29 KB
/
sandbox.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
// Copyright 2016 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 pod
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"strconv"
"github.com/appc/spec/schema"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/common"
"github.com/rkt/rkt/pkg/lock"
)
// ErrImmutable is the error that is returned by SandboxManifest if the pod is immutable,
// hence changes on the pod manifest are not allowed.
var ErrImmutable = errors.New("immutable pod")
// SandboxManifest loads the underlying pod manifest and checks whether mutable operations are allowed.
// It returns ErrImmutable if the pod does not allow mutable operations or any other error if the operation failed.
// Upon success a reference to the pod manifest is returned and mutable operations are possible.
func (p *Pod) SandboxManifest() (*schema.PodManifest, error) {
_, pm, err := p.PodManifest() // this takes the lock fd to load the manifest, hence path is not needed here
if err != nil {
return nil, errwrap.Wrap(errors.New("error loading pod manifest"), err)
}
ms, ok := pm.Annotations.Get("coreos.com/rkt/stage1/mutable")
if ok {
p.mutable, err = strconv.ParseBool(ms)
if err != nil {
return nil, errwrap.Wrap(errors.New("error parsing mutable annotation"), err)
}
}
if !p.mutable {
return nil, ErrImmutable
}
return pm, nil
}
// UpdateManifest updates the given pod manifest in the given path atomically on the file system.
// The pod manifest has to be locked using LockManifest first to avoid races in case of concurrent writes.
func (p *Pod) UpdateManifest(m *schema.PodManifest, path string) error {
if !p.mutable {
return ErrImmutable
}
mpath := common.PodManifestPath(path)
mstat, err := os.Stat(mpath)
if err != nil {
return err
}
tmpf, err := ioutil.TempFile(path, "")
if err != nil {
return err
}
defer func() {
tmpf.Close()
os.Remove(tmpf.Name())
}()
if err := tmpf.Chmod(mstat.Mode().Perm()); err != nil {
return err
}
if err := json.NewEncoder(tmpf).Encode(m); err != nil {
return err
}
if err := os.Rename(tmpf.Name(), mpath); err != nil {
return err
}
return nil
}
// ExclusiveLockManifest gets an exclusive lock on only the pod manifest in the app sandbox.
// Since the pod might already be running, we can't just get an exclusive lock on the pod itself.
func (p *Pod) ExclusiveLockManifest() error {
if p.manifestLock != nil {
return p.manifestLock.ExclusiveLock() // This is idempotent
}
l, err := lock.ExclusiveLock(common.PodManifestLockPath(p.Path()), lock.RegFile)
if err != nil {
return err
}
p.manifestLock = l
return nil
}
// UnlockManifest unlocks the pod manifest lock.
func (p *Pod) UnlockManifest() error {
if p.manifestLock == nil {
return nil
}
if err := p.manifestLock.Close(); err != nil {
return err
}
p.manifestLock = nil
return nil
}