This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
fetch.go
145 lines (126 loc) · 4.35 KB
/
fetch.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
// 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 main
import (
"github.com/appc/spec/schema/types"
"github.com/rkt/rkt/common"
"github.com/rkt/rkt/common/apps"
"github.com/rkt/rkt/rkt/image"
"github.com/rkt/rkt/store/imagestore"
"github.com/rkt/rkt/store/treestore"
"github.com/spf13/cobra"
)
var (
defaultOS = common.GetOS()
defaultArch = common.GetArch()
cmdFetch = &cobra.Command{
Use: "fetch IMAGE_URL...",
Short: "Fetch image(s) and store them in the local store",
Long: `Locates and downloads remote ACIs and their attached signatures.
If the ACI is available in the local store, the image will not be fetched
again.`,
Run: runWrapper(runFetch),
}
flagFullHash bool
// We can't have different defaults for a given flag variable shared across
// subcommands, so we can't use pullPolicyUpdate here
flagPullPolicyDefaultUpdate string
)
func init() {
// Disable interspersed flags to stop parsing after the first non flag
// argument. All the subsequent parsing will be done by parseApps.
// This is needed to correctly handle multiple IMAGE --signature=sigfile options
cmdFetch.Flags().SetInterspersed(false)
cmdFetch.Flags().Var((*appAsc)(&rktApps), "signature", "local signature file to use in validating the preceding image")
cmdFetch.Flags().BoolVar(&flagStoreOnly, "store-only", false, "use only available images in the store (do not discover or download from remote URLs)")
cmdFetch.Flags().MarkDeprecated("store-only", "please use --pull-policy=never")
cmdFetch.Flags().BoolVar(&flagNoStore, "no-store", false, "fetch images ignoring the local store")
cmdFetch.Flags().MarkDeprecated("no-store", "please use --pull-policy=update")
cmdFetch.Flags().BoolVar(&flagFullHash, "full", false, "print the full image hash after fetching")
cmdFetch.Flags().StringVar(&flagPullPolicyDefaultUpdate, "pull-policy", image.PullPolicyUpdate, "when to pull an image")
cmdRkt.AddCommand(cmdFetch)
// Hide image fetch option in command list
cmdImageFetch := *cmdFetch
cmdImageFetch.Hidden = true
cmdImage.AddCommand(&cmdImageFetch)
}
func runFetch(cmd *cobra.Command, args []string) (exit int) {
if err := parseApps(&rktApps, args, cmd.Flags(), false); err != nil {
stderr.PrintE("unable to parse arguments", err)
return 254
}
if rktApps.Count() < 1 {
stderr.Print("must provide at least one image")
return 254
}
// flagPullPolicy defaults to new regardless of subcommand, so we use a
// different variable for the flag on fetch and then set it here
flagPullPolicy = flagPullPolicyDefaultUpdate
if flagStoreOnly && flagNoStore {
stderr.Print("both --store-only and --no-store specified")
return 254
}
if flagStoreOnly {
flagPullPolicy = image.PullPolicyNever
}
if flagNoStore {
flagPullPolicy = image.PullPolicyUpdate
}
s, err := imagestore.NewStore(storeDir())
if err != nil {
stderr.PrintE("cannot open store", err)
return 254
}
ts, err := treestore.NewStore(treeStoreDir(), s)
if err != nil {
stderr.PrintE("cannot open treestore", err)
return 254
}
ks := getKeystore()
config, err := getConfig()
if err != nil {
stderr.PrintE("cannot get configuration", err)
return 254
}
ft := &image.Fetcher{
S: s,
Ts: ts,
Ks: ks,
Headers: config.AuthPerHost,
DockerAuth: config.DockerCredentialsPerRegistry,
InsecureFlags: globalFlags.InsecureFlags,
Debug: globalFlags.Debug,
TrustKeysFromHTTPS: globalFlags.TrustKeysFromHTTPS,
PullPolicy: flagPullPolicy,
WithDeps: true,
}
err = ft.FetchImages(&rktApps)
if err != nil {
stderr.Error(err)
return 254
}
err = rktApps.Walk(func(app *apps.App) error {
hash := app.ImageID.String()
if !flagFullHash {
hash = types.ShortHash(hash)
}
stdout.Print(hash)
return nil
})
if err != nil {
stderr.Error(err)
return 254
}
return
}