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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
|
// 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 (
"fmt"
"sort"
"strings"
"github.com/mendersoftware/mender-artifact/artifact"
"github.com/urfave/cli"
)
const (
errArtifactInvalidParameters = iota
errArtifactUnsupportedVersion
errArtifactCreate
errArtifactOpen
errArtifactInvalid
errArtifactUnsupportedFeature
errSystemError
)
const (
clearsProvidesFlag = "clears-provides"
deleteClearsProvidesFlag = "delete-clears-provides"
noDefaultSoftwareVersionFlag = "no-default-software-version"
noDefaultClearsProvidesFlag = "no-default-clears-provides"
softwareNameFlag = "software-name"
softwareVersionFlag = "software-version"
softwareFilesystemFlag = "software-filesystem"
)
// Version of the mender-artifact CLI tool
var Version = "unknown"
// LatestFormatVersion is the latest version of the format, which is
// also what we default to.
const LatestFormatVersion = 3
// Copied from urfave/cli/template.go
// with the addition of the NOTE on the `global` `--compression flag`
//nolint:lll
var menderAppHelpTemplate = `NAME:
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}}
USAGE:
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
VERSION:
{{.Version}}{{end}}{{end}}{{if .Description}}
DESCRIPTION:
{{.Description}}{{end}}{{if len .Authors}}
AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
{{end}}{{$author}}{{end}}{{end}}{{if .VisibleCommands}}
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}{{end}}
NOTE:
For the commands <write>, <modify>, the '--compression' flag functions as
a global option
`
func applyCompressionInCommand(c *cli.Context) error {
// Let --compression argument work after command as well. Latest one
// applies.
if c.String("compression") != "" {
_ = c.GlobalSet("compression", c.String("compression"))
}
return nil
}
func Run(args []string) error {
return getCliContext().Run(args)
}
func getCliContext() *cli.App {
app := cli.NewApp()
app.Name = "mender-artifact"
app.Usage = "interface for manipulating Mender artifacts"
app.UsageText = "mender-artifact [--version][--help] <command> [<args>]"
app.Version = Version
app.Author = "Northern.tech AS"
app.Email = "contact@northern.tech"
app.EnableBashCompletion = true
compressors := artifact.GetRegisteredCompressorIds()
compressionFlag := cli.StringFlag{
Name: "compression",
Usage: fmt.Sprintf("Compression to use for the artifact, "+
"currently supports: %v.", strings.Join(compressors, ", ")),
}
globalCompressionFlag := compressionFlag
// The global flag is the last fallback, so here we provide a default.
globalCompressionFlag.Value = "gzip"
globalCompressionFlag.Hidden = true
privateKeyFlag := cli.StringFlag{
Name: "key, k",
Usage: "Full path to the private key that will be used to sign " +
"the Artifact.",
}
gcpKMSKeyFlag := cli.StringFlag{
Name: "gcp-kms-key",
Usage: "Resource ID of the GCP KMS key that will be used to sign " +
"the Artifact.",
}
pkcs11Flag := cli.StringFlag{
Name: "key-pkcs11",
Usage: "Use PKCS#11 interface to sign and verify artifacts",
}
publicKeyFlag := cli.StringFlag{
Name: "key, k",
Usage: "Full path to the public key that will be used to verify " +
"the Artifact signature.",
}
//
// Common Artifact flags
//
artifactName := cli.StringFlag{
Name: "artifact-name, n",
Usage: "Name of the artifact",
Required: true,
}
artifactNameDepends := cli.StringSliceFlag{
Name: "artifact-name-depends, N",
Usage: "Sets the name(s) of the artifact(s) which this update depends upon",
}
artifactProvidesGroup := cli.StringFlag{
Name: "provides-group, g",
Usage: "The group the artifact provides",
}
artifactDependsGroups := cli.StringSliceFlag{
Name: "depends-groups, G",
Usage: "The group(s) the artifact depends on",
}
// Common Software Version flags
softwareVersionNoDefault := cli.BoolFlag{
Name: noDefaultSoftwareVersionFlag,
Usage: "Disable the software version field for compatibility with old clients",
}
softwareVersionValue := cli.StringFlag{
Name: softwareVersionFlag,
Usage: "Value for the software version, defaults to the name of the artifact",
}
softwareFilesystem := cli.StringFlag{
Name: softwareFilesystemFlag,
Usage: "If specified, is used instead of rootfs-image",
}
//
// Common Payload flags
//
payloadProvides := cli.StringSliceFlag{
Name: "provides, p",
Usage: "Generic `KEY:VALUE` which is added to the type-info -> artifact_provides section." +
" Can be given multiple times",
}
payloadDepends := cli.StringSliceFlag{
Name: "depends, d",
Usage: "Generic `KEY:VALUE` which is added to the type-info -> artifact_depends section." +
" Can be given multiple times",
}
payloadMetaData := cli.StringFlag{
Name: "meta-data, m",
Usage: "The meta-data JSON `FILE` for this payload",
}
clearsArtifactProvides := cli.StringSliceFlag{
Name: clearsProvidesFlag,
Usage: "Add a clears_artifact_provides field to Artifact payload",
}
noDefaultClearsArtifactProvides := cli.BoolFlag{
Name: noDefaultClearsProvidesFlag,
Usage: "Do not add any default clears_artifact_provides fields to Artifact payload",
}
//
// write
//
writeRootfsCommand := cli.Command{
Name: "rootfs-image",
Action: writeRootfs,
Usage: "Writes Mender artifact containing rootfs image",
}
writeRootfsCommand.CustomHelpTemplate = CustomSubcommandHelpTemplate
writeRootfsCommand.Flags = []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "Payload `FILE` path or ssh-url to device for system " +
"snapshot (e.g. ssh://user@device:22022).",
Required: true,
},
cli.StringSliceFlag{
Name: "device-type, t",
Usage: "Type of device(s) supported by the Artifact. You can specify multiple " +
"compatible devices providing this parameter multiple times.",
Required: true,
},
artifactName,
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output artifact file.",
},
cli.IntFlag{
Name: "version, v",
Usage: "Version of the artifact.",
Value: LatestFormatVersion,
},
privateKeyFlag,
gcpKMSKeyFlag,
cli.StringSliceFlag{
Name: "script, s",
Usage: "Full path to the state script(s). You can specify multiple " +
"scripts providing this parameter multiple times.",
},
cli.BoolFlag{
Name: "legacy-rootfs-image-checksum",
Usage: "Use the legacy key name rootfs_image_checksum to store the providese checksum" +
" to the Artifact provides parameters instead of rootfs-image.checksum.",
},
cli.BoolFlag{
Name: "no-checksum-provide",
Usage: "Disable writing the provides checksum to the Artifact provides " +
"parameters. This is needed in case the targeted devices do not support " +
"provides and depends yet.",
},
cli.StringSliceFlag{
Name: "ssh-args, S",
Usage: "Arguments to pass to ssh - only applies when " +
"creating artifact from snapshot (i.e. FILE " +
"contains 'ssh://' schema)",
},
cli.BoolFlag{
Name: "no-progress",
Usage: "Suppress the progressbar output",
},
/////////////////////////
// Version 3 specifics.//
/////////////////////////
artifactNameDepends,
artifactProvidesGroup,
artifactDependsGroups,
payloadDepends,
payloadProvides,
clearsArtifactProvides,
noDefaultClearsArtifactProvides,
compressionFlag,
//////////////////////
// Sotware versions //
//////////////////////
softwareVersionNoDefault,
cli.StringFlag{
Name: softwareNameFlag,
Usage: "Name of the key to store the software version: rootfs-image.NAME.version," +
" instead of rootfs-image.version",
},
softwareVersionValue,
softwareFilesystem,
}
writeRootfsCommand.Before = applyCompressionInCommand
//
// Update modules: module-image
//
writeModuleCommand := cli.Command{
Name: "module-image",
Action: writeModuleImage,
Usage: "Writes Mender artifact for an update module",
UsageText: "Writes a generic Mender artifact that will be used by an update module. " +
"This command is not meant to be used directly, but should rather be wrapped by an " +
"update module build command, which prepares all the necessary files and headers " +
"for that update module.",
}
writeModuleCommand.CustomHelpTemplate = CustomSubcommandHelpTemplate
writeModuleCommand.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "device-type, t",
Usage: "Type of device(s) supported by the Artifact. You can specify multiple " +
"compatible devices providing this parameter multiple times.",
Required: true,
},
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output artifact file.",
},
cli.IntFlag{
Name: "version, v",
Usage: "Version of the artifact.",
Value: LatestFormatVersion,
},
cli.StringSliceFlag{
Name: "script, s",
Usage: "Full path to the state script(s). You can specify multiple " +
"scripts providing this parameter multiple times.",
},
artifactName,
artifactNameDepends,
artifactProvidesGroup,
artifactDependsGroups,
cli.StringFlag{
Name: "type, T",
Usage: "Type of payload. This is the same as the name of the update module",
Required: true,
},
payloadProvides,
payloadDepends,
payloadMetaData,
cli.StringSliceFlag{
Name: "file, f",
Usage: "Include `FILE` in payload. Can be given more than once.",
},
cli.StringFlag{
Name: "augment-type",
Usage: "Type of augmented payload. This is the same as the name of the update module",
},
cli.StringSliceFlag{
Name: "augment-provides",
Usage: "Generic `KEY:VALUE` which is added to the augmented type-info ->" +
" artifact_provides section. Can be given multiple times",
},
cli.StringSliceFlag{
Name: "augment-depends",
Usage: "Generic `KEY:VALUE` which is added to the augmented type-info ->" +
" artifact_depends section. Can be given multiple times",
},
cli.StringFlag{
Name: "augment-meta-data",
Usage: "The meta-data JSON `FILE` for this payload, for the augmented section",
},
cli.StringSliceFlag{
Name: "augment-file",
Usage: "Include `FILE` in payload in the augment section. Can be given more than once.",
},
clearsArtifactProvides,
noDefaultClearsArtifactProvides,
compressionFlag,
privateKeyFlag,
gcpKMSKeyFlag,
//////////////////////
// Sotware versions //
//////////////////////
softwareVersionNoDefault,
cli.StringFlag{
Name: softwareNameFlag,
Usage: "Name of the key to store the software version: rootfs-image.NAME.version," +
" instead of rootfs-image.PAYLOAD_TYPE.version",
},
softwareVersionValue,
softwareFilesystem,
}
writeModuleCommand.Before = applyCompressionInCommand
//
// Write Bootstrap artifact
//
writeBootstrapArtifactCommand := cli.Command{
Name: "bootstrap-artifact",
Action: writeBootstrapArtifact,
Usage: "Writes Mender bootstrap artifact containing empty payload",
}
writeBootstrapArtifactCommand.CustomHelpTemplate = CustomSubcommandHelpTemplate
writeBootstrapArtifactCommand.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "device-type, t",
Usage: "Type of device(s) supported by the Artifact. You can specify multiple " +
"compatible devices providing this parameter multiple times.",
Required: true,
},
artifactName,
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output artifact file.",
},
cli.IntFlag{
Name: "version, v",
Usage: "Version of the artifact.",
Value: LatestFormatVersion,
},
cli.BoolFlag{
Name: "no-progress",
Usage: "Suppress the progressbar output",
},
compressionFlag,
clearsArtifactProvides,
payloadProvides,
payloadDepends,
privateKeyFlag,
gcpKMSKeyFlag,
/////////////////////////
// Version 3 specifics.//
/////////////////////////
artifactNameDepends,
artifactProvidesGroup,
artifactDependsGroups,
}
writeBootstrapArtifactCommand.Before = applyCompressionInCommand
writeCommand := cli.Command{
Name: "write",
Usage: "Writes artifact file.",
Category: "Artifact creation and validation",
Subcommands: []cli.Command{
writeRootfsCommand,
writeModuleCommand,
writeBootstrapArtifactCommand,
},
}
//
// validate
//
validate := cli.Command{
Name: "validate",
Usage: "Validates artifact file.",
Category: "Artifact creation and validation",
Action: validateArtifact,
UsageText: "mender-artifact validate [options] <pathspec>",
Description: "This command validates artifact file provided by pathspec.",
Flags: []cli.Flag{
publicKeyFlag,
gcpKMSKeyFlag,
pkcs11Flag,
},
}
//
// read
//
readCommand := cli.Command{
Name: "read",
Usage: "Reads artifact file.",
ArgsUsage: "<artifact path>",
Category: "Artifact inspection",
Action: readArtifact,
Description: "This command validates artifact file provided by pathspec.",
Flags: []cli.Flag{
publicKeyFlag,
gcpKMSKeyFlag,
pkcs11Flag,
cli.BoolFlag{
Name: "no-progress",
Usage: "Suppress the progressbar output",
},
},
}
//
// sign
//
sign := cli.Command{
Name: "sign",
Usage: "Signs existing artifact file.",
Category: "Artifact modification",
Action: signExisting,
UsageText: "mender-artifact sign [options] <pathspec>",
Description: "This command signs artifact file provided by pathspec.",
}
sign.Flags = []cli.Flag{
privateKeyFlag,
gcpKMSKeyFlag,
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output signed artifact file; " +
"if none is provided existing artifact will be replaced with the signed one",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Force creating new signature if the artifact is already signed",
},
pkcs11Flag,
}
//
// modify existing
//
modify := cli.Command{
Name: "modify",
Usage: "Modifies image or artifact file.",
Category: "Artifact modification",
Action: modifyArtifact,
UsageText: "mender-artifact modify [options] <pathspec>",
Description: "This command modifies existing image or artifact file provided by pathspec." +
" NOTE: Currently only ext4 payloads can be modified",
}
modify.Flags = []cli.Flag{
cli.StringFlag{
Name: "server-uri, u",
Usage: "Mender server URI; the default URI will be replaced with given one.",
},
cli.StringFlag{
Name: "server-cert, c",
Usage: "Full path to the certificate file that will be used for validating " +
"Mender server by the client.",
},
cli.StringFlag{
Name: "verification-key, v",
Usage: "Full path to the public verification key that is used by the client " +
"to verify the artifact.",
},
cli.StringFlag{
Name: "artifact-name, n",
Usage: "Name of the artifact",
},
cli.StringFlag{
Name: "name",
Usage: "Deprecated. This is an alias for --artifact-name",
},
artifactNameDepends,
artifactProvidesGroup,
artifactDependsGroups,
payloadProvides,
payloadDepends,
payloadMetaData,
clearsArtifactProvides,
cli.StringSliceFlag{
Name: deleteClearsProvidesFlag,
Usage: "Erase one \"Clears Provides\" filter from the Artifact.",
},
cli.StringFlag{
Name: "tenant-token, t",
Usage: "Full path to the tenant token that will be injected into modified file.",
},
privateKeyFlag,
gcpKMSKeyFlag,
compressionFlag,
}
modify.Before = func(c *cli.Context) error {
if c.String("name") != "" {
_ = c.Set("artifact-name", c.String("name"))
}
return applyCompressionInCommand(c)
}
copy := cli.Command{
Name: "cp",
Usage: "cp <src> <dst>",
Category: "Artifact modification",
Description: "Copies a file into or out of a mender artifact, or sdimg",
UsageText: "Copy from or into an artifact, or sdimg where either the <src>" +
" or <dst> has to be of the form [artifact|sdimg]:<filepath>, <src> can" +
"come from stdin in the case that <src> is '-'",
Action: Copy,
}
copy.Flags = []cli.Flag{
compressionFlag,
privateKeyFlag,
gcpKMSKeyFlag,
}
cat := cli.Command{
Name: "cat",
Usage: "cat [artifact|sdimg|uefiimg]:<filepath>",
Description: "Cat can output a file from a mender artifact or mender image to stdout.",
Category: "Artifact modification",
Action: Cat,
}
install := cli.Command{
Name: "install",
Usage: "install -m <permissions> <hostfile> [artifact|sdimg|uefiimg]:<filepath> or" +
" install -d [artifact|sdimg|uefiimg]:<directory>",
Description: "Installs a directory, or a file from the host filesystem, to the artifact" +
" or sdimg.",
Category: "Artifact modification",
Action: Install,
}
install.Flags = []cli.Flag{
cli.IntFlag{
Name: "mode, m",
Usage: "Set the permission bits in the file",
},
cli.BoolFlag{
Name: "directory, d",
Usage: "Create a directory inside an artifact",
},
}
remove := cli.Command{
Name: "rm",
Usage: "rm [artifact|sdimg|uefiimg]:<filepath>",
Category: "Artifact modification",
Description: "Removes the given file or directory from an Artifact or sdimg.",
Action: Remove,
}
remove.Flags = []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "remove directories and their contents recursively",
},
}
//
// dump
//
dumpCommand := cli.Command{
Name: "dump",
Usage: "Dump contents from Artifacts",
ArgsUsage: "<Artifact>",
Description: "Dump various raw files from the Artifact. These can be used to create a new" +
" Artifact with the same components.",
Category: "Artifact inspection",
Action: DumpCommand,
}
dumpCommand.Flags = []cli.Flag{
cli.StringFlag{
Name: "files",
Usage: "Dump all included files in the first payload into given folder",
},
cli.StringFlag{
Name: "meta-data",
Usage: "Dump the contents of the meta-data field in the first payload into given" +
" folder",
},
cli.StringFlag{
Name: "scripts",
Usage: "Dump all included state scripts into given folder",
},
cli.BoolFlag{
Name: "print-cmdline",
Usage: "Print the command line that can recreate the same Artifact with the" +
" components being dumped. If all the components are being dumped, a nearly" +
" identical Artifact can be created. Note that timestamps will cause the checksum" +
" of the Artifact to be different, and signatures can not be recreated this way." +
" The command line will only use long option names.",
},
cli.BoolFlag{
Name: "print0-cmdline",
Usage: "Same as 'print-cmdline', except that the arguments are separated by a null" +
" character (0x00).",
},
}
globalFlags := []cli.Flag{
globalCompressionFlag,
}
app.Commands = []cli.Command{
writeCommand,
readCommand,
validate,
sign,
modify,
copy,
cat,
install,
remove,
dumpCommand,
}
app.Flags = append([]cli.Flag{}, globalFlags...)
// Display all flags and commands alphabetically
for _, cmd := range app.Commands {
sortFlags(cmd)
}
app.CustomAppHelpTemplate = menderAppHelpTemplate
return app
}
func sortFlags(c cli.Command) {
sort.Sort(cli.FlagsByName(c.Flags))
sort.Sort(cli.CommandsByName(c.Subcommands))
for _, cmd := range c.Subcommands {
sortFlags(cmd)
}
}
|