Browse code

Detect and mark ghost container.

Guillaume J. Charmes authored on 2013/04/12 01:26:17
Showing 3 changed files
... ...
@@ -561,6 +561,12 @@ func (container *Container) kill() error {
561 561
 func (container *Container) Kill() error {
562 562
 	container.State.lock()
563 563
 	defer container.State.unlock()
564
+	if !container.State.Running {
565
+		return nil
566
+	}
567
+	if container.State.Ghost {
568
+		return fmt.Errorf("Impossible to kill ghost containers")
569
+	}
564 570
 	return container.kill()
565 571
 }
566 572
 
... ...
@@ -570,6 +576,9 @@ func (container *Container) Stop() error {
570 570
 	if !container.State.Running {
571 571
 		return nil
572 572
 	}
573
+	if container.State.Ghost {
574
+		return fmt.Errorf("Impossible to stop ghost containers")
575
+	}
573 576
 
574 577
 	// 1. Send a SIGTERM
575 578
 	if output, err := exec.Command("lxc-kill", "-n", container.Id, "15").CombinedOutput(); err != nil {
... ...
@@ -119,6 +119,9 @@ func (runtime *Runtime) Load(id string) (*Container, error) {
119 119
 	if container.Id != id {
120 120
 		return container, fmt.Errorf("Container %s is stored at %s", container.Id, id)
121 121
 	}
122
+	if container.State.Running {
123
+		container.State.Ghost = true
124
+	}
122 125
 	if err := runtime.Register(container); err != nil {
123 126
 		return nil, err
124 127
 	}
... ...
@@ -12,11 +12,15 @@ type State struct {
12 12
 	ExitCode  int
13 13
 	StartedAt time.Time
14 14
 	l         *sync.Mutex
15
+	Ghost     bool
15 16
 }
16 17
 
17 18
 // String returns a human-readable description of the state
18 19
 func (s *State) String() string {
19 20
 	if s.Running {
21
+		if s.Ghost {
22
+			return fmt.Sprintf("Running ghost")
23
+		}
20 24
 		return fmt.Sprintf("Up %s", HumanDuration(time.Now().Sub(s.StartedAt)))
21 25
 	}
22 26
 	return fmt.Sprintf("Exit %d", s.ExitCode)