Example #1
0
File: main.go Project: swasd/dpm
func doRemove(c *cli.Context) {
	home := os.Getenv("HOME")
	packageName := c.Args().First()
	e, err := repo.LoadIndex(filepath.Join(home, ".dpm", "index", "dpm.index"))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	entry := e.FindByName(packageName)
	if entry == nil {
		fmt.Println("Cannot find package in the index")
		os.Exit(1)
	}

	packageFile := filepath.Join(home, "/.dpm/cache/", entry.Filename)
	_, err = os.Stat(packageFile)
	if err != nil {
		// not existed
		doInstall(c)
	}

	p, err := build.LoadPackage(packageFile)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	packageSpec, err := p.Spec()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	provisionFile := filepath.Join(home, "/.dpm/workspace", entry.Hash, packageSpec.Provision)
	provSpec, err := provision.LoadFromFile(provisionFile)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	err = provSpec.RemoveMachines()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
Example #2
0
File: main.go Project: swasd/dpm
func install(c *cli.Context) {
	home := os.Getenv("HOME")
	packageName := c.Args().First()
	entry, err := repo.Get(packageName, "")
	if err != nil {
		fmt.Println("Cannot find package in the index")
		os.Exit(1)
	}

	packageFile := filepath.Join(home, ".dpm", "cache", entry.Filename)
	_, err = os.Stat(packageFile)
	if err != nil {
		// not existed
		doInstall(c)
	}

	p, err := build.LoadPackage(packageFile)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// extract the package
	// it will extract all dependencies in process
	_, err = os.Stat(filepath.Join(home, ".dpm", "workspace", entry.Hash))
	if err != nil {
		err = p.Extract(filepath.Join(home, ".dpm", "workspace", entry.Hash))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}

	hashes, err := p.Order()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("Dependencies resolved...")

	var em provision.ExportedMachine
	for _, hash := range hashes {

		packageSpec, err := build.ReadSpec(hash)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Printf("Installing %s:%s (%s)...\n", packageSpec.Name, packageSpec.Version, hash[0:8])

		provisionFile := filepath.Join(home, ".dpm", "workspace", hash, packageSpec.Provision)
		provSpec, err := provision.LoadFromFile(provisionFile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		times := 0
	loop:
		err = provSpec.Provision()
		if err != nil {
			fmt.Println(err)
			times++
			if times < 10 {
				goto loop
			}
			os.Exit(1)
		}

		err = provSpec.ExportEnvsToFile(filepath.Join(home, ".dpm", "workspace", hash, ".env"))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		em = provSpec.ExportedMachine()

		compose, err := composition.NewProject(em, hash, packageSpec)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		err = compose.Up()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}

	flag := ""
	mode := "engine"
	if em.Mode == provision.Swarm {
		flag = "--swarm "
		mode = "cluster"
	}
	fmt.Printf("\nExported machine is %s.\n", em.Name)
	fmt.Printf("Run \"docker-machine env %s%s\" to see how to connect to your Docker %s.\n", flag, em.Name, mode)

}