Skip to content

Commit 1066b65

Browse files
anroszkiewiczglatosinski
authored andcommitted
[#88148] devices: linux-client: Get metadata from file
Signed-off-by: Anna Roszkiewicz <[email protected]>
1 parent d46280f commit 1066b65

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

devices/linux-client/conf/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const DEFAULT_FILESYSTEM_ENABLE = true
3939
const DEFAULT_FILESYSTEM_BASE_DIR = "/"
4040
const DEFAULT_VERIFICATION_SCRIPT_PATH = ""
4141
const DEFAULT_UPDATE_PROGRESS_ENABLE = true
42+
const DEFAULT_METADATA_FILE_PATH = ""
4243

4344
type RDFMConfig struct {
4445
// Path to the device type file
@@ -101,6 +102,9 @@ type RDFMConfig struct {
101102

102103
// Is reporting update progress enabled? Default: true
103104
UpdateProgressEnable bool `json:",omitempty"`
105+
106+
// Path to the file with device metadata. Default: ""
107+
MetadataFilePath string `json:",omitempty"`
104108
}
105109

106110
var rdfmConfigInstance *RDFMConfig
@@ -152,6 +156,7 @@ func LoadConfig(mainConfigFile string, overlayConfigFile string) (*RDFMConfig, *
152156
FileSystemBaseDir: DEFAULT_FILESYSTEM_BASE_DIR,
153157
VerificationScriptPath: DEFAULT_VERIFICATION_SCRIPT_PATH,
154158
UpdateProgressEnable: DEFAULT_UPDATE_PROGRESS_ENABLE,
159+
MetadataFilePath: DEFAULT_METADATA_FILE_PATH,
155160
}
156161

157162
// Load Mender config
@@ -227,3 +232,21 @@ func LoadTagsConfig(path string) ([]string, error) {
227232

228233
return config, nil
229234
}
235+
236+
func LoadMetadataFile(path string) (map[string]interface{}, error) {
237+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
238+
return make(map[string]interface{}), errors.New(err.Error())
239+
}
240+
241+
content, err := ioutil.ReadFile(path)
242+
if err != nil {
243+
return make(map[string]interface{}), errors.New(err.Error())
244+
}
245+
246+
var metadata map[string]interface{}
247+
if err := json.Unmarshal(content, &metadata); err != nil {
248+
return make(map[string]interface{}), errors.New("Error parsing metadata file: " + err.Error())
249+
}
250+
251+
return metadata, nil
252+
}

devices/linux-client/daemon/device.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,11 @@ func (d *Device) authenticateDeviceWithServer() error {
658658

659659
// collectMetadata gathers the RDFM metadata for this device.
660660
func (d *Device) collectMetadata() (map[string]interface{}, error) {
661+
metadata, err := conf.LoadMetadataFile(d.rdfmCtx.RdfmConfig.MetadataFilePath)
662+
if err != nil {
663+
log.Warnln("Error collecting metadata: failed to get base metadata file:", err)
664+
}
665+
661666
// make sure we have a MAC
662667
if d.macAddr == "" {
663668
mac, err := netUtils.GetUniqueId()
@@ -693,7 +698,10 @@ func (d *Device) collectMetadata() (map[string]interface{}, error) {
693698
if len(tags) == 0 {
694699
delete(m, "rdfm.software.tags")
695700
}
696-
return m, nil
701+
for k, v := range m {
702+
metadata[k] = v
703+
}
704+
return metadata, nil
697705
}
698706

699707
func (d *Device) requestUpdateCheck() {

0 commit comments

Comments
 (0)