// Generate launches the cluster and generates the terraform state file on disk.
Generate launches the cluster and generates the terraform state file on disk.
// Generate launches the cluster and generates the terraform state file on disk .
func (c *Cluster) Generate(parents asset.Parents) error {
	terraformVariables := &TerraformVariables{}
	adminKubeconfig := &kubeconfig.Admin{}
	parents.Get(terraformVariables, adminKubeconfig)
	tmpDir, err := ioutil.TempDir(os.TempDir(), "openshift-install-")
	if err != nil {
		return errors.Wrap(err, "failed to create temp dir for terraform execution")
	}
	defer os.RemoveAll(tmpDir)
	terraformVariablesFile := terraformVariables.Files()[0]
	if err := ioutil.WriteFile(filepath.Join(tmpDir, terraformVariablesFile.Filename), terraformVariablesFile.Data, 0600); err != nil {
		return errors.Wrap(err, "failed to write terraform.tfvars file")
	}
	platform := terraformVariables.platform
	if err := data.Unpack(tmpDir, platform); err != nil {
		return err
	}
	if err := data.Unpack(filepath.Join(tmpDir, "config.tf"), "config.tf"); err != nil {
		return err
	}
	logrus.Infof("Using Terraform to create cluster...")
	if err := terraform.Init(tmpDir); err != nil {
		return errors.Wrap(err, "failed to initialize terraform")
	}
	stateFile, err := terraform.Apply(tmpDir)
	if err != nil {
		err = errors.Wrap(err, "failed to run terraform")
	}
	data, err2 := ioutil.ReadFile(stateFile)
	if err2 != nil {
		if err == nil {
			err = err2
		} else {
			logrus.Errorf("Failed to read tfstate: %v", err2)
		}
	}
	c.file = &asset.File{
		Filename: stateFileName,
		Data:     data,
	}
	return nil
}
func ( c * Cluster ) Generate ( parents asset . Parents ) error { terraformVariables := & TerraformVariables { } adminKubeconfig := & kubeconfig . Admin { } parents . Get ( terraformVariables , adminKubeconfig ) tmpDir , err := ioutil . TempDir ( os . TempDir ( ) , " " ) if err != nil { return errors . Wrap ( err , " " ) } defer os . RemoveAll ( tmpDir ) terraformVariablesFile := terraformVariables . Files ( ) [ 0 ] if err := ioutil . WriteFile ( filepath . Join ( tmpDir , terraformVariablesFile . Filename ) , terraformVariablesFile . Data , 0600 ) ; err != nil { return errors . Wrap ( err , " " ) } platform := terraformVariables . platform if err := data . Unpack ( tmpDir , platform ) ; err != nil { return err } if err := data . Unpack ( filepath . Join ( tmpDir , " " ) , " " ) ; err != nil { return err } logrus . Infof ( " " ) if err := terraform . Init ( tmpDir ) ; err != nil { return errors . Wrap ( err , " " ) } stateFile , err := terraform . Apply ( tmpDir ) if err != nil { err = errors . Wrap ( err , " " ) } data , err2 := ioutil . ReadFile ( stateFile ) if err2 != nil { if err == nil { err = err2 } else { logrus . Errorf ( " " , err2 ) } } c . file = & asset . File { Filename : stateFileName , Data : data , } return nil }
Generate launches the cluster and generates the terraform state file on disk.
Generate launches the cluster and generates the terraform state file on disk .
// Copy the terraform.tfvars to a temp directory where the terraform will be invoked within. // This runs the terraform in a temp directory, the tfstate file will be returned // to the asset store to persist it on the disk. // TODO(yifan): Use the kubeconfig to verify the cluster is up.