This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
as_generator.go
402 lines (367 loc) · 10.7 KB
/
as_generator.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package generator
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"path"
"text/template"
"github.com/Microsoft/kunlun/artifacts"
"github.com/Microsoft/kunlun/artifacts/builtinroles"
"github.com/Microsoft/kunlun/artifacts/deployments"
"github.com/Microsoft/kunlun/common/fileio"
"github.com/Microsoft/kunlun/common/storage"
"github.com/Microsoft/kunlun/common/ui"
patching "github.com/Microsoft/kunlun/patching"
yaml "gopkg.in/yaml.v2"
)
type ASGenerator struct {
stateStore storage.Store
ui *ui.UI
fs fileio.Fs
}
func NewASGenerator(
stateStore storage.Store,
ui *ui.UI,
fs fileio.Fs,
) ASGenerator {
return ASGenerator{
stateStore: stateStore,
ui: ui,
fs: fs,
}
}
type SSHConfig struct {
UserKnownHostsFile string
SSHPrivateKey string
JumpboxUser string
JumpboxHost string
StrictHostKeyChecking string
}
// https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html?highlight=roles
func (a ASGenerator) Generate(hostGroups []deployments.HostGroup, deployments []deployments.Deployment) error {
// generate the ansible config file.
filesToCopy := []string{"ansible.cfg"}
for _, f := range filesToCopy {
builtInRolesFS, err := builtinroles.FSByte(false, fmt.Sprintf("/%s", f))
if err != nil {
return err
}
ansibleDir, err := a.stateStore.GetAnsibleDir()
filePath := path.Join(ansibleDir, f)
a.fs.WriteFile(filePath, builtInRolesFS, 0644)
}
// generate the hosts files.
hostsFileContent, err := a.generateHostsFile(hostGroups)
if err != nil {
return err
}
ansibleInventoriesDir, _ := a.stateStore.GetAnsibleInventoriesDir()
hostsFile := path.Join(ansibleInventoriesDir, "hosts.yml")
a.ui.Printf("writing hosts file to %s\n", hostsFile)
err = a.fs.WriteFile(hostsFile, hostsFileContent, 0644)
if err != nil {
a.ui.Printf("write file failed: %s\n", err.Error())
return err
}
err = a.prepareBuiltInRoles(deployments)
if err != nil {
a.ui.Printf("prepare built in roles failed: %s\n", err.Error())
return err
}
// generate the roles files.
playbookContent := a.generatePlaybookFile(deployments)
ansibleMainFile, err := a.stateStore.GetAnsibleMainFile()
a.ui.Printf("writing playbook file to %s\n", ansibleMainFile)
err = ioutil.WriteFile(ansibleMainFile, playbookContent, 0644)
if err != nil {
a.ui.Printf("write file failed: %s\n", err.Error())
return err
}
// generate the private key.
privateSshKey, err := a.getAdminSSHPrivateKey()
if err != nil {
a.ui.Printf("get admin ssh private key failed: %s\n", err.Error())
return err
}
sshPrivateKeyPath, err := a.stateStore.GetAdminSSHPrivateKeyPath()
if err != nil {
return err
}
err = a.fs.WriteFile(sshPrivateKeyPath, ([]byte)(privateSshKey), 0600)
if err != nil {
return err
}
// generate the deployment script file.
deploymentScriptFilePath, err := a.stateStore.GetDeploymentScriptFile()
deploymentScriptContent, err := a.generateDeploymentScript()
if err != nil {
return err
}
err = a.fs.WriteFile(deploymentScriptFilePath, deploymentScriptContent, 0744)
if err != nil {
a.ui.Printf("write file failed: %s\n", err.Error())
return err
}
return nil
}
func (a ASGenerator) getSSHKnownHostsFilePath() (string, error) {
deploymentFolder, err := a.stateStore.GetDeploymentsDir()
if err != nil {
return "", err
}
sshKnownHostsFilePath := path.Join(deploymentFolder, "known_hosts")
return sshKnownHostsFilePath, nil
}
func (a ASGenerator) getAdminSSHPrivateKey() (string, error) {
varsStore := patching.NewVarsFSStore(
a.fs,
)
varsStoreFilePath, err := a.stateStore.GetMainArtifactVarsStoreFilePath()
if err != nil {
return "", err
}
err = varsStore.UnmarshalFlag(varsStoreFilePath)
if err != nil {
return "", err
}
privateKey := patching.VariableDefinition{
Name: "admin_ssh", // TODO(andy) HARD CODE THIS, think about a better way to get the private key.
}
adminSSH, _, err := varsStore.Get(privateKey)
if err != nil {
return "", err
}
for k, v := range adminSSH.(map[interface{}]interface{}) {
if k.(string) == "private_key" {
return v.(string), nil
}
}
return "", errors.New("no privat key found")
}
// TODO error handling.
func (a ASGenerator) generateHostsFile(hostGroups []deployments.HostGroup) ([]byte, error) {
// ---
// sample_server:
// hosts:
// 172.16.8.4:
// ansible_ssh_user: andy
// ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q andy@65.52.176.243" -i private key'
hostGroupsSlices := yaml.MapSlice{}
err := a.provisionJumpboxParameters(hostGroups)
if err != nil {
return nil, err
}
for _, hostGroup := range hostGroups {
hosts := yaml.MapSlice{}
for _, host := range hostGroup.Hosts {
hostSlice := yaml.MapItem{
Key: host.Alias,
Value: AnsibleHost{
Host: host.Host,
SSHUser: host.User,
SSHCommonArgs: host.SSHCommonArgs,
},
}
hosts = append(hosts, hostSlice)
}
hostGroupSlice := yaml.MapItem{
Key: hostGroup.Name,
Value: yaml.MapSlice{
{
Key: "hosts",
Value: hosts,
},
},
}
hostGroupsSlices = append(hostGroupsSlices, hostGroupSlice)
}
content, _ := yaml.Marshal(hostGroupsSlices)
return content, nil
}
func (a ASGenerator) provisionJumpboxParameters(hostGroups []deployments.HostGroup) error {
// find the ip of the jumpbox
var (
jumpboxUser string
jumpboxHost string
)
for _, hostGroup := range hostGroups {
if hostGroup.GroupType == artifacts.JumpboxHostGroupType {
jumpboxUser = hostGroup.Hosts[0].User
jumpboxHost = hostGroup.Hosts[0].Host
}
}
privateKeyPath, err := a.stateStore.GetAdminSSHPrivateKeyPath()
if err != nil {
return err
}
knownHostsFilePath, err := a.getSSHKnownHostsFilePath()
if err != nil {
return err
}
jumpBoxSSHCommonArgs := "-o UserKnownHostsFile={{.UserKnownHostsFile}} -o StrictHostKeyChecking={{.StrictHostKeyChecking}}"
nonJumpboxSSHCommonArgs := "-o UserKnownHostsFile={{.UserKnownHostsFile}} -o StrictHostKeyChecking={{.StrictHostKeyChecking}} " +
"-o ProxyCommand=\"ssh -o StrictHostKeyChecking={{.StrictHostKeyChecking}} -W %h:%p -q {{.JumpboxUser}}@{{.JumpboxHost}} -i {{.SSHPrivateKey}}\""
sshConfig := SSHConfig{
UserKnownHostsFile: knownHostsFilePath,
SSHPrivateKey: privateKeyPath,
JumpboxUser: jumpboxUser,
JumpboxHost: jumpboxHost,
StrictHostKeyChecking: "{{ansible_host_key_checking}}",
}
jumpBoxSSHCommonArgs, err = a.provisionSSHCommonArgs(jumpBoxSSHCommonArgs, sshConfig)
if err != nil {
return err
}
nonJumpboxSSHCommonArgs, err = a.provisionSSHCommonArgs(nonJumpboxSSHCommonArgs, sshConfig)
if err != nil {
return err
}
for _, hostGroup := range hostGroups {
for index := range hostGroup.Hosts {
if hostGroup.GroupType != artifacts.JumpboxHostGroupType {
hostGroup.Hosts[index].SSHCommonArgs = nonJumpboxSSHCommonArgs
} else {
hostGroup.Hosts[index].SSHCommonArgs = jumpBoxSSHCommonArgs
}
}
}
return nil
}
type AnsibleHost struct {
Host string `yaml:"ansible_host"`
SSHUser string `yaml:"ansible_ssh_user"`
SSHCommonArgs string `yaml:"ansible_ssh_common_args,omitempty"`
}
type role struct {
Role string `yaml:"role"`
Become string `yaml:"become,omitempty"`
BecomeUser string `yaml:"become_user,omitempty"`
}
type depItem struct {
Hosts string `yaml:"hosts"`
VarsFile []string `yaml:"vars_files"`
Roles []role `yaml:"roles"`
}
func (a ASGenerator) provisionSSHCommonArgs(commonArgs string, sshConfig SSHConfig) (string, error) {
var tpl bytes.Buffer
tmpl, err := template.New("ssh_args").Parse(commonArgs)
if err != nil {
return "", err
}
err = tmpl.Execute(&tpl, sshConfig)
if err != nil {
return "", err
}
sshCommonArgs := tpl.String()
return sshCommonArgs, nil
}
// TODO error handling.
func (a ASGenerator) generatePlaybookFile(deployments []deployments.Deployment) []byte {
// ---
// - hosts: sample_server
// vars_files:
// - vars/sample.yml
// roles:
// - role: 'geerlingguy.composer'
// become: true
// become_user: root
// - hosts: sample_server2
// vars_files:
// - vars/sample.yml
// roles:
// - role: 'geerlingguy.php'
// become: true
// become_user: root
depItems := []depItem{}
// write the vars files
for _, dep := range deployments {
// write the files
mainVarsFilePath, _ := a.stateStore.GetMainArtifactVarsFilePath()
varsDir, _ := a.stateStore.GetAnsibleDir()
varsFile := path.Join(varsDir, dep.HostGroupName+".yml")
varsContent, _ := yaml.Marshal(dep.Vars)
a.ui.Printf("writing vars file to %s\n", varsFile)
err := ioutil.WriteFile(varsFile, varsContent, 0644)
if err != nil {
a.ui.Printf("write vars file failed: %s\n", err.Error())
}
depItem := depItem{
Hosts: dep.HostGroupName,
VarsFile: []string{mainVarsFilePath, varsFile},
}
roles := []role{}
for _, r := range dep.Roles {
if r.BecomeUser != "" {
roles = append(roles, role{
Role: r.Name,
Become: "true",
BecomeUser: r.BecomeUser,
})
} else {
roles = append(roles, role{
Role: r.Name,
})
}
}
depItem.Roles = roles
depItems = append(depItems, depItem)
}
content, _ := yaml.Marshal(depItems)
return content
}
func (a ASGenerator) prepareBuiltInRoles(deployments []deployments.Deployment) error {
ansibleDir, err := a.stateStore.GetAnsibleDir()
if err != nil {
return err
}
stack := NewStack()
stack.Push("/built.in")
stack.Push("/roles.galaxy")
fs := builtinroles.FS(false)
for stack.Len() > 0 {
currentItem := stack.Pop()
currentPath := currentItem.(string)
file, err := fs.Open(currentPath)
if err != nil {
return err
}
files, err := file.Readdir(0)
for _, file := range files {
filePath := path.Join(currentPath, file.Name())
if file.IsDir() {
stack.Push(filePath)
} else {
content, err := builtinroles.FSByte(false, filePath)
if err != nil {
return err
}
targetPath := path.Join(ansibleDir, "./"+filePath)
// a.ui.Printf("base dir is %s\n", path.Dir(targetPath))
err = a.fs.MkdirAll(path.Dir(targetPath), 0744)
if err != nil {
return err
}
// a.ui.Printf("writing to %s\n", targetPath)
err = a.fs.WriteFile(targetPath, content, 0644)
if err != nil {
return err
}
}
}
}
return nil
}
func (a ASGenerator) generateDeploymentScript() ([]byte, error) {
// varsDir, _ := a.stateStore.GetAnsibleDir()
sshPrivateKeyPath, err := a.stateStore.GetAdminSSHPrivateKeyPath()
if err != nil {
return nil, err
}
deploymentScript := fmt.Sprintf(`#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export ANSIBLE_CONFIG=$DIR/ansible/ansible.cfg
ansible-playbook -i $DIR/ansible/inventories $DIR/ansible/main.yml -vv --private-key=%s
`, sshPrivateKeyPath)
return []byte(deploymentScript), nil
}