This repository has been archived by the owner on Jul 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
diff.go
68 lines (59 loc) · 2.12 KB
/
diff.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
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)
}