2014-03-11 13:40:06 -04:00
package server
2013-05-06 05:31:22 -04:00
import (
2013-07-15 12:17:58 -04:00
"encoding/json"
2013-05-06 05:31:22 -04:00
"fmt"
2014-04-01 20:30:02 -04:00
"github.com/dotcloud/docker/api"
2013-10-31 19:57:45 -04:00
"github.com/dotcloud/docker/archive"
2014-03-07 18:22:23 -05:00
"github.com/dotcloud/docker/daemonconfig"
2014-02-11 19:26:54 -05:00
"github.com/dotcloud/docker/dockerversion"
2013-10-31 19:57:45 -04:00
"github.com/dotcloud/docker/engine"
2014-03-07 21:04:38 -05:00
"github.com/dotcloud/docker/graph"
2014-03-07 20:36:47 -05:00
"github.com/dotcloud/docker/image"
2013-12-23 18:33:06 -05:00
"github.com/dotcloud/docker/pkg/graphdb"
2014-03-10 17:22:27 -04:00
"github.com/dotcloud/docker/pkg/signal"
2013-05-14 21:41:39 -04:00
"github.com/dotcloud/docker/registry"
2014-02-11 23:04:39 -05:00
"github.com/dotcloud/docker/runconfig"
2014-03-07 21:42:29 -05:00
"github.com/dotcloud/docker/runtime"
2013-05-14 18:37:35 -04:00
"github.com/dotcloud/docker/utils"
2013-05-06 05:31:22 -04:00
"io"
2013-05-15 14:30:40 -04:00
"io/ioutil"
2013-05-06 05:31:22 -04:00
"log"
"net/http"
"net/url"
"os"
2013-06-28 11:51:58 -04:00
"os/exec"
2014-03-10 17:22:27 -04:00
gosignal "os/signal"
2013-05-15 14:30:40 -04:00
"path"
2013-10-08 12:35:47 -04:00
"path/filepath"
2014-03-07 21:42:29 -05:00
goruntime "runtime"
2013-11-16 19:26:04 -05:00
"strconv"
2013-05-06 05:31:22 -04:00
"strings"
2013-06-17 19:10:00 -04:00
"sync"
2013-10-21 12:04:42 -04:00
"syscall"
2013-10-31 19:57:45 -04:00
"time"
2013-05-06 05:31:22 -04:00
)
2013-10-26 20:19:35 -04:00
// jobInitApi runs the remote api server `srv` as a daemon,
// Only one api server can run at the same time - this is enforced by a pidfile.
2013-11-29 17:13:00 -05:00
// The signals SIGINT, SIGQUIT and SIGTERM are intercepted for cleanup.
2014-02-19 18:27:57 -05:00
func InitServer ( job * engine . Job ) engine . Status {
2013-10-27 03:16:32 -04:00
job . Logf ( "Creating server" )
2014-03-07 18:22:23 -05:00
srv , err := NewServer ( job . Eng , daemonconfig . ConfigFromJob ( job ) )
2013-10-21 12:04:42 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-10-21 12:04:42 -04:00
}
2014-03-07 21:42:29 -05:00
if srv . runtime . Config ( ) . Pidfile != "" {
2013-10-27 02:51:43 -04:00
job . Logf ( "Creating pidfile" )
2014-03-07 21:42:29 -05:00
if err := utils . CreatePidFile ( srv . runtime . Config ( ) . Pidfile ) ; err != nil {
2013-11-20 02:37:03 -05:00
// FIXME: do we need fatal here instead of returning a job error?
2013-10-27 02:51:43 -04:00
log . Fatal ( err )
}
2013-10-25 02:30:34 -04:00
}
2013-10-27 03:16:32 -04:00
job . Logf ( "Setting up signal traps" )
2013-10-25 02:30:34 -04:00
c := make ( chan os . Signal , 1 )
2014-03-10 17:22:27 -04:00
gosignal . Notify ( c , os . Interrupt , syscall . SIGTERM , syscall . SIGQUIT )
2013-10-25 02:30:34 -04:00
go func ( ) {
sig := <- c
2014-04-01 04:18:52 -04:00
log . Printf ( "Received signal '%v', starting shutdown of docker...\n" , sig )
2014-03-07 21:42:29 -05:00
utils . RemovePidFile ( srv . runtime . Config ( ) . Pidfile )
2013-10-26 17:28:53 -04:00
srv . Close ( )
2013-10-25 02:30:34 -04:00
os . Exit ( 0 )
} ( )
2013-10-26 22:24:01 -04:00
job . Eng . Hack_SetGlobalVar ( "httpapi.server" , srv )
2013-11-14 01:08:08 -05:00
job . Eng . Hack_SetGlobalVar ( "httpapi.runtime" , srv . runtime )
2014-01-29 21:34:43 -05:00
2014-01-17 19:57:43 -05:00
for name , handler := range map [ string ] engine . Handler {
"export" : srv . ContainerExport ,
"create" : srv . ContainerCreate ,
"stop" : srv . ContainerStop ,
"restart" : srv . ContainerRestart ,
"start" : srv . ContainerStart ,
"kill" : srv . ContainerKill ,
"wait" : srv . ContainerWait ,
"tag" : srv . ImageTag ,
"resize" : srv . ContainerResize ,
"commit" : srv . ContainerCommit ,
"info" : srv . DockerInfo ,
"container_delete" : srv . ContainerDestroy ,
"image_export" : srv . ImageExport ,
"images" : srv . Images ,
"history" : srv . ImageHistory ,
"viz" : srv . ImagesViz ,
"container_copy" : srv . ContainerCopy ,
"insert" : srv . ImageInsert ,
"attach" : srv . ContainerAttach ,
"search" : srv . ImagesSearch ,
"changes" : srv . ContainerChanges ,
"top" : srv . ContainerTop ,
2014-03-10 17:23:29 -04:00
"version" : srv . DockerVersion ,
2014-01-20 19:09:17 -05:00
"load" : srv . ImageLoad ,
2014-01-23 15:19:52 -05:00
"build" : srv . Build ,
2014-01-22 16:35:35 -05:00
"pull" : srv . ImagePull ,
"import" : srv . ImageImport ,
2014-01-20 17:10:23 -05:00
"image_delete" : srv . ImageDelete ,
2014-01-20 18:15:00 -05:00
"inspect" : srv . JobInspect ,
2014-01-23 14:12:17 -05:00
"events" : srv . Events ,
2014-01-22 20:33:29 -05:00
"push" : srv . ImagePush ,
2014-01-16 17:00:18 -05:00
"containers" : srv . Containers ,
2014-01-24 19:39:41 -05:00
"auth" : srv . Auth ,
2014-01-17 19:57:43 -05:00
} {
if err := job . Eng . Register ( name , handler ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-17 19:57:43 -05:00
}
2014-01-16 17:58:20 -05:00
}
2013-11-20 02:37:03 -05:00
return engine . StatusOK
2013-10-26 20:19:35 -04:00
}
2013-07-23 17:05:13 -04:00
// simpleVersionInfo is a simple implementation of
// the interface VersionInfo, which is used
2013-07-18 14:22:49 -04:00
// to provide version information for some product,
// component, etc. It stores the product name and the version
// in string and returns them on calls to Name() and Version().
2013-07-23 17:05:13 -04:00
type simpleVersionInfo struct {
2013-06-28 17:42:04 -04:00
name string
version string
}
2013-07-23 17:05:13 -04:00
func ( v * simpleVersionInfo ) Name ( ) string {
2013-06-28 17:42:04 -04:00
return v . name
}
2013-07-23 17:05:13 -04:00
func ( v * simpleVersionInfo ) Version ( ) string {
2013-06-28 17:42:04 -04:00
return v . version
}
2013-10-08 15:15:29 -04:00
// ContainerKill send signal to the container
// If no signal is given (sig 0), then Kill with SIGKILL and wait
// for the container to exit.
// If a signal is given, then just send it to the container and return.
2013-11-21 22:41:17 -05:00
func ( srv * Server ) ContainerKill ( job * engine . Job ) engine . Status {
2013-11-16 19:26:04 -05:00
if n := len ( job . Args ) ; n < 1 || n > 2 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER [SIGNAL]" , job . Name )
2013-11-16 19:26:04 -05:00
}
2014-03-10 17:22:27 -04:00
var (
name = job . Args [ 0 ]
sig uint64
err error
)
// If we have a signal, look at it. Otherwise, do nothing
2013-11-16 19:26:04 -05:00
if len ( job . Args ) == 2 && job . Args [ 1 ] != "" {
2014-03-10 22:51:19 -04:00
// Check if we passed the signal as a number:
2014-03-10 17:22:27 -04:00
// The largest legal signal is 31, so let's parse on 5 bits
sig , err = strconv . ParseUint ( job . Args [ 1 ] , 10 , 5 )
if err != nil {
2014-04-01 15:33:46 -04:00
// The signal is not a number, treat it as a string (either like "KILL" or like "SIGKILL")
sig = uint64 ( signal . SignalMap [ strings . TrimPrefix ( job . Args [ 1 ] , "SIG" ) ] )
2014-03-10 17:22:27 -04:00
if sig == 0 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Invalid signal: %s" , job . Args [ 1 ] )
2014-01-21 23:42:36 -05:00
}
2014-03-10 17:22:27 -04:00
2013-11-16 19:26:04 -05:00
}
}
2014-03-10 17:22:27 -04:00
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2014-01-21 23:42:36 -05:00
// If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait())
if sig == 0 || syscall . Signal ( sig ) == syscall . SIGKILL {
2013-10-08 15:15:29 -04:00
if err := container . Kill ( ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot kill container %s: %s" , name , err )
2013-10-08 15:15:29 -04:00
}
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "kill" , container . ID , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-10-08 15:15:29 -04:00
} else {
// Otherwise, just send the requested signal
2014-03-07 21:42:29 -05:00
if err := container . KillSig ( int ( sig ) ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot kill container %s: %s" , name , err )
2013-10-08 15:15:29 -04:00
}
// FIXME: Add event for signals
2013-05-06 05:31:22 -04:00
}
} else {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-05-06 05:31:22 -04:00
}
2013-11-21 22:41:17 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-01-24 19:39:41 -05:00
func ( srv * Server ) Auth ( job * engine . Job ) engine . Status {
2014-02-05 19:49:43 -05:00
var (
err error
2014-03-10 20:16:58 -04:00
authConfig = & registry . AuthConfig { }
2014-02-05 19:49:43 -05:00
)
2014-01-24 19:39:41 -05:00
job . GetenvJson ( "authConfig" , authConfig )
2014-02-05 19:49:43 -05:00
// TODO: this is only done here because auth and registry need to be merged into one pkg
2014-03-10 20:16:58 -04:00
if addr := authConfig . ServerAddress ; addr != "" && addr != registry . IndexServerAddress ( ) {
2014-02-05 19:49:43 -05:00
addr , err = registry . ExpandAndVerifyRegistryUrl ( addr )
if err != nil {
return job . Error ( err )
}
authConfig . ServerAddress = addr
}
2014-03-10 20:16:58 -04:00
status , err := registry . Login ( authConfig , srv . HTTPRequestFactory ( nil ) )
2014-01-24 19:39:41 -05:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-24 19:39:41 -05:00
}
job . Printf ( "%s\n" , status )
return engine . StatusOK
}
2014-01-23 14:12:17 -05:00
func ( srv * Server ) Events ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s FROM" , job . Name )
2014-01-23 14:12:17 -05:00
}
var (
from = job . Args [ 0 ]
since = job . GetenvInt64 ( "since" )
)
sendEvent := func ( event * utils . JSONMessage ) error {
b , err := json . Marshal ( event )
if err != nil {
return fmt . Errorf ( "JSON error" )
}
_ , err = job . Stdout . Write ( b )
if err != nil {
// On error, evict the listener
utils . Errorf ( "%s" , err )
srv . Lock ( )
delete ( srv . listeners , from )
srv . Unlock ( )
return err
}
return nil
}
listener := make ( chan utils . JSONMessage )
srv . Lock ( )
2014-03-24 14:31:05 -04:00
if old , ok := srv . listeners [ from ] ; ok {
delete ( srv . listeners , from )
close ( old )
}
2014-01-23 14:12:17 -05:00
srv . listeners [ from ] = listener
srv . Unlock ( )
job . Stdout . Write ( nil ) // flush
if since != 0 {
// If since, send previous events that happened after the timestamp
for _ , event := range srv . GetEvents ( ) {
if event . Time >= since {
err := sendEvent ( & event )
if err != nil && err . Error ( ) == "JSON error" {
continue
}
if err != nil {
job . Error ( err )
return engine . StatusErr
}
}
}
}
for event := range listener {
err := sendEvent ( & event )
if err != nil && err . Error ( ) == "JSON error" {
continue
}
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 14:12:17 -05:00
}
}
return engine . StatusOK
}
2013-05-06 05:31:22 -04:00
2013-12-07 20:33:37 -05:00
func ( srv * Server ) ContainerExport ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s container_id" , job . Name )
2013-12-07 20:33:37 -05:00
}
name := job . Args [ 0 ]
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
data , err := container . Export ( )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "%s: %s" , name , err )
2013-05-06 05:31:22 -04:00
}
2014-02-14 06:41:46 -05:00
defer data . Close ( )
2013-05-06 05:31:22 -04:00
// Stream the entire contents of the container (basically a volatile snapshot)
2013-12-07 20:33:37 -05:00
if _ , err := io . Copy ( job . Stdout , data ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "%s: %s" , name , err )
2013-05-06 05:31:22 -04:00
}
2013-12-07 20:33:37 -05:00
// FIXME: factor job-specific LogEvent to engine.Job.Run()
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "export" , container . ID , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-12-07 20:33:37 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-05-06 05:31:22 -04:00
}
2013-09-02 12:06:17 -04:00
// ImageExport exports all images with the given tag. All versions
// containing the same tag are exported. The resulting output is an
// uncompressed tar ball.
// name is the set of tags to export.
// out is the writer where the images are written to.
2014-01-07 15:39:15 -05:00
func ( srv * Server ) ImageExport ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER\n" , job . Name )
2014-01-07 15:39:15 -05:00
}
name := job . Args [ 0 ]
2013-09-02 12:06:17 -04:00
// get image json
tempdir , err := ioutil . TempDir ( "" , "docker-export-" )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-02 12:06:17 -04:00
}
2013-11-18 15:34:34 -05:00
defer os . RemoveAll ( tempdir )
2013-09-02 12:06:17 -04:00
utils . Debugf ( "Serializing %s" , name )
2014-03-07 21:42:29 -05:00
rootRepo , err := srv . runtime . Repositories ( ) . Get ( name )
2013-11-20 20:28:19 -05:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-20 20:28:19 -05:00
}
if rootRepo != nil {
for _ , id := range rootRepo {
image , err := srv . ImageInspect ( id )
2013-09-02 12:06:17 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-02 12:06:17 -04:00
}
2013-11-18 14:48:55 -05:00
2013-11-20 20:28:19 -05:00
if err := srv . exportImage ( image , tempdir ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-02 12:06:17 -04:00
}
}
2013-11-20 20:28:19 -05:00
// write repositories
2014-03-07 21:04:38 -05:00
rootRepoMap := map [ string ] graph . Repository { }
2013-11-20 20:28:19 -05:00
rootRepoMap [ name ] = rootRepo
rootRepoJson , _ := json . Marshal ( rootRepoMap )
2013-09-02 12:06:17 -04:00
2013-11-20 20:28:19 -05:00
if err := ioutil . WriteFile ( path . Join ( tempdir , "repositories" ) , rootRepoJson , os . ModeAppend ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-20 20:28:19 -05:00
}
} else {
image , err := srv . ImageInspect ( name )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-20 20:28:19 -05:00
}
if err := srv . exportImage ( image , tempdir ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-20 20:28:19 -05:00
}
2013-11-18 14:48:55 -05:00
}
2013-09-02 12:06:17 -04:00
2013-11-13 18:41:42 -05:00
fs , err := archive . Tar ( tempdir , archive . Uncompressed )
2013-09-02 12:06:17 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-02 12:06:17 -04:00
}
2014-02-14 06:41:46 -05:00
defer fs . Close ( )
2013-11-18 14:48:55 -05:00
2014-01-07 15:39:15 -05:00
if _ , err := io . Copy ( job . Stdout , fs ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-02 12:06:17 -04:00
}
2014-01-07 15:39:15 -05:00
return engine . StatusOK
2013-09-02 12:06:17 -04:00
}
2014-03-07 20:36:47 -05:00
func ( srv * Server ) exportImage ( img * image . Image , tempdir string ) error {
for i := img ; i != nil ; {
2013-11-20 20:28:19 -05:00
// temporary directory
tmpImageDir := path . Join ( tempdir , i . ID )
if err := os . Mkdir ( tmpImageDir , os . ModeDir ) ; err != nil {
2013-12-04 14:55:42 -05:00
if os . IsExist ( err ) {
return nil
}
2013-11-20 20:28:19 -05:00
return err
}
var version = "1.0"
var versionBuf = [ ] byte ( version )
if err := ioutil . WriteFile ( path . Join ( tmpImageDir , "VERSION" ) , versionBuf , os . ModeAppend ) ; err != nil {
return err
}
// serialize json
b , err := json . Marshal ( i )
if err != nil {
return err
}
if err := ioutil . WriteFile ( path . Join ( tmpImageDir , "json" ) , b , os . ModeAppend ) ; err != nil {
return err
}
// serialize filesystem
2013-11-21 13:26:21 -05:00
fs , err := i . TarLayer ( )
2013-11-20 20:28:19 -05:00
if err != nil {
return err
}
2014-02-14 06:41:46 -05:00
defer fs . Close ( )
2013-11-20 20:28:19 -05:00
fsTar , err := os . Create ( path . Join ( tmpImageDir , "layer.tar" ) )
if err != nil {
return err
}
2014-03-19 14:34:12 -04:00
if written , err := io . Copy ( fsTar , fs ) ; err != nil {
return err
} else {
utils . Debugf ( "rendered layer for %s of [%d] size" , i . ID , written )
}
if err = fsTar . Close ( ) ; err != nil {
2013-11-20 20:28:19 -05:00
return err
}
// find parent
if i . Parent != "" {
i , err = srv . ImageInspect ( i . Parent )
if err != nil {
return err
}
} else {
i = nil
}
}
return nil
}
2014-01-23 15:19:52 -05:00
func ( srv * Server ) Build ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 0 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s\n" , job . Name )
2014-01-23 15:19:52 -05:00
}
var (
remoteURL = job . Getenv ( "remote" )
repoName = job . Getenv ( "t" )
suppressOutput = job . GetenvBool ( "q" )
noCache = job . GetenvBool ( "nocache" )
rm = job . GetenvBool ( "rm" )
2014-03-10 20:16:58 -04:00
authConfig = & registry . AuthConfig { }
configFile = & registry . ConfigFile { }
2014-01-23 15:19:52 -05:00
tag string
2014-02-14 06:41:46 -05:00
context io . ReadCloser
2014-01-23 15:19:52 -05:00
)
job . GetenvJson ( "authConfig" , authConfig )
job . GetenvJson ( "configFile" , configFile )
repoName , tag = utils . ParseRepositoryTag ( repoName )
if remoteURL == "" {
2014-02-14 06:41:46 -05:00
context = ioutil . NopCloser ( job . Stdin )
2014-01-23 15:19:52 -05:00
} else if utils . IsGIT ( remoteURL ) {
if ! strings . HasPrefix ( remoteURL , "git://" ) {
remoteURL = "https://" + remoteURL
}
root , err := ioutil . TempDir ( "" , "docker-build-git" )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 15:19:52 -05:00
}
defer os . RemoveAll ( root )
2014-01-06 13:06:05 -05:00
if output , err := exec . Command ( "git" , "clone" , "--recursive" , remoteURL , root ) . CombinedOutput ( ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Error trying to use git: %s (%s)" , err , output )
2014-01-23 15:19:52 -05:00
}
c , err := archive . Tar ( root , archive . Uncompressed )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 15:19:52 -05:00
}
context = c
} else if utils . IsURL ( remoteURL ) {
f , err := utils . Download ( remoteURL )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 15:19:52 -05:00
}
defer f . Body . Close ( )
dockerFile , err := ioutil . ReadAll ( f . Body )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 15:19:52 -05:00
}
2014-02-12 18:10:41 -05:00
c , err := archive . Generate ( "Dockerfile" , string ( dockerFile ) )
2014-01-23 15:19:52 -05:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 15:19:52 -05:00
}
context = c
}
2014-02-14 06:41:46 -05:00
defer context . Close ( )
2014-01-23 15:19:52 -05:00
sf := utils . NewStreamFormatter ( job . GetenvBool ( "json" ) )
b := NewBuildFile ( srv ,
2014-03-11 13:40:06 -04:00
& utils . StdoutFormater {
2014-01-23 15:19:52 -05:00
Writer : job . Stdout ,
StreamFormatter : sf ,
} ,
2014-03-11 13:40:06 -04:00
& utils . StderrFormater {
2014-01-23 15:19:52 -05:00
Writer : job . Stdout ,
StreamFormatter : sf ,
} ,
! suppressOutput , ! noCache , rm , job . Stdout , sf , authConfig , configFile )
id , err := b . Build ( context )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-23 15:19:52 -05:00
}
if repoName != "" {
2014-03-07 21:42:29 -05:00
srv . runtime . Repositories ( ) . Set ( repoName , tag , id , false )
2014-01-23 15:19:52 -05:00
}
return engine . StatusOK
}
2013-09-02 12:06:17 -04:00
// Loads a set of images into the repository. This is the complementary of ImageExport.
// The input stream is an uncompressed tar ball containing images and metadata.
2014-01-20 19:09:17 -05:00
func ( srv * Server ) ImageLoad ( job * engine . Job ) engine . Status {
2013-11-18 14:48:55 -05:00
tmpImageDir , err := ioutil . TempDir ( "" , "docker-import-" )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2013-10-12 01:11:49 -04:00
defer os . RemoveAll ( tmpImageDir )
2013-11-18 14:48:55 -05:00
var (
repoTarFile = path . Join ( tmpImageDir , "repo.tar" )
repoDir = path . Join ( tmpImageDir , "repo" )
)
tarFile , err := os . Create ( repoTarFile )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2014-01-20 19:09:17 -05:00
if _ , err := io . Copy ( tarFile , job . Stdin ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2013-09-02 12:06:17 -04:00
tarFile . Close ( )
2013-11-18 14:48:55 -05:00
repoFile , err := os . Open ( repoTarFile )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
if err := os . Mkdir ( repoDir , os . ModeDir ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2013-11-20 14:07:42 -05:00
if err := archive . Untar ( repoFile , repoDir , nil ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2013-11-20 20:28:19 -05:00
dirs , err := ioutil . ReadDir ( repoDir )
2013-11-18 14:48:55 -05:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2013-09-02 12:06:17 -04:00
2013-11-20 20:28:19 -05:00
for _ , d := range dirs {
if d . IsDir ( ) {
if err := srv . recursiveLoad ( d . Name ( ) , tmpImageDir ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-18 14:48:55 -05:00
}
2013-11-20 20:28:19 -05:00
}
}
repositoriesJson , err := ioutil . ReadFile ( path . Join ( tmpImageDir , "repo" , "repositories" ) )
if err == nil {
2014-03-07 21:04:38 -05:00
repositories := map [ string ] graph . Repository { }
2013-11-20 20:28:19 -05:00
if err := json . Unmarshal ( repositoriesJson , & repositories ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-20 20:28:19 -05:00
}
for imageName , tagMap := range repositories {
for tag , address := range tagMap {
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Repositories ( ) . Set ( imageName , tag , address , true ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-20 20:28:19 -05:00
}
2013-09-02 12:06:17 -04:00
}
}
2013-11-20 20:28:19 -05:00
} else if ! os . IsNotExist ( err ) {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-02 12:06:17 -04:00
}
2013-11-20 20:28:19 -05:00
2014-01-20 19:09:17 -05:00
return engine . StatusOK
2013-09-02 12:06:17 -04:00
}
func ( srv * Server ) recursiveLoad ( address , tmpImageDir string ) error {
2013-11-18 14:48:55 -05:00
if _ , err := srv . ImageInspect ( address ) ; err != nil {
2013-09-02 12:06:17 -04:00
utils . Debugf ( "Loading %s" , address )
2013-11-18 14:48:55 -05:00
2013-09-02 12:06:17 -04:00
imageJson , err := ioutil . ReadFile ( path . Join ( tmpImageDir , "repo" , address , "json" ) )
if err != nil {
utils . Debugf ( "Error reading json" , err )
2013-11-29 19:53:20 -05:00
return err
2013-09-02 12:06:17 -04:00
}
2013-11-18 14:48:55 -05:00
2013-09-02 12:06:17 -04:00
layer , err := os . Open ( path . Join ( tmpImageDir , "repo" , address , "layer.tar" ) )
if err != nil {
utils . Debugf ( "Error reading embedded tar" , err )
return err
}
2014-03-07 20:36:47 -05:00
img , err := image . NewImgJSON ( imageJson )
2013-09-02 12:06:17 -04:00
if err != nil {
utils . Debugf ( "Error unmarshalling json" , err )
return err
}
if img . Parent != "" {
2014-03-07 21:42:29 -05:00
if ! srv . runtime . Graph ( ) . Exists ( img . Parent ) {
2013-11-18 14:48:55 -05:00
if err := srv . recursiveLoad ( img . Parent , tmpImageDir ) ; err != nil {
return err
}
2013-09-02 12:06:17 -04:00
}
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Graph ( ) . Register ( imageJson , layer , img ) ; err != nil {
2013-11-18 14:48:55 -05:00
return err
2013-09-02 12:06:17 -04:00
}
}
utils . Debugf ( "Completed processing %s" , address )
2013-11-20 20:28:19 -05:00
2013-09-02 12:06:17 -04:00
return nil
}
2014-01-15 17:43:12 -05:00
func ( srv * Server ) ImagesSearch ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s TERM" , job . Name )
2014-01-15 17:43:12 -05:00
}
2014-01-20 23:11:40 -05:00
var (
term = job . Args [ 0 ]
metaHeaders = map [ string ] [ ] string { }
2014-03-10 20:16:58 -04:00
authConfig = & registry . AuthConfig { }
2014-01-20 23:11:40 -05:00
)
job . GetenvJson ( "authConfig" , authConfig )
job . GetenvJson ( "metaHeaders" , metaHeaders )
2014-01-15 17:43:12 -05:00
2014-03-10 20:16:58 -04:00
r , err := registry . NewRegistry ( authConfig , srv . HTTPRequestFactory ( metaHeaders ) , registry . IndexServerAddress ( ) )
2013-06-17 14:13:40 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-06-17 14:13:40 -04:00
}
results , err := r . SearchRepositories ( term )
2013-05-07 14:37:35 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 14:59:04 -04:00
}
2014-01-15 17:43:12 -05:00
outs := engine . NewTable ( "star_count" , 0 )
for _ , result := range results . Results {
out := & engine . Env { }
out . Import ( result )
outs . Add ( out )
}
outs . ReverseSort ( )
2014-01-21 18:06:23 -05:00
if _ , err := outs . WriteListTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-15 17:43:12 -05:00
}
return engine . StatusOK
2013-05-07 14:37:35 -04:00
}
2014-01-09 13:06:16 -05:00
func ( srv * Server ) ImageInsert ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 3 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s IMAGE URL PATH\n" , job . Name )
2014-01-09 13:06:16 -05:00
}
var (
name = job . Args [ 0 ]
url = job . Args [ 1 ]
path = job . Args [ 2 ]
)
sf := utils . NewStreamFormatter ( job . GetenvBool ( "json" ) )
out := utils . NewWriteFlusher ( job . Stdout )
2014-03-07 21:42:29 -05:00
img , err := srv . runtime . Repositories ( ) . LookupImage ( name )
2013-05-07 13:23:50 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 13:23:50 -04:00
}
2013-12-04 14:54:11 -05:00
file , err := utils . Download ( url )
2013-05-07 13:23:50 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 13:23:50 -04:00
}
defer file . Body . Close ( )
2014-03-07 21:42:29 -05:00
config , _ , _ , err := runconfig . Parse ( [ ] string { img . ID , "echo" , "insert" , url , path } , srv . runtime . SystemConfig ( ) )
2013-05-07 13:23:50 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 13:23:50 -04:00
}
2013-10-28 19:58:59 -04:00
c , _ , err := srv . runtime . Create ( config , "" )
2013-05-07 13:23:50 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 13:23:50 -04:00
}
2014-01-15 18:41:57 -05:00
if err := c . Inject ( utils . ProgressReader ( file . Body , int ( file . ContentLength ) , out , sf , false , utils . TruncateID ( img . ID ) , "Downloading" ) , path ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 13:23:50 -04:00
}
// FIXME: Handle custom repo, tag comment, author
2013-09-06 20:33:05 -04:00
img , err = srv . runtime . Commit ( c , "" , "" , img . Comment , img . Author , nil )
2013-05-07 13:23:50 -04:00
if err != nil {
2014-01-09 13:06:16 -05:00
out . Write ( sf . FormatError ( err ) )
2014-01-10 17:54:54 -05:00
return engine . StatusErr
2013-05-07 13:23:50 -04:00
}
2014-01-09 13:06:16 -05:00
out . Write ( sf . FormatStatus ( "" , img . ID ) )
return engine . StatusOK
2013-05-07 13:23:50 -04:00
}
2014-01-15 16:52:35 -05:00
func ( srv * Server ) ImagesViz ( job * engine . Job ) engine . Status {
2014-03-07 21:42:29 -05:00
images , _ := srv . runtime . Graph ( ) . Map ( )
2013-10-08 09:52:36 -04:00
if images == nil {
2014-01-15 16:52:35 -05:00
return engine . StatusOK
2013-10-08 09:52:36 -04:00
}
2014-01-15 16:52:35 -05:00
job . Stdout . Write ( [ ] byte ( "digraph docker {\n" ) )
2013-10-08 09:52:36 -04:00
var (
2014-03-07 20:36:47 -05:00
parentImage * image . Image
2013-10-08 09:52:36 -04:00
err error
)
for _ , image := range images {
parentImage , err = image . GetParent ( )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Error while getting parent image: %v" , err )
2013-10-08 09:52:36 -04:00
}
if parentImage != nil {
2014-01-15 16:52:35 -05:00
job . Stdout . Write ( [ ] byte ( " \"" + parentImage . ID + "\" -> \"" + image . ID + "\"\n" ) )
2013-10-08 09:52:36 -04:00
} else {
2014-01-15 16:52:35 -05:00
job . Stdout . Write ( [ ] byte ( " base -> \"" + image . ID + "\" [style=invis]\n" ) )
2013-10-08 09:52:36 -04:00
}
}
reporefs := make ( map [ string ] [ ] string )
2014-03-07 21:42:29 -05:00
for name , repository := range srv . runtime . Repositories ( ) . Repositories {
2013-10-08 09:52:36 -04:00
for tag , id := range repository {
reporefs [ utils . TruncateID ( id ) ] = append ( reporefs [ utils . TruncateID ( id ) ] , fmt . Sprintf ( "%s:%s" , name , tag ) )
}
}
for id , repos := range reporefs {
2014-01-15 16:52:35 -05:00
job . Stdout . Write ( [ ] byte ( " \"" + id + "\" [label=\"" + id + "\\n" + strings . Join ( repos , "\\n" ) + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n" ) )
2013-10-08 09:52:36 -04:00
}
2014-01-15 16:52:35 -05:00
job . Stdout . Write ( [ ] byte ( " base [style=invisible]\n}\n" ) )
return engine . StatusOK
2013-10-08 09:52:36 -04:00
}
2013-12-12 17:39:35 -05:00
func ( srv * Server ) Images ( job * engine . Job ) engine . Status {
2013-05-19 13:46:24 -04:00
var (
2014-03-07 20:36:47 -05:00
allImages map [ string ] * image . Image
2013-05-19 13:46:24 -04:00
err error
)
2013-12-12 17:39:35 -05:00
if job . GetenvBool ( "all" ) {
2014-03-07 21:42:29 -05:00
allImages , err = srv . runtime . Graph ( ) . Map ( )
2013-05-06 05:31:22 -04:00
} else {
2014-03-07 21:42:29 -05:00
allImages , err = srv . runtime . Graph ( ) . Heads ( )
2013-05-06 05:31:22 -04:00
}
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
2013-12-12 17:39:35 -05:00
lookup := make ( map [ string ] * engine . Env )
2014-03-07 21:42:29 -05:00
for name , repository := range srv . runtime . Repositories ( ) . Repositories {
2013-12-12 17:39:35 -05:00
if job . Getenv ( "filter" ) != "" {
if match , _ := path . Match ( job . Getenv ( "filter" ) , name ) ; ! match {
2013-09-06 15:51:49 -04:00
continue
}
2013-05-06 05:31:22 -04:00
}
for tag , id := range repository {
2014-03-07 21:42:29 -05:00
image , err := srv . runtime . Graph ( ) . Get ( id )
2013-05-06 05:31:22 -04:00
if err != nil {
log . Printf ( "Warning: couldn't load %s from %s/%s: %s" , id , name , tag , err )
continue
}
2013-10-06 01:44:04 -04:00
if out , exists := lookup [ id ] ; exists {
2014-01-13 17:55:31 -05:00
out . SetList ( "RepoTags" , append ( out . GetList ( "RepoTags" ) , fmt . Sprintf ( "%s:%s" , name , tag ) ) )
2013-10-06 01:44:04 -04:00
} else {
2013-12-12 17:39:35 -05:00
out := & engine . Env { }
2013-10-06 01:44:04 -04:00
delete ( allImages , id )
2014-01-13 17:55:31 -05:00
out . Set ( "ParentId" , image . Parent )
out . SetList ( "RepoTags" , [ ] string { fmt . Sprintf ( "%s:%s" , name , tag ) } )
2014-01-29 15:31:49 -05:00
out . Set ( "Id" , image . ID )
2013-12-12 17:39:35 -05:00
out . SetInt64 ( "Created" , image . Created . Unix ( ) )
out . SetInt64 ( "Size" , image . Size )
2014-03-07 20:36:47 -05:00
out . SetInt64 ( "VirtualSize" , image . GetParentsSize ( 0 ) + image . Size )
2013-10-06 01:44:04 -04:00
lookup [ id ] = out
}
2013-05-06 05:31:22 -04:00
}
}
2013-10-06 01:44:04 -04:00
2013-12-12 17:39:35 -05:00
outs := engine . NewTable ( "Created" , len ( lookup ) )
2013-10-06 01:44:04 -04:00
for _ , value := range lookup {
2013-12-12 17:39:35 -05:00
outs . Add ( value )
2013-10-06 01:44:04 -04:00
}
// Display images which aren't part of a repository/tag
2013-12-12 17:39:35 -05:00
if job . Getenv ( "filter" ) == "" {
2013-05-13 06:18:55 -04:00
for _ , image := range allImages {
2013-12-12 17:39:35 -05:00
out := & engine . Env { }
2014-01-13 17:55:31 -05:00
out . Set ( "ParentId" , image . Parent )
out . SetList ( "RepoTags" , [ ] string { "<none>:<none>" } )
2014-01-29 15:31:49 -05:00
out . Set ( "Id" , image . ID )
2013-12-12 17:39:35 -05:00
out . SetInt64 ( "Created" , image . Created . Unix ( ) )
out . SetInt64 ( "Size" , image . Size )
2014-03-07 20:36:47 -05:00
out . SetInt64 ( "VirtualSize" , image . GetParentsSize ( 0 ) + image . Size )
2013-12-12 17:39:35 -05:00
outs . Add ( out )
2013-05-06 05:31:22 -04:00
}
}
2013-08-03 18:33:51 -04:00
2013-12-13 13:26:00 -05:00
outs . ReverseSort ( )
2014-01-21 18:06:23 -05:00
if _ , err := outs . WriteListTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-12 17:39:35 -05:00
}
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-12-11 13:35:21 -05:00
func ( srv * Server ) DockerInfo ( job * engine . Job ) engine . Status {
2014-03-07 21:42:29 -05:00
images , _ := srv . runtime . Graph ( ) . Map ( )
2013-12-12 16:35:50 -05:00
var imgcount int
2013-05-06 05:31:22 -04:00
if images == nil {
imgcount = 0
} else {
2013-12-12 16:35:50 -05:00
imgcount = len ( images )
2013-05-06 05:31:22 -04:00
}
2013-07-24 09:35:38 -04:00
kernelVersion := "<unknown>"
if kv , err := utils . GetKernelVersion ( ) ; err == nil {
kernelVersion = kv . String ( )
}
2013-07-19 12:36:23 -04:00
2013-12-05 04:14:15 -05:00
// if we still have the original dockerinit binary from before we copied it locally, let's return the path to that, since that's more intuitive (the copied path is trivial to derive by hand given VERSION)
initPath := utils . DockerInitPath ( "" )
if initPath == "" {
// if that fails, we'll just return the path from the runtime
2014-03-07 21:42:29 -05:00
initPath = srv . runtime . SystemInitPath ( )
2013-12-05 04:14:15 -05:00
}
2013-12-11 13:35:21 -05:00
v := & engine . Env { }
2013-12-12 16:35:50 -05:00
v . SetInt ( "Containers" , len ( srv . runtime . List ( ) ) )
2013-12-11 13:35:21 -05:00
v . SetInt ( "Images" , imgcount )
2014-03-07 21:42:29 -05:00
v . Set ( "Driver" , srv . runtime . GraphDriver ( ) . String ( ) )
v . SetJson ( "DriverStatus" , srv . runtime . GraphDriver ( ) . Status ( ) )
v . SetBool ( "MemoryLimit" , srv . runtime . SystemConfig ( ) . MemoryLimit )
v . SetBool ( "SwapLimit" , srv . runtime . SystemConfig ( ) . SwapLimit )
v . SetBool ( "IPv4Forwarding" , ! srv . runtime . SystemConfig ( ) . IPv4ForwardingDisabled )
2013-12-11 13:35:21 -05:00
v . SetBool ( "Debug" , os . Getenv ( "DEBUG" ) != "" )
2013-12-12 16:35:50 -05:00
v . SetInt ( "NFd" , utils . GetTotalUsedFds ( ) )
2014-03-07 21:42:29 -05:00
v . SetInt ( "NGoroutines" , goruntime . NumGoroutine ( ) )
v . Set ( "ExecutionDriver" , srv . runtime . ExecutionDriver ( ) . Name ( ) )
2014-02-28 14:06:36 -05:00
v . SetInt ( "NEventsListener" , len ( srv . listeners ) )
2013-12-11 13:35:21 -05:00
v . Set ( "KernelVersion" , kernelVersion )
2014-03-10 20:16:58 -04:00
v . Set ( "IndexServerAddress" , registry . IndexServerAddress ( ) )
2014-02-11 19:26:54 -05:00
v . Set ( "InitSha1" , dockerversion . INITSHA1 )
2013-12-05 04:14:15 -05:00
v . Set ( "InitPath" , initPath )
2013-12-11 13:35:21 -05:00
if _ , err := v . WriteTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-31 18:53:57 -04:00
}
2013-12-11 13:35:21 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-03-10 17:23:29 -04:00
func ( srv * Server ) DockerVersion ( job * engine . Job ) engine . Status {
v := & engine . Env { }
v . Set ( "Version" , dockerversion . VERSION )
2014-04-01 21:19:26 -04:00
v . SetJson ( "ApiVersion" , api . APIVERSION )
2014-03-10 17:23:29 -04:00
v . Set ( "GitCommit" , dockerversion . GITCOMMIT )
v . Set ( "GoVersion" , goruntime . Version ( ) )
v . Set ( "Os" , goruntime . GOOS )
v . Set ( "Arch" , goruntime . GOARCH )
if kernelVersion , err := utils . GetKernelVersion ( ) ; err == nil {
v . Set ( "KernelVersion" , kernelVersion . String ( ) )
}
if _ , err := v . WriteTo ( job . Stdout ) ; err != nil {
return job . Error ( err )
}
return engine . StatusOK
}
2014-01-13 19:50:10 -05:00
func ( srv * Server ) ImageHistory ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s IMAGE" , job . Name )
2014-01-13 19:50:10 -05:00
}
name := job . Args [ 0 ]
2014-03-07 21:42:29 -05:00
foundImage , err := srv . runtime . Repositories ( ) . LookupImage ( name )
2013-05-06 05:31:22 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
2013-06-18 13:31:07 -04:00
lookupMap := make ( map [ string ] [ ] string )
2014-03-07 21:42:29 -05:00
for name , repository := range srv . runtime . Repositories ( ) . Repositories {
2013-06-17 21:39:30 -04:00
for tag , id := range repository {
// If the ID already has a reverse lookup, do not update it unless for "latest"
2013-06-18 13:31:07 -04:00
if _ , exists := lookupMap [ id ] ; ! exists {
lookupMap [ id ] = [ ] string { }
2013-06-17 21:39:30 -04:00
}
2013-06-18 13:31:07 -04:00
lookupMap [ id ] = append ( lookupMap [ id ] , name + ":" + tag )
2013-06-17 21:39:30 -04:00
}
}
2014-01-13 19:50:10 -05:00
outs := engine . NewTable ( "Created" , 0 )
2014-03-07 20:36:47 -05:00
err = foundImage . WalkHistory ( func ( img * image . Image ) error {
2014-01-13 19:50:10 -05:00
out := & engine . Env { }
2014-01-29 15:31:49 -05:00
out . Set ( "Id" , img . ID )
2014-01-13 19:50:10 -05:00
out . SetInt64 ( "Created" , img . Created . Unix ( ) )
out . Set ( "CreatedBy" , strings . Join ( img . ContainerConfig . Cmd , " " ) )
out . SetList ( "Tags" , lookupMap [ img . ID ] )
out . SetInt64 ( "Size" , img . Size )
outs . Add ( out )
2013-05-06 05:31:22 -04:00
return nil
} )
2014-01-13 19:50:10 -05:00
outs . ReverseSort ( )
2014-01-21 18:06:23 -05:00
if _ , err := outs . WriteListTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-13 19:50:10 -05:00
}
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-01-16 17:58:20 -05:00
func ( srv * Server ) ContainerTop ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 && len ( job . Args ) != 2 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Not enough arguments. Usage: %s CONTAINER [PS_ARGS]\n" , job . Name )
2014-01-16 17:58:20 -05:00
}
var (
name = job . Args [ 0 ]
psArgs = "-ef"
)
if len ( job . Args ) == 2 && job . Args [ 1 ] != "" {
psArgs = job . Args [ 1 ]
}
2013-06-28 11:51:58 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2013-12-23 17:42:04 -05:00
if ! container . State . IsRunning ( ) {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Container %s is not running" , name )
2013-12-23 17:42:04 -05:00
}
2014-03-07 21:42:29 -05:00
pids , err := srv . runtime . ExecutionDriver ( ) . GetPidsForContainer ( container . ID )
2013-06-28 11:51:58 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-11 09:56:44 -05:00
}
output , err := exec . Command ( "ps" , psArgs ) . Output ( )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Error running ps: %s" , err )
2013-12-11 09:56:44 -05:00
}
lines := strings . Split ( string ( output ) , "\n" )
header := strings . Fields ( lines [ 0 ] )
2014-01-16 17:58:20 -05:00
out := & engine . Env { }
out . SetList ( "Titles" , header )
2013-12-11 09:56:44 -05:00
pidIndex := - 1
for i , name := range header {
if name == "PID" {
pidIndex = i
}
}
if pidIndex == - 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Couldn't find PID field in ps output" )
2013-12-11 09:56:44 -05:00
}
2014-01-16 17:58:20 -05:00
processes := [ ] [ ] string { }
2013-12-11 09:56:44 -05:00
for _ , line := range lines [ 1 : ] {
2013-07-19 06:06:32 -04:00
if len ( line ) == 0 {
2013-06-28 11:51:58 -04:00
continue
}
2013-12-11 09:56:44 -05:00
fields := strings . Fields ( line )
p , err := strconv . Atoi ( fields [ pidIndex ] )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Unexpected pid '%s': %s" , fields [ pidIndex ] , err )
2013-06-28 11:51:58 -04:00
}
2013-12-11 09:56:44 -05:00
for _ , pid := range pids {
if pid == p {
// Make sure number of fields equals number of header titles
// merging "overhanging" fields
2014-01-16 17:58:20 -05:00
process := fields [ : len ( header ) - 1 ]
process = append ( process , strings . Join ( fields [ len ( header ) - 1 : ] , " " ) )
processes = append ( processes , process )
2013-10-29 22:03:41 -04:00
}
2013-07-19 06:06:32 -04:00
}
2013-06-28 11:51:58 -04:00
}
2014-01-16 17:58:20 -05:00
out . SetJson ( "Processes" , processes )
out . WriteTo ( job . Stdout )
return engine . StatusOK
2013-06-28 11:51:58 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-06-28 11:51:58 -04:00
}
2014-01-14 19:51:59 -05:00
func ( srv * Server ) ContainerChanges ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER" , job . Name )
2014-01-14 19:51:59 -05:00
}
name := job . Args [ 0 ]
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2014-01-14 19:51:59 -05:00
outs := engine . NewTable ( "" , 0 )
changes , err := container . Changes ( )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-14 19:51:59 -05:00
}
for _ , change := range changes {
out := & engine . Env { }
if err := out . Import ( change ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-14 19:51:59 -05:00
}
outs . Add ( out )
}
2014-01-21 18:06:23 -05:00
if _ , err := outs . WriteListTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-14 19:51:59 -05:00
}
} else {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-05-06 05:31:22 -04:00
}
2014-01-14 19:51:59 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-01-16 17:00:18 -05:00
func ( srv * Server ) Containers ( job * engine . Job ) engine . Status {
var (
foundBefore bool
displayed int
all = job . GetenvBool ( "all" )
since = job . Getenv ( "since" )
before = job . Getenv ( "before" )
n = job . GetenvInt ( "limit" )
size = job . GetenvBool ( "size" )
)
outs := engine . NewTable ( "Created" , 0 )
2013-05-09 20:50:56 -04:00
2013-11-19 14:02:10 -05:00
names := map [ string ] [ ] string { }
2014-03-07 21:42:29 -05:00
srv . runtime . ContainerGraph ( ) . Walk ( "/" , func ( p string , e * graphdb . Entity ) error {
2013-11-19 14:02:10 -05:00
names [ e . ID ( ) ] = append ( names [ e . ID ( ) ] , p )
return nil
} , - 1 )
2014-03-18 13:38:56 -04:00
var beforeCont , sinceCont * runtime . Container
if before != "" {
beforeCont = srv . runtime . Get ( before )
if beforeCont == nil {
return job . Error ( fmt . Errorf ( "Could not find container with name or id %s" , before ) )
}
}
if since != "" {
sinceCont = srv . runtime . Get ( since )
if sinceCont == nil {
return job . Error ( fmt . Errorf ( "Could not find container with name or id %s" , since ) )
}
}
2013-05-08 12:28:11 -04:00
for _ , container := range srv . runtime . List ( ) {
2014-01-30 21:06:08 -05:00
if ! container . State . IsRunning ( ) && ! all && n <= 0 && since == "" && before == "" {
2013-05-06 05:31:22 -04:00
continue
}
2013-11-13 04:29:00 -05:00
if before != "" && ! foundBefore {
2014-03-18 13:38:56 -04:00
if container . ID == beforeCont . ID {
2013-05-08 12:28:11 -04:00
foundBefore = true
}
2013-11-13 04:29:00 -05:00
continue
2013-05-08 12:28:11 -04:00
}
2014-01-30 21:06:08 -05:00
if n > 0 && displayed == n {
2013-05-08 12:28:11 -04:00
break
}
2014-03-18 13:38:56 -04:00
if since != "" {
if container . ID == sinceCont . ID {
break
}
2013-05-06 05:31:22 -04:00
}
2013-05-09 20:50:56 -04:00
displayed ++
2014-01-16 17:00:18 -05:00
out := & engine . Env { }
2014-01-29 15:31:49 -05:00
out . Set ( "Id" , container . ID )
2014-01-16 17:00:18 -05:00
out . SetList ( "Names" , names [ container . ID ] )
2014-03-07 21:42:29 -05:00
out . Set ( "Image" , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2014-03-07 15:07:17 -05:00
if len ( container . Args ) > 0 {
2014-03-17 14:36:15 -04:00
out . Set ( "Command" , fmt . Sprintf ( "\"%s %s\"" , container . Path , container . ArgsAsString ( ) ) )
2014-03-07 15:07:17 -05:00
} else {
out . Set ( "Command" , fmt . Sprintf ( "\"%s\"" , container . Path ) )
}
2014-01-16 17:00:18 -05:00
out . SetInt64 ( "Created" , container . Created . Unix ( ) )
out . Set ( "Status" , container . State . String ( ) )
str , err := container . NetworkSettings . PortMappingAPI ( ) . ToListString ( )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-16 17:00:18 -05:00
}
out . Set ( "Ports" , str )
if size {
sizeRw , sizeRootFs := container . GetSize ( )
out . SetInt64 ( "SizeRw" , sizeRw )
out . SetInt64 ( "SizeRootFs" , sizeRootFs )
}
outs . Add ( out )
2013-10-04 22:25:15 -04:00
}
2014-01-16 17:00:18 -05:00
outs . ReverseSort ( )
if _ , err := outs . WriteListTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-10-04 22:25:15 -04:00
}
2014-01-16 17:00:18 -05:00
return engine . StatusOK
2013-10-04 22:25:15 -04:00
}
2014-01-16 17:00:18 -05:00
2013-12-11 20:03:48 -05:00
func ( srv * Server ) ContainerCommit ( job * engine . Job ) engine . Status {
2013-12-13 17:29:27 -05:00
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Not enough arguments. Usage: %s CONTAINER\n" , job . Name )
2013-12-11 20:03:48 -05:00
}
2013-12-13 17:29:27 -05:00
name := job . Args [ 0 ]
2013-12-11 20:03:48 -05:00
2013-05-07 13:23:50 -04:00
container := srv . runtime . Get ( name )
if container == nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-12-11 20:03:48 -05:00
}
2014-01-30 14:00:18 -05:00
var config = container . Config
var newConfig runconfig . Config
if err := job . GetenvJson ( "config" , & newConfig ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-07 13:23:50 -04:00
}
2013-12-11 20:03:48 -05:00
2014-01-30 14:00:18 -05:00
if err := runconfig . Merge ( & newConfig , config ) ; err != nil {
return job . Error ( err )
}
img , err := srv . runtime . Commit ( container , job . Getenv ( "repo" ) , job . Getenv ( "tag" ) , job . Getenv ( "comment" ) , job . Getenv ( "author" ) , & newConfig )
2013-05-06 05:31:22 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
2013-12-11 20:03:48 -05:00
job . Printf ( "%s\n" , img . ID )
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-12-11 20:52:41 -05:00
func ( srv * Server ) ImageTag ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 2 && len ( job . Args ) != 3 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s IMAGE REPOSITORY [TAG]\n" , job . Name )
2013-05-06 05:31:22 -04:00
}
2013-12-11 20:52:41 -05:00
var tag string
if len ( job . Args ) == 3 {
tag = job . Args [ 2 ]
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Repositories ( ) . Set ( job . Args [ 1 ] , tag , job . Args [ 0 ] , job . GetenvBool ( "force" ) ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-11 20:52:41 -05:00
}
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-07-02 18:27:22 -04:00
func ( srv * Server ) pullImage ( r * registry . Registry , out io . Writer , imgID , endpoint string , token [ ] string , sf * utils . StreamFormatter ) error {
history , err := r . GetRemoteHistory ( imgID , endpoint , token )
2013-05-14 21:41:39 -04:00
if err != nil {
2013-05-06 05:31:22 -04:00
return err
}
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( imgID ) , "Pulling dependent layers" , nil ) )
2013-05-14 21:41:39 -04:00
// FIXME: Try to stream the images?
// FIXME: Launch the getRemoteImage() in goroutines
2013-08-22 07:23:05 -04:00
2013-11-12 14:48:35 -05:00
for i := len ( history ) - 1 ; i >= 0 ; i -- {
id := history [ i ]
2013-08-22 07:23:05 -04:00
// ensure no two downloads of the same layer happen at the same time
2013-11-20 16:51:05 -05:00
if c , err := srv . poolAdd ( "pull" , "layer:" + id ) ; err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Image (id: %s) pull is already running, skipping: %v" , id , err )
2013-11-20 16:51:05 -05:00
<- c
2013-08-22 07:23:05 -04:00
}
defer srv . poolRemove ( "pull" , "layer:" + id )
2014-03-07 21:42:29 -05:00
if ! srv . runtime . Graph ( ) . Exists ( id ) {
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Pulling metadata" , nil ) )
2014-02-28 11:42:20 -05:00
var (
imgJSON [ ] byte
imgSize int
err error
img * image . Image
)
retries := 5
for j := 1 ; j <= retries ; j ++ {
imgJSON , imgSize , err = r . GetRemoteImageJSON ( id , endpoint , token )
if err != nil && j == retries {
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Error pulling dependent layers" , nil ) )
return err
} else if err != nil {
time . Sleep ( time . Duration ( j ) * 500 * time . Millisecond )
continue
}
img , err = image . NewImgJSON ( imgJSON )
if err != nil && j == retries {
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Error pulling dependent layers" , nil ) )
return fmt . Errorf ( "Failed to parse json: %s" , err )
} else if err != nil {
time . Sleep ( time . Duration ( j ) * 500 * time . Millisecond )
continue
} else {
break
}
2013-05-14 21:41:39 -04:00
}
// Get the layer
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Pulling fs layer" , nil ) )
2013-06-06 21:16:16 -04:00
layer , err := r . GetRemoteImageLayer ( img . ID , endpoint , token )
2013-05-14 21:41:39 -04:00
if err != nil {
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Error pulling dependent layers" , nil ) )
2013-05-14 21:41:39 -04:00
return err
}
2013-06-03 15:14:57 -04:00
defer layer . Close ( )
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Graph ( ) . Register ( imgJSON , utils . ProgressReader ( layer , imgSize , out , sf , false , utils . TruncateID ( id ) , "Downloading" ) , img ) ; err != nil {
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Error downloading dependent layers" , nil ) )
2013-05-14 21:41:39 -04:00
return err
}
}
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( id ) , "Download complete" , nil ) )
2013-08-22 07:23:05 -04:00
2013-05-14 21:41:39 -04:00
}
2013-05-06 05:31:22 -04:00
return nil
}
2013-10-22 14:49:13 -04:00
func ( srv * Server ) pullRepository ( r * registry . Registry , out io . Writer , localName , remoteName , askedTag string , sf * utils . StreamFormatter , parallel bool ) error {
2013-07-24 13:10:59 -04:00
out . Write ( sf . FormatStatus ( "" , "Pulling repository %s" , localName ) )
2013-05-14 21:41:39 -04:00
2013-10-22 14:49:13 -04:00
repoData , err := r . GetRepositoryData ( remoteName )
2013-05-06 07:34:31 -04:00
if err != nil {
2013-05-14 21:41:39 -04:00
return err
}
utils . Debugf ( "Retrieving the tag list" )
2013-07-08 20:26:50 -04:00
tagsList , err := r . GetRemoteTags ( repoData . Endpoints , remoteName , repoData . Tokens )
2013-05-14 21:41:39 -04:00
if err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "%v" , err )
2013-05-14 21:41:39 -04:00
return err
}
2013-05-24 13:37:34 -04:00
2013-07-05 15:20:58 -04:00
for tag , id := range tagsList {
repoData . ImgList [ id ] = & registry . ImgData {
ID : id ,
Tag : tag ,
Checksum : "" ,
2013-05-24 13:37:34 -04:00
}
}
2013-05-16 15:29:16 -04:00
utils . Debugf ( "Registering tags" )
2013-05-24 13:37:34 -04:00
// If no tag has been specified, pull them all
2013-05-16 15:29:16 -04:00
if askedTag == "" {
for tag , id := range tagsList {
repoData . ImgList [ id ] . Tag = tag
}
} else {
// Otherwise, check that the tag exists and use only that one
2013-06-04 09:51:12 -04:00
id , exists := tagsList [ askedTag ]
if ! exists {
2013-07-08 20:26:50 -04:00
return fmt . Errorf ( "Tag %s not found in repository %s" , askedTag , localName )
2013-05-16 15:29:16 -04:00
}
2013-06-04 09:51:12 -04:00
repoData . ImgList [ id ] . Tag = askedTag
2013-05-14 21:41:39 -04:00
}
2013-07-24 11:41:34 -04:00
errors := make ( chan error )
for _ , image := range repoData . ImgList {
2013-07-30 08:09:07 -04:00
downloadImage := func ( img * registry . ImgData ) {
2013-07-24 11:41:34 -04:00
if askedTag != "" && img . Tag != askedTag {
utils . Debugf ( "(%s) does not match %s (id: %s), skipping" , img . Tag , askedTag , img . ID )
2013-08-22 07:23:05 -04:00
if parallel {
errors <- nil
}
2013-07-24 11:41:34 -04:00
return
}
2013-07-02 12:25:06 -04:00
2013-07-24 11:41:34 -04:00
if img . Tag == "" {
utils . Debugf ( "Image (id: %s) present in this repository but untagged, skipping" , img . ID )
2013-08-22 07:23:05 -04:00
if parallel {
errors <- nil
}
return
}
// ensure no two downloads of the same image happen at the same time
2013-12-05 17:41:56 -05:00
if c , err := srv . poolAdd ( "pull" , "img:" + img . ID ) ; err != nil {
if c != nil {
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , "Layer already being pulled by another client. Waiting." , nil ) )
<- c
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , "Download complete" , nil ) )
} else {
utils . Errorf ( "Image (id: %s) pull is already running, skipping: %v" , img . ID , err )
}
2013-08-22 07:23:05 -04:00
if parallel {
errors <- nil
}
2013-07-24 11:41:34 -04:00
return
2013-05-14 21:41:39 -04:00
}
2013-08-22 07:23:05 -04:00
defer srv . poolRemove ( "pull" , "img:" + img . ID )
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , fmt . Sprintf ( "Pulling image (%s) from %s" , img . Tag , localName ) , nil ) )
2013-07-24 11:41:34 -04:00
success := false
2013-08-22 07:23:05 -04:00
var lastErr error
2013-07-24 11:41:34 -04:00
for _ , ep := range repoData . Endpoints {
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , fmt . Sprintf ( "Pulling image (%s) from %s, endpoint: %s" , img . Tag , localName , ep ) , nil ) )
2013-07-24 11:41:34 -04:00
if err := srv . pullImage ( r , out , img . ID , ep , repoData . Tokens , sf ) ; err != nil {
2013-08-22 07:23:05 -04:00
// Its not ideal that only the last error is returned, it would be better to concatenate the errors.
// As the error is also given to the output stream the user will see the error.
lastErr = err
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , fmt . Sprintf ( "Error pulling image (%s) from %s, endpoint: %s, %s" , img . Tag , localName , ep , err ) , nil ) )
2013-07-24 11:41:34 -04:00
continue
}
success = true
break
}
if ! success {
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , fmt . Sprintf ( "Error pulling image (%s) from %s, %s" , img . Tag , localName , lastErr ) , nil ) )
2013-08-22 07:23:05 -04:00
if parallel {
errors <- fmt . Errorf ( "Could not find repository on any of the indexed registries." )
return
}
}
2013-11-28 15:16:57 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( img . ID ) , "Download complete" , nil ) )
2013-08-22 07:23:05 -04:00
if parallel {
errors <- nil
2013-07-24 11:41:34 -04:00
}
2013-05-14 21:41:39 -04:00
}
2013-07-02 12:25:06 -04:00
2013-07-30 08:09:07 -04:00
if parallel {
go downloadImage ( image )
} else {
downloadImage ( image )
2013-07-02 12:25:06 -04:00
}
2013-07-24 11:41:34 -04:00
}
2013-07-30 08:09:07 -04:00
if parallel {
2013-08-22 07:23:05 -04:00
var lastError error
2013-07-30 08:09:07 -04:00
for i := 0 ; i < len ( repoData . ImgList ) ; i ++ {
if err := <- errors ; err != nil {
2013-08-22 07:23:05 -04:00
lastError = err
2013-05-14 21:41:39 -04:00
}
}
2013-08-22 07:23:05 -04:00
if lastError != nil {
return lastError
}
2013-07-24 11:41:34 -04:00
2013-08-22 07:23:05 -04:00
}
2013-05-14 21:41:39 -04:00
for tag , id := range tagsList {
2013-05-20 20:30:33 -04:00
if askedTag != "" && tag != askedTag {
continue
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Repositories ( ) . Set ( localName , tag , id , true ) ; err != nil {
2013-05-14 21:41:39 -04:00
return err
}
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Repositories ( ) . Save ( ) ; err != nil {
2013-05-06 07:34:31 -04:00
return err
}
2013-05-14 21:41:39 -04:00
return nil
}
2013-11-20 16:51:05 -05:00
func ( srv * Server ) poolAdd ( kind , key string ) ( chan struct { } , error ) {
2013-07-02 18:46:32 -04:00
srv . Lock ( )
defer srv . Unlock ( )
2013-06-17 19:10:00 -04:00
2013-11-20 16:51:05 -05:00
if c , exists := srv . pullingPool [ key ] ; exists {
return c , fmt . Errorf ( "pull %s is already in progress" , key )
2013-07-17 16:39:36 -04:00
}
2013-11-20 16:51:05 -05:00
if c , exists := srv . pushingPool [ key ] ; exists {
return c , fmt . Errorf ( "push %s is already in progress" , key )
2013-06-17 19:10:00 -04:00
}
2013-11-20 16:51:05 -05:00
c := make ( chan struct { } )
2013-06-17 19:10:00 -04:00
switch kind {
case "pull" :
2013-11-20 16:51:05 -05:00
srv . pullingPool [ key ] = c
2013-06-17 19:10:00 -04:00
case "push" :
2013-11-20 16:51:05 -05:00
srv . pushingPool [ key ] = c
2013-06-17 19:10:00 -04:00
default :
2013-11-20 16:51:05 -05:00
return nil , fmt . Errorf ( "Unknown pool type" )
2013-06-17 19:10:00 -04:00
}
2013-11-20 16:51:05 -05:00
return c , nil
2013-06-17 19:10:00 -04:00
}
func ( srv * Server ) poolRemove ( kind , key string ) error {
2013-11-25 13:58:17 -05:00
srv . Lock ( )
defer srv . Unlock ( )
2013-06-17 19:10:00 -04:00
switch kind {
case "pull" :
2013-11-20 16:51:05 -05:00
if c , exists := srv . pullingPool [ key ] ; exists {
close ( c )
delete ( srv . pullingPool , key )
}
2013-06-17 19:10:00 -04:00
case "push" :
2013-11-20 16:51:05 -05:00
if c , exists := srv . pushingPool [ key ] ; exists {
close ( c )
delete ( srv . pushingPool , key )
}
2013-06-17 19:10:00 -04:00
default :
2013-08-12 13:53:06 -04:00
return fmt . Errorf ( "Unknown pool type" )
2013-06-17 19:10:00 -04:00
}
return nil
}
2014-01-22 16:35:35 -05:00
func ( srv * Server ) ImagePull ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 1 && n != 2 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s IMAGE [TAG]" , job . Name )
2014-01-22 16:35:35 -05:00
}
var (
localName = job . Args [ 0 ]
tag string
sf = utils . NewStreamFormatter ( job . GetenvBool ( "json" ) )
2014-03-10 20:16:58 -04:00
authConfig = & registry . AuthConfig { }
2014-01-22 16:35:35 -05:00
metaHeaders map [ string ] [ ] string
)
if len ( job . Args ) > 1 {
tag = job . Args [ 1 ]
}
job . GetenvJson ( "authConfig" , authConfig )
job . GetenvJson ( "metaHeaders" , metaHeaders )
2013-11-27 07:18:01 -05:00
2013-11-27 11:53:36 -05:00
c , err := srv . poolAdd ( "pull" , localName + ":" + tag )
if err != nil {
if c != nil {
// Another pull of the same repository is already taking place; just wait for it to finish
2014-01-23 19:00:07 -05:00
job . Stdout . Write ( sf . FormatStatus ( "" , "Repository %s already being pulled by another client. Waiting." , localName ) )
2013-11-27 11:53:36 -05:00
<- c
2014-01-22 16:35:35 -05:00
return engine . StatusOK
2013-11-27 11:53:36 -05:00
}
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-06-17 19:10:00 -04:00
}
2013-07-08 20:26:50 -04:00
defer srv . poolRemove ( "pull" , localName + ":" + tag )
2013-06-17 19:10:00 -04:00
2013-07-05 15:20:58 -04:00
// Resolve the Repository name from fqn to endpoint + name
2014-02-20 17:57:58 -05:00
hostname , remoteName , err := registry . ResolveRepositoryName ( localName )
if err != nil {
return job . Error ( err )
}
endpoint , err := registry . ExpandAndVerifyRegistryUrl ( hostname )
2013-07-05 15:20:58 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-06-17 19:10:00 -04:00
}
2013-07-05 15:20:58 -04:00
2013-10-22 14:49:13 -04:00
r , err := registry . NewRegistry ( authConfig , srv . HTTPRequestFactory ( metaHeaders ) , endpoint )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-10-22 14:49:13 -04:00
}
2014-03-10 20:16:58 -04:00
if endpoint == registry . IndexServerAddress ( ) {
2013-07-09 14:30:12 -04:00
// If pull "index.docker.io/foo/bar", it's stored locally under "foo/bar"
localName = remoteName
}
2013-06-17 19:10:00 -04:00
2014-01-23 19:00:07 -05:00
if err = srv . pullRepository ( r , job . Stdout , localName , remoteName , tag , sf , job . GetenvBool ( "parallel" ) ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-09-03 14:45:49 -04:00
}
2013-06-05 18:13:01 -04:00
2014-01-22 16:35:35 -05:00
return engine . StatusOK
2013-05-14 21:41:39 -04:00
}
2013-05-14 23:27:15 -04:00
// Retrieve the all the images to be uploaded in the correct order
2014-03-31 21:50:10 -04:00
func ( srv * Server ) getImageList ( localRepo map [ string ] string , requestedTag string ) ( [ ] string , map [ string ] [ ] string , error ) {
2013-12-09 09:58:04 -05:00
var (
2014-01-08 05:37:22 -05:00
imageList [ ] string
imagesSeen map [ string ] bool = make ( map [ string ] bool )
2013-12-09 09:58:04 -05:00
tagsByImage map [ string ] [ ] string = make ( map [ string ] [ ] string )
)
2013-05-14 23:27:15 -04:00
for tag , id := range localRepo {
2014-03-31 21:50:10 -04:00
if requestedTag != "" && requestedTag != tag {
continue
}
2013-12-09 12:01:53 -05:00
var imageListForThisTag [ ] string
2013-12-09 09:58:04 -05:00
tagsByImage [ id ] = append ( tagsByImage [ id ] , tag )
2014-03-07 21:42:29 -05:00
for img , err := srv . runtime . Graph ( ) . Get ( id ) ; img != nil ; img , err = img . GetParent ( ) {
2013-09-03 20:20:50 -04:00
if err != nil {
2013-12-09 12:01:53 -05:00
return nil , nil , err
2013-09-03 20:20:50 -04:00
}
2013-12-09 09:58:04 -05:00
if imagesSeen [ img . ID ] {
// This image is already on the list, we can ignore it and all its parents
break
2013-05-14 23:27:15 -04:00
}
2013-09-03 20:20:50 -04:00
2013-12-09 09:58:04 -05:00
imagesSeen [ img . ID ] = true
imageListForThisTag = append ( imageListForThisTag , img . ID )
2013-12-09 12:01:32 -05:00
}
2013-09-03 20:20:50 -04:00
2013-12-09 12:01:53 -05:00
// reverse the image list for this tag (so the "most"-parent image is first)
2014-01-08 05:37:22 -05:00
for i , j := 0 , len ( imageListForThisTag ) - 1 ; i < j ; i , j = i + 1 , j - 1 {
2013-12-09 12:01:53 -05:00
imageListForThisTag [ i ] , imageListForThisTag [ j ] = imageListForThisTag [ j ] , imageListForThisTag [ i ]
2013-09-03 20:20:50 -04:00
}
2013-12-09 12:01:53 -05:00
// append to main image list
imageList = append ( imageList , imageListForThisTag ... )
2013-09-03 20:20:50 -04:00
}
2014-03-31 21:50:10 -04:00
if len ( imageList ) == 0 {
return nil , nil , fmt . Errorf ( "No images found for the requested repository / tag" )
}
2013-12-09 09:58:04 -05:00
utils . Debugf ( "Image list: %v" , imageList )
utils . Debugf ( "Tags by image: %v" , tagsByImage )
return imageList , tagsByImage , nil
2013-05-14 23:27:15 -04:00
}
2014-03-31 21:50:10 -04:00
func ( srv * Server ) pushRepository ( r * registry . Registry , out io . Writer , localName , remoteName string , localRepo map [ string ] string , tag string , sf * utils . StreamFormatter ) error {
2013-05-20 13:58:35 -04:00
out = utils . NewWriteFlusher ( out )
2013-12-09 09:58:04 -05:00
utils . Debugf ( "Local repo: %s" , localRepo )
2014-03-31 21:50:10 -04:00
imgList , tagsByImage , err := srv . getImageList ( localRepo , tag )
2013-05-14 23:27:15 -04:00
if err != nil {
return err
}
2013-12-09 09:58:04 -05:00
2013-07-24 13:10:59 -04:00
out . Write ( sf . FormatStatus ( "" , "Sending image list" ) )
2013-05-14 23:27:15 -04:00
2014-03-31 21:50:10 -04:00
var (
repoData * registry . RepositoryData
imageIndex [ ] * registry . ImgData
)
2013-12-09 09:58:04 -05:00
2014-01-20 16:39:35 -05:00
for _ , imgId := range imgList {
if tags , exists := tagsByImage [ imgId ] ; exists {
// If an image has tags you must add an entry in the image index
// for each tag
for _ , tag := range tags {
imageIndex = append ( imageIndex , & registry . ImgData {
ID : imgId ,
Tag : tag ,
} )
}
} else {
// If the image does not have a tag it still needs to be sent to the
// registry with an empty tag so that it is accociated with the repository
2013-12-09 09:58:04 -05:00
imageIndex = append ( imageIndex , & registry . ImgData {
2014-01-08 05:37:22 -05:00
ID : imgId ,
2014-01-20 16:39:35 -05:00
Tag : "" ,
2013-12-09 09:58:04 -05:00
} )
2014-01-20 16:39:35 -05:00
2013-12-09 09:58:04 -05:00
}
}
2014-01-20 16:39:35 -05:00
utils . Debugf ( "Preparing to push %s with the following images and tags\n" , localRepo )
for _ , data := range imageIndex {
utils . Debugf ( "Pushing ID: %s with Tag: %s\n" , data . ID , data . Tag )
}
// Register all the images in a repository with the registry
// If an image is not in this list it will not be associated with the repository
2013-12-09 09:58:04 -05:00
repoData , err = r . PushImageJSONIndex ( remoteName , imageIndex , false , nil )
2013-05-14 23:27:15 -04:00
if err != nil {
return err
}
2014-03-31 21:50:10 -04:00
nTag := 1
if tag == "" {
nTag = len ( localRepo )
}
2013-05-14 23:27:15 -04:00
for _ , ep := range repoData . Endpoints {
2014-03-31 21:50:10 -04:00
out . Write ( sf . FormatStatus ( "" , "Pushing repository %s (%d tags)" , localName , nTag ) )
2013-12-09 09:58:04 -05:00
for _ , imgId := range imgList {
if r . LookupRemoteImage ( imgId , ep , repoData . Tokens ) {
out . Write ( sf . FormatStatus ( "" , "Image %s already pushed, skipping" , utils . TruncateID ( imgId ) ) )
2013-12-09 12:01:32 -05:00
} else {
2013-12-09 12:01:53 -05:00
if _ , err := srv . pushImage ( r , out , remoteName , imgId , ep , repoData . Tokens , sf ) ; err != nil {
2013-09-03 20:20:50 -04:00
// FIXME: Continue on error?
return err
}
2013-12-09 12:01:53 -05:00
}
2013-11-18 18:35:56 -05:00
2013-12-09 12:01:53 -05:00
for _ , tag := range tagsByImage [ imgId ] {
out . Write ( sf . FormatStatus ( "" , "Pushing tag for rev [%s] on {%s}" , utils . TruncateID ( imgId ) , ep + "repositories/" + remoteName + "/tags/" + tag ) )
2013-11-18 18:35:56 -05:00
2013-12-09 12:01:53 -05:00
if err := r . PushRegistryTag ( remoteName , imgId , tag , ep , repoData . Tokens ) ; err != nil {
2013-09-11 13:39:33 -04:00
return err
}
2013-05-15 14:30:40 -04:00
}
2013-05-14 23:27:15 -04:00
}
}
2013-12-09 09:58:04 -05:00
if _ , err := r . PushImageJSONIndex ( remoteName , imageIndex , true , repoData . Endpoints ) ; err != nil {
2013-05-14 23:27:15 -04:00
return err
}
2013-05-24 13:37:34 -04:00
2013-05-15 14:30:40 -04:00
return nil
2013-05-14 23:27:15 -04:00
}
2013-07-22 19:44:34 -04:00
func ( srv * Server ) pushImage ( r * registry . Registry , out io . Writer , remote , imgID , ep string , token [ ] string , sf * utils . StreamFormatter ) ( checksum string , err error ) {
2013-05-20 13:58:35 -04:00
out = utils . NewWriteFlusher ( out )
2014-03-07 21:42:29 -05:00
jsonRaw , err := ioutil . ReadFile ( path . Join ( srv . runtime . Graph ( ) . Root , imgID , "json" ) )
2013-05-14 23:27:15 -04:00
if err != nil {
2013-10-30 14:45:11 -04:00
return "" , fmt . Errorf ( "Cannot retrieve the path for {%s}: %s" , imgID , err )
2013-05-14 23:27:15 -04:00
}
2013-12-10 13:57:16 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( imgID ) , "Pushing" , nil ) )
2013-05-14 23:27:15 -04:00
imgData := & registry . ImgData {
2013-07-17 15:13:22 -04:00
ID : imgID ,
2013-05-14 23:27:15 -04:00
}
2013-05-15 16:22:57 -04:00
// Send the json
2013-06-04 14:00:22 -04:00
if err := r . PushImageJSONRegistry ( imgData , jsonRaw , ep , token ) ; err != nil {
2013-05-15 16:22:57 -04:00
if err == registry . ErrAlreadyExists {
2013-12-10 13:57:16 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( imgData . ID ) , "Image already pushed, skipping" , nil ) )
2013-07-22 19:44:34 -04:00
return "" , nil
2013-05-15 16:22:57 -04:00
}
2013-07-22 19:44:34 -04:00
return "" , err
2013-05-15 16:22:57 -04:00
}
2014-03-07 21:42:29 -05:00
layerData , err := srv . runtime . Graph ( ) . TempLayerArchive ( imgID , archive . Uncompressed , sf , out )
2013-05-14 23:27:15 -04:00
if err != nil {
2013-07-22 19:44:34 -04:00
return "" , fmt . Errorf ( "Failed to generate layer archive: %s" , err )
2013-05-14 23:27:15 -04:00
}
2013-11-15 19:23:55 -05:00
defer os . RemoveAll ( layerData . Name ( ) )
2013-05-15 14:30:40 -04:00
// Send the layer
2014-03-19 14:34:12 -04:00
utils . Debugf ( "rendered layer for %s of [%d] size" , imgData . ID , layerData . Size )
2014-02-23 21:33:46 -05:00
checksum , checksumPayload , err := r . PushImageLayerRegistry ( imgData . ID , utils . ProgressReader ( layerData , int ( layerData . Size ) , out , sf , false , utils . TruncateID ( imgData . ID ) , "Pushing" ) , ep , token , jsonRaw )
2013-11-18 18:35:56 -05:00
if err != nil {
2013-07-22 19:44:34 -04:00
return "" , err
2013-05-06 05:31:22 -04:00
}
2013-11-18 18:35:56 -05:00
imgData . Checksum = checksum
2014-02-25 19:06:04 -05:00
imgData . ChecksumPayload = checksumPayload
2013-07-17 15:13:22 -04:00
// Send the checksum
if err := r . PushImageChecksumRegistry ( imgData , ep , token ) ; err != nil {
2013-07-22 19:44:34 -04:00
return "" , err
2013-07-17 15:13:22 -04:00
}
2013-12-19 15:32:58 -05:00
out . Write ( sf . FormatProgress ( utils . TruncateID ( imgData . ID ) , "Image successfully pushed" , nil ) )
2013-07-22 19:44:34 -04:00
return imgData . Checksum , nil
2013-05-06 05:31:22 -04:00
}
2013-08-12 13:53:06 -04:00
// FIXME: Allow to interrupt current push when new push of same image is done.
2014-01-22 20:33:29 -05:00
func ( srv * Server ) ImagePush ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s IMAGE" , job . Name )
2013-06-17 19:10:00 -04:00
}
2014-01-22 20:33:29 -05:00
var (
localName = job . Args [ 0 ]
sf = utils . NewStreamFormatter ( job . GetenvBool ( "json" ) )
2014-03-10 20:16:58 -04:00
authConfig = & registry . AuthConfig { }
2014-01-22 20:33:29 -05:00
metaHeaders map [ string ] [ ] string
)
2013-06-17 19:10:00 -04:00
2014-03-31 21:50:10 -04:00
tag := job . Getenv ( "tag" )
2014-01-22 20:33:29 -05:00
job . GetenvJson ( "authConfig" , authConfig )
job . GetenvJson ( "metaHeaders" , metaHeaders )
if _ , err := srv . poolAdd ( "push" , localName ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-22 20:33:29 -05:00
}
2014-01-27 10:22:59 -05:00
defer srv . poolRemove ( "push" , localName )
2013-07-05 15:20:58 -04:00
// Resolve the Repository name from fqn to endpoint + name
2014-02-20 17:57:58 -05:00
hostname , remoteName , err := registry . ResolveRepositoryName ( localName )
if err != nil {
return job . Error ( err )
}
endpoint , err := registry . ExpandAndVerifyRegistryUrl ( hostname )
2013-07-05 18:26:08 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-06-17 19:10:00 -04:00
}
2014-03-07 21:42:29 -05:00
img , err := srv . runtime . Graph ( ) . Get ( localName )
2013-10-22 14:49:13 -04:00
r , err2 := registry . NewRegistry ( authConfig , srv . HTTPRequestFactory ( metaHeaders ) , endpoint )
2013-06-17 14:13:40 -04:00
if err2 != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err2 )
2013-06-17 14:13:40 -04:00
}
2013-06-05 18:12:50 -04:00
2013-05-06 07:34:31 -04:00
if err != nil {
2014-03-31 21:50:10 -04:00
reposLen := 1
if tag == "" {
reposLen = len ( srv . runtime . Repositories ( ) . Repositories [ localName ] )
}
2014-01-22 20:33:29 -05:00
job . Stdout . Write ( sf . FormatStatus ( "" , "The push refers to a repository [%s] (len: %d)" , localName , reposLen ) )
2013-05-07 19:33:12 -04:00
// If it fails, try to get the repository
2014-03-07 21:42:29 -05:00
if localRepo , exists := srv . runtime . Repositories ( ) . Repositories [ localName ] ; exists {
2014-03-31 21:50:10 -04:00
if err := srv . pushRepository ( r , job . Stdout , localName , remoteName , localRepo , tag , sf ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 07:34:31 -04:00
}
2014-01-22 20:33:29 -05:00
return engine . StatusOK
2013-05-06 07:34:31 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 07:34:31 -04:00
}
2013-07-05 15:20:58 -04:00
var token [ ] string
2014-01-22 20:33:29 -05:00
job . Stdout . Write ( sf . FormatStatus ( "" , "The push refers to an image: [%s]" , localName ) )
if _ , err := srv . pushImage ( r , job . Stdout , remoteName , img . ID , endpoint , token , sf ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 07:34:31 -04:00
}
2014-01-22 20:33:29 -05:00
return engine . StatusOK
2013-05-06 07:34:31 -04:00
}
2014-01-22 16:35:35 -05:00
func ( srv * Server ) ImageImport ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 2 && n != 3 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s SRC REPO [TAG]" , job . Name )
2014-01-22 16:35:35 -05:00
}
var (
src = job . Args [ 0 ]
repo = job . Args [ 1 ]
tag string
sf = utils . NewStreamFormatter ( job . GetenvBool ( "json" ) )
2014-02-14 06:41:46 -05:00
archive archive . ArchiveReader
2014-01-22 16:35:35 -05:00
resp * http . Response
)
if len ( job . Args ) > 2 {
tag = job . Args [ 2 ]
}
2013-05-06 05:31:22 -04:00
if src == "-" {
2014-01-22 16:35:35 -05:00
archive = job . Stdin
2013-05-06 05:31:22 -04:00
} else {
u , err := url . Parse ( src )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
if u . Scheme == "" {
u . Scheme = "http"
u . Host = src
u . Path = ""
}
2014-01-23 19:00:07 -05:00
job . Stdout . Write ( sf . FormatStatus ( "" , "Downloading from %s" , u ) )
2013-05-07 19:33:12 -04:00
// Download with curl (pretty progress bar)
// If curl is not available, fallback to http.Get()
2013-12-04 14:54:11 -05:00
resp , err = utils . Download ( u . String ( ) )
2013-05-06 05:31:22 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
2014-02-14 06:41:46 -05:00
progressReader := utils . ProgressReader ( resp . Body , int ( resp . ContentLength ) , job . Stdout , sf , true , "" , "Importing" )
defer progressReader . Close ( )
archive = progressReader
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
img , err := srv . runtime . Graph ( ) . Create ( archive , "" , "" , "Imported from " + src , "" , nil , nil )
2013-05-06 05:31:22 -04:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
2013-05-07 19:33:12 -04:00
// Optionally register the image at REPO/TAG
2013-05-06 05:31:22 -04:00
if repo != "" {
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Repositories ( ) . Set ( repo , tag , img . ID , true ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
}
2014-01-23 19:00:07 -05:00
job . Stdout . Write ( sf . FormatStatus ( "" , img . ID ) )
2014-01-22 16:35:35 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-11-20 02:37:03 -05:00
func ( srv * Server ) ContainerCreate ( job * engine . Job ) engine . Status {
2013-10-27 22:20:00 -04:00
var name string
if len ( job . Args ) == 1 {
name = job . Args [ 0 ]
} else if len ( job . Args ) > 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s" , job . Name )
2013-10-27 22:20:00 -04:00
}
2014-02-11 23:04:39 -05:00
config := runconfig . ContainerConfigFromJob ( job )
2013-06-14 12:46:04 -04:00
if config . Memory != 0 && config . Memory < 524288 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Minimum memory limit allowed is 512k" )
2013-06-14 12:46:04 -04:00
}
2014-03-07 21:42:29 -05:00
if config . Memory > 0 && ! srv . runtime . SystemConfig ( ) . MemoryLimit {
2014-02-27 21:40:15 -05:00
job . Errorf ( "Your kernel does not support memory limit capabilities. Limitation discarded.\n" )
2013-05-06 05:31:22 -04:00
config . Memory = 0
}
2014-03-07 21:42:29 -05:00
if config . Memory > 0 && ! srv . runtime . SystemConfig ( ) . SwapLimit {
2014-02-27 21:40:15 -05:00
job . Errorf ( "Your kernel does not support swap limit capabilities. Limitation discarded.\n" )
2013-05-06 05:31:22 -04:00
config . MemorySwap = - 1
}
2014-01-28 20:17:55 -05:00
resolvConf , err := utils . GetResolvConf ( )
if err != nil {
2014-01-27 22:26:24 -05:00
return job . Error ( err )
2014-01-28 20:17:55 -05:00
}
2014-03-07 21:42:29 -05:00
if ! config . NetworkDisabled && len ( config . Dns ) == 0 && len ( srv . runtime . Config ( ) . Dns ) == 0 && utils . CheckLocalDns ( resolvConf ) {
job . Errorf ( "Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v\n" , runtime . DefaultDns )
config . Dns = runtime . DefaultDns
2014-01-28 20:17:55 -05:00
}
2014-01-29 19:56:42 -05:00
container , buildWarnings , err := srv . runtime . Create ( config , name )
2013-05-06 05:31:22 -04:00
if err != nil {
2014-03-07 21:42:29 -05:00
if srv . runtime . Graph ( ) . IsNotExist ( err ) {
2013-08-17 23:03:54 -04:00
_ , tag := utils . ParseRepositoryTag ( config . Image )
if tag == "" {
2014-03-07 21:04:38 -05:00
tag = graph . DEFAULTTAG
2013-08-17 23:03:54 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such image: %s (tag: %s)" , config . Image , tag )
2013-05-06 05:31:22 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
if ! container . Config . NetworkDisabled && srv . runtime . SystemConfig ( ) . IPv4ForwardingDisabled {
2014-02-27 21:40:15 -05:00
job . Errorf ( "IPv4 forwarding is disabled.\n" )
2014-01-28 20:17:55 -05:00
}
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "create" , container . ID , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-11-14 01:08:08 -05:00
// FIXME: this is necessary because runtime.Create might return a nil container
// with a non-nil error. This should not happen! Once it's fixed we
// can remove this workaround.
if container != nil {
job . Printf ( "%s\n" , container . ID )
}
2013-10-27 22:20:00 -04:00
for _ , warning := range buildWarnings {
2014-03-03 17:07:42 -05:00
job . Errorf ( "%s\n" , warning )
2013-10-27 22:20:00 -04:00
}
2013-11-20 02:37:03 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-01-06 20:34:51 -05:00
func ( srv * Server ) ContainerRestart ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER\n" , job . Name )
2014-01-06 20:34:51 -05:00
}
2014-01-30 21:21:59 -05:00
var (
name = job . Args [ 0 ]
t = 10
)
if job . EnvExists ( "t" ) {
t = job . GetenvInt ( "t" )
2014-01-06 20:34:51 -05:00
}
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2014-01-06 20:34:51 -05:00
if err := container . Restart ( int ( t ) ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot restart container %s: %s\n" , name , err )
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "restart" , container . ID , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-05-06 05:31:22 -04:00
} else {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s\n" , name )
2013-05-06 05:31:22 -04:00
}
2014-01-06 20:34:51 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-12-11 18:23:38 -05:00
func ( srv * Server ) ContainerDestroy ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Not enough arguments. Usage: %s CONTAINER\n" , job . Name )
2013-12-11 18:23:38 -05:00
}
name := job . Args [ 0 ]
removeVolume := job . GetenvBool ( "removeVolume" )
removeLink := job . GetenvBool ( "removeLink" )
2014-02-05 23:10:57 -05:00
forceRemove := job . GetenvBool ( "forceRemove" )
2013-12-11 18:23:38 -05:00
2013-10-25 19:49:49 -04:00
container := srv . runtime . Get ( name )
2013-10-04 22:25:15 -04:00
if removeLink {
2013-10-25 19:49:49 -04:00
if container == nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such link: %s" , name )
2013-10-04 22:25:15 -04:00
}
2014-03-07 21:42:29 -05:00
name , err := runtime . GetFullContainerName ( name )
2013-11-04 12:28:40 -05:00
if err != nil {
2013-12-11 18:23:38 -05:00
job . Error ( err )
2013-11-04 12:28:40 -05:00
}
2013-10-25 19:49:49 -04:00
parent , n := path . Split ( name )
2013-10-30 14:45:11 -04:00
if parent == "/" {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Conflict, cannot remove the default name of the container" )
2013-10-30 14:45:11 -04:00
}
2014-03-07 21:42:29 -05:00
pe := srv . runtime . ContainerGraph ( ) . Get ( parent )
2013-10-28 22:19:31 -04:00
if pe == nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot get parent %s for name %s" , parent , name )
2013-10-04 22:25:15 -04:00
}
2013-10-28 22:19:31 -04:00
parentContainer := srv . runtime . Get ( pe . ID ( ) )
2013-10-04 22:25:15 -04:00
2014-03-07 21:42:29 -05:00
if parentContainer != nil {
parentContainer . DisableLink ( n )
2013-10-25 19:49:49 -04:00
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . ContainerGraph ( ) . Delete ( name ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-10-04 22:25:15 -04:00
}
2013-12-11 18:23:38 -05:00
return engine . StatusOK
2013-10-04 22:25:15 -04:00
}
2013-10-28 22:19:31 -04:00
2013-10-25 19:49:49 -04:00
if container != nil {
2013-11-21 15:21:03 -05:00
if container . State . IsRunning ( ) {
2014-02-05 23:10:57 -05:00
if forceRemove {
if err := container . Stop ( 5 ) ; err != nil {
return job . Errorf ( "Could not stop running container, cannot remove - %v" , err )
}
} else {
return job . Errorf ( "Impossible to remove a running container, please stop it first or use -f" )
}
2013-06-20 11:45:30 -04:00
}
2013-05-06 05:31:22 -04:00
if err := srv . runtime . Destroy ( container ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot destroy container %s: %s" , name , err )
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "destroy" , container . ID , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-05-06 05:52:15 -04:00
2013-05-09 22:19:55 -04:00
if removeVolume {
2014-02-03 16:18:10 -05:00
var (
volumes = make ( map [ string ] struct { } )
binds = make ( map [ string ] struct { } )
2014-03-07 21:42:29 -05:00
usedVolumes = make ( map [ string ] * runtime . Container )
2014-02-03 16:18:10 -05:00
)
// the volume id is always the base of the path
getVolumeId := func ( p string ) string {
return filepath . Base ( strings . TrimSuffix ( p , "/layer" ) )
}
// populate bind map so that they can be skipped and not removed
2014-03-07 21:42:29 -05:00
for _ , bind := range container . HostConfig ( ) . Binds {
2014-02-03 16:18:10 -05:00
source := strings . Split ( bind , ":" ) [ 0 ]
// TODO: refactor all volume stuff, all of it
// this is very important that we eval the link
// or comparing the keys to container.Volumes will not work
p , err := filepath . EvalSymlinks ( source )
if err != nil {
return job . Error ( err )
}
source = p
binds [ source ] = struct { } { }
}
// Store all the deleted containers volumes
for _ , volumeId := range container . Volumes {
// Skip the volumes mounted from external
// bind mounts here will will be evaluated for a symlink
if _ , exists := binds [ volumeId ] ; exists {
continue
}
volumeId = getVolumeId ( volumeId )
volumes [ volumeId ] = struct { } { }
}
2013-05-06 05:52:15 -04:00
// Retrieve all volumes from all remaining containers
for _ , container := range srv . runtime . List ( ) {
for _ , containerVolumeId := range container . Volumes {
2014-02-03 16:18:10 -05:00
containerVolumeId = getVolumeId ( containerVolumeId )
2013-05-06 05:52:15 -04:00
usedVolumes [ containerVolumeId ] = container
}
}
for volumeId := range volumes {
// If the requested volu
if c , exists := usedVolumes [ volumeId ] ; exists {
2013-06-04 14:00:22 -04:00
log . Printf ( "The volume %s is used by the container %s. Impossible to remove it. Skipping.\n" , volumeId , c . ID )
2013-05-06 05:52:15 -04:00
continue
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Volumes ( ) . Delete ( volumeId ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Error calling volumes.Delete(%q): %v" , volumeId , err )
2013-05-06 05:52:15 -04:00
}
}
}
2013-05-06 05:31:22 -04:00
} else {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-05-06 05:31:22 -04:00
}
2013-12-11 18:23:38 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-03-13 21:02:28 -04:00
func ( srv * Server ) DeleteImage ( name string , imgs * engine . Table , first , force , noprune bool ) error {
2013-12-16 16:29:43 -05:00
var (
2014-01-16 21:40:33 -05:00
repoName , tag string
tags = [ ] string { }
2013-12-16 16:29:43 -05:00
)
2013-07-17 11:48:53 -04:00
2014-02-17 22:03:19 -05:00
repoName , tag = utils . ParseRepositoryTag ( name )
if tag == "" {
2014-03-07 21:04:38 -05:00
tag = graph . DEFAULTTAG
2014-02-17 22:03:19 -05:00
}
2014-03-07 21:42:29 -05:00
img , err := srv . runtime . Repositories ( ) . LookupImage ( name )
2014-01-16 21:40:33 -05:00
if err != nil {
2014-03-07 21:42:29 -05:00
if r , _ := srv . runtime . Repositories ( ) . Get ( repoName ) ; r != nil {
2014-02-17 22:03:19 -05:00
return fmt . Errorf ( "No such image: %s:%s" , repoName , tag )
}
2014-02-10 21:44:17 -05:00
return fmt . Errorf ( "No such image: %s" , name )
2014-01-16 21:40:33 -05:00
}
2014-02-17 22:03:19 -05:00
if strings . Contains ( img . ID , name ) {
repoName = ""
tag = ""
2014-01-16 21:40:33 -05:00
}
2014-03-07 21:42:29 -05:00
byParents , err := srv . runtime . Graph ( ) . ByParent ( )
2014-02-10 21:44:17 -05:00
if err != nil {
return err
2014-01-16 21:40:33 -05:00
}
2013-07-17 11:48:53 -04:00
//If delete by id, see if the id belong only to one repository
2013-10-21 19:54:02 -04:00
if repoName == "" {
2014-03-07 21:42:29 -05:00
for _ , repoAndTag := range srv . runtime . Repositories ( ) . ByID ( ) [ img . ID ] {
2013-08-14 12:59:21 -04:00
parsedRepo , parsedTag := utils . ParseRepositoryTag ( repoAndTag )
2013-10-21 19:54:02 -04:00
if repoName == "" || repoName == parsedRepo {
2013-07-17 11:48:53 -04:00
repoName = parsedRepo
2013-10-21 19:54:02 -04:00
if parsedTag != "" {
tags = append ( tags , parsedTag )
2013-07-26 05:19:26 -04:00
}
2014-02-10 21:44:17 -05:00
} else if repoName != parsedRepo && ! force {
2013-07-17 11:48:53 -04:00
// the id belongs to multiple repos, like base:latest and user:test,
// in that case return conflict
2014-02-10 21:44:17 -05:00
return fmt . Errorf ( "Conflict, cannot delete image %s because it is tagged in multiple repositories, use -f to force" , name )
2013-07-17 11:48:53 -04:00
}
}
2013-10-21 19:54:02 -04:00
} else {
tags = append ( tags , tag )
2013-07-17 11:48:53 -04:00
}
2013-12-16 16:29:43 -05:00
2014-02-18 20:34:37 -05:00
if ! first && len ( tags ) > 0 {
return nil
}
2013-07-17 11:48:53 -04:00
//Untag the current image
2013-10-21 19:54:02 -04:00
for _ , tag := range tags {
2014-03-07 21:42:29 -05:00
tagDeleted , err := srv . runtime . Repositories ( ) . Delete ( repoName , tag )
2013-10-21 19:54:02 -04:00
if err != nil {
2014-02-10 21:44:17 -05:00
return err
2013-10-21 19:54:02 -04:00
}
if tagDeleted {
2014-01-16 21:40:33 -05:00
out := & engine . Env { }
2014-02-10 21:44:17 -05:00
out . Set ( "Untagged" , repoName + ":" + tag )
2014-01-16 21:40:33 -05:00
imgs . Add ( out )
2013-10-24 21:59:59 -04:00
srv . LogEvent ( "untag" , img . ID , "" )
2013-10-21 19:54:02 -04:00
}
2013-05-30 15:30:21 -04:00
}
2014-03-07 21:42:29 -05:00
tags = srv . runtime . Repositories ( ) . ByID ( ) [ img . ID ]
2014-02-10 21:44:17 -05:00
if ( len ( tags ) <= 1 && repoName == "" ) || len ( tags ) == 0 {
if len ( byParents [ img . ID ] ) == 0 {
if err := srv . canDeleteImage ( img . ID ) ; err != nil {
return err
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Repositories ( ) . DeleteAll ( img . ID ) ; err != nil {
2014-02-10 21:44:17 -05:00
return err
2013-05-30 18:53:45 -04:00
}
2014-03-07 21:42:29 -05:00
if err := srv . runtime . Graph ( ) . Delete ( img . ID ) ; err != nil {
2014-02-10 21:44:17 -05:00
return err
2013-05-30 15:30:21 -04:00
}
2014-02-10 21:44:17 -05:00
out := & engine . Env { }
out . Set ( "Deleted" , img . ID )
imgs . Add ( out )
srv . LogEvent ( "delete" , img . ID , "" )
2014-03-13 21:02:28 -04:00
if img . Parent != "" && ! noprune {
err := srv . DeleteImage ( img . Parent , imgs , false , force , noprune )
2014-02-10 21:44:17 -05:00
if first {
return err
}
}
2013-05-30 15:30:21 -04:00
}
}
2014-02-10 21:44:17 -05:00
return nil
2013-05-30 15:30:21 -04:00
}
2014-01-16 21:40:33 -05:00
func ( srv * Server ) ImageDelete ( job * engine . Job ) engine . Status {
if n := len ( job . Args ) ; n != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s IMAGE" , job . Name )
2013-05-30 18:53:45 -04:00
}
2014-02-14 19:10:57 -05:00
imgs := engine . NewTable ( "" , 0 )
2014-03-13 21:02:28 -04:00
if err := srv . DeleteImage ( job . Args [ 0 ] , imgs , true , job . GetenvBool ( "force" ) , job . GetenvBool ( "noprune" ) ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-16 16:29:43 -05:00
}
2014-01-20 17:10:23 -05:00
if len ( imgs . Data ) == 0 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Conflict, %s wasn't deleted" , job . Args [ 0 ] )
2014-01-20 17:10:23 -05:00
}
if _ , err := imgs . WriteListTo ( job . Stdout ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-11-08 18:01:01 -05:00
}
2014-01-16 21:40:33 -05:00
return engine . StatusOK
2013-12-20 19:26:02 -05:00
}
2013-11-08 18:01:01 -05:00
2013-12-20 19:26:02 -05:00
func ( srv * Server ) canDeleteImage ( imgID string ) error {
for _ , container := range srv . runtime . List ( ) {
2014-03-07 21:42:29 -05:00
parent , err := srv . runtime . Repositories ( ) . LookupImage ( container . Image )
2013-12-20 19:26:02 -05:00
if err != nil {
return err
}
2013-12-16 16:29:43 -05:00
2014-03-07 20:36:47 -05:00
if err := parent . WalkHistory ( func ( p * image . Image ) error {
2013-12-20 19:26:02 -05:00
if imgID == p . ID {
return fmt . Errorf ( "Conflict, cannot delete %s because the container %s is using it" , utils . TruncateID ( imgID ) , utils . TruncateID ( container . ID ) )
2013-12-16 16:29:43 -05:00
}
2013-12-20 19:26:02 -05:00
return nil
} ) ; err != nil {
return err
2013-12-16 16:29:43 -05:00
}
2013-10-21 19:54:02 -04:00
}
2013-12-20 19:26:02 -05:00
return nil
2013-05-06 05:31:22 -04:00
}
2014-03-07 20:36:47 -05:00
func ( srv * Server ) ImageGetCached ( imgID string , config * runconfig . Config ) ( * image . Image , error ) {
2013-05-19 13:46:24 -04:00
// Retrieve all images
2014-03-07 21:42:29 -05:00
images , err := srv . runtime . Graph ( ) . Map ( )
2013-05-19 13:46:24 -04:00
if err != nil {
return nil , err
}
// Store the tree in a map of map (map[parentId][childId])
2014-01-07 03:15:04 -05:00
imageMap := make ( map [ string ] map [ string ] struct { } )
2013-05-19 13:46:24 -04:00
for _ , img := range images {
2014-01-07 03:15:04 -05:00
if _ , exists := imageMap [ img . Parent ] ; ! exists {
imageMap [ img . Parent ] = make ( map [ string ] struct { } )
}
imageMap [ img . Parent ] [ img . ID ] = struct { } { }
2013-05-19 13:46:24 -04:00
}
2014-01-07 03:15:04 -05:00
2013-05-19 13:46:24 -04:00
// Loop on the children of the given image and check the config
2014-03-07 20:36:47 -05:00
var match * image . Image
2014-01-07 03:15:04 -05:00
for elem := range imageMap [ imgID ] {
2014-03-07 21:42:29 -05:00
img , err := srv . runtime . Graph ( ) . Get ( elem )
2013-05-19 13:46:24 -04:00
if err != nil {
return nil , err
}
2014-02-11 23:04:39 -05:00
if runconfig . Compare ( & img . ContainerConfig , config ) {
2014-01-01 11:38:25 -05:00
if match == nil || match . Created . Before ( img . Created ) {
match = img
}
2013-05-19 13:46:24 -04:00
}
}
2014-01-01 11:38:25 -05:00
return match , nil
2013-05-19 13:46:24 -04:00
}
2014-03-07 21:42:29 -05:00
func ( srv * Server ) RegisterLinks ( container * runtime . Container , hostConfig * runconfig . HostConfig ) error {
2013-10-04 22:25:15 -04:00
runtime := srv . runtime
2013-10-28 22:19:31 -04:00
2013-10-04 22:25:15 -04:00
if hostConfig != nil && hostConfig . Links != nil {
for _ , l := range hostConfig . Links {
2014-03-10 17:06:27 -04:00
parts , err := utils . PartParser ( "name:alias" , l )
2013-10-04 22:25:15 -04:00
if err != nil {
return err
}
2013-10-28 19:58:59 -04:00
child , err := srv . runtime . GetByName ( parts [ "name" ] )
if err != nil {
return err
}
if child == nil {
return fmt . Errorf ( "Could not get container for %s" , parts [ "name" ] )
2013-10-18 17:15:24 -04:00
}
2013-10-28 19:58:59 -04:00
if err := runtime . RegisterLink ( container , child , parts [ "alias" ] ) ; err != nil {
2013-10-04 22:25:15 -04:00
return err
}
}
2013-10-28 22:19:31 -04:00
// After we load all the links into the runtime
// set them to nil on the hostconfig
hostConfig . Links = nil
2014-03-07 21:42:29 -05:00
if err := container . WriteHostConfig ( ) ; err != nil {
2013-10-28 22:19:31 -04:00
return err
}
2013-10-04 22:25:15 -04:00
}
2013-10-28 19:58:59 -04:00
return nil
}
2013-11-20 02:37:03 -05:00
func ( srv * Server ) ContainerStart ( job * engine . Job ) engine . Status {
2013-10-26 22:24:01 -04:00
if len ( job . Args ) < 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s container_id" , job . Name )
2013-10-26 22:24:01 -04:00
}
2014-03-30 01:12:43 -04:00
var (
name = job . Args [ 0 ]
runtime = srv . runtime
container = runtime . Get ( name )
)
2013-10-18 18:56:52 -04:00
2013-10-28 19:58:59 -04:00
if container == nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-10-28 19:58:59 -04:00
}
2013-10-26 22:24:01 -04:00
// If no environment was set, then no hostconfig was passed.
if len ( job . Environ ( ) ) > 0 {
2014-02-11 23:04:39 -05:00
hostConfig := runconfig . ContainerHostConfigFromJob ( job )
2013-11-12 19:36:20 -05:00
// Validate the HostConfig binds. Make sure that:
2013-11-13 14:25:55 -05:00
// 1) the source of a bind mount isn't /
// The bind mount "/:/foo" isn't allowed.
// 2) Check that the source exists
// The source to be bind mounted must exist.
for _ , bind := range hostConfig . Binds {
splitBind := strings . Split ( bind , ":" )
source := splitBind [ 0 ]
// refuse to bind mount "/" to the container
if source == "/" {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Invalid bind mount '%s' : source can't be '/'" , bind )
2013-11-13 14:25:55 -05:00
}
// ensure the source exists on the host
_ , err := os . Stat ( source )
if err != nil && os . IsNotExist ( err ) {
2013-11-11 16:29:56 -05:00
err = os . MkdirAll ( source , 0755 )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Could not create local directory '%s' for bind mount: %s!" , source , err . Error ( ) )
2013-11-11 16:29:56 -05:00
}
2013-11-13 14:25:55 -05:00
}
}
2013-10-26 22:24:01 -04:00
// Register any links from the host config before starting the container
2014-01-29 19:56:42 -05:00
if err := srv . RegisterLinks ( container , hostConfig ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-10-26 22:24:01 -04:00
}
2014-03-07 21:42:29 -05:00
container . SetHostConfig ( hostConfig )
2013-10-31 17:58:43 -04:00
container . ToDisk ( )
}
if err := container . Start ( ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot start container %s: %s" , name , err )
2013-10-04 22:25:15 -04:00
}
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "start" , container . ID , runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-10-04 22:25:15 -04:00
2013-11-20 02:37:03 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-11-16 22:00:16 -05:00
func ( srv * Server ) ContainerStop ( job * engine . Job ) engine . Status {
2013-12-11 18:36:50 -05:00
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER\n" , job . Name )
2013-11-16 22:00:16 -05:00
}
2014-01-30 21:21:59 -05:00
var (
name = job . Args [ 0 ]
t = 10
)
if job . EnvExists ( "t" ) {
t = job . GetenvInt ( "t" )
2013-11-16 22:00:16 -05:00
}
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2013-11-16 22:00:16 -05:00
if err := container . Stop ( int ( t ) ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Cannot stop container %s: %s\n" , name , err )
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
srv . LogEvent ( "stop" , container . ID , srv . runtime . Repositories ( ) . ImageName ( container . Image ) )
2013-05-06 05:31:22 -04:00
} else {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s\n" , name )
2013-05-06 05:31:22 -04:00
}
2013-11-16 22:00:16 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2013-11-24 20:05:59 -05:00
func ( srv * Server ) ContainerWait ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s" , job . Name )
2013-11-24 20:05:59 -05:00
}
name := job . Args [ 0 ]
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2013-11-24 20:05:59 -05:00
status := container . Wait ( )
job . Printf ( "%d\n" , status )
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Errorf ( "%s: no such container: %s" , job . Name , name )
2013-05-06 05:31:22 -04:00
}
2013-12-11 21:25:30 -05:00
func ( srv * Server ) ContainerResize ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 3 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Not enough arguments. Usage: %s CONTAINER HEIGHT WIDTH\n" , job . Name )
2013-12-11 21:25:30 -05:00
}
name := job . Args [ 0 ]
height , err := strconv . Atoi ( job . Args [ 1 ] )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-11 21:25:30 -05:00
}
width , err := strconv . Atoi ( job . Args [ 2 ] )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-11 21:25:30 -05:00
}
2013-05-23 22:33:28 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
2013-12-11 21:25:30 -05:00
if err := container . Resize ( height , width ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-12-11 21:25:30 -05:00
}
return engine . StatusOK
2013-05-23 22:33:28 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-05-23 22:33:28 -04:00
}
2014-01-08 17:05:05 -05:00
func ( srv * Server ) ContainerAttach ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 1 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER\n" , job . Name )
2014-01-08 17:05:05 -05:00
}
var (
name = job . Args [ 0 ]
logs = job . GetenvBool ( "logs" )
stream = job . GetenvBool ( "stream" )
stdin = job . GetenvBool ( "stdin" )
stdout = job . GetenvBool ( "stdout" )
stderr = job . GetenvBool ( "stderr" )
)
2013-05-08 02:32:17 -04:00
container := srv . runtime . Get ( name )
if container == nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-05-08 02:32:17 -04:00
}
2013-09-11 14:35:09 -04:00
2013-05-08 02:32:17 -04:00
//logs
if logs {
2013-07-15 12:17:58 -04:00
cLog , err := container . ReadLog ( "json" )
2013-07-18 09:25:47 -04:00
if err != nil && os . IsNotExist ( err ) {
// Legacy logs
2014-01-08 17:05:05 -05:00
utils . Debugf ( "Old logs format" )
2013-07-18 09:25:47 -04:00
if stdout {
cLog , err := container . ReadLog ( "stdout" )
if err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Error reading logs (stdout): %s" , err )
2014-01-08 17:05:05 -05:00
} else if _ , err := io . Copy ( job . Stdout , cLog ) ; err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Error streaming logs (stdout): %s" , err )
2013-07-18 09:25:47 -04:00
}
2013-05-06 05:31:22 -04:00
}
2013-07-18 09:25:47 -04:00
if stderr {
cLog , err := container . ReadLog ( "stderr" )
if err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Error reading logs (stderr): %s" , err )
2014-01-08 17:05:05 -05:00
} else if _ , err := io . Copy ( job . Stderr , cLog ) ; err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Error streaming logs (stderr): %s" , err )
2013-07-18 09:25:47 -04:00
}
2013-05-06 05:31:22 -04:00
}
2013-07-18 09:25:47 -04:00
} else if err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Error reading logs (json): %s" , err )
2013-07-18 09:25:47 -04:00
} else {
dec := json . NewDecoder ( cLog )
for {
2013-09-26 18:59:02 -04:00
l := & utils . JSONLog { }
if err := dec . Decode ( l ) ; err == io . EOF {
2013-07-18 09:25:47 -04:00
break
} else if err != nil {
2013-10-08 03:54:47 -04:00
utils . Errorf ( "Error streaming logs: %s" , err )
2013-07-18 09:25:47 -04:00
break
}
2013-09-26 18:59:02 -04:00
if l . Stream == "stdout" && stdout {
2014-01-08 17:05:05 -05:00
fmt . Fprintf ( job . Stdout , "%s" , l . Log )
2013-07-18 09:25:47 -04:00
}
2013-09-26 18:59:02 -04:00
if l . Stream == "stderr" && stderr {
2014-01-08 17:05:05 -05:00
fmt . Fprintf ( job . Stderr , "%s" , l . Log )
2013-07-18 09:25:47 -04:00
}
2013-05-06 05:31:22 -04:00
}
}
2013-05-08 02:32:17 -04:00
}
2013-05-06 05:31:22 -04:00
2013-05-08 02:32:17 -04:00
//stream
if stream {
2013-11-21 15:21:03 -05:00
if container . State . IsGhost ( ) {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Impossible to attach to a ghost container" )
2013-05-08 02:32:17 -04:00
}
2013-05-06 05:31:22 -04:00
2013-05-08 02:32:17 -04:00
var (
cStdin io . ReadCloser
cStdout , cStderr io . Writer
cStdinCloser io . Closer
)
2013-05-06 05:31:22 -04:00
2013-05-08 02:32:17 -04:00
if stdin {
r , w := io . Pipe ( )
go func ( ) {
defer w . Close ( )
2013-05-14 18:37:35 -04:00
defer utils . Debugf ( "Closing buffered stdin pipe" )
2014-01-08 17:05:05 -05:00
io . Copy ( w , job . Stdin )
2013-05-08 02:32:17 -04:00
} ( )
cStdin = r
2014-01-08 17:05:05 -05:00
cStdinCloser = job . Stdin
2013-05-08 02:32:17 -04:00
}
if stdout {
2014-01-08 17:05:05 -05:00
cStdout = job . Stdout
2013-05-08 02:32:17 -04:00
}
if stderr {
2014-01-08 17:05:05 -05:00
cStderr = job . Stderr
2013-05-08 02:32:17 -04:00
}
2013-05-07 17:15:42 -04:00
2013-05-08 02:32:17 -04:00
<- container . Attach ( cStdin , cStdinCloser , cStdout , cStderr )
// If we are in stdinonce mode, wait for the process to end
// otherwise, simply return
if container . Config . StdinOnce && ! container . Config . Tty {
container . Wait ( )
2013-05-06 05:31:22 -04:00
}
}
2014-01-08 17:05:05 -05:00
return engine . StatusOK
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
func ( srv * Server ) ContainerInspect ( name string ) ( * runtime . Container , error ) {
2013-05-06 05:31:22 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
return container , nil
}
return nil , fmt . Errorf ( "No such container: %s" , name )
}
2014-03-07 20:36:47 -05:00
func ( srv * Server ) ImageInspect ( name string ) ( * image . Image , error ) {
2014-03-07 21:42:29 -05:00
if image , err := srv . runtime . Repositories ( ) . LookupImage ( name ) ; err == nil && image != nil {
2013-05-06 05:31:22 -04:00
return image , nil
}
return nil , fmt . Errorf ( "No such image: %s" , name )
}
2014-01-20 18:15:00 -05:00
func ( srv * Server ) JobInspect ( job * engine . Job ) engine . Status {
// TODO: deprecate KIND/conflict
if n := len ( job . Args ) ; n != 2 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER|IMAGE KIND" , job . Name )
2014-01-15 20:09:05 -05:00
}
2014-01-20 18:15:00 -05:00
var (
name = job . Args [ 0 ]
kind = job . Args [ 1 ]
object interface { }
conflict = job . GetenvBool ( "conflict" ) //should the job detect conflict between containers and images
image , errImage = srv . ImageInspect ( name )
container , errContainer = srv . ContainerInspect ( name )
)
if conflict && image != nil && container != nil {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Conflict between containers and images" )
2014-01-15 20:09:05 -05:00
}
2014-01-20 18:15:00 -05:00
switch kind {
case "image" :
if errImage != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( errImage )
2014-01-20 18:15:00 -05:00
}
object = image
case "container" :
if errContainer != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( errContainer )
2014-01-20 18:15:00 -05:00
}
object = & struct {
2014-03-07 21:42:29 -05:00
* runtime . Container
2014-02-11 23:04:39 -05:00
HostConfig * runconfig . HostConfig
2014-03-07 21:42:29 -05:00
} { container , container . HostConfig ( ) }
2014-01-20 18:15:00 -05:00
default :
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Unknown kind: %s" , kind )
2014-01-20 18:15:00 -05:00
}
b , err := json . Marshal ( object )
2014-01-15 20:09:05 -05:00
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2014-01-15 20:09:05 -05:00
}
job . Stdout . Write ( b )
return engine . StatusOK
}
2014-01-09 19:37:56 -05:00
func ( srv * Server ) ContainerCopy ( job * engine . Job ) engine . Status {
if len ( job . Args ) != 2 {
2014-01-27 19:27:02 -05:00
return job . Errorf ( "Usage: %s CONTAINER RESOURCE\n" , job . Name )
2014-01-09 19:37:56 -05:00
}
var (
name = job . Args [ 0 ]
resource = job . Args [ 1 ]
)
2013-07-17 00:07:41 -04:00
if container := srv . runtime . Get ( name ) ; container != nil {
data , err := container . Copy ( resource )
if err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-07-17 00:07:41 -04:00
}
2014-02-11 09:21:33 -05:00
defer data . Close ( )
2013-07-17 00:07:41 -04:00
2014-01-09 19:37:56 -05:00
if _ , err := io . Copy ( job . Stdout , data ) ; err != nil {
2014-01-27 19:27:02 -05:00
return job . Error ( err )
2013-07-17 00:07:41 -04:00
}
2014-01-09 19:37:56 -05:00
return engine . StatusOK
2013-07-17 00:07:41 -04:00
}
2014-01-27 19:27:02 -05:00
return job . Errorf ( "No such container: %s" , name )
2013-07-17 00:07:41 -04:00
}
2014-03-07 18:22:23 -05:00
func NewServer ( eng * engine . Engine , config * daemonconfig . Config ) ( * Server , error ) {
2014-03-07 21:42:29 -05:00
runtime , err := runtime . NewRuntime ( config , eng )
2013-05-06 05:31:22 -04:00
if err != nil {
return nil , err
}
srv := & Server {
2013-10-26 22:24:01 -04:00
Eng : eng ,
2013-06-17 19:10:00 -04:00
runtime : runtime ,
2013-11-20 16:51:05 -05:00
pullingPool : make ( map [ string ] chan struct { } ) ,
pushingPool : make ( map [ string ] chan struct { } ) ,
2013-07-12 12:29:23 -04:00
events : make ( [ ] utils . JSONMessage , 0 , 64 ) , //only keeps the 64 last events
listeners : make ( map [ string ] chan utils . JSONMessage ) ,
2014-03-07 14:51:28 -05:00
running : true ,
2013-05-06 05:31:22 -04:00
}
2014-03-07 21:42:29 -05:00
runtime . SetServer ( srv )
2013-05-06 05:31:22 -04:00
return srv , nil
}
2013-08-22 15:15:31 -04:00
func ( srv * Server ) HTTPRequestFactory ( metaHeaders map [ string ] [ ] string ) * utils . HTTPRequestFactory {
2013-12-08 02:35:24 -05:00
httpVersion := make ( [ ] utils . VersionInfo , 0 , 4 )
2014-03-10 17:23:29 -04:00
httpVersion = append ( httpVersion , & simpleVersionInfo { "docker" , dockerversion . VERSION } )
httpVersion = append ( httpVersion , & simpleVersionInfo { "go" , goruntime . Version ( ) } )
httpVersion = append ( httpVersion , & simpleVersionInfo { "git-commit" , dockerversion . GITCOMMIT } )
if kernelVersion , err := utils . GetKernelVersion ( ) ; err == nil {
httpVersion = append ( httpVersion , & simpleVersionInfo { "kernel" , kernelVersion . String ( ) } )
}
httpVersion = append ( httpVersion , & simpleVersionInfo { "os" , goruntime . GOOS } )
httpVersion = append ( httpVersion , & simpleVersionInfo { "arch" , goruntime . GOARCH } )
2013-12-08 02:35:24 -05:00
ud := utils . NewHTTPUserAgentDecorator ( httpVersion ... )
2013-10-22 14:49:13 -04:00
md := & utils . HTTPMetaHeadersDecorator {
Headers : metaHeaders ,
2013-08-02 03:30:45 -04:00
}
2013-10-22 14:49:13 -04:00
factory := utils . NewHTTPRequestFactory ( ud , md )
return factory
2013-08-02 03:30:45 -04:00
}
2013-11-14 01:08:08 -05:00
func ( srv * Server ) LogEvent ( action , id , from string ) * utils . JSONMessage {
2013-11-21 19:41:41 -05:00
now := time . Now ( ) . UTC ( ) . Unix ( )
2013-08-12 07:50:03 -04:00
jm := utils . JSONMessage { Status : action , ID : id , From : from , Time : now }
2013-11-25 13:58:17 -05:00
srv . AddEvent ( jm )
2013-07-12 12:29:23 -04:00
for _ , c := range srv . listeners {
2013-07-18 10:35:14 -04:00
select { // non blocking channel
case c <- jm :
default :
}
2013-07-10 08:55:05 -04:00
}
2013-11-14 01:08:08 -05:00
return & jm
2013-07-10 08:55:05 -04:00
}
2013-11-25 13:58:17 -05:00
func ( srv * Server ) AddEvent ( jm utils . JSONMessage ) {
srv . Lock ( )
defer srv . Unlock ( )
srv . events = append ( srv . events , jm )
}
func ( srv * Server ) GetEvents ( ) [ ] utils . JSONMessage {
srv . RLock ( )
defer srv . RUnlock ( )
return srv . events
}
2014-03-07 14:51:28 -05:00
func ( srv * Server ) SetRunning ( status bool ) {
srv . Lock ( )
defer srv . Unlock ( )
srv . running = status
}
func ( srv * Server ) IsRunning ( ) bool {
srv . RLock ( )
defer srv . RUnlock ( )
return srv . running
}
func ( srv * Server ) Close ( ) error {
2014-03-11 14:39:28 -04:00
if srv == nil {
return nil
}
2014-03-07 14:51:28 -05:00
srv . SetRunning ( false )
2014-03-11 14:39:28 -04:00
if srv . runtime == nil {
return nil
}
2014-03-07 14:51:28 -05:00
return srv . runtime . Close ( )
}
2013-05-06 05:31:22 -04:00
type Server struct {
2013-11-25 13:58:17 -05:00
sync . RWMutex
2014-03-07 21:42:29 -05:00
runtime * runtime . Runtime
2013-11-20 16:51:05 -05:00
pullingPool map [ string ] chan struct { }
pushingPool map [ string ] chan struct { }
2013-07-12 12:29:23 -04:00
events [ ] utils . JSONMessage
listeners map [ string ] chan utils . JSONMessage
2013-10-26 22:24:01 -04:00
Eng * engine . Engine
2014-03-07 14:51:28 -05:00
running bool
2013-05-06 05:31:22 -04:00
}