-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch-packages.go
163 lines (137 loc) · 4.9 KB
/
patch-packages.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
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
/*
MIT License
(C) Copyright 2023 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package patch
import (
"encoding/json"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"log"
"os"
)
// Repo is a list of repositories to add to the client node.
type Repo struct {
ID string `json:"id"`
Name string `json:"name"`
Baseurl string `json:"baseurl"`
Enabled int `json:"enabled"`
Autorefresh int `json:"autorefresh"`
Gpgcheck int `json:"gpgcheck"`
RepoGpgcheck int `json:"repo_gpgcheck"`
}
// Packages is a list of packages to install on the client node.
type Packages []string
// Zypper is a map for Zypper cloud-init data.
type Zypper struct {
Repos []Repo `json:"repos"`
}
// ConfigFileData matches the current “cloud-init“ file in the CSM tarball.
type ConfigFileData struct {
Repos []Repo `json:"repos"`
Packages Packages `json:"packages"`
}
// UserData is the cloud-init structure/representation of the new data.
type UserData struct {
Zypper Zypper `json:"zypper"`
Packages Packages `json:"packages"`
}
// Host is the cloud-init structure/representation of each of our host entries in the cloud-init data.
type Host struct {
UserData UserData `json:"user-data"`
}
// CloudInitHost is the representation of all the new cloud-init data.
type CloudInitHost map[string]Host
// configFile is our input, our config to patch into cloud-init data.
var configFile string
var patchPackages = &cobra.Command{
Use: "packages",
Short: "Patch cloud-init metadata with repositories and packages",
Long: `
Patch cloud-init metadata (in place) with a list of repositories to add, and packages to install, during cloud-init from
CSM's cloud-init.yaml.`,
Run: func(cmd *cobra.Command, args []string) {
userdata, err := loadPackagesConfig(configFile)
if err != nil {
log.Fatalf("Unable to load config data from CSM cloud-init config file, %v \n", err)
}
packageData, err := os.ReadFile(cloudInitSeedFile)
if err != nil {
log.Fatalf("Unable to load cloud-init seed data, %v \n", err)
}
cloudInit := mergePackagesData(userdata, packageData)
update, err := json.Marshal(cloudInit)
if err != nil {
log.Fatalf("Unable to marshal zypper data into JSON, %v \n", err)
}
data, err := backupCloudInitData()
if err != nil {
log.Fatalf("Failed to write backup file, %v \n", err)
}
if err := writeCloudInit(data, update); err != nil {
log.Fatalf("Unable to patch cloud-init seed data in place, %v \n", err)
}
log.Println("Patched cloud-init seed data in place")
},
}
func init() {
PatchCmd.AddCommand(patchPackages)
patchPackages.DisableAutoGenTag = true
patchPackages.Flags().StringVarP(&configFile, "config-file", "", "", "Path to cloud-init.yaml")
patchPackages.MarkFlagRequired("config-file")
}
// loadPackagesConfig reads a configFile, returning its contents as unmarshalled YAML.
func loadPackagesConfig(filePath string) (UserData, error) {
var configData ConfigFileData
var data UserData
config, err := os.ReadFile(filePath)
if err != nil {
return data, err
}
// Handle config files with or without the ``zypper`` root key.
if err := yaml.Unmarshal(config, &data); err != nil {
return data, err
}
if data.Zypper.Repos == nil {
if err := yaml.Unmarshal(config, &configData); err != nil {
return data, err
}
data.Zypper = Zypper{
Repos: configData.Repos,
}
data.Packages = configData.Packages
}
return data, err
}
// mergePackagesData takes assembled userdata and merges it into each user-data entry for each host in a given cloud-init datasource.
func mergePackagesData(userdata UserData, data []byte) CloudInitHost {
cloudInit := make(map[string]Host)
var datas map[string]interface{}
if err := json.Unmarshal(data, &datas); err != nil {
log.Fatalf(string(data[:]))
}
for k := range datas {
if k == "Global" {
continue
}
var host Host
host.UserData = userdata
cloudInit[k] = host
}
return cloudInit
}