Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

(TC-151) add diff/changed files capability

  • Loading branch information...
johnmccabe committed Jun 14, 2017
1 parent e87def4 commit 06c372812ca7cdaeed6f8c0f591471292a6b0e10
Showing with 87 additions and 0 deletions.
  1. +68 −0 capabilities/diff/diff.go
  2. +7 −0 capabilities/diff/init.go
  3. +2 −0 capabilities/init.go
  4. +10 −0 types/changedfile.go
@@ -0,0 +1,68 @@
package diff

import (
"context"
"fmt"

"github.com/docker/docker/pkg/archive"
"github.com/puppetlabs/lumogon/capabilities/payloadfilter"
"github.com/puppetlabs/lumogon/capabilities/registry"
"github.com/puppetlabs/lumogon/dockeradapter"
"github.com/puppetlabs/lumogon/logging"
"github.com/puppetlabs/lumogon/types"
)

var diffDescription = `The diff capability returns files changed from the initial image as a map["changed file"]"change type"`

// The Diff capability output from the container runtime inspect
var diffCapability = dockeradapter.DockerAPICapability{
Capability: types.Capability{
Schema: "http://puppet.com/lumogon/capability/diff/draft-01/schema#1",
Title: "Changed Files",
Name: "diff",
Description: diffDescription,
Type: "dockerapi",
Payload: nil,
SupportedOS: map[string]int{"all": 1},
},
Harvest: func(capability *dockeradapter.DockerAPICapability, client dockeradapter.Harvester, id string, target types.TargetContainer) {
logging.Debug("[Diff] Harvesting diff from %s [%s]", target.Name, target.ID)
capability.HarvestID = id
logging.Debug("[Diff] Harvesting diff capability, harvestid: %s", capability.HarvestID)

ctx := context.Background()
output := make(map[string]interface{})

diffs, err := client.ContainerDiff(ctx, target.ID)
if err != nil {
errorMsg := fmt.Sprintf("[Diff] Error getting diff from targetContainer: %s, error: %s", target.Name, err)
logging.Debug(errorMsg)
capability.PayloadError(errorMsg)
return
}

for _, diff := range diffs {
logging.Debug("[Diff] Path: %s, Kind %d", diff.Path, diff.Kind)
var kind string
switch diff.Kind {
case archive.ChangeModify:
kind = "Modified"
case archive.ChangeAdd:
kind = "Added"
case archive.ChangeDelete:
kind = "Deleted"
}
output[diff.Path] = kind
}
filtered, _ := payloadfilter.Filter(output)

logging.Debug("[Diff] Output: %v", output)
logging.Debug("[Diff] Filtered: %v", filtered)
capability.Payload = filtered
},
}

func init() {
logging.Debug("[Diff] Initialising capability: %s", diffCapability.Title)
registry.Registry.Add(diffCapability)
}
@@ -0,0 +1,7 @@
package diff

// Init exists to allow host init() functions to run when
// invoked from the capabilities Init function, which is
// itself invoked by the Lumogon command handler.
func Init() {
}
@@ -1,6 +1,7 @@
package capabilities

import (
"github.com/puppetlabs/lumogon/capabilities/diff"
"github.com/puppetlabs/lumogon/capabilities/host"
"github.com/puppetlabs/lumogon/capabilities/label"
"github.com/puppetlabs/lumogon/capabilities/ospackages"
@@ -12,4 +13,5 @@ func Init() {
host.Init()
label.Init()
ospackages.Init()
diff.Init()
}
@@ -0,0 +1,10 @@
package types

// ChangedFile represents a file in a container that has been changed
// from that originally provided in the containers image, where Path
// is the file which has changed and Kind is the type of change.
// This is based on Dockers container.ContainerChangeResponseItem
type ChangedFile struct {
Kind uint8 `json:"kind"`
Path string `json:"path"`
}

0 comments on commit 06c3728

Please sign in to comment.
You can’t perform that action at this time.