Beispiel #1
0
// FIXME: Allow to interupt current push when new push of same image is done.
func (srv *Server) ImagePush(name, endpoint string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
	if err := srv.poolAdd("push", name); err != nil {
		return err
	}
	defer srv.poolRemove("push", name)

	out = utils.NewWriteFlusher(out)
	img, err := srv.runtime.graph.Get(name)
	r, err2 := registry.NewRegistry(srv.runtime.root, authConfig)
	if err2 != nil {
		return err2
	}

	if err != nil {
		out.Write(sf.FormatStatus("The push refers to a repository [%s] (len: %d)", name, len(srv.runtime.repositories.Repositories[name])))
		// If it fails, try to get the repository
		if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
			if err := srv.pushRepository(r, out, name, endpoint, localRepo, sf); err != nil {
				return err
			}
			return nil
		}

		return err
	}
	out.Write(sf.FormatStatus("The push refers to an image: [%s]", name))
	if err := srv.pushImage(r, out, name, img.ID, endpoint, nil, sf); err != nil {
		return err
	}
	return nil
}
Beispiel #2
0
func (srv *Server) ImagePull(name string, tag string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
	r, err := registry.NewRegistry(srv.runtime.root, authConfig)
	if err != nil {
		return err
	}
	if err := srv.poolAdd("pull", name+":"+tag); err != nil {
		return err
	}
	defer srv.poolRemove("pull", name+":"+tag)

	// Resolve the Repository name from fqn to endpoint + name
	var endpoint string
	endpoint, name, err = registry.ResolveRepositoryName(name)
	if err != nil {
		return err
	}

	out = utils.NewWriteFlusher(out)
	err = srv.pullRepository(r, out, name, tag, endpoint, sf)
	if err != nil {
		if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
			return err
		}
		return nil
	}

	return nil
}
Beispiel #3
0
func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
	r, err := registry.NewRegistry(srv.runtime.root, authConfig)
	if err != nil {
		return err
	}
	if err := srv.poolAdd("pull", name+":"+tag); err != nil {
		return err
	}
	defer srv.poolRemove("pull", name+":"+tag)

	remote := name
	parts := strings.Split(name, "/")
	if len(parts) > 2 {
		remote = fmt.Sprintf("src/%s", url.QueryEscape(strings.Join(parts, "/")))
	}
	out = utils.NewWriteFlusher(out)
	err = srv.pullRepository(r, out, name, remote, tag, endpoint, sf)
	if err != nil && endpoint != "" {
		if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
			return err
		}
		return nil
	}

	return nil
}
Beispiel #4
0
func (srv *Server) ImagePull(localName string, tag string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig, metaHeaders map[string][]string, parallel bool) error {
	r, err := registry.NewRegistry(srv.runtime.root, authConfig, srv.HTTPRequestFactory(metaHeaders))
	if err != nil {
		return err
	}
	if err := srv.poolAdd("pull", localName+":"+tag); err != nil {
		return err
	}
	defer srv.poolRemove("pull", localName+":"+tag)

	// Resolve the Repository name from fqn to endpoint + name
	endpoint, remoteName, err := registry.ResolveRepositoryName(localName)
	if err != nil {
		return err
	}

	if endpoint == auth.IndexServerAddress() {
		// If pull "index.docker.io/foo/bar", it's stored locally under "foo/bar"
		localName = remoteName
	}

	out = utils.NewWriteFlusher(out)
	err = srv.pullRepository(r, out, localName, remoteName, tag, endpoint, sf, parallel)
	if err == registry.ErrLoginRequired {
		return err
	}
	if err != nil {
		if err := srv.pullImage(r, out, remoteName, endpoint, nil, sf); err != nil {
			return err
		}
		return nil
	}

	return nil
}
Beispiel #5
0
func TestGetImagesSearch(t *testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	srv := &Server{
		runtime:  runtime,
		registry: registry.NewRegistry(runtime.root),
	}

	r := httptest.NewRecorder()

	req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
	if err != nil {
		t.Fatal(err)
	}

	if err := getImagesSearch(srv, r, req, nil); err != nil {
		t.Fatal(err)
	}

	results := []ApiSearch{}
	if err := json.Unmarshal(r.Body.Bytes(), &results); err != nil {
		t.Fatal(err)
	}
	if len(results) < 2 {
		t.Errorf("Excepted at least 2 lines, %d found", len(results))
	}
}
Beispiel #6
0
func TestPostAuth(t *testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	srv := &Server{
		runtime:  runtime,
		registry: registry.NewRegistry(runtime.root),
	}

	authConfigOrig := &auth.AuthConfig{
		Username: "******",
		Email:    "*****@*****.**",
	}
	srv.registry.ResetClient(authConfigOrig)

	r := httptest.NewRecorder()
	if err := getAuth(srv, r, nil, nil); err != nil {
		t.Fatal(err)
	}

	authConfig := &auth.AuthConfig{}
	if err := json.Unmarshal(r.Body.Bytes(), authConfig); err != nil {
		t.Fatal(err)
	}

	if authConfig.Username != authConfigOrig.Username || authConfig.Email != authConfigOrig.Email {
		t.Errorf("The retrieve auth mismatch with the one set.")
	}
}
Beispiel #7
0
func init() {
	// Hack to run sys init during unit testing
	if utils.SelfPath() == "/sbin/init" {
		SysInit()
		return
	}

	if usr, err := user.Current(); err != nil {
		panic(err)
	} else if usr.Uid != "0" {
		panic("docker tests needs to be run as root")
	}

	NetworkBridgeIface = "testdockbr0"

	// Make it our Store root
	runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
	if err != nil {
		panic(err)
	}

	// Create the "Server"
	srv := &Server{
		runtime:  runtime,
		registry: registry.NewRegistry(runtime.root),
	}
	// Retrieve the Image
	if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout); err != nil {
		panic(err)
	}
}
Beispiel #8
0
func (srv *Server) ImagesSearch(term string) ([]registry.SearchResult, error) {
	r, err := registry.NewRegistry(srv.runtime.config.Root, nil, srv.HTTPRequestFactory(nil))
	if err != nil {
		return nil, err
	}
	results, err := r.SearchRepositories(term)
	if err != nil {
		return nil, err
	}
	return results.Results, nil
}
Beispiel #9
0
func NewServer(autoRestart bool) (*Server, error) {
	if runtime.GOARCH != "amd64" {
		log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
	}
	runtime, err := NewRuntime(autoRestart)
	if err != nil {
		return nil, err
	}
	srv := &Server{
		runtime:  runtime,
		registry: registry.NewRegistry(runtime.root),
	}
	runtime.srv = srv
	return srv, nil
}
Beispiel #10
0
func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, sf *utils.StreamFormatter) error {
	r := registry.NewRegistry(srv.runtime.root)
	out = utils.NewWriteFlusher(out)
	if endpoint != "" {
		if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
			return err
		}
		return nil
	}

	if err := srv.pullRepository(r, out, name, tag, sf); err != nil {
		return err
	}

	return nil
}
Beispiel #11
0
func (srv *Server) ImagesSearch(term string) ([]APISearch, error) {

	results, err := registry.NewRegistry(srv.runtime.root, nil).SearchRepositories(term)
	if err != nil {
		return nil, err
	}

	var outs []APISearch
	for _, repo := range results.Results {
		var out APISearch
		out.Description = repo["description"]
		out.Name = repo["name"]
		outs = append(outs, out)
	}
	return outs, nil
}
Beispiel #12
0
func TestGetAuth(t *testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	srv := &Server{
		runtime:  runtime,
		registry: registry.NewRegistry(runtime.root),
	}

	r := httptest.NewRecorder()

	authConfig := &auth.AuthConfig{
		Username: "******",
		Password: "******",
		Email:    "*****@*****.**",
	}

	authConfigJson, err := json.Marshal(authConfig)
	if err != nil {
		t.Fatal(err)
	}

	req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
	if err != nil {
		t.Fatal(err)
	}

	if err := postAuth(srv, r, req, nil); err != nil {
		t.Fatal(err)
	}

	if r.Code != http.StatusOK && r.Code != 0 {
		t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
	}

	newAuthConfig := srv.registry.GetAuthConfig()
	if newAuthConfig.Username != authConfig.Username ||
		newAuthConfig.Email != authConfig.Email {
		t.Fatalf("The auth configuration hasn't been set correctly")
	}
}
Beispiel #13
0
func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, sf *utils.StreamFormatter) error {
	r := registry.NewRegistry(srv.runtime.root)
	out = utils.NewWriteFlusher(out)
	if endpoint != "" {
		if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
			return err
		}
		return nil
	}
	remote := name
	parts := strings.Split(name, "/")
	if len(parts) > 2 {
		remote = fmt.Sprintf("src/%s", url.QueryEscape(strings.Join(parts, "/")))
	}
	if err := srv.pullRepository(r, out, name, remote, tag, sf); err != nil {
		return err
	}

	return nil
}
Beispiel #14
0
// FIXME: Allow to interrupt current push when new push of same image is done.
func (srv *Server) ImagePush(localName string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig, metaHeaders map[string][]string) error {
	if err := srv.poolAdd("push", localName); err != nil {
		return err
	}
	defer srv.poolRemove("push", localName)

	// Resolve the Repository name from fqn to endpoint + name
	endpoint, remoteName, err := registry.ResolveRepositoryName(localName)
	if err != nil {
		return err
	}

	out = utils.NewWriteFlusher(out)
	img, err := srv.runtime.graph.Get(localName)
	r, err2 := registry.NewRegistry(srv.runtime.root, authConfig, srv.HTTPRequestFactory(metaHeaders))
	if err2 != nil {
		return err2
	}

	if err != nil {
		reposLen := len(srv.runtime.repositories.Repositories[localName])
		out.Write(sf.FormatStatus("", "The push refers to a repository [%s] (len: %d)", localName, reposLen))
		// If it fails, try to get the repository
		if localRepo, exists := srv.runtime.repositories.Repositories[localName]; exists {
			if err := srv.pushRepository(r, out, localName, remoteName, localRepo, endpoint, sf); err != nil {
				return err
			}
			return nil
		}
		return err
	}

	var token []string
	out.Write(sf.FormatStatus("", "The push refers to an image: [%s]", localName))
	if _, err := srv.pushImage(r, out, remoteName, img.ID, endpoint, token, sf); err != nil {
		return err
	}
	return nil
}
Beispiel #15
0
func (srv *Server) ImagePush(name, endpoint string, out io.Writer) error {
	out = utils.NewWriteFlusher(out)
	img, err := srv.runtime.graph.Get(name)
	r := registry.NewRegistry(srv.runtime.root)

	if err != nil {
		fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name]))
		// If it fails, try to get the repository
		if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
			if err := srv.pushRepository(r, out, name, localRepo); err != nil {
				return err
			}
			return nil
		}

		return err
	}
	fmt.Fprintf(out, "The push refers to an image: [%s]\n", name)
	if err := srv.pushImage(r, out, name, img.Id, endpoint, nil); err != nil {
		return err
	}
	return nil
}
	var graph *fake_graph.FakeGraph
	var fetcher RepositoryFetcher

	var server *ghttp.Server
	var endpoint1 *ghttp.Server
	var endpoint2 *ghttp.Server

	BeforeEach(func() {
		graph = fake_graph.New()

		server = ghttp.NewServer()

		endpoint1 = ghttp.NewServer()
		endpoint2 = ghttp.NewServer()

		registry, err := registry.NewRegistry(nil, nil, server.URL()+"/v1/")
		Ω(err).ShouldNot(HaveOccurred())

		fetcher = New(registry, graph)
	})

	setupSuccessfulFetch := func(endpoint *ghttp.Server) {
		endpoint.AppendHandlers(
			ghttp.CombineHandlers(
				ghttp.VerifyRequest("GET", "/v1/images/layer-3/json"),
				http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
					w.Header().Add("X-Docker-Size", "123")
					w.Write([]byte(`{"id":"layer-3","parent":"parent-3"}`))
				}),
			),
			ghttp.CombineHandlers(
Beispiel #17
0
func main() {
	flag.Parse()

	maxProcs := runtime.NumCPU()
	prevMaxProcs := runtime.GOMAXPROCS(maxProcs)

	log.Println("set GOMAXPROCS to", maxProcs, "was", prevMaxProcs)

	if *binPath == "" {
		log.Fatalln("must specify -bin with linux backend")
	}

	if *depotPath == "" {
		log.Fatalln("must specify -depot with linux backend")
	}

	if *rootFSPath == "" {
		log.Fatalln("must specify -rootfs with linux backend")
	}

	uidPool := uid_pool.New(uint32(*uidPoolStart), uint32(*uidPoolSize))

	_, ipNet, err := net.ParseCIDR(*networkPool)
	if err != nil {
		log.Fatalln("error parsing CIDR:", err)
	}

	networkPool := network_pool.New(ipNet)

	// TODO: use /proc/sys/net/ipv4/ip_local_port_range by default (end + 1)
	portPool := port_pool.New(uint32(*portPoolStart), uint32(*portPoolSize))

	runner := linux_command_runner.New(*debug)

	quotaManager, err := quota_manager.New(*depotPath, *binPath, runner)
	if err != nil {
		log.Fatalln("error creating quota manager:", err)
	}

	if *disableQuotas {
		quotaManager.Disable()
	}

	graphDriver, err := graphdriver.New(*graphRoot)
	if err != nil {
		log.Fatalln("error constructing graph driver:", err)
	}

	graph, err := graph.NewGraph(*graphRoot, graphDriver)
	if err != nil {
		log.Fatalln("error constructing graph:", err)
	}

	reg, err := registry.NewRegistry(nil, nil, *dockerRegistry)
	if err != nil {
		log.Fatalln(err)
	}

	pool := container_pool.New(
		*binPath,
		*depotPath,
		*rootFSPath,
		repository_fetcher.Retryable{repository_fetcher.New(reg, graph)},
		graphDriver,
		uidPool,
		networkPool,
		portPool,
		strings.Split(*denyNetworks, ","),
		strings.Split(*allowNetworks, ","),
		runner,
		quotaManager,
	)

	systemInfo := system_info.NewProvider(*depotPath)

	backend := linux_backend.New(pool, systemInfo, *snapshotsPath)

	log.Println("setting up backend")

	err = backend.Setup()
	if err != nil {
		log.Fatalln("failed to set up backend:", err)
	}

	log.Println("starting server; listening with", *listenNetwork, "on", *listenAddr)

	graceTime := *containerGraceTime

	wardenServer := server.New(*listenNetwork, *listenAddr, graceTime, backend)

	err = wardenServer.Start()
	if err != nil {
		log.Fatalln("failed to start:", err)
	}

	signals := make(chan os.Signal, 1)

	go func() {
		<-signals
		log.Println("stopping...")
		wardenServer.Stop()
		os.Exit(0)
	}()

	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

	select {}
}