github.com/wunderio/silta-cli@v0.0.0-20240508100559-3017e4ab3a20/internal/common/ciChartFunctions.go (about)

     1  package common
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"os/exec"
    10  	"strings"
    11  
    12  	"gopkg.in/yaml.v2"
    13  )
    14  
    15  const ExtendedFolder = "extended-helm-chart"
    16  
    17  func ReadCharts(deploymentsFile string) chartList {
    18  
    19  	ecfile, err := ioutil.ReadFile(deploymentsFile)
    20  	if err != nil {
    21  		log.Fatal(err)
    22  	}
    23  
    24  	var data chartList
    25  	err2 := yaml.Unmarshal(ecfile, &data)
    26  
    27  	if err2 != nil {
    28  		log.Fatal(err2)
    29  	}
    30  	return data
    31  }
    32  
    33  func ReadChartDefinition(chartFile string) chartDefinition {
    34  
    35  	ecfile, err := ioutil.ReadFile(chartFile)
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  
    40  	var data chartDefinition
    41  	err = yaml.Unmarshal(ecfile, &data)
    42  
    43  	if err != nil {
    44  		log.Fatal(err)
    45  	}
    46  
    47  	return data
    48  }
    49  
    50  func AppendExtraCharts(charts *chartList, mainchart *chartDefinition) {
    51  	if len(charts.Charts) < 1 {
    52  		return
    53  	}
    54  
    55  	for _, dependency := range charts.Charts {
    56  		if strings.HasPrefix(dependency.Repository, "https://") {
    57  			command := "helm repo add " + dependency.Name + " " + dependency.Repository
    58  			helmCmd, _ := exec.Command("bash", "-c", command).CombinedOutput()
    59  			log.Print(string(helmCmd[:]))
    60  
    61  		}
    62  	}
    63  	mainchart.Dependencies = append(mainchart.Dependencies, charts.Charts...)
    64  
    65  	const searchStr = "file://.."
    66  	for i, dependency := range mainchart.Dependencies {
    67  		if strings.HasPrefix(dependency.Repository, "file://..") {
    68  			var finalStr = searchStr + "/" + mainchart.Name + "/charts"
    69  			mainchart.Dependencies[i].Repository = strings.Replace(dependency.Repository, searchStr, finalStr, 1)
    70  		}
    71  	}
    72  }
    73  
    74  func WriteChartDefinition(mainchart chartDefinition, ymlfile string) {
    75  
    76  	data, err := yaml.Marshal(&mainchart)
    77  	if err != nil {
    78  		log.Fatal(err)
    79  	}
    80  
    81  	err2 := ioutil.WriteFile(ymlfile, data, 0644)
    82  	if err2 != nil {
    83  		log.Fatal(err)
    84  	}
    85  }
    86  
    87  func DownloadUntarChart(chartName *ChartNameVersion, toExtendedFolder bool) {
    88  
    89  	command := ""
    90  	destinationArg := " "
    91  
    92  	if toExtendedFolder == true {
    93  		destinationArg = " -d " + ExtendedFolder + " "
    94  	}
    95  
    96  	if len(chartName.Version) < 1 {
    97  		command = "helm pull " + chartName.Name + " --untar" + destinationArg
    98  	} else {
    99  		command = "helm pull " + chartName.Name + " --version " + chartName.Version + " --untar" + destinationArg
   100  	}
   101  	helmCmd, err := exec.Command("bash", "-c", command).CombinedOutput()
   102  
   103  	if err != nil {
   104  		log.SetFlags(0) //remove timestamp
   105  		log.Fatal(string(helmCmd[:]))
   106  	}
   107  
   108  }
   109  
   110  func AppendToChartSchemaFile(schemaFile string, chartNames []string) {
   111  	file, err := ioutil.ReadFile(schemaFile)
   112  	log.Println(schemaFile)
   113  	if err != nil {
   114  		log.Println(err)
   115  	}
   116  
   117  	var j interface{}
   118  	err = json.Unmarshal(file, &j)
   119  	m := j.(map[string]interface{}) //mapped schema json
   120  
   121  	var propertiesArray = m["properties"].(map[string]interface{})
   122  	for _, v := range chartNames {
   123  		propertiesArray[v] = map[string]interface{}{"type": "object"}
   124  	}
   125  	m["properties"] = propertiesArray
   126  
   127  	out, _ := json.Marshal(m)
   128  	ioutil.WriteFile(schemaFile, out, 0644)
   129  }
   130  
   131  func GetChartNamesFromDependencies(dependencies []dependency) []string {
   132  	names := []string{}
   133  
   134  	for _, v := range dependencies {
   135  		names = append(names, v.Name)
   136  	}
   137  
   138  	return names
   139  }
   140  
   141  // GetChartName reduces chart name to a simple name
   142  func GetChartName(chartName string) string {
   143  	// Charts can be stored stored locally, can't use remote repository name as chart name
   144  	if chartName == "simple" || strings.HasSuffix(chartName, "/simple") {
   145  		return "simple"
   146  	} else if chartName == "frontend" || strings.HasSuffix(chartName, "/frontend") {
   147  		return "frontend"
   148  	} else if chartName == "drupal" || strings.HasSuffix(chartName, "/drupal") {
   149  		return "drupal"
   150  	}
   151  	return ""
   152  }
   153  
   154  // Creates a configuration file that can be used for helm release
   155  func CreateChartConfigurationFile(chartName string) string {
   156  
   157  	chartConfigOverride := os.Getenv("SILTA_" + strings.ToUpper(GetChartName(chartName)) + "_CONFIG_VALUES")
   158  	if chartConfigOverride == "" {
   159  		return ""
   160  	}
   161  	rawConfig, err := base64.StdEncoding.DecodeString(chartConfigOverride)
   162  	if err != nil {
   163  		log.Fatal("base64 decoding failed for silta configuration overrides file")
   164  	}
   165  	// Write configuration to temporary file
   166  	chartConfigOverrideFile, err := os.CreateTemp("", "silta-config-*")
   167  	if err != nil {
   168  		log.Fatal("failed to create temporary values file")
   169  	}
   170  	if _, err := chartConfigOverrideFile.Write(rawConfig); err != nil {
   171  		log.Fatal("failed to write to temporary values file")
   172  	}
   173  	return chartConfigOverrideFile.Name()
   174  }
   175  
   176  // Uses PrependChartConfigOverrides from "SILTA_" + strings.ToUpper(GetChartName(chartName)) + "_CONFIG_VALUES"
   177  // environment variable and prepends it to configuration
   178  func PrependChartConfigOverrides(chartOverrideFile string, configuration string) string {
   179  
   180  	if chartOverrideFile == "" {
   181  		return configuration
   182  	}
   183  	if configuration == "" {
   184  		return chartOverrideFile
   185  	}
   186  	return chartOverrideFile + "," + configuration
   187  }