示例#1
0
文件: main.go 项目: swasd/dpm
func generateIndex(dir string, outdir string) error {
	infos, err := ioutil.ReadDir(dir)
	if err != nil {
		return err
	}
	os.MkdirAll(outdir, 0755)
	if err != nil {
		return err
	}

	entries := make(repo.Entries, 0)
	for _, f := range infos {
		if strings.HasSuffix(f.Name(), ".dpm") {
			p, err := build.LoadPackage(filepath.Join(dir, f.Name()))
			if err != nil {
				return err
			}
			s, err := p.Spec()
			if err != nil {
				return err
			}

			entries = append(entries, &repo.Entry{
				s.Name,
				s.Version,
				f.Name(),
				p.Sha256(),
			})
		}
	}
	return entries.Save(filepath.Join(outdir, "dpm.index"))
}
示例#2
0
文件: main.go 项目: 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)
	}
}
示例#3
0
文件: main.go 项目: swasd/dpm
func doInstall(c *cli.Context) {
	home := os.Getenv("HOME")
	packageName := c.Args().First()
	packageFile := ""
	if _, err := os.Stat(packageName); err == nil {
		fmt.Println("Install from a local package")
		pwd, err := os.Getwd()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		packageFile = filepath.Join(home, "/.dpm/cache/", packageName)
		err = cp(filepath.Join(pwd, packageName), packageFile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		err = generateIndex(filepath.Join(home, "/.dpm/cache/"),
			filepath.Join(home, "/.dpm/index/"))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	} else {
		fmt.Println("Install from a remote repository")
		_, err = repo.Get(packageName, "")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}

	if packageFile != "" {
		p, err := build.LoadPackage(packageFile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		p.Extract(filepath.Join(home, "/.dpm/workspace", p.Sha256()))
	}
}
示例#4
0
文件: main.go 项目: swasd/dpm
func doInfo(c *cli.Context) {
	home := os.Getenv("HOME")
	packageName := c.Args().First()
	e, err := repo.LoadIndex(filepath.Join(home, ".dpm", "index", "dpm.index"))

	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)
	}
	fmt.Println("Package Information:")
	fmt.Printf("  Title:   %s\n", packageSpec.Title)
	fmt.Printf("  Name:    %s\n", packageSpec.Name)
	fmt.Printf("  Version: %s\n", packageSpec.Version)
	fmt.Printf("  SHA256:  %s\n", p.Sha256())
	fmt.Printf("  %s\n", packageSpec.Description)
}
示例#5
0
文件: main.go 项目: 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)

}