core

package
v0.0.0-...-1f4b2c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: MPL-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// `tfm core clone` command
	CloneCmd = &cobra.Command{
		Use:   "clone",
		Short: "Clone VCS repositories containing terraform code.",
		Long:  "clone VCS repositories containing terraform code. These will be iterated upon by tfm to download state files, read them, and push them to workspaces.",
		RunE: func(cmd *cobra.Command, args []string) error {

			vcsType := viper.GetString("vcs_type")

			switch vcsType {
			case "github":
				return main(vcsclients.CreateContext())
			case "gitlab":
				return main(vcsclients.CreateContextGitlab())
			default:
				return fmt.Errorf("unsupported VCS type: %s", vcsType)
			}
		},
		PostRun: func(cmd *cobra.Command, args []string) {
			o.Close()
		},
	}
)
View Source
var CoreCmd = &cobra.Command{
	Use:   "core",
	Short: "Command used to perform terraform open source (core) to TFE/TFC migration commands",
	Long:  "Command used to perform terraform open source (core) to TFE/TFC migration commands",
}

`tfm core` commands

View Source
var CreateWorkspacesCmd = &cobra.Command{
	Use:   "create-workspaces",
	Short: "Create TFE/TFC workspaces for each cloned repo in the clone_repos_path that contains a pulled_terraform.tfstate file.",
	Long:  `Create TFE/TFC workspaces for each cloned repo in the clone_repos_path that contains a pulled_terraform.tfstate file.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		clonePath := viper.GetString("clone_repos_path")

		return CreateWorkspaces(tfclient.GetDestinationClientContexts(), clonePath)
	},
}

tfm core create-workspaces command

View Source
var (

	// `tfm core getstate` command
	GetStateCmd = &cobra.Command{
		Use:   "getstate",
		Short: "Initialize and get state from terraform repos in the clone_repos_path.",
		Long:  "Initialize and get state from terraform repos in the clone_repos_path.",
		RunE: func(cmd *cobra.Command, args []string) error {

			return initializeRepos()
		},
		PostRun: func(cmd *cobra.Command, args []string) {
			o.Close()
		},
	}
)
View Source
var InitReposCmd = &cobra.Command{
	Use:   "init-repos",
	Short: "Scan cloned repositories for Terraform configurations and build metadata",
	Long: `Scans all cloned repositories based on the 'clone_repos_path' from the configuration file,
identifies directories containing Terraform configurations, and builds a metadata file summarizing these findings.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		return initRepos()
	},
}

Define the command

View Source
var LinkVCSCmd = &cobra.Command{
	Use:   "link-vcs",
	Short: "Link repos in the clone_repos_path to their corresponding workspaces in TFE/TFC.",
	Long:  `Iterates over cloned repositories containing .terraform/pulled_terraform.tfstate files, finds the corresponding TFE/TFC workspace, and links it to the VCS repository.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		clonePath := viper.GetString("clone_repos_path")
		if clonePath == "" {
			clonePath = "test"
		}
		return LinkVCS(tfclient.GetDestinationClientContexts(), clonePath)
	},
}
View Source
var RemoveBackendCmd = &cobra.Command{

	Use:   "remove-backend",
	Short: "Create a branch, remove Terraform backend configurations from cloned repos in clone_repos_path, commit the changes, and push to the origin.",
	Long:  `Searches through .tf files in the root of cloned repositories to remove backend configurations and commit them back on a new branch.`,
	RunE: func(cmd *cobra.Command, args []string) error {

		if !autoApprove {
			promptMessage := `
This command will perform the following actions in each cloned repository specified in the 'clone_repos_path':
	1. Create a new branch named 'update-backend-<today's date>'.
	2. Search for and remove the 'backend {}' block within the 'terraform {}' block in all .tf files.
	3. Commit the changes with a message indicating the removal of the backend configuration.
	4. Push the new branch to the origin repository.
			
	Are you sure you want to proceed? Type 'yes' to continue: `
			o.AddPassUserProvided(promptMessage)
			var response string
			_, err := fmt.Scanln(&response)
			if err != nil || response != "yes" {
				fmt.Println("Operation aborted by the user.")
				return nil
			}
		}

		metadata, err := loadMetadataRemoveBackend("terraform_config_metadata.json")
		if err != nil {
			return fmt.Errorf("error loading metadata: %v. Run tfm core init-repos first", err)
		}

		_ = metadata

		clonePath := viper.GetString("clone_repos_path")
		branchName := "update-backend-" + time.Now().Format("20060102")

		reposWithNewBranches, err := createBranchIfNeeded(clonePath, branchName)
		if err != nil {
			return err
		}

		if commentFlag {
			err = commentOutBackendInRepos(clonePath)
			if err != nil {
				return err
			}
		} else {
			err = removeBackendFromRepos(clonePath)
			if err != nil {
				return err
			}
		}

		if len(reposWithNewBranches) > 0 {
			err = commitChangesInRepos(reposWithNewBranches, branchName, "Remove backend configuration")
			err = pushBranches(githubclient.CreateContext(), reposWithNewBranches, branchName, "origin")
			if err != nil {
				fmt.Println("Error committing or pushing branches:", err)
			}

		} else {
			fmt.Println("No new branches were created, skipping commit step.")
		}

		return nil
	},
}
View Source
var UploadStateCmd = &cobra.Command{
	Use:   "upload-state",
	Short: "Upload .terraform/pulled_terraform.tfstate files from repos cloned into the clone_repos_path to TFE/TFC workspaces.",
	Long: `Iterates over directories containing .terraform/pulled_terraform.tfstate files, 
           finds corresponding TFE workspaces, locks the workspace, uploads the state file, 
           and then unlocks the workspace.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		clonePath := viper.GetString("clone_repos_path")

		return uploadStateFiles(tfclient.GetDestinationClientContexts(), clonePath)
	},
}

Functions

func CreateWorkspaces

func CreateWorkspaces(c tfclient.DestinationContexts, clonePath string) error

Main function that creates the workspaces in the destination TFC/TFE organization.

func LinkVCS

func LinkVCS(c tfclient.DestinationContexts, clonePath string) error

Types

type ConfigPathInfo

type ConfigPathInfo struct {
	Path          string        `json:"path"`
	WorkspaceInfo WorkspaceInfo `json:"workspace_info"`
}

type RepoConfig

type RepoConfig struct {
	RepoName    string           `json:"repo_name"`
	ConfigPaths []ConfigPathInfo `json:"config_paths"`
}

type TerraformState

type TerraformState struct {
	Lineage string `json:"lineage"`
}

type WorkspaceInfo

type WorkspaceInfo struct {
	UsesWorkspaces bool     `json:"uses_workspaces"`
	WorkspaceNames []string `json:"workspace_names,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL