This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
configure.go
233 lines (193 loc) · 5.95 KB
/
configure.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
boomapi "github.com/caos/orbos/internal/operator/boom/api"
"github.com/caos/orbos/internal/start"
"github.com/caos/orbos/internal/operator/orbiter"
"github.com/caos/orbos/internal/operator/orbiter/kinds/orb"
"github.com/caos/orbos/internal/ssh"
"github.com/caos/orbos/internal/stores/github"
"github.com/caos/orbos/internal/api"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/kubernetes"
"github.com/caos/orbos/internal/secret"
"github.com/spf13/cobra"
)
func ConfigCommand(rv RootValues) *cobra.Command {
var (
kubeconfig string
newMasterKey string
newRepoURL string
cmd = &cobra.Command{
Use: "configure",
Short: "Configures and reconfigures an orb",
Long: "Configures and reconfigures an orb",
Aliases: []string{"reconfigure", "config", "reconfig"},
}
)
flags := cmd.Flags()
flags.StringVar(&kubeconfig, "kubeconfig", "", "Needed in boom-only scenarios")
flags.StringVar(&newMasterKey, "masterkey", "", "Reencrypts all secrets")
flags.StringVar(&newRepoURL, "repourl", "", "Configures the repository URL")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx, monitor, orbConfig, gitClient, errFunc := rv()
if errFunc != nil {
return errFunc(cmd)
}
if orbConfig.URL == "" && newRepoURL == "" {
return errors.New("repository url is neighter passed by flag repourl nor written in orbconfig")
}
// TODO: Remove?
if orbConfig.URL != "" && newRepoURL != "" && orbConfig.URL != newRepoURL {
return fmt.Errorf("repository url %s is not reconfigurable", orbConfig.URL)
}
if orbConfig.Masterkey == "" && newMasterKey == "" {
return errors.New("master key is neighter passed by flag masterkey nor written in orbconfig")
}
var changes bool
if newMasterKey != "" {
monitor.Info("Changing masterkey in current orbconfig")
if orbConfig.Masterkey == "" {
secret.Masterkey = newMasterKey
}
orbConfig.Masterkey = newMasterKey
changes = true
}
if newRepoURL != "" {
monitor.Info("Changing repository url in current orbconfig")
orbConfig.URL = newRepoURL
changes = true
}
configureGit := func() error {
return gitClient.Configure(orbConfig.URL, []byte(orbConfig.Repokey))
}
// If the repokey already has read/write permissions, don't generate a new one.
// This ensures git providers other than github keep being supported
if err := configureGit(); err != nil {
monitor.Info("Starting connection with git-repository")
dir := filepath.Dir(orbConfig.Path)
deployKeyPrivLocal, deployKeyPub, err := ssh.Generate()
if err != nil {
panic(errors.New("failed to generate ssh key for deploy key"))
}
g := github.New(monitor).LoginOAuth(ctx, dir)
if g.GetStatus() != nil {
return errors.New("failed github oauth login ")
}
repo, err := g.GetRepositorySSH(orbConfig.URL)
if err != nil {
return errors.New("failed to get github repository")
}
if err := g.EnsureNoDeployKey(repo).GetStatus(); err != nil {
monitor.Error(errors.New("failed to clear deploy keys in repository"))
}
if err := g.CreateDeployKey(repo, deployKeyPub).GetStatus(); err != nil {
return errors.New("failed to create deploy keys in repository")
}
orbConfig.Repokey = deployKeyPrivLocal
if err := configureGit(); err != nil {
return err
}
changes = true
}
if changes {
monitor.Info("Writing local orbconfig")
if err := orbConfig.WriteBackOrbConfig(); err != nil {
monitor.Info("Failed to change local configuration")
return err
}
}
if err := gitClient.Clone(); err != nil {
return err
}
allKubeconfigs := make([]string, 0)
foundOrbiter, err := api.ExistsOrbiterYml(gitClient)
if err != nil {
return err
}
rewriteKey := orbConfig.Masterkey
if newMasterKey != "" {
rewriteKey = newMasterKey
}
if foundOrbiter {
_, _, configure, _, desired, _, _, err := orbiter.Adapt(gitClient, monitor, make(chan struct{}), orb.AdaptFunc(
orbConfig,
gitCommit,
true,
false,
gitClient,
))
if err != nil {
return err
}
if err := configure(*orbConfig); err != nil {
return err
}
monitor.Info("Repopulating orbiter secrets")
if err := secret.Rewrite(
monitor,
gitClient,
rewriteKey,
desired,
api.PushOrbiterDesiredFunc); err != nil {
panic(err)
}
monitor.Info("Reading kubeconfigs from orbiter.yml")
kubeconfigs, err := start.GetKubeconfigs(monitor, gitClient, orbConfig)
if err == nil {
allKubeconfigs = append(allKubeconfigs, kubeconfigs...)
}
} else {
monitor.Info("No orbiter.yml existent, reading kubeconfig from path provided as parameter")
if kubeconfig == "" {
return errors.New("error to change config as no kubeconfig is provided")
}
value, err := ioutil.ReadFile(kubeconfig)
if err != nil {
return err
}
allKubeconfigs = append(allKubeconfigs, string(value))
}
foundBoom, err := api.ExistsBoomYml(gitClient)
if err != nil {
return err
}
if foundBoom {
monitor.Info("Repopulating boom secrets")
tree, err := api.ReadBoomYml(gitClient)
if err != nil {
return err
}
toolset, _, _, err := boomapi.ParseToolset(tree)
if err != nil {
return err
}
tree.Parsed = toolset
if err := secret.Rewrite(
monitor,
gitClient,
rewriteKey,
tree,
api.PushBoomDesiredFunc); err != nil {
return err
}
}
for _, kubeconfig := range allKubeconfigs {
k8sClient := kubernetes.NewK8sClient(monitor, &kubeconfig)
if k8sClient.Available() {
monitor.Info("Ensuring orbconfig in kubernetes cluster")
if err := kubernetes.EnsureConfigArtifacts(monitor, k8sClient, orbConfig); err != nil {
monitor.Error(errors.New("failed to apply configuration resources into k8s-cluster"))
return err
}
monitor.Info("Applied configuration resources")
} else {
monitor.Info("No connection to the k8s-cluster possible")
}
}
return nil
}
return cmd
}