Browse code

cleanup: move container's functions to its file

Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>

Ma Shimiao authored on 2015/05/04 21:39:51
Showing 2 changed files
... ...
@@ -1557,3 +1557,72 @@ func (c *Container) LogDriverType() string {
1557 1557
 	}
1558 1558
 	return c.hostConfig.LogConfig.Type
1559 1559
 }
1560
+
1561
+func (container *Container) GetExecIDs() []string {
1562
+	return container.execCommands.List()
1563
+}
1564
+
1565
+func (container *Container) Exec(execConfig *execConfig) error {
1566
+	container.Lock()
1567
+	defer container.Unlock()
1568
+
1569
+	waitStart := make(chan struct{})
1570
+
1571
+	callback := func(processConfig *execdriver.ProcessConfig, pid int) {
1572
+		if processConfig.Tty {
1573
+			// The callback is called after the process Start()
1574
+			// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
1575
+			// which we close here.
1576
+			if c, ok := processConfig.Stdout.(io.Closer); ok {
1577
+				c.Close()
1578
+			}
1579
+		}
1580
+		close(waitStart)
1581
+	}
1582
+
1583
+	// We use a callback here instead of a goroutine and an chan for
1584
+	// syncronization purposes
1585
+	cErr := promise.Go(func() error { return container.monitorExec(execConfig, callback) })
1586
+
1587
+	// Exec should not return until the process is actually running
1588
+	select {
1589
+	case <-waitStart:
1590
+	case err := <-cErr:
1591
+		return err
1592
+	}
1593
+
1594
+	return nil
1595
+}
1596
+
1597
+func (container *Container) monitorExec(execConfig *execConfig, callback execdriver.StartCallback) error {
1598
+	var (
1599
+		err      error
1600
+		exitCode int
1601
+	)
1602
+
1603
+	pipes := execdriver.NewPipes(execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdout, execConfig.StreamConfig.stderr, execConfig.OpenStdin)
1604
+	exitCode, err = container.daemon.Exec(container, execConfig, pipes, callback)
1605
+	if err != nil {
1606
+		logrus.Errorf("Error running command in existing container %s: %s", container.ID, err)
1607
+	}
1608
+
1609
+	logrus.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
1610
+	if execConfig.OpenStdin {
1611
+		if err := execConfig.StreamConfig.stdin.Close(); err != nil {
1612
+			logrus.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
1613
+		}
1614
+	}
1615
+	if err := execConfig.StreamConfig.stdout.Clean(); err != nil {
1616
+		logrus.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
1617
+	}
1618
+	if err := execConfig.StreamConfig.stderr.Clean(); err != nil {
1619
+		logrus.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
1620
+	}
1621
+	if execConfig.ProcessConfig.Terminal != nil {
1622
+		if err := execConfig.ProcessConfig.Terminal.Close(); err != nil {
1623
+			logrus.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
1624
+		}
1625
+	}
1626
+
1627
+	return err
1628
+}
... ...
@@ -12,7 +12,6 @@ import (
12 12
 	"github.com/docker/docker/daemon/execdriver/lxc"
13 13
 	"github.com/docker/docker/pkg/broadcastwriter"
14 14
 	"github.com/docker/docker/pkg/ioutils"
15
-	"github.com/docker/docker/pkg/promise"
16 15
 	"github.com/docker/docker/pkg/stringid"
17 16
 	"github.com/docker/docker/runconfig"
18 17
 )
... ...
@@ -245,72 +244,3 @@ func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pi
245 245
 
246 246
 	return exitStatus, err
247 247
 }
248
-
249
-func (container *Container) GetExecIDs() []string {
250
-	return container.execCommands.List()
251
-}
252
-
253
-func (container *Container) Exec(execConfig *execConfig) error {
254
-	container.Lock()
255
-	defer container.Unlock()
256
-
257
-	waitStart := make(chan struct{})
258
-
259
-	callback := func(processConfig *execdriver.ProcessConfig, pid int) {
260
-		if processConfig.Tty {
261
-			// The callback is called after the process Start()
262
-			// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
263
-			// which we close here.
264
-			if c, ok := processConfig.Stdout.(io.Closer); ok {
265
-				c.Close()
266
-			}
267
-		}
268
-		close(waitStart)
269
-	}
270
-
271
-	// We use a callback here instead of a goroutine and an chan for
272
-	// syncronization purposes
273
-	cErr := promise.Go(func() error { return container.monitorExec(execConfig, callback) })
274
-
275
-	// Exec should not return until the process is actually running
276
-	select {
277
-	case <-waitStart:
278
-	case err := <-cErr:
279
-		return err
280
-	}
281
-
282
-	return nil
283
-}
284
-
285
-func (container *Container) monitorExec(execConfig *execConfig, callback execdriver.StartCallback) error {
286
-	var (
287
-		err      error
288
-		exitCode int
289
-	)
290
-
291
-	pipes := execdriver.NewPipes(execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdout, execConfig.StreamConfig.stderr, execConfig.OpenStdin)
292
-	exitCode, err = container.daemon.Exec(container, execConfig, pipes, callback)
293
-	if err != nil {
294
-		logrus.Errorf("Error running command in existing container %s: %s", container.ID, err)
295
-	}
296
-
297
-	logrus.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
298
-	if execConfig.OpenStdin {
299
-		if err := execConfig.StreamConfig.stdin.Close(); err != nil {
300
-			logrus.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
301
-		}
302
-	}
303
-	if err := execConfig.StreamConfig.stdout.Clean(); err != nil {
304
-		logrus.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
305
-	}
306
-	if err := execConfig.StreamConfig.stderr.Clean(); err != nil {
307
-		logrus.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
308
-	}
309
-	if execConfig.ProcessConfig.Terminal != nil {
310
-		if err := execConfig.ProcessConfig.Terminal.Close(); err != nil {
311
-			logrus.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
312
-		}
313
-	}
314
-
315
-	return err
316
-}