/
run.go
175 lines (159 loc) · 4.51 KB
/
run.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
// Copyright 2014 CoreOS, Inc.
//
// 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.
//+build linux
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/appc/spec/schema/types"
"github.com/coreos/rocket/cas"
"github.com/coreos/rocket/pkg/keystore"
"github.com/coreos/rocket/stage0"
)
var (
flagStage1Init string
flagStage1Rootfs string
flagVolumes volumeMap
flagPrivateNet bool
cmdRun = &Command{
Name: "run",
Summary: "Run image(s) in an application container in rocket",
Usage: "[--volume LABEL:SOURCE] IMAGE...",
Description: `IMAGE should be a string referencing an image; either a hash, local file on disk, or URL.
They will be checked in that order and the first match will be used.`,
Run: runRun,
}
)
func init() {
commands = append(commands, cmdRun)
cmdRun.Flags.StringVar(&flagStage1Init, "stage1-init", "", "path to stage1 binary override")
cmdRun.Flags.StringVar(&flagStage1Rootfs, "stage1-rootfs", "", "path to stage1 rootfs tarball override")
cmdRun.Flags.Var(&flagVolumes, "volume", "volumes to mount into the shared container environment")
cmdRun.Flags.BoolVar(&flagPrivateNet, "private-net", false, "give container a private network")
flagVolumes = volumeMap{}
}
// findImages will recognize a ACI hash and use that, import a local file, use
// discovery or download an ACI directly.
func findImages(args []string, ds *cas.Store, ks *keystore.Keystore) (out []types.Hash, err error) {
out = make([]types.Hash, len(args))
for i, img := range args {
// check if it is a valid hash, if so let it pass through
h, err := types.NewHash(img)
if err == nil {
fullKey, err := ds.ResolveKey(img)
if err != nil {
return nil, fmt.Errorf("could not resolve key: %v", err)
}
h, err = types.NewHash(fullKey)
if err != nil {
// should never happen
panic(err)
}
out[i] = *h
continue
}
// import the local file if it exists
file, err := os.Open(img)
if err == nil {
key, err := ds.WriteACI(file)
file.Close()
if err != nil {
return nil, fmt.Errorf("%s: %v", img, err)
}
h, err := types.NewHash(key)
if err != nil {
// should never happen
panic(err)
}
out[i] = *h
continue
}
key, err := fetchImage(img, ds, ks)
if err != nil {
return nil, err
}
h, err = types.NewHash(key)
if err != nil {
// should never happen
panic(err)
}
out[i] = *h
}
return out, nil
}
func runRun(args []string) (exit int) {
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "run: Must provide at least one image\n")
return 1
}
if globalFlags.Dir == "" {
log.Printf("dir unset - using temporary directory")
var err error
globalFlags.Dir, err = ioutil.TempDir("", "rkt")
if err != nil {
fmt.Fprintf(os.Stderr, "error creating temporary directory: %v\n", err)
return 1
}
}
ds := cas.NewStore(globalFlags.Dir)
ks := getKeystore()
imgs, err := findImages(args, ds, ks)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
cfg := stage0.Config{
Store: ds,
ContainersDir: containersDir(),
Debug: globalFlags.Debug,
Stage1Init: flagStage1Init,
Stage1Rootfs: flagStage1Rootfs,
Images: imgs,
Volumes: flagVolumes,
PrivateNet: flagPrivateNet,
}
cdir, err := stage0.Setup(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "run: error setting up stage0: %v\n", err)
return 1
}
stage0.Run(cfg, cdir) // execs, never returns
return 1
}
// volumeMap implements the flag.Value interface to contain a set of mappings
// from mount label --> mount path
type volumeMap map[string]string
func (vm *volumeMap) Set(s string) error {
elems := strings.Split(s, ":")
if len(elems) != 2 {
return errors.New("volume must be of form key:path")
}
key := elems[0]
if _, ok := (*vm)[key]; ok {
return fmt.Errorf("got multiple flags for volume %q", key)
}
(*vm)[key] = elems[1]
return nil
}
func (vm *volumeMap) String() string {
var ss []string
for k, v := range *vm {
ss = append(ss, fmt.Sprintf("%s:%s", k, v))
}
return strings.Join(ss, ",")
}