daemon.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. package daemon
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net"
  11. "net/http"
  12. "net/http/httputil"
  13. "net/url"
  14. "os"
  15. "os/exec"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/docker/docker/api/types/events"
  21. "github.com/docker/docker/opts"
  22. "github.com/docker/docker/pkg/integration"
  23. "github.com/docker/docker/pkg/integration/checker"
  24. icmd "github.com/docker/docker/pkg/integration/cmd"
  25. "github.com/docker/docker/pkg/ioutils"
  26. "github.com/docker/docker/pkg/stringid"
  27. "github.com/docker/go-connections/sockets"
  28. "github.com/docker/go-connections/tlsconfig"
  29. "github.com/go-check/check"
  30. )
  31. // SockRoot holds the path of the default docker integration daemon socket
  32. var SockRoot = filepath.Join(os.TempDir(), "docker-integration")
  33. // Daemon represents a Docker daemon for the testing framework.
  34. type Daemon struct {
  35. GlobalFlags []string
  36. Root string
  37. Folder string
  38. Wait chan error
  39. UseDefaultHost bool
  40. UseDefaultTLSHost bool
  41. // FIXME(vdemeester) either should be used everywhere (do not return error) or nowhere,
  42. // so I think we should remove it or use it for everything
  43. c *check.C
  44. id string
  45. logFile *os.File
  46. stdin io.WriteCloser
  47. stdout, stderr io.ReadCloser
  48. cmd *exec.Cmd
  49. storageDriver string
  50. userlandProxy bool
  51. execRoot string
  52. experimental bool
  53. dockerBinary string
  54. dockerdBinary string
  55. }
  56. // Config holds docker daemon integration configuration
  57. type Config struct {
  58. Experimental bool
  59. }
  60. type clientConfig struct {
  61. transport *http.Transport
  62. scheme string
  63. addr string
  64. }
  65. // New returns a Daemon instance to be used for testing.
  66. // This will create a directory such as d123456789 in the folder specified by $DEST.
  67. // The daemon will not automatically start.
  68. func New(c *check.C, dockerBinary string, dockerdBinary string, config Config) *Daemon {
  69. dest := os.Getenv("DEST")
  70. c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
  71. err := os.MkdirAll(SockRoot, 0700)
  72. c.Assert(err, checker.IsNil, check.Commentf("could not create daemon socket root"))
  73. id := fmt.Sprintf("d%s", stringid.TruncateID(stringid.GenerateRandomID()))
  74. dir := filepath.Join(dest, id)
  75. daemonFolder, err := filepath.Abs(dir)
  76. c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
  77. daemonRoot := filepath.Join(daemonFolder, "root")
  78. c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
  79. userlandProxy := true
  80. if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
  81. if val, err := strconv.ParseBool(env); err != nil {
  82. userlandProxy = val
  83. }
  84. }
  85. return &Daemon{
  86. id: id,
  87. c: c,
  88. Folder: daemonFolder,
  89. Root: daemonRoot,
  90. storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
  91. userlandProxy: userlandProxy,
  92. execRoot: filepath.Join(os.TempDir(), "docker-execroot", id),
  93. dockerBinary: dockerBinary,
  94. dockerdBinary: dockerdBinary,
  95. experimental: config.Experimental,
  96. }
  97. }
  98. // RootDir returns the root directory of the daemon.
  99. func (d *Daemon) RootDir() string {
  100. return d.Root
  101. }
  102. // ID returns the generated id of the daemon
  103. func (d *Daemon) ID() string {
  104. return d.id
  105. }
  106. // StorageDriver returns the configured storage driver of the daemon
  107. func (d *Daemon) StorageDriver() string {
  108. return d.storageDriver
  109. }
  110. // CleanupExecRoot cleans the daemon exec root (network namespaces, ...)
  111. func (d *Daemon) CleanupExecRoot(c *check.C) {
  112. cleanupExecRoot(c, d.execRoot)
  113. }
  114. func (d *Daemon) getClientConfig() (*clientConfig, error) {
  115. var (
  116. transport *http.Transport
  117. scheme string
  118. addr string
  119. proto string
  120. )
  121. if d.UseDefaultTLSHost {
  122. option := &tlsconfig.Options{
  123. CAFile: "fixtures/https/ca.pem",
  124. CertFile: "fixtures/https/client-cert.pem",
  125. KeyFile: "fixtures/https/client-key.pem",
  126. }
  127. tlsConfig, err := tlsconfig.Client(*option)
  128. if err != nil {
  129. return nil, err
  130. }
  131. transport = &http.Transport{
  132. TLSClientConfig: tlsConfig,
  133. }
  134. addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
  135. scheme = "https"
  136. proto = "tcp"
  137. } else if d.UseDefaultHost {
  138. addr = opts.DefaultUnixSocket
  139. proto = "unix"
  140. scheme = "http"
  141. transport = &http.Transport{}
  142. } else {
  143. addr = d.sockPath()
  144. proto = "unix"
  145. scheme = "http"
  146. transport = &http.Transport{}
  147. }
  148. d.c.Assert(sockets.ConfigureTransport(transport, proto, addr), check.IsNil)
  149. return &clientConfig{
  150. transport: transport,
  151. scheme: scheme,
  152. addr: addr,
  153. }, nil
  154. }
  155. // Start will start the daemon and return once it is ready to receive requests.
  156. // You can specify additional daemon flags.
  157. func (d *Daemon) Start(args ...string) error {
  158. logFile, err := os.OpenFile(filepath.Join(d.Folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  159. d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.Folder))
  160. return d.StartWithLogFile(logFile, args...)
  161. }
  162. // StartWithLogFile will start the daemon and attach its streams to a given file.
  163. func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
  164. dockerdBinary, err := exec.LookPath(d.dockerdBinary)
  165. d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
  166. args := append(d.GlobalFlags,
  167. "--containerd", "/var/run/docker/libcontainerd/docker-containerd.sock",
  168. "--graph", d.Root,
  169. "--exec-root", d.execRoot,
  170. "--pidfile", fmt.Sprintf("%s/docker.pid", d.Folder),
  171. fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
  172. )
  173. if d.experimental {
  174. args = append(args, "--experimental", "--init")
  175. }
  176. if !(d.UseDefaultHost || d.UseDefaultTLSHost) {
  177. args = append(args, []string{"--host", d.Sock()}...)
  178. }
  179. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  180. args = append(args, []string{"--userns-remap", root}...)
  181. }
  182. // If we don't explicitly set the log-level or debug flag(-D) then
  183. // turn on debug mode
  184. foundLog := false
  185. foundSd := false
  186. for _, a := range providedArgs {
  187. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
  188. foundLog = true
  189. }
  190. if strings.Contains(a, "--storage-driver") {
  191. foundSd = true
  192. }
  193. }
  194. if !foundLog {
  195. args = append(args, "--debug")
  196. }
  197. if d.storageDriver != "" && !foundSd {
  198. args = append(args, "--storage-driver", d.storageDriver)
  199. }
  200. args = append(args, providedArgs...)
  201. d.cmd = exec.Command(dockerdBinary, args...)
  202. d.cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1")
  203. d.cmd.Stdout = out
  204. d.cmd.Stderr = out
  205. d.logFile = out
  206. if err := d.cmd.Start(); err != nil {
  207. return fmt.Errorf("[%s] could not start daemon container: %v", d.id, err)
  208. }
  209. wait := make(chan error)
  210. go func() {
  211. wait <- d.cmd.Wait()
  212. d.c.Logf("[%s] exiting daemon", d.id)
  213. close(wait)
  214. }()
  215. d.Wait = wait
  216. tick := time.Tick(500 * time.Millisecond)
  217. // make sure daemon is ready to receive requests
  218. startTime := time.Now().Unix()
  219. for {
  220. d.c.Logf("[%s] waiting for daemon to start", d.id)
  221. if time.Now().Unix()-startTime > 5 {
  222. // After 5 seconds, give up
  223. return fmt.Errorf("[%s] Daemon exited and never started", d.id)
  224. }
  225. select {
  226. case <-time.After(2 * time.Second):
  227. return fmt.Errorf("[%s] timeout: daemon does not respond", d.id)
  228. case <-tick:
  229. clientConfig, err := d.getClientConfig()
  230. if err != nil {
  231. return err
  232. }
  233. client := &http.Client{
  234. Transport: clientConfig.transport,
  235. }
  236. req, err := http.NewRequest("GET", "/_ping", nil)
  237. d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not create new request", d.id))
  238. req.URL.Host = clientConfig.addr
  239. req.URL.Scheme = clientConfig.scheme
  240. resp, err := client.Do(req)
  241. if err != nil {
  242. continue
  243. }
  244. if resp.StatusCode != http.StatusOK {
  245. d.c.Logf("[%s] received status != 200 OK: %s", d.id, resp.Status)
  246. }
  247. d.c.Logf("[%s] daemon started", d.id)
  248. d.Root, err = d.queryRootDir()
  249. if err != nil {
  250. return fmt.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
  251. }
  252. return nil
  253. case <-d.Wait:
  254. return fmt.Errorf("[%s] Daemon exited during startup", d.id)
  255. }
  256. }
  257. }
  258. // StartWithBusybox will first start the daemon with Daemon.Start()
  259. // then save the busybox image from the main daemon and load it into this Daemon instance.
  260. func (d *Daemon) StartWithBusybox(arg ...string) error {
  261. if err := d.Start(arg...); err != nil {
  262. return err
  263. }
  264. return d.LoadBusybox()
  265. }
  266. // Kill will send a SIGKILL to the daemon
  267. func (d *Daemon) Kill() error {
  268. if d.cmd == nil || d.Wait == nil {
  269. return errors.New("daemon not started")
  270. }
  271. defer func() {
  272. d.logFile.Close()
  273. d.cmd = nil
  274. }()
  275. if err := d.cmd.Process.Kill(); err != nil {
  276. d.c.Logf("Could not kill daemon: %v", err)
  277. return err
  278. }
  279. if err := os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder)); err != nil {
  280. return err
  281. }
  282. return nil
  283. }
  284. // Pid returns the pid of the daemon
  285. func (d *Daemon) Pid() int {
  286. return d.cmd.Process.Pid
  287. }
  288. // Interrupt stops the daemon by sending it an Interrupt signal
  289. func (d *Daemon) Interrupt() error {
  290. return d.Signal(os.Interrupt)
  291. }
  292. // Signal sends the specified signal to the daemon if running
  293. func (d *Daemon) Signal(signal os.Signal) error {
  294. if d.cmd == nil || d.Wait == nil {
  295. return errors.New("daemon not started")
  296. }
  297. return d.cmd.Process.Signal(signal)
  298. }
  299. // DumpStackAndQuit sends SIGQUIT to the daemon, which triggers it to dump its
  300. // stack to its log file and exit
  301. // This is used primarily for gathering debug information on test timeout
  302. func (d *Daemon) DumpStackAndQuit() {
  303. if d.cmd == nil || d.cmd.Process == nil {
  304. return
  305. }
  306. SignalDaemonDump(d.cmd.Process.Pid)
  307. }
  308. // Stop will send a SIGINT every second and wait for the daemon to stop.
  309. // If it timeouts, a SIGKILL is sent.
  310. // Stop will not delete the daemon directory. If a purged daemon is needed,
  311. // instantiate a new one with NewDaemon.
  312. func (d *Daemon) Stop() error {
  313. if d.cmd == nil || d.Wait == nil {
  314. return errors.New("daemon not started")
  315. }
  316. defer func() {
  317. d.logFile.Close()
  318. d.cmd = nil
  319. }()
  320. i := 1
  321. tick := time.Tick(time.Second)
  322. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  323. return fmt.Errorf("could not send signal: %v", err)
  324. }
  325. out1:
  326. for {
  327. select {
  328. case err := <-d.Wait:
  329. return err
  330. case <-time.After(20 * time.Second):
  331. // time for stopping jobs and run onShutdown hooks
  332. d.c.Logf("timeout: %v", d.id)
  333. break out1
  334. }
  335. }
  336. out2:
  337. for {
  338. select {
  339. case err := <-d.Wait:
  340. return err
  341. case <-tick:
  342. i++
  343. if i > 5 {
  344. d.c.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  345. break out2
  346. }
  347. d.c.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  348. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  349. return fmt.Errorf("could not send signal: %v", err)
  350. }
  351. }
  352. }
  353. if err := d.cmd.Process.Kill(); err != nil {
  354. d.c.Logf("Could not kill daemon: %v", err)
  355. return err
  356. }
  357. if err := os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder)); err != nil {
  358. return err
  359. }
  360. return nil
  361. }
  362. // Restart will restart the daemon by first stopping it and then starting it.
  363. func (d *Daemon) Restart(arg ...string) error {
  364. d.Stop()
  365. // in the case of tests running a user namespace-enabled daemon, we have resolved
  366. // d.Root to be the actual final path of the graph dir after the "uid.gid" of
  367. // remapped root is added--we need to subtract it from the path before calling
  368. // start or else we will continue making subdirectories rather than truly restarting
  369. // with the same location/root:
  370. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  371. d.Root = filepath.Dir(d.Root)
  372. }
  373. return d.Start(arg...)
  374. }
  375. // LoadBusybox will load the stored busybox into a newly started daemon
  376. func (d *Daemon) LoadBusybox() error {
  377. bb := filepath.Join(d.Folder, "busybox.tar")
  378. if _, err := os.Stat(bb); err != nil {
  379. if !os.IsNotExist(err) {
  380. return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
  381. }
  382. // saving busybox image from main daemon
  383. if out, err := exec.Command(d.dockerBinary, "save", "--output", bb, "busybox:latest").CombinedOutput(); err != nil {
  384. imagesOut, _ := exec.Command(d.dockerBinary, "images", "--format", "{{ .Repository }}:{{ .Tag }}").CombinedOutput()
  385. return fmt.Errorf("could not save busybox image: %s\n%s", string(out), strings.TrimSpace(string(imagesOut)))
  386. }
  387. }
  388. // loading busybox image to this daemon
  389. if out, err := d.Cmd("load", "--input", bb); err != nil {
  390. return fmt.Errorf("could not load busybox image: %s", out)
  391. }
  392. if err := os.Remove(bb); err != nil {
  393. d.c.Logf("could not remove %s: %v", bb, err)
  394. }
  395. return nil
  396. }
  397. func (d *Daemon) queryRootDir() (string, error) {
  398. // update daemon root by asking /info endpoint (to support user
  399. // namespaced daemon with root remapped uid.gid directory)
  400. clientConfig, err := d.getClientConfig()
  401. if err != nil {
  402. return "", err
  403. }
  404. client := &http.Client{
  405. Transport: clientConfig.transport,
  406. }
  407. req, err := http.NewRequest("GET", "/info", nil)
  408. if err != nil {
  409. return "", err
  410. }
  411. req.Header.Set("Content-Type", "application/json")
  412. req.URL.Host = clientConfig.addr
  413. req.URL.Scheme = clientConfig.scheme
  414. resp, err := client.Do(req)
  415. if err != nil {
  416. return "", err
  417. }
  418. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  419. return resp.Body.Close()
  420. })
  421. type Info struct {
  422. DockerRootDir string
  423. }
  424. var b []byte
  425. var i Info
  426. b, err = integration.ReadBody(body)
  427. if err == nil && resp.StatusCode == http.StatusOK {
  428. // read the docker root dir
  429. if err = json.Unmarshal(b, &i); err == nil {
  430. return i.DockerRootDir, nil
  431. }
  432. }
  433. return "", err
  434. }
  435. // Sock returns the socket path of the daemon
  436. func (d *Daemon) Sock() string {
  437. return fmt.Sprintf("unix://" + d.sockPath())
  438. }
  439. func (d *Daemon) sockPath() string {
  440. return filepath.Join(SockRoot, d.id+".sock")
  441. }
  442. // WaitRun waits for a container to be running for 10s
  443. func (d *Daemon) WaitRun(contID string) error {
  444. args := []string{"--host", d.Sock()}
  445. return WaitInspectWithArgs(d.dockerBinary, contID, "{{.State.Running}}", "true", 10*time.Second, args...)
  446. }
  447. // GetBaseDeviceSize returns the base device size of the daemon
  448. func (d *Daemon) GetBaseDeviceSize(c *check.C) int64 {
  449. infoCmdOutput, _, err := integration.RunCommandPipelineWithOutput(
  450. exec.Command(d.dockerBinary, "-H", d.Sock(), "info"),
  451. exec.Command("grep", "Base Device Size"),
  452. )
  453. c.Assert(err, checker.IsNil)
  454. basesizeSlice := strings.Split(infoCmdOutput, ":")
  455. basesize := strings.Trim(basesizeSlice[1], " ")
  456. basesize = strings.Trim(basesize, "\n")[:len(basesize)-3]
  457. basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
  458. c.Assert(err, checker.IsNil)
  459. basesizeBytes := int64(basesizeFloat) * (1024 * 1024 * 1024)
  460. return basesizeBytes
  461. }
  462. // Cmd will execute a docker CLI command against this Daemon.
  463. // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
  464. func (d *Daemon) Cmd(args ...string) (string, error) {
  465. b, err := d.Command(args...).CombinedOutput()
  466. return string(b), err
  467. }
  468. // Command will create a docker CLI command against this Daeomn.
  469. func (d *Daemon) Command(args ...string) *exec.Cmd {
  470. return exec.Command(d.dockerBinary, d.PrependHostArg(args)...)
  471. }
  472. // PrependHostArg prepend the specified arguments by the daemon host flags
  473. func (d *Daemon) PrependHostArg(args []string) []string {
  474. for _, arg := range args {
  475. if arg == "--host" || arg == "-H" {
  476. return args
  477. }
  478. }
  479. return append([]string{"--host", d.Sock()}, args...)
  480. }
  481. // SockRequest executes a socket request on a daemon and returns statuscode and output.
  482. func (d *Daemon) SockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
  483. jsonData := bytes.NewBuffer(nil)
  484. if err := json.NewEncoder(jsonData).Encode(data); err != nil {
  485. return -1, nil, err
  486. }
  487. res, body, err := d.SockRequestRaw(method, endpoint, jsonData, "application/json")
  488. if err != nil {
  489. return -1, nil, err
  490. }
  491. b, err := integration.ReadBody(body)
  492. return res.StatusCode, b, err
  493. }
  494. // SockRequestRaw executes a socket request on a daemon and returns an http
  495. // response and a reader for the output data.
  496. func (d *Daemon) SockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
  497. return SockRequestRawToDaemon(method, endpoint, data, ct, d.Sock())
  498. }
  499. // LogFileName returns the path the the daemon's log file
  500. func (d *Daemon) LogFileName() string {
  501. return d.logFile.Name()
  502. }
  503. // GetIDByName returns the ID of an object (container, volume, …) given its name
  504. func (d *Daemon) GetIDByName(name string) (string, error) {
  505. return d.inspectFieldWithError(name, "Id")
  506. }
  507. // ActiveContainers returns the list of ids of the currently running containers
  508. func (d *Daemon) ActiveContainers() (ids []string) {
  509. // FIXME(vdemeester) shouldn't ignore the error
  510. out, _ := d.Cmd("ps", "-q")
  511. for _, id := range strings.Split(out, "\n") {
  512. if id = strings.TrimSpace(id); id != "" {
  513. ids = append(ids, id)
  514. }
  515. }
  516. return
  517. }
  518. // ReadLogFile returns the content of the daemon log file
  519. func (d *Daemon) ReadLogFile() ([]byte, error) {
  520. return ioutil.ReadFile(d.logFile.Name())
  521. }
  522. func (d *Daemon) inspectFilter(name, filter string) (string, error) {
  523. format := fmt.Sprintf("{{%s}}", filter)
  524. out, err := d.Cmd("inspect", "-f", format, name)
  525. if err != nil {
  526. return "", fmt.Errorf("failed to inspect %s: %s", name, out)
  527. }
  528. return strings.TrimSpace(out), nil
  529. }
  530. func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
  531. return d.inspectFilter(name, fmt.Sprintf(".%s", field))
  532. }
  533. // FindContainerIP returns the ip of the specified container
  534. // FIXME(vdemeester) should probably erroring out
  535. func (d *Daemon) FindContainerIP(id string) string {
  536. out, err := d.Cmd("inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'"), id)
  537. if err != nil {
  538. d.c.Log(err)
  539. }
  540. return strings.Trim(out, " \r\n'")
  541. }
  542. // BuildImageWithOut builds an image with the specified dockerfile and options and returns the output
  543. func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) {
  544. buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...)
  545. result := icmd.RunCmd(icmd.Cmd{
  546. Command: buildCmd.Args,
  547. Env: buildCmd.Env,
  548. Dir: buildCmd.Dir,
  549. Stdin: buildCmd.Stdin,
  550. Stdout: buildCmd.Stdout,
  551. })
  552. return result.Combined(), result.ExitCode, result.Error
  553. }
  554. // CheckActiveContainerCount returns the number of active containers
  555. // FIXME(vdemeester) should re-use ActivateContainers in some way
  556. func (d *Daemon) CheckActiveContainerCount(c *check.C) (interface{}, check.CommentInterface) {
  557. out, err := d.Cmd("ps", "-q")
  558. c.Assert(err, checker.IsNil)
  559. if len(strings.TrimSpace(out)) == 0 {
  560. return 0, nil
  561. }
  562. return len(strings.Split(strings.TrimSpace(out), "\n")), check.Commentf("output: %q", string(out))
  563. }
  564. // ReloadConfig asks the daemon to reload its configuration
  565. func (d *Daemon) ReloadConfig() error {
  566. if d.cmd == nil || d.cmd.Process == nil {
  567. return fmt.Errorf("daemon is not running")
  568. }
  569. errCh := make(chan error)
  570. started := make(chan struct{})
  571. go func() {
  572. _, body, err := SockRequestRawToDaemon("GET", "/events", nil, "", d.Sock())
  573. close(started)
  574. if err != nil {
  575. errCh <- err
  576. }
  577. defer body.Close()
  578. dec := json.NewDecoder(body)
  579. for {
  580. var e events.Message
  581. if err := dec.Decode(&e); err != nil {
  582. errCh <- err
  583. return
  584. }
  585. if e.Type != events.DaemonEventType {
  586. continue
  587. }
  588. if e.Action != "reload" {
  589. continue
  590. }
  591. close(errCh) // notify that we are done
  592. return
  593. }
  594. }()
  595. <-started
  596. if err := signalDaemonReload(d.cmd.Process.Pid); err != nil {
  597. return fmt.Errorf("error signaling daemon reload: %v", err)
  598. }
  599. select {
  600. case err := <-errCh:
  601. if err != nil {
  602. return fmt.Errorf("error waiting for daemon reload event: %v", err)
  603. }
  604. case <-time.After(30 * time.Second):
  605. return fmt.Errorf("timeout waiting for daemon reload event")
  606. }
  607. return nil
  608. }
  609. // WaitInspectWithArgs waits for the specified expression to be equals to the specified expected string in the given time.
  610. // FIXME(vdemeester) Attach this to the Daemon struct
  611. func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time.Duration, arg ...string) error {
  612. after := time.After(timeout)
  613. args := append(arg, "inspect", "-f", expr, name)
  614. for {
  615. result := icmd.RunCommand(dockerBinary, args...)
  616. if result.Error != nil {
  617. if !strings.Contains(result.Stderr(), "No such") {
  618. return fmt.Errorf("error executing docker inspect: %v\n%s",
  619. result.Stderr(), result.Stdout())
  620. }
  621. select {
  622. case <-after:
  623. return result.Error
  624. default:
  625. time.Sleep(10 * time.Millisecond)
  626. continue
  627. }
  628. }
  629. out := strings.TrimSpace(result.Stdout())
  630. if out == expected {
  631. break
  632. }
  633. select {
  634. case <-after:
  635. return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
  636. default:
  637. }
  638. time.Sleep(100 * time.Millisecond)
  639. }
  640. return nil
  641. }
  642. // SockRequestRawToDaemon creates an http request against the specified daemon socket
  643. // FIXME(vdemeester) attach this to daemon ?
  644. func SockRequestRawToDaemon(method, endpoint string, data io.Reader, ct, daemon string) (*http.Response, io.ReadCloser, error) {
  645. req, client, err := newRequestClient(method, endpoint, data, ct, daemon)
  646. if err != nil {
  647. return nil, nil, err
  648. }
  649. resp, err := client.Do(req)
  650. if err != nil {
  651. client.Close()
  652. return nil, nil, err
  653. }
  654. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  655. defer resp.Body.Close()
  656. return client.Close()
  657. })
  658. return resp, body, nil
  659. }
  660. func getTLSConfig() (*tls.Config, error) {
  661. dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
  662. if dockerCertPath == "" {
  663. return nil, fmt.Errorf("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
  664. }
  665. option := &tlsconfig.Options{
  666. CAFile: filepath.Join(dockerCertPath, "ca.pem"),
  667. CertFile: filepath.Join(dockerCertPath, "cert.pem"),
  668. KeyFile: filepath.Join(dockerCertPath, "key.pem"),
  669. }
  670. tlsConfig, err := tlsconfig.Client(*option)
  671. if err != nil {
  672. return nil, err
  673. }
  674. return tlsConfig, nil
  675. }
  676. // SockConn opens a connection on the specified socket
  677. func SockConn(timeout time.Duration, daemon string) (net.Conn, error) {
  678. daemonURL, err := url.Parse(daemon)
  679. if err != nil {
  680. return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
  681. }
  682. var c net.Conn
  683. switch daemonURL.Scheme {
  684. case "npipe":
  685. return npipeDial(daemonURL.Path, timeout)
  686. case "unix":
  687. return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout)
  688. case "tcp":
  689. if os.Getenv("DOCKER_TLS_VERIFY") != "" {
  690. // Setup the socket TLS configuration.
  691. tlsConfig, err := getTLSConfig()
  692. if err != nil {
  693. return nil, err
  694. }
  695. dialer := &net.Dialer{Timeout: timeout}
  696. return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig)
  697. }
  698. return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout)
  699. default:
  700. return c, fmt.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
  701. }
  702. }
  703. func newRequestClient(method, endpoint string, data io.Reader, ct, daemon string) (*http.Request, *httputil.ClientConn, error) {
  704. c, err := SockConn(time.Duration(10*time.Second), daemon)
  705. if err != nil {
  706. return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
  707. }
  708. client := httputil.NewClientConn(c, nil)
  709. req, err := http.NewRequest(method, endpoint, data)
  710. if err != nil {
  711. client.Close()
  712. return nil, nil, fmt.Errorf("could not create new request: %v", err)
  713. }
  714. if ct != "" {
  715. req.Header.Set("Content-Type", ct)
  716. }
  717. return req, client, nil
  718. }
  719. // BuildImageCmdWithHost create a build command with the specified arguments.
  720. // FIXME(vdemeester) move this away
  721. func BuildImageCmdWithHost(dockerBinary, name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd {
  722. args := []string{}
  723. if host != "" {
  724. args = append(args, "--host", host)
  725. }
  726. args = append(args, "build", "-t", name)
  727. if !useCache {
  728. args = append(args, "--no-cache")
  729. }
  730. args = append(args, buildFlags...)
  731. args = append(args, "-")
  732. buildCmd := exec.Command(dockerBinary, args...)
  733. buildCmd.Stdin = strings.NewReader(dockerfile)
  734. return buildCmd
  735. }