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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// Copyright 2022 Northern.tech AS
//
// 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 cli
import (
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
"github.com/mendersoftware/mender-artifact/areader"
"github.com/mendersoftware/mender-artifact/artifact"
"github.com/mendersoftware/mender-artifact/handlers"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
type dumpFileStore struct {
fileDir string
args *[]string
}
func DumpCommand(c *cli.Context) error {
var dumpArgs []string
if c.NArg() != 1 {
return cli.NewExitError("Need to specify exactly one Artifact with dump command",
errArtifactInvalidParameters)
}
art, err := os.Open(c.Args().First())
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"Error opening Artifact: %s", err.Error()),
errArtifactOpen)
}
defer art.Close()
ar := areader.NewReader(art)
scriptsReadCallback := func(r io.Reader, i os.FileInfo) error {
fullPath := path.Join(c.String("scripts"), i.Name())
script, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0755)
if err != nil {
return err
}
defer script.Close()
_, err = io.Copy(script, r)
if err != nil {
return err
}
dumpArgs = append(dumpArgs, "--script", fullPath)
return nil
}
if len(c.String("scripts")) > 0 {
err = os.MkdirAll(c.String("scripts"), 0755)
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"Could not create directory: %s", err.Error()), errSystemError)
}
ar.ScriptsReadCallback = scriptsReadCallback
}
err = ar.ReadArtifactHeaders()
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error dumping Artifact: %s",
err.Error()), errArtifactInvalid)
}
err = dumpPayloads(c, ar, &dumpArgs)
if err != nil {
return err
}
if c.Bool("print-cmdline") && c.Bool("print0-cmdline") {
return errors.New("--print-cmdline and --print0-cmdline are conflicting options.")
} else if c.Bool("print-cmdline") {
printCmdline(ar, dumpArgs, ' ', '\n')
} else if c.Bool("print0-cmdline") {
printCmdline(ar, dumpArgs, 0, 0)
}
return nil
}
func dumpPayloads(c *cli.Context, ar *areader.Reader, dumpArgs *[]string) error {
handlers := ar.GetHandlers()
if len(handlers) != 1 {
return cli.NewExitError("The dump command can handle one payload only",
errArtifactUnsupportedFeature)
}
if len(c.String("meta-data")) > 0 {
err := dumpMetaData(c.String("meta-data"), dumpArgs, handlers)
if err != nil {
return err
}
}
if len(c.String("files")) > 0 {
store := &dumpFileStore{
fileDir: c.String("files"),
args: dumpArgs,
}
for _, h := range handlers {
h.SetUpdateStorerProducer(store)
}
}
err := ar.ReadArtifactData()
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error dumping Artifact: %s",
err.Error()), errArtifactInvalid)
}
return nil
}
func dumpMetaData(
metaDataDir string,
dumpArgs *[]string,
handlers map[int]handlers.Installer,
) error {
err := os.MkdirAll(metaDataDir, 0755)
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"Unable to create directory: %s", err.Error()), errSystemError)
}
// Hardcode to 0 index for now.
handler := handlers[0]
for _, augmented := range []bool{false, true} {
var metaData map[string]interface{}
var fullPath string
var metaDataArg string
if augmented {
metaData = handler.GetUpdateAugmentMetaData()
fullPath = path.Join(metaDataDir, "0000.meta-data-augment")
metaDataArg = "--augment-meta-data"
} else {
metaData = handler.GetUpdateOriginalMetaData()
fullPath = path.Join(metaDataDir, "0000.meta-data")
metaDataArg = "--meta-data"
}
if len(metaData) == 0 {
continue
}
metaDataFd, err := os.OpenFile(fullPath,
os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"Unable to create meta-data file: %s", err.Error()), errSystemError)
}
defer metaDataFd.Close()
w := json.NewEncoder(metaDataFd)
err = w.Encode(metaData)
if err != nil {
return errors.New("Unencodeable map in dumpPayloads. Should not happen.")
}
*dumpArgs = append(*dumpArgs, metaDataArg, fullPath)
}
return nil
}
func printCmdline(ar *areader.Reader, args []string, sep, endChar rune) {
// Even if it is a rootfs payload, we use the module-image writer, since
// this can recreate either type.
fmt.Printf("write%cmodule-image", sep)
if ar.GetInfo().Version == 3 {
artProvs := ar.GetArtifactProvides()
fmt.Printf("%c--artifact-name%c%s", sep, sep, artProvs.ArtifactName)
if len(artProvs.ArtifactGroup) > 0 {
fmt.Printf("%c--provides-group%c%s", sep, sep, artProvs.ArtifactGroup)
}
artDeps := ar.GetArtifactDepends()
if len(artDeps.ArtifactName) > 0 {
fmt.Printf("%c--artifact-name-depends%c%s", sep, sep,
strings.Join(artDeps.ArtifactName,
fmt.Sprintf("%c--artifact-name-depends%c", sep, sep)))
}
fmt.Printf("%c--device-type%c%s", sep, sep,
strings.Join(artDeps.CompatibleDevices, fmt.Sprintf("%c--device-type%c", sep, sep)))
if len(artDeps.ArtifactGroup) > 0 {
fmt.Printf("%c--depends-groups%c%s", sep, sep,
strings.Join(artDeps.ArtifactGroup, fmt.Sprintf("%c--depends-groups%c", sep, sep)))
}
} else if ar.GetInfo().Version == 2 {
fmt.Printf("%c--artifact-name%c%s", sep, sep, ar.GetArtifactName())
fmt.Printf("%c--device-type%c%s", sep, sep,
strings.Join(ar.GetCompatibleDevices(), " --device-type "))
}
handlers := ar.GetHandlers()
handler := handlers[0]
fmt.Printf("%c--type%c%s", sep, sep, *handler.GetUpdateType())
// Always add this flag, since we will write custom flags.
fmt.Printf("%c--%s", sep, noDefaultSoftwareVersionFlag)
provs := handler.GetUpdateOriginalProvides()
for key, value := range provs {
fmt.Printf("%c--provides%c%s:%s", sep, sep, key, value)
}
deps := handler.GetUpdateOriginalDepends()
for key, value := range deps {
fmt.Printf("%c--depends%c%s:%s", sep, sep, key, value)
}
// Always add this flag, since we will write custom flags.
fmt.Printf("%c--%s", sep, noDefaultClearsProvidesFlag)
caps := handler.GetUpdateOriginalClearsProvides()
for _, value := range caps {
fmt.Printf("%c--%s%c%s", sep, clearsProvidesFlag, sep, value)
}
if len(args) > 0 {
fmt.Printf("%c%s", sep, strings.Join(args, string(sep)))
}
fmt.Printf("%c", endChar)
}
func (d *dumpFileStore) NewUpdateStorer(
updateType *string,
payloadNum int,
) (handlers.UpdateStorer, error) {
return d, nil
}
func (d *dumpFileStore) Initialize(artifactHeaders,
artifactAugmentedHeaders artifact.HeaderInfoer,
payloadHeaders handlers.ArtifactUpdateHeaders) error {
return nil
}
func (d *dumpFileStore) PrepareStoreUpdate() error {
return os.MkdirAll(d.fileDir, 0755)
}
func (d *dumpFileStore) StoreUpdate(r io.Reader, info os.FileInfo) error {
fullPath := path.Join(d.fileDir, info.Name())
file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, r)
if err != nil {
return err
}
*d.args = append(*d.args, "--file", fullPath)
return nil
}
func (d *dumpFileStore) FinishStoreUpdate() error {
return nil
}
|