This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
services.go
60 lines (54 loc) · 2.17 KB
/
services.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
package terraform
import (
"fmt"
"strings"
"github.com/Originate/exosphere/src/types"
"github.com/Originate/exosphere/src/types/deploy"
"github.com/pkg/errors"
)
// GenerateServices generates the contents of the main terraform file given application and service configuration
func GenerateServices(deployConfig deploy.Config) (string, error) {
fileData := []string{}
moduleData, err := generateMainServiceModule(deployConfig)
if err != nil {
return "", errors.Wrap(err, "Failed to generate main service Terraform module")
}
fileData = append(fileData, moduleData)
moduleData, err = generateServiceModules(deployConfig)
if err != nil {
return "", errors.Wrap(err, "Failed to generate service Terraform modules")
}
fileData = append(fileData, moduleData)
return strings.Join(fileData, "\n"), nil
}
func generateMainServiceModule(deployConfig deploy.Config) (string, error) {
varsMap := map[string]string{
"appName": deployConfig.AppContext.Config.Name,
"lockTable": deployConfig.GetAwsTerraformLockTable(),
"terraformVersion": TerraformVersion,
}
return RenderTemplates("services.tf", varsMap)
}
func generateServiceModules(deployConfig deploy.Config) (string, error) {
serviceModules := []string{}
for _, serviceRole := range deployConfig.AppContext.Config.GetSortedServiceRoles() {
serviceConfig := deployConfig.AppContext.ServiceContexts[serviceRole].Config
module, err := generateServiceModule(serviceRole, deployConfig, serviceConfig, fmt.Sprintf("%s_service.tf", serviceConfig.Type))
if err != nil {
return "", err
}
serviceModules = append(serviceModules, module)
}
return strings.Join(serviceModules, "\n"), nil
}
func generateServiceModule(serviceRole string, deployConfig deploy.Config, serviceConfig types.ServiceConfig, filename string) (string, error) {
varsMap := map[string]string{
"serviceRole": serviceRole,
"publicPort": serviceConfig.Production.Port,
"cpu": serviceConfig.Remote.CPU,
"memory": serviceConfig.Remote.Memory,
"healthCheck": serviceConfig.Production.HealthCheck,
"terraformCommitHash": TerraformModulesRef,
}
return RenderTemplates(filename, varsMap)
}