This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
infra_producer.go
90 lines (75 loc) · 2.39 KB
/
infra_producer.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
package apis
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
artifacts "github.com/Microsoft/kunlun/artifacts"
"github.com/Microsoft/kunlun/common/errors"
"github.com/Microsoft/kunlun/common/storage"
"github.com/Microsoft/kunlun/common/ui"
"github.com/Microsoft/kunlun/producers/infra-producer/handler"
"github.com/Microsoft/kunlun/producers/infra-producer/tfhandler/terraform"
"github.com/spf13/afero"
)
type InfraProducer struct {
manager handler.Manager
}
func NewInfraProducer(stateStore storage.Store, handlerType string, debug bool) (InfraProducer, error) {
log.SetFlags(0)
ui := ui.NewUI(os.Stdout, os.Stdin)
fs := afero.NewOsFs()
afs := &afero.Afero{Fs: fs}
if handlerType == handler.TerraformHandlerType {
terraformOutputBuffer := bytes.NewBuffer([]byte{})
terraformDir, _ := stateStore.GetTerraformDir()
dotTerraformDir := filepath.Join(terraformDir, ".terraform")
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir)
var (
terraformCLI terraform.CLI
out io.Writer
)
if debug {
errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer)
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir)
out = os.Stdout
} else {
terraformCLI = bufferingCLI
out = ioutil.Discard
}
terraformExecutor := terraform.NewExecutor(terraformCLI, bufferingCLI, stateStore, afs, debug, out)
inputGenerator := terraform.NewInputGenerator()
templateGenerator := terraform.NewTemplateGenerator()
terraformManager := terraform.NewManager(terraformExecutor, templateGenerator, inputGenerator, terraformOutputBuffer, ui)
return InfraProducer{
manager: terraformManager,
}, nil
} else if handlerType == handler.ARMTemplateHandlerType {
return InfraProducer{}, &errors.NotImplementedError{}
} else {
return InfraProducer{}, &errors.NotSupportedError{}
}
}
func (ip InfraProducer) Setup(manifest artifacts.Manifest, state storage.State) error {
return ip.manager.Setup(manifest, state)
}
func (ip InfraProducer) Apply(state storage.State) error {
_, err := ip.manager.Apply(state)
if err != nil {
return err
}
return nil
}
func (ip InfraProducer) GetOutputs() (string, error) {
outputs, err := ip.manager.GetOutputs()
if err != nil {
return "", err
}
contents, err := handler.ToOutputsOpsFile(outputs)
if err != nil {
return "", err
}
return contents, nil
}