Merge pull request #468 from vieux/ps_n_l

add support for docker ps -l / -n
This commit is contained in:
Victor Vieux 2015-03-10 16:42:58 -07:00
commit 7945e0a482
3 changed files with 34 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"runtime" "runtime"
"sort" "sort"
"strconv"
"strings" "strings"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
@ -117,12 +118,13 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
} }
all := r.Form.Get("all") == "1" all := r.Form.Get("all") == "1"
limit, _ := strconv.Atoi(r.Form.Get("limit"))
out := []*dockerclient.Container{} out := []*dockerclient.Container{}
for _, container := range c.cluster.Containers() { for _, container := range c.cluster.Containers() {
tmp := (*container).Container tmp := (*container).Container
// Skip stopped containers unless -a was specified. // Skip stopped containers unless -a was specified.
if !strings.Contains(tmp.Status, "Up") && !all { if !strings.Contains(tmp.Status, "Up") && !all && limit <= 0 {
continue continue
} }
// Skip swarm containers unless -a was specified. // Skip swarm containers unless -a was specified.
@ -151,7 +153,11 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
sort.Sort(sort.Reverse(ContainerSorter(out))) sort.Sort(sort.Reverse(ContainerSorter(out)))
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if limit > 0 && limit < len(out) {
json.NewEncoder(w).Encode(out[:limit])
} else {
json.NewEncoder(w).Encode(out) json.NewEncoder(w).Encode(out)
}
} }
// GET /containers/{name:.*}/json // GET /containers/{name:.*}/json

View File

@ -14,3 +14,26 @@ function teardown() {
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[[ "${lines[1]}" == *"Nodes: 3" ]] [[ "${lines[1]}" == *"Nodes: 3" ]]
} }
@test "docker ps -n 3 should return the 3 last containers, including non running one" {
skip
start_docker 1
start_manager
run docker_swarm run -d busybox sleep 42
run docker_swarm run -d busybox false
run docker_swarm run -d busybox true
run docker_swarm ps -a -n 3
[ "${#lines[@]}" -eq 3 ]
}
@test "docker ps -l should return the last container, including non running one" {
skip
start_docker 1
start_manager
run docker_swarm run -d busybox sleep 42
run docker_swarm run -d busybox false
run docker_swarm run -d busybox true
run docker_swarm ps -l
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[1]}" == *"true"* ]]
}

View File

@ -28,10 +28,12 @@ function swarm() {
# Waits until the given docker engine API becomes reachable. # Waits until the given docker engine API becomes reachable.
function wait_until_reachable() { function wait_until_reachable() {
local attempts=0 local attempts=0
until docker -H $1 info &> /dev/null || [ $attempts -ge 10 ]; do local max_attempts=5
until docker -H $1 info || [ $attempts -ge $max_attempts ]; do
echo "Attempt to connect to ${HOSTS[$i]} failed for the $((++attempts)) time" >&2 echo "Attempt to connect to ${HOSTS[$i]} failed for the $((++attempts)) time" >&2
sleep 0.5 sleep 0.5
done done
[[ $attempts -lt $max_attempts ]]
} }
# Start the swarm manager in background. # Start the swarm manager in background.