-
Notifications
You must be signed in to change notification settings - Fork 2
/
lightweight.go
96 lines (86 loc) · 2.35 KB
/
lightweight.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
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
// Copyright (C) 2015-2020 the Gprovision Authors. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
package appliance
import (
"encoding/json"
"io/ioutil"
fp "path/filepath"
"github.com/purecloudlabs/gprovision/pkg/common/strs"
"github.com/purecloudlabs/gprovision/pkg/log"
)
// Functions in this file are so that apps in the image don't
// need to query smbios data etc etc (thus 'lightweight.go')
// factory restore writes the platform's info to a file
// (pjson, above) once the image has been written.
type PlatFacts struct {
Variant_
Mfg, Prod, SKU, Serial string
//when writing in image, does it make sense to include macs?
}
// Use in factory restore to write the file. Path is the image root. Lcd type is
// untouched if ltype == NoLCD
func (v *Variant) WriteOut(path string, ltype LcdType) {
file := fp.Join(path, strs.ConfDir(), "platform_facts.json")
//make it possible to override lcd existence for prototyping
if v.i.Prototype && ltype != NoLCD {
current := v.i.Lcd
defer func() { v.i.Lcd = current }()
v.i.Lcd = ltype
}
v.write(file)
}
func (v *Variant) json() []byte {
s := PlatFacts{
Serial: v.serial,
Mfg: v.mfg,
Prod: v.prod,
SKU: v.sku,
Variant_: v.i,
}
data, err := json.Marshal(s)
if err != nil {
log.Logf("marshalling platform facts: %s", err)
log.Fatalf("error creating platform_facts.json")
}
return data
}
func (v *Variant) write(file string) {
data := v.json()
err := ioutil.WriteFile(file, data, 0644)
if err != nil {
log.Fatalf("writing %s: %s", file, err)
}
log.Logf("wrote %s", file)
}
// Returns platform info. If appliance.Identify() has been called, uses its
// data. Otherwise loads info from platform_facts.json (see Pjson), written by
// factory restore.
func Read() *Variant {
if identifiedVariant != nil {
return identifiedVariant
}
return read(fp.Join(strs.ConfDir(), "platform_facts.json"))
}
func read(path string) (v *Variant) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Logf("reading %s: %s", path, err)
return
}
var s PlatFacts
err = json.Unmarshal(data, &s)
if err != nil {
log.Logf("unmarshalling %s: %s", path, err)
}
v = new(Variant)
v.i = s.Variant_
v.serial = s.Serial
v.mfg = s.Mfg
v.prod = s.Prod
v.sku = s.SKU
return
}