cmd

package
v0.0.0-...-155ec6d Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2018 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateThinImage = &cobra.Command{
	Use:   "thin",
	Short: "Make thin image.",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 2 {
			fmt.Println("Error: invalid arguments.")
			fmt.Println("  [docker image] [cvmfs repository + path prefix]")
			fmt.Println("  Example: library/ubuntu:latest images.cern.ch/layers")
			return
		}
		repoLocation := args[1]

		flag := cmd.Flags().Lookup("registry")
		var registry string = string(flag.Value.String())

		manifest, _ := lib.GetManifest(registry, args[0])
		origin := args[0] + "@" + registry
		thin := lib.MakeThinImage(manifest, repoLocation, origin)
		j, _ := json.MarshalIndent(thin, "", "  ")
		fmt.Println(string(j))
	},
}
View Source
var MakeThin = &cobra.Command{
	Use:   "make-thin creates a thin image out of a regular docker images storing the files inside the provided repository.",
	Short: "Directly creates a thin image from a regular docker image.",
	Run: func(cmd *cobra.Command, args []string) {
		flags := cmd.Flags()
		registry := flags.Lookup("registry").Value.String()
		inputReference := flags.Lookup("input-reference").Value.String()
		outputReference := flags.Lookup("output-reference").Value.String()
		repository := flags.Lookup("repository").Value.String()
		subdirectory := flags.Lookup("subdirectory").Value.String()
		err := lib.PullLayers(registry, inputReference, repository, subdirectory)
		if err != nil {
			log.Fatal(err)
		}
		manifest, err := lib.GetManifest(registry, inputReference)
		if err != nil {
			log.Fatal(err)
		}

		changes := []string{"ENV CVMFS_IMAGE true"}
		configString, err := lib.GetConfig(registry, inputReference)
		if err != nil {
			log.Println("Unable to get the configuration for the image")
		} else {
			// setting the option for the docker image
			var config map[string]interface{}
			json.Unmarshal([]byte(configString), &config)
			configConfigInterface, ok := config["config"]
			if ok {
				configConfig := configConfigInterface.(map[string]interface{})
				envInterface, okEnv := configConfig["Env"]
				if okEnv {
					env := envInterface.([]interface{})
					for e := range env {
						envStr := interface{}(env[e]).(string)
						changes = append(changes,
							fmt.Sprintf("ENV %s", envStr))
					}
				}
				cmdInterface, okCmd := configConfig["Cmd"]

				if okCmd {
					cmd := cmdInterface.([]interface{})
					for c := range cmd {
						cmdStr := interface{}(cmd[c].(string))
						changes = append(changes,
							fmt.Sprintf("CMD %s", cmdStr))
					}
				}
			}
		}

		origin := inputReference + "@" + registry
		thinImage := lib.MakeThinImage(manifest, repository+"/"+strings.TrimSuffix(subdirectory, "/"), origin)
		thinImageJson, err := json.MarshalIndent(thinImage, "", "  ")
		if err != nil {
			log.Fatal(err)
		}

		var imageTarFileStorange bytes.Buffer
		tarFile := tar.NewWriter(&imageTarFileStorange)
		header := &tar.Header{Name: "thin.json",
			Mode: 0600,
			Size: int64(len(thinImageJson)),
		}
		err = tarFile.WriteHeader(header)
		if err != nil {
			log.Fatal("Error in creating the tarfile for the thin image. [WriteHeader]", err)
		}
		_, err = tarFile.Write(thinImageJson)
		if err != nil {
			log.Fatal("Error in creating the tarfile for the thin image. [Write] ", err)
		}
		err = tarFile.Close()
		if err != nil {
			log.Fatal("Error in creating the tarfile for the thin image. [Close] ", err)
		}

		dockerClient, err := client.NewEnvClient()
		if err != nil {
			log.Fatal("Impossible to get a docker client using your env variables: ", err)
		}
		image := types.ImageImportSource{
			Source:     bytes.NewBuffer(imageTarFileStorange.Bytes()),
			SourceName: "-",
		}

		options := types.ImageImportOptions{
			Tag:     "",
			Message: "",
			Changes: changes,
		}
		importResult, err := dockerClient.ImageImport(context.Background(), image, outputReference, options)
		if err != nil {
			log.Fatal("Error in importing the images: ", err)
		} else {
			defer importResult.Close()
		}
		importResultBuffer := new(bytes.Buffer)
		importResultBuffer.ReadFrom(importResult)
	},
}
View Source
var PrintConfig = &cobra.Command{
	Use:   "config",
	Short: "Print image config",
	Run: func(cmd *cobra.Command, args []string) {
		flag := cmd.Flags().Lookup("registry")
		var registry string = string(flag.Value.String())
		config, err := lib.GetConfig(registry, args[0])
		if err != nil {
			log.Fatal("Impossible to retrieve the configuration")
		}
		fmt.Println(config)
	},
}
View Source
var PrintManifest = &cobra.Command{
	Use:   "manifest",
	Short: "Show manifest",
	Run: func(cmd *cobra.Command, args []string) {
		flag := cmd.Flags().Lookup("registry")
		var registry string = string(flag.Value.String())

		manifest, _ := lib.GetManifest(registry, args[0])
		buffer, _ := json.MarshalIndent(manifest, "", " ")
		fmt.Println(string(buffer))
	},
}
View Source
var PullLayers = &cobra.Command{
	Use:   "pull layers",
	Short: "pull the layers",
	Run: func(cmd *cobra.Command, args []string) {
		flag := cmd.Flags().Lookup("registry")
		var registry string = string(flag.Value.String())
		inputReference := args[0]
		lib.PullLayers(registry, inputReference, "docker.cern.ch", "layers/")
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "docker2cvmfs",
	Short: "Human friendly interaction with the docker hub...",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Use `docker2cvmfs help` for a list of supported commands.")
		fmt.Println("Registry: ")
		fmt.Println("    " + cmd.PersistentFlags().Lookup("registry").Value.String())
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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