Example #1
0
func cleanDockerGraph(graphDir string, v versionutil.Version) error {
	logrus.Debugf("Cleaning for version %s", v)
	// Handle migration files
	migratedVersion := versionutil.StaticVersion(1, 10, 0)
	migratedVersion.Tag = "dev"
	if v.LessThan(migratedVersion) {

		if err := removeIfExists(filepath.Join(graphDir, ".migration-v1-images.json")); err != nil {
			return err
		}
		if err := removeIfExists(filepath.Join(graphDir, ".migration-v1-tags")); err != nil {
			return err
		}

		root := filepath.Join(graphDir, "graph")
		migrationPurger := func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			if !info.IsDir() {
				if strings.HasPrefix(filepath.Base(path), ".migrat") {
					logrus.Debugf("Removing migration file %s", path)
					if err := os.Remove(path); err != nil {
						return err
					}
				}
			}
			return nil
		}
		if err := filepath.Walk(root, migrationPurger); err != nil {
			return err
		}

		// Remove all containers
		infos, err := ioutil.ReadDir(filepath.Join(graphDir, "containers"))
		if err != nil {
			if os.IsNotExist(err) {
				return nil
			}
			return err
		}
		drivers, err := getAllGraphDrivers(graphDir)
		if err != nil {
			return err
		}
		for _, info := range infos {
			container := info.Name()
			for _, graphDriver := range drivers {
				if err := removeLayerGraphContent(container, "mount-id", graphDriver, graphDir); err != nil && !os.IsNotExist(err) {
					return err
				}
				if err := removeLayerGraphContent(container, "init-id", graphDriver, graphDir); err != nil && !os.IsNotExist(err) {
					return err
				}
			}
			if err := os.RemoveAll(filepath.Join(graphDir, "containers", container)); err != nil {
				return err
			}
		}
		if err := os.RemoveAll(filepath.Join(graphDir, "image")); err != nil {
			return err
		}
		if err := removeIfExists(filepath.Join(graphDir, "linkgraph.db")); err != nil {
			return err
		}

		// TODO: Remove everything in graph driver directory which is not in graph
	}

	return nil
}
Example #2
0
func (bc *fsBuildCache) InstallVersion(v versionutil.Version, target string) error {
	cached := bc.getCached(v)
	var cachedInit string
	if cached == "" {
		if v.Commit != "" {
			return ErrCannotDownloadCommit
		}
		resp, err := http.Get(v.DownloadURL())
		if err != nil {
			return err
		}

		tf, err := bc.tempFile()
		if err != nil {
			return err
		}

		_, err = io.Copy(tf, resp.Body)
		if err != nil {
			if err := bc.cleanupTempFile(tf); err != nil {
				// Just log
				log.Printf("Error cleaning up temp file %v: %s", tf.Name(), err)
			}
			return err
		}

		cached, err = bc.saveVersion(tf, v)
		if err != nil {
			return err
		}

		// Remove any "-init"
		cachedInit = initFile(cached)
		if _, err := os.Stat(cachedInit); err == nil {
			if err := os.Remove(cachedInit); err != nil {
				return err
			}
		}
	} else {
		cachedInit = initFile(cached)
	}

	if err := CopyFile(cached, target, 0755); err != nil {
		return err
	}

	targetInit := initFile(target)
	if _, err := os.Stat(cachedInit); err == nil {
		// Create target file, check if name starts with docker, replace with dockerinit
		return CopyFile(cachedInit, targetInit, 0755)
	}

	if _, err := os.Stat(targetInit); err == nil {
		// Truncate file, do not remove since operator may only have access
		// to file and not directory. Future calls may rely on overwriting
		// the content of this file.
		vf, err := os.OpenFile(targetInit, os.O_TRUNC|os.O_WRONLY, 0755)
		if err != nil {
			return err
		}
		return vf.Close()
	}

	return nil
}