/
generator.go
170 lines (149 loc) · 5.93 KB
/
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
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/ystia/yorc/config"
"github.com/ystia/yorc/deployments"
"github.com/ystia/yorc/helper/consulutil"
"github.com/ystia/yorc/helper/sshutil"
"github.com/ystia/yorc/log"
"github.com/ystia/yorc/prov/terraform/commons"
"github.com/ystia/yorc/tosca"
)
const infrastructureName = "google"
type googleGenerator struct {
}
func (g *googleGenerator) GenerateTerraformInfraForNode(ctx context.Context, cfg config.Configuration, deploymentID, nodeName, infrastructurePath string) (bool, map[string]string, []string, commons.PostApplyCallback, error) {
log.Debugf("Generating infrastructure for deployment with id %s", deploymentID)
cClient, err := cfg.GetConsulClient()
if err != nil {
return false, nil, nil, nil, err
}
kv := cClient.KV()
nodeKey := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "nodes", nodeName)
terraformStateKey := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "terraform-state", nodeName)
infrastructure := commons.Infrastructure{}
// Remote Configuration for Terraform State to store it in the Consul KV store
infrastructure.Terraform = commons.GetBackendConfiguration(terraformStateKey, cfg)
// Define Terraform provider environment variables
var cmdEnv []string
configParams := []string{"application_credentials", "credentials", "project", "region"}
for _, configParam := range configParams {
value := cfg.Infrastructures[infrastructureName].GetString(configParam)
if value != "" {
cmdEnv = append(cmdEnv,
fmt.Sprintf("%s=%s",
"GOOGLE_"+strings.ToUpper(configParam),
value))
}
}
// Management of variables for Terraform
infrastructure.Provider = map[string]interface{}{
"google": map[string]interface{}{
"version": cfg.Terraform.GooglePluginVersionConstraint,
},
"consul": commons.GetConsulProviderfiguration(cfg),
"null": map[string]interface{}{
"version": commons.NullPluginVersionConstraint,
},
}
log.Debugf("inspecting node %s", nodeKey)
nodeType, err := deployments.GetNodeType(kv, deploymentID, nodeName)
if err != nil {
return false, nil, nil, nil, err
}
outputs := make(map[string]string)
instances, err := deployments.GetNodeInstancesIds(kv, deploymentID, nodeName)
if err != nil {
return false, nil, nil, nil, err
}
var sshAgent *sshutil.SSHAgent
for instNb, instanceName := range instances {
instanceState, err := deployments.GetInstanceState(kv, deploymentID, nodeName, instanceName)
if err != nil {
return false, nil, nil, nil, err
}
if instanceState == tosca.NodeStateDeleting || instanceState == tosca.NodeStateDeleted {
// Do not generate something for this node instance (will be deleted if exists)
continue
}
switch nodeType {
case "yorc.nodes.google.Compute":
instances, err = deployments.GetNodeInstancesIds(kv, deploymentID, nodeName)
if err != nil {
return false, nil, nil, nil, err
}
err = g.generateComputeInstance(ctx, kv, cfg, deploymentID, nodeName, instanceName, instNb, &infrastructure, outputs, &cmdEnv, sshAgent)
if err != nil {
return false, nil, nil, nil, err
}
case "yorc.nodes.google.Address":
err = g.generateComputeAddress(ctx, kv, cfg, deploymentID, nodeName, instanceName, instNb, &infrastructure, outputs)
if err != nil {
return false, nil, nil, nil, err
}
case "yorc.nodes.google.PersistentDisk":
err = g.generatePersistentDisk(ctx, kv, cfg, deploymentID, nodeName, instanceName, instNb, &infrastructure, outputs)
if err != nil {
return false, nil, nil, nil, err
}
case "yorc.nodes.google.PrivateNetwork":
err = g.generatePrivateNetwork(ctx, kv, cfg, deploymentID, nodeName, &infrastructure, outputs)
if err != nil {
return false, nil, nil, nil, err
}
case "yorc.nodes.google.Subnetwork":
err = g.generateSubNetwork(ctx, kv, cfg, deploymentID, nodeName, &infrastructure, outputs)
if err != nil {
return false, nil, nil, nil, err
}
default:
return false, nil, nil, nil, errors.Errorf("Unsupported node type '%s' for node '%s' in deployment '%s'", nodeType, nodeName, deploymentID)
}
}
// If ssh-agent has been created, it needs to be stopped after the infrastructure creation
// This is done with this callback
var postInstallCb func()
if sshAgent != nil {
postInstallCb = func() {
// Stop the sshAgent if used during provisioning
// Do not return any error if failure occured during this
err := sshAgent.RemoveAllKeys()
if err != nil {
log.Debugf("Warning: failed to remove all SSH agents keys due to error:%+v", err)
}
err = sshAgent.Stop()
if err != nil {
log.Debugf("Warning: failed to stop SSH agent due to error:%+v", err)
}
}
}
jsonInfra, err := json.MarshalIndent(infrastructure, "", " ")
if err != nil {
return false, nil, nil, nil, errors.Wrap(err, "Failed to generate JSON of terraform Infrastructure description")
}
if err = ioutil.WriteFile(filepath.Join(infrastructurePath, "infra.tf.json"), jsonInfra, 0664); err != nil {
return false, nil, nil, nil, errors.Wrapf(err, "Failed to write file %q", filepath.Join(infrastructurePath, "infra.tf.json"))
}
log.Debugf("Infrastructure generated for deployment with id %s", deploymentID)
return true, outputs, cmdEnv, postInstallCb, nil
}