From a031b83a09a8628435317a03f199cdc18b78262f Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Wed, 1 Nov 2017 11:24:59 -0400 Subject: Initial checkin from CRI-O repo Signed-off-by: Matthew Heon --- cmd/kpod/history.go | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 cmd/kpod/history.go (limited to 'cmd/kpod/history.go') diff --git a/cmd/kpod/history.go b/cmd/kpod/history.go new file mode 100644 index 000000000..dd0da38a6 --- /dev/null +++ b/cmd/kpod/history.go @@ -0,0 +1,243 @@ +package main + +import ( + "reflect" + "strconv" + "strings" + "time" + + "github.com/containers/image/types" + units "github.com/docker/go-units" + "github.com/kubernetes-incubator/cri-o/cmd/kpod/formats" + "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/pkg/errors" + "github.com/urfave/cli" +) + +const ( + createdByTruncLength = 45 + idTruncLength = 13 +) + +// historyTemplateParams stores info about each layer +type historyTemplateParams struct { + ID string + Created string + CreatedBy string + Size string + Comment string +} + +// historyJSONParams is only used when the JSON format is specified, +// and is better for data processing from JSON. +// historyJSONParams will be populated by data from v1.History and types.BlobInfo, +// the members of the struct are the sama data types as their sources. +type historyJSONParams struct { + ID string `json:"id"` + Created *time.Time `json:"created"` + CreatedBy string `json:"createdBy"` + Size int64 `json:"size"` + Comment string `json:"comment"` +} + +// historyOptions stores cli flag values +type historyOptions struct { + human bool + noTrunc bool + quiet bool + format string +} + +var ( + historyFlags = []cli.Flag{ + cli.BoolTFlag{ + Name: "human, H", + Usage: "Display sizes and dates in human readable format", + }, + cli.BoolFlag{ + Name: "no-trunc, notruncate", + Usage: "Do not truncate the output", + }, + cli.BoolFlag{ + Name: "quiet, q", + Usage: "Display the numeric IDs only", + }, + cli.StringFlag{ + Name: "format", + Usage: "Change the output to JSON or a Go template", + }, + } + + historyDescription = "Displays the history of an image. The information can be printed out in an easy to read, " + + "or user specified format, and can be truncated." + historyCommand = cli.Command{ + Name: "history", + Usage: "Show history of a specified image", + Description: historyDescription, + Flags: historyFlags, + Action: historyCmd, + ArgsUsage: "", + } +) + +func historyCmd(c *cli.Context) error { + if err := validateFlags(c, historyFlags); err != nil { + return err + } + + runtime, err := getRuntime(c) + if err != nil { + return errors.Wrapf(err, "Could not get config") + } + defer runtime.Shutdown(false) + + format := genHistoryFormat(c.Bool("quiet")) + if c.IsSet("format") { + format = c.String("format") + } + + args := c.Args() + if len(args) == 0 { + return errors.Errorf("an image name must be specified") + } + if len(args) > 1 { + return errors.Errorf("Kpod history takes at most 1 argument") + } + imgName := args[0] + + opts := historyOptions{ + human: c.BoolT("human"), + noTrunc: c.Bool("no-trunc"), + quiet: c.Bool("quiet"), + format: format, + } + + history, layers, imageID, err := runtime.GetHistory(imgName) + if err != nil { + return errors.Wrapf(err, "error getting history of image %q", imgName) + } + + return generateHistoryOutput(history, layers, imageID, opts) +} + +func genHistoryFormat(quiet bool) (format string) { + if quiet { + return formats.IDString + } + return "table {{.ID}}\t{{.Created}}\t{{.CreatedBy}}\t{{.Size}}\t{{.Comment}}\t" +} + +// historyToGeneric makes an empty array of interfaces for output +func historyToGeneric(templParams []historyTemplateParams, JSONParams []historyJSONParams) (genericParams []interface{}) { + if len(templParams) > 0 { + for _, v := range templParams { + genericParams = append(genericParams, interface{}(v)) + } + return + } + for _, v := range JSONParams { + genericParams = append(genericParams, interface{}(v)) + } + return +} + +// generate the header based on the template provided +func (h *historyTemplateParams) headerMap() map[string]string { + v := reflect.Indirect(reflect.ValueOf(h)) + values := make(map[string]string) + for h := 0; h < v.NumField(); h++ { + key := v.Type().Field(h).Name + value := key + values[key] = strings.ToUpper(splitCamelCase(value)) + } + return values +} + +// getHistorytemplateOutput gets the modified history information to be printed in human readable format +func getHistoryTemplateOutput(history []v1.History, layers []types.BlobInfo, imageID string, opts historyOptions) (historyOutput []historyTemplateParams) { + var ( + outputSize string + createdTime string + createdBy string + count = 1 + ) + for i := len(history) - 1; i >= 0; i-- { + if i != len(history)-1 { + imageID = "" + } + if !opts.noTrunc && i == len(history)-1 { + imageID = imageID[:idTruncLength] + } + + var size int64 + if !history[i].EmptyLayer { + size = layers[len(layers)-count].Size + count++ + } + + if opts.human { + createdTime = units.HumanDuration(time.Since((*history[i].Created))) + " ago" + outputSize = units.HumanSize(float64(size)) + } else { + createdTime = (history[i].Created).Format(time.RFC3339) + outputSize = strconv.FormatInt(size, 10) + } + + createdBy = strings.Join(strings.Fields(history[i].CreatedBy), " ") + if !opts.noTrunc && len(createdBy) > createdByTruncLength { + createdBy = createdBy[:createdByTruncLength-3] + "..." + } + + params := historyTemplateParams{ + ID: imageID, + Created: createdTime, + CreatedBy: createdBy, + Size: outputSize, + Comment: history[i].Comment, + } + historyOutput = append(historyOutput, params) + } + return +} + +// getHistoryJSONOutput returns the history information in its raw form +func getHistoryJSONOutput(history []v1.History, layers []types.BlobInfo, imageID string) (historyOutput []historyJSONParams) { + count := 1 + for i := len(history) - 1; i >= 0; i-- { + var size int64 + if !history[i].EmptyLayer { + size = layers[len(layers)-count].Size + count++ + } + + params := historyJSONParams{ + ID: imageID, + Created: history[i].Created, + CreatedBy: history[i].CreatedBy, + Size: size, + Comment: history[i].Comment, + } + historyOutput = append(historyOutput, params) + } + return +} + +// generateHistoryOutput generates the history based on the format given +func generateHistoryOutput(history []v1.History, layers []types.BlobInfo, imageID string, opts historyOptions) error { + if len(history) == 0 { + return nil + } + + var out formats.Writer + + switch opts.format { + case formats.JSONString: + historyOutput := getHistoryJSONOutput(history, layers, imageID) + out = formats.JSONStructArray{Output: historyToGeneric([]historyTemplateParams{}, historyOutput)} + default: + historyOutput := getHistoryTemplateOutput(history, layers, imageID, opts) + out = formats.StdoutTemplateArray{Output: historyToGeneric(historyOutput, []historyJSONParams{}), Template: opts.format, Fields: historyOutput[0].headerMap()} + } + + return formats.Writer(out).Out() +} -- cgit v1.2.3-54-g00ecf From c13f61798aa7bcf7b4de7ee31aa30148a3b08d97 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Wed, 1 Nov 2017 13:22:04 -0400 Subject: Prune Server package. Convert to new github location. Signed-off-by: Matthew Heon --- CONTRIBUTING.md | 8 +- Dockerfile | 4 +- Makefile | 4 +- cmd/kpod/common.go | 11 +- cmd/kpod/diff.go | 2 +- cmd/kpod/history.go | 2 +- cmd/kpod/images.go | 6 +- cmd/kpod/info.go | 2 +- cmd/kpod/inspect.go | 6 +- cmd/kpod/kill.go | 2 +- cmd/kpod/load.go | 2 +- cmd/kpod/login.go | 2 +- cmd/kpod/logout.go | 2 +- cmd/kpod/logs.go | 2 +- cmd/kpod/mount.go | 2 +- cmd/kpod/pause.go | 2 +- cmd/kpod/ps.go | 6 +- cmd/kpod/pull.go | 4 +- cmd/kpod/push.go | 4 +- cmd/kpod/rename.go | 2 +- cmd/kpod/rm.go | 2 +- cmd/kpod/save.go | 2 +- cmd/kpod/stats.go | 4 +- cmd/kpod/stop.go | 2 +- cmd/kpod/tag.go | 2 +- cmd/kpod/unpause.go | 2 +- cmd/kpod/wait.go | 2 +- contrib/rpm/Makefile | 14 - contrib/rpm/crio.spec | 76 -- contrib/systemd/crio-shutdown.service | 14 - contrib/systemd/crio.service | 24 - contrib/test/integration/README.md | 21 - contrib/test/integration/ansible.cfg | 359 ------ contrib/test/integration/build/bats.yml | 17 - contrib/test/integration/build/cri-o.yml | 79 -- contrib/test/integration/build/cri-tools.yml | 16 - contrib/test/integration/build/kubernetes.yml | 63 - contrib/test/integration/build/plugins.yml | 50 - contrib/test/integration/build/runc.yml | 23 - .../test/integration/callback_plugins/default.py | 156 --- contrib/test/integration/e2e.yml | 57 - contrib/test/integration/golang.yml | 51 - contrib/test/integration/main.yml | 58 - contrib/test/integration/results.yml | 62 - contrib/test/integration/system.yml | 117 -- contrib/test/integration/test.yml | 25 - contrib/test/integration/vars.yml | 8 - contrib/test/requirements.txt | 54 - contrib/test/venv-ansible-playbook.sh | 106 -- kubernetes.md | 105 -- libkpod/config.go | 2 +- libkpod/container.go | 6 +- libkpod/container_data.go | 6 +- libkpod/container_server.go | 10 +- libkpod/kill.go | 4 +- libkpod/pause.go | 2 +- libkpod/remove.go | 2 +- libkpod/rename.go | 4 +- libkpod/sandbox/sandbox.go | 2 +- libkpod/stats.go | 2 +- libkpod/stop.go | 2 +- libkpod/wait.go | 2 +- libpod/container.go | 2 +- libpod/diff.go | 2 +- libpod/images/image_data.go | 2 +- libpod/in_memory_state.go | 2 +- libpod/oci.go | 2 +- libpod/runtime_img.go | 2 +- oci/oci.go | 2 +- server/apparmor/aaparser.go | 89 -- server/apparmor/apparmor_common.go | 14 - server/apparmor/apparmor_supported.go | 145 --- server/apparmor/apparmor_unsupported.go | 18 - server/apparmor/template.go | 45 - server/config.go | 112 -- server/container_attach.go | 147 --- server/container_create.go | 1215 -------------------- server/container_exec.go | 108 -- server/container_execsync.go | 46 - server/container_list.go | 112 -- server/container_portforward.go | 91 -- server/container_remove.go | 20 - server/container_start.go | 43 - server/container_stats.go | 14 - server/container_stats_list.go | 13 - server/container_status.go | 102 -- server/container_stop.go | 19 - server/container_updateruntimeconfig.go | 11 - server/image_fs_info.go | 13 - server/image_list.go | 41 - server/image_pull.go | 108 -- server/image_remove.go | 52 - server/image_status.go | 53 - server/inspect.go | 105 -- server/inspect_test.go | 235 ---- server/naming.go | 86 -- server/runtime_status.go | 41 - server/sandbox_list.go | 94 -- server/sandbox_network.go | 70 -- server/sandbox_remove.go | 98 -- server/sandbox_run.go | 615 ---------- server/sandbox_status.go | 41 - server/sandbox_stop.go | 114 -- server/seccomp/seccomp.go | 165 --- server/seccomp/seccomp_unsupported.go | 20 - server/seccomp/types.go | 93 -- server/secrets.go | 162 --- server/server.go | 423 ------- server/utils.go | 183 --- server/version.go | 27 - tutorial.md | 425 ------- 111 files changed, 75 insertions(+), 7257 deletions(-) delete mode 100644 contrib/rpm/Makefile delete mode 100644 contrib/rpm/crio.spec delete mode 100644 contrib/systemd/crio-shutdown.service delete mode 100644 contrib/systemd/crio.service delete mode 100644 contrib/test/integration/README.md delete mode 100644 contrib/test/integration/ansible.cfg delete mode 100644 contrib/test/integration/build/bats.yml delete mode 100644 contrib/test/integration/build/cri-o.yml delete mode 100644 contrib/test/integration/build/cri-tools.yml delete mode 100644 contrib/test/integration/build/kubernetes.yml delete mode 100644 contrib/test/integration/build/plugins.yml delete mode 100644 contrib/test/integration/build/runc.yml delete mode 100644 contrib/test/integration/callback_plugins/default.py delete mode 100644 contrib/test/integration/e2e.yml delete mode 100644 contrib/test/integration/golang.yml delete mode 100644 contrib/test/integration/main.yml delete mode 100644 contrib/test/integration/results.yml delete mode 100644 contrib/test/integration/system.yml delete mode 100644 contrib/test/integration/test.yml delete mode 100644 contrib/test/integration/vars.yml delete mode 100644 contrib/test/requirements.txt delete mode 100755 contrib/test/venv-ansible-playbook.sh delete mode 100644 kubernetes.md delete mode 100644 server/apparmor/aaparser.go delete mode 100644 server/apparmor/apparmor_common.go delete mode 100644 server/apparmor/apparmor_supported.go delete mode 100644 server/apparmor/apparmor_unsupported.go delete mode 100644 server/apparmor/template.go delete mode 100644 server/config.go delete mode 100644 server/container_attach.go delete mode 100644 server/container_create.go delete mode 100644 server/container_exec.go delete mode 100644 server/container_execsync.go delete mode 100644 server/container_list.go delete mode 100644 server/container_portforward.go delete mode 100644 server/container_remove.go delete mode 100644 server/container_start.go delete mode 100644 server/container_stats.go delete mode 100644 server/container_stats_list.go delete mode 100644 server/container_status.go delete mode 100644 server/container_stop.go delete mode 100644 server/container_updateruntimeconfig.go delete mode 100644 server/image_fs_info.go delete mode 100644 server/image_list.go delete mode 100644 server/image_pull.go delete mode 100644 server/image_remove.go delete mode 100644 server/image_status.go delete mode 100644 server/inspect.go delete mode 100644 server/inspect_test.go delete mode 100644 server/naming.go delete mode 100644 server/runtime_status.go delete mode 100644 server/sandbox_list.go delete mode 100644 server/sandbox_network.go delete mode 100644 server/sandbox_remove.go delete mode 100644 server/sandbox_run.go delete mode 100644 server/sandbox_status.go delete mode 100644 server/sandbox_stop.go delete mode 100644 server/seccomp/seccomp.go delete mode 100644 server/seccomp/seccomp_unsupported.go delete mode 100644 server/seccomp/types.go delete mode 100644 server/secrets.go delete mode 100644 server/server.go delete mode 100644 server/utils.go delete mode 100644 server/version.go delete mode 100644 tutorial.md (limited to 'cmd/kpod/history.go') diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cc549116d..c121ac416 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -# Contributing to CRI-O +# Contributing to Libpod We'd love to have you join the community! Below summarizes the processes that we follow. @@ -13,7 +13,7 @@ that we follow. ## Reporting Issues Before reporting an issue, check our backlog of -[open issues](https://github.com/kubernetes-incubator/cri-o/issues) +[open issues](https://github.com/projectatomic/libpod/issues) to see if someone else has already reported it. If so, feel free to add your scenario, or additional information, to the discussion. Or simply "subscribe" to it to be notified when it is updated. @@ -120,9 +120,9 @@ IRC group on `irc.freenode.net` called `cri-o` that has been setup. For discussions around issues/bugs and features, you can use the github -[issues](https://github.com/kubernetes-incubator/cri-o/issues) +[issues](https://github.com/projectatomic/libpod/issues) and -[PRs](https://github.com/kubernetes-incubator/cri-o/pulls) +[PRs](https://github.com/projectatomic/libpod/pulls) tracking system.