Example #1
0
File: main.go Project: strootman/DE
func main() {
	if *internalRepo == "" {
		log.Fatal("--internal-repo must be set")
	}
	if *deRepo == "" {
		log.Fatal("--de-repo must be set")
	}
	var err error
	git, err := git.New()
	if err != nil {
		log.Fatal("git couldn't be found in your $PATH")
	}
	home, err = homedir.Dir()
	if err != nil {
		log.Fatal(err)
	}

	//Create the ~/.de-ansible directory
	var deAnsiblePath string
	if *workingDir == "~/.de-ansible" {
		deAnsiblePath = path.Join(home, ".de-ansible")
	} else {
		deAnsiblePath, err = filepath.Abs(*workingDir)
		if err != nil {
			log.Fatal(err)
		}
	}
	if _, err = os.Stat(deAnsiblePath); err != nil {
		if err = os.MkdirAll(deAnsiblePath, 0755); err != nil {
			log.Fatal(err)
		}
	}

	dePath := path.Join(deAnsiblePath, "DE")

	// cd into the ~/.de-ansible directory
	if err = os.Chdir(deAnsiblePath); err != nil {
		log.Fatal(err)
	}

	if internalPath, err = filepath.Abs(internal); err != nil {
		log.Fatal(err)
	}

	if externalPath, err = filepath.Abs(external); err != nil {
		log.Fatal(err)
	}

	if _, err := os.Stat(internalPath); err == nil {
		if err = os.RemoveAll(internalPath); err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
	}

	if _, err := os.Stat(externalPath); err == nil {
		if err = os.RemoveAll(externalPath); err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
	}

	if _, err := os.Stat(dePath); err == nil {
		if err = os.RemoveAll(dePath); err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
	}

	// clone repos
	if err = git.Clone(*internalRepo, internal); err != nil {
		log.Fatal(err)
	}

	if err = git.Clone(*deRepo, "DE"); err != nil {
		log.Fatal(err)
	}

	// Checkout dev branches
	if err = git.CheckoutBranch(internalPath, *internalBranch); err != nil {
		log.Fatal(err)
	}

	if err = git.CheckoutBranch(dePath, *externalBranch); err != nil {
		log.Fatal(err)
	}

	// Pull the branchs
	if err = git.Pull(internalPath); err != nil {
		log.Fatal(err)
	}

	if err = git.Pull(dePath); err != nil {
		log.Fatal(err)
	}

	// Move the ansible subdir into the external directory
	if err = MoveAnsible(dePath, externalPath); err != nil {
		log.Fatal(err)
	}

	if err = CopyGroupVars(internalPath, externalPath); err != nil {
		log.Fatal(err)
	}

	if err = CopyInventories(internalPath, externalPath); err != nil {
		log.Fatal(err)
	}

	if err = CopySecret(internalPath, externalPath); err != nil {
		log.Fatal(err)
	}

	if err = CopyCompose(internalPath, externalPath); err != nil {
		log.Fatal(err)
	}

	reminder := `Don't forget to run:
  cd %s
  export ANSIBLE_ROLES_PATH=%s:%s
  export ANSIBLE_HASH_BEHAVIOUR=merge
  ansible-galaxy install --force -r requirements.yaml
`
	fmt.Printf(reminder, externalPath, path.Join(externalPath, "roles"), path.Join(internalPath, "roles"))
	// if err = LaunchDocker(*imageName, deAnsiblePath); err != nil {
	// 	log.Fatal(err)
	// }

}
Example #2
0
File: main.go Project: strootman/DE
func main() {
	if *gitRepo == "" {
		fmt.Println("--git-repo-internal must be set.")
		os.Exit(-1)
	}

	if *externalRepo == "" {
		fmt.Println("--git-repo-external must be set.")
	}

	if *account == "" {
		fmt.Println("--account must be set.")
		os.Exit(-1)
	}

	if *repo == "" {
		fmt.Println("--repo must be set.")
		os.Exit(-1)
	}

	if *vaultPass == "" {
		fmt.Println("--vault-pass must be set.")
		os.Exit(-1)
	}

	if *secretFile == "" {
		fmt.Println("--secret must be set")
		os.Exit(-1)
	}

	if *inventory == "" {
		fmt.Println("--inventory must be set")
		os.Exit(-1)
	}

	if *user == "" {
		fmt.Println("--user must be set")
		os.Exit(-1)
	}

	if *service == "" {
		fmt.Println("--service must be set")
		os.Exit(-1)
	}

	if *serviceVar == "" {
		fmt.Println("--service-var must be set")
		os.Exit(-1)
	}

	if *serviceInvGroup == "" {
		fmt.Println("--service-inv-group must be set")
		os.Exit(-1)
	}

	if *playbook == "" {
		fmt.Println("--playbook must be set")
		os.Exit(-1)
	}

	git, err := git.New()
	if err != nil {
		log.Fatal(err)
	}

	ansiblePlaybook, err := exec.LookPath("ansible-playbook")
	if err != nil {
		log.Fatal(err)
	}

	ansible, err := exec.LookPath("ansible")
	if err != nil {
		log.Fatal(err)
	}

	if _, err := os.Stat(internalDir); err == nil {
		if err = os.RemoveAll(internalDir); err != nil {
			log.Fatal(err)
		}
	}

	if _, err := os.Stat(externalDir); err == nil {
		if err = os.RemoveAll(externalDir); err != nil {
			log.Fatal(err)
		}
	}

	if _, err := os.Stat("DE"); err == nil {
		if err = os.RemoveAll("DE"); err != nil {
			log.Fatal(err)
		}
	}

	fmt.Printf("Cloning the internal repo %s \n", *gitRepo)
	if err = git.Clone(*gitRepo, internalDir); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Cloning the external repo %s\n", *externalRepo)
	if err = git.Clone(*externalRepo, "DE"); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Checking out the %s branch from the internal repo\n", *gitBranch)
	if err = git.CheckoutBranch(internalDir, *gitBranch); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Pulling the %s branch from the internal repo\n", *gitBranch)
	if err = git.Pull(internalDir); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Checking out the %s branch from the external repo\n", *externalBranch)
	if err = git.CheckoutBranch("DE", *externalBranch); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Pulling the %s branch from the external repo\n", *externalBranch)
	if err = git.Pull("DE"); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Moving DE/ansible to %s", externalDir)
	err = os.Rename("DE/ansible", externalDir)
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	internalGroupVars, err := filepath.Abs(path.Join(internalDir, "group_vars"))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}
	externalGroupVars, err := filepath.Abs(path.Join(externalDir, "group_vars"))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	fmt.Printf("Copying files from %s to %s\n", internalGroupVars, externalGroupVars)
	var copyPaths []string

	visit := func(p string, i os.FileInfo, err error) error {
		if !i.IsDir() {
			fmt.Printf("Found file %s to copy\n", p)
			copyPaths = append(copyPaths, p)
		}
		return err
	}

	err = filepath.Walk(internalGroupVars, visit)
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	if _, err := os.Stat(externalGroupVars); os.IsNotExist(err) {
		fmt.Printf("Creating %s\n", externalGroupVars)
		err = os.MkdirAll(externalGroupVars, 0755)
		if err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
	}

	for _, copyPath := range copyPaths {
		destPath := path.Join(externalGroupVars, path.Base(copyPath))
		fmt.Printf("Copying %s to %s\n", copyPath, destPath)
		contents, err := ioutil.ReadFile(copyPath)
		if err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
		err = ioutil.WriteFile(destPath, contents, 0644)
		if err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
	}

	internalInventories, err := filepath.Abs(path.Join(internalDir, "inventories"))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}
	externalInventories, err := filepath.Abs(path.Join(externalDir, "inventories"))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	fmt.Printf("Copying files from %s to %s\n", internalInventories, externalInventories)
	copyPaths = []string{}

	err = filepath.Walk(internalInventories, visit)
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	for _, copyPath := range copyPaths {
		destPath := path.Join(externalInventories, path.Base(copyPath))
		fmt.Printf("Copying %s to %s\n", copyPath, destPath)
		contents, err := ioutil.ReadFile(copyPath)
		if err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
		err = ioutil.WriteFile(destPath, contents, 0644)
		if err != nil {
			fmt.Print(err)
			os.Exit(-1)
		}
	}

	origPath, err := filepath.Abs(path.Join(internalDir, "sudo_secret.txt"))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}
	destPath, err := filepath.Abs(path.Join(externalDir, "sudo_secret.txt"))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}
	fmt.Printf("Copying %s to %s\n", origPath, destPath)
	contents, err := ioutil.ReadFile(origPath)
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}
	err = ioutil.WriteFile(destPath, contents, 0644)
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	fmt.Printf("cd'ing into %s\n", externalDir)
	err = os.Chdir(externalDir)
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	pullVars := NewExtraVars(false, false, true, false, []string{*serviceVar}).String()
	fmt.Printf("Updating %s/%s:%s with ansible\n", *account, *repo, *tag)
	cmd := exec.Command(
		ansiblePlaybook,
		"-e",
		fmt.Sprintf("@%s", *secretFile),
		fmt.Sprintf("--vault-password-file=%s", *vaultPass),
		"-i",
		*inventory,
		"--sudo",
		"-u",
		*user,
		"--extra-vars",
		pullVars,
		*playbook,
	)
	fmt.Printf("%s %s\n", cmd.Path, strings.Join(cmd.Args, " "))
	output, err := cmd.CombinedOutput()
	fmt.Println(string(output[:]))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	configVars := NewExtraVars(true, true, false, true, []string{*serviceVar}).String()
	fmt.Printf("Configuring %s with ansible\n", *repo)
	cmd = exec.Command(
		ansiblePlaybook,
		"-e",
		fmt.Sprintf("@%s", *secretFile),
		fmt.Sprintf("--vault-password-file=%s", *vaultPass),
		"-i",
		*inventory,
		"--sudo",
		"-u",
		*user,
		"--extra-vars",
		configVars,
		*playbook,
	)
	fmt.Printf("%s %s\n", cmd.Path, strings.Join(cmd.Args, " "))
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}

	// serviceVars := NewExtraVars
	// fmt.Printf("Updating service file for %s with ansible\n", *repo)
	// cmd = exec.Command(
	// 	ansiblePlaybook,
	// 	"-e",
	// 	fmt.Sprintf("@%s", *secretFile),
	// 	fmt.Sprintf("--vault-password-file=%s", *vaultPass),
	// 	"-i",
	// 	*inventory,
	// 	"--sudo",
	// 	"-u",
	// 	*user,
	// 	"--tags",
	// 	*serviceTag,
	// 	*playbook,
	// )
	// fmt.Printf("%s %s\n", cmd.Path, strings.Join(cmd.Args, " "))
	// output, err = cmd.CombinedOutput()
	// fmt.Println(string(output[:]))
	// if err != nil {
	// 	fmt.Print(err)
	// 	os.Exit(-1)
	// }

	fmt.Printf("Restarting %s with ansible\n", *repo)
	cmd = exec.Command(
		ansible,
		*serviceInvGroup,
		"-e",
		fmt.Sprintf("@%s", *secretFile),
		"-e",
		fmt.Sprintf("ansible_ssh_port=%s", *ansibleSSHPort),
		fmt.Sprintf("--vault-password-file=%s", *vaultPass),
		"-i",
		*inventory,
		"--sudo",
		"-u",
		*user,
		"-m", "service",
		"-a", fmt.Sprintf("name=%s state=restarted", *service),
	)
	fmt.Printf("%s %s\n", cmd.Path, strings.Join(cmd.Args, " "))
	output, err = cmd.CombinedOutput()
	fmt.Println(string(output[:]))
	if err != nil {
		fmt.Print(err)
		os.Exit(-1)
	}
}