-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathhandlecontent.go
More file actions
173 lines (150 loc) · 5.42 KB
/
handlecontent.go
File metadata and controls
173 lines (150 loc) · 5.42 KB
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
// Copyright (c) 2017-2018 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package zedagent
// content info specific parser/utility routines
import (
"bytes"
"crypto/sha256"
"strings"
zconfig "github.com/lf-edge/eve-api/go/config"
"github.com/lf-edge/eve/pkg/pillar/types"
uuid "github.com/satori/go.uuid"
)
var contentInfoHash []byte
// stringsToUuids() - converts list of strings to a list of uuids,
//
// returns a list with a nil uuid and a last error if
// conversion fails
func stringsToUuids(strings []string) ([]uuid.UUID, error) {
list := make([]uuid.UUID, len(strings))
for i, str := range strings {
var err error
list[i], err = uuid.FromString(str)
if err != nil {
log.Errorf("stringsToUuids(): error parsing UUID '%s' index %d, %v\n",
str, i, err)
return []uuid.UUID{nilUUID}, err
}
}
return list, nil
}
// getDatastoreIDList() - returns list of datastores UUIDs
func getDatastoreIDList(contentTree *zconfig.ContentTree) ([]uuid.UUID, error) {
idsStrList := contentTree.GetDsIdsList()
if len(idsStrList) == 0 {
// Compatibility with the old controller, which does not support
// list of datastores
idsStrList = []string{contentTree.GetDsId()}
}
return stringsToUuids(idsStrList)
}
// content info parsing routine
func parseContentInfoConfig(ctx *getconfigContext,
config *zconfig.EdgeDevConfig) {
log.Tracef("Started parsing content info config")
cfgContentTreeList := config.GetContentInfo()
h := sha256.New()
for _, cfgContentTree := range cfgContentTreeList {
computeConfigElementSha(h, cfgContentTree)
}
newHash := h.Sum(nil)
if bytes.Equal(newHash, contentInfoHash) {
return
}
log.Functionf("parseContentInfo: Applying updated config "+
"Last Sha: % x, "+
"New Sha: % x, "+
"Num of cfgContentInfo: %d",
contentInfoHash, newHash, len(cfgContentTreeList))
contentInfoHash = newHash
// First look for deleted ones
items := ctx.pubContentTreeConfig.GetAll()
for idStr := range items {
found := false
for _, cfgContentTree := range cfgContentTreeList {
if cfgContentTree.GetUuid() == idStr {
found = true
break
}
}
// content tree not found, delete
if !found {
log.Functionf("parseContentInfo: deleting %s\n", idStr)
unpublishContentTreeConfig(ctx, idStr)
}
}
for _, cfgContentTree := range cfgContentTreeList {
contentConfig := new(types.ContentTreeConfig)
contentConfig.ContentID, _ = uuid.FromString(cfgContentTree.GetUuid())
contentConfig.DatastoreIDList, _ = getDatastoreIDList(cfgContentTree)
contentConfig.RelativeURL = cfgContentTree.GetURL()
contentConfig.Format = cfgContentTree.GetIformat()
contentConfig.ContentSha256 = strings.ToLower(cfgContentTree.GetSha256())
contentConfig.MaxDownloadSize = cfgContentTree.GetMaxSizeBytes()
contentConfig.DisplayName = cfgContentTree.GetDisplayName()
contentConfig.CustomMeta = cfgContentTree.GetCustomMetaData()
contentConfig.IsLocal = true
controllerDNID := cfgContentTree.GetDesignatedNodeId()
// If this node is not designated node id set IsLocal to false.
// Content will be downloaded to only to the designated node id of that content tree.
// So on other nodes in the cluster mark the content tree as non-local.
// On single node EVE (either kvm or 'k'), this node will always be designated node.
// But if this is the contenttree of a container, we download it to all nodes of the cluster,
// so set IsLocal true in such case.
if controllerDNID != "" && controllerDNID != devUUID.String() {
if contentConfig.Format == zconfig.Format_CONTAINER {
contentConfig.IsLocal = true
} else {
contentConfig.IsLocal = false
}
}
log.Noticef("parseContentInfo designated ID copy from volume config: %v, contentid %v, url %s", controllerDNID, contentConfig.ContentID, contentConfig.RelativeURL)
publishContentTreeConfig(ctx, *contentConfig)
}
ctx.pubContentTreeConfig.SignalRestarted()
log.Functionf("parsing content info config done\n")
}
func publishContentTreeConfig(ctx *getconfigContext,
config types.ContentTreeConfig) {
key := config.Key()
log.Tracef("publishContentTreeConfig(%s)\n", key)
pub := ctx.pubContentTreeConfig
pub.Publish(key, config)
log.Tracef("publishContentTreeConfig(%s) done\n", key)
}
func unpublishContentTreeConfig(ctx *getconfigContext, key string) {
log.Tracef("unpublishContentTreeConfig(%s)\n", key)
pub := ctx.pubContentTreeConfig
config, _ := pub.Get(key)
if config == nil {
log.Errorf("unpublishContentTreeConfig(%s) not found\n", key)
return
}
pub.Unpublish(key)
log.Tracef("unpublishContentTreeConfig(%s) done\n", key)
}
// content tree event watch to capture transitions
// and publish to zedCloud
func handleContentTreeStatusCreate(ctxArg interface{}, key string,
statusArg interface{}) {
handleContentTreeStatusImpl(ctxArg, key, statusArg)
}
func handleContentTreeStatusModify(ctxArg interface{}, key string,
statusArg interface{}, oldStatusArg interface{}) {
handleContentTreeStatusImpl(ctxArg, key, statusArg)
}
func handleContentTreeStatusImpl(ctxArg interface{}, key string,
statusArg interface{}) {
status := statusArg.(types.ContentTreeStatus)
ctx := ctxArg.(*zedagentContext)
uuidStr := status.Key()
PublishContentInfoToZedCloud(ctx, uuidStr, &status, ctx.iteration, AllDest)
ctx.iteration++
}
func handleContentTreeStatusDelete(ctxArg interface{}, key string,
statusArg interface{}) {
ctx := ctxArg.(*zedagentContext)
uuidStr := key
PublishContentInfoToZedCloud(ctx, uuidStr, nil, ctx.iteration, AllDest)
ctx.iteration++
}