improve rename and update dockerclient

Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
Xian Chaobo 2015-04-24 21:09:26 -04:00
parent e496d04d47
commit b47e002e6c
11 changed files with 120 additions and 4 deletions

2
Godeps/Godeps.json generated
View File

@ -60,7 +60,7 @@
},
{
"ImportPath": "github.com/samalba/dockerclient",
"Rev": "c37a52f55ab5a9edb9ffd4cf6e78692962b29b8d"
"Rev": "8b9427265e82fddde577648b986fbe78d816b333"
},
{
"ImportPath": "github.com/samuel/go-zookeeper/zk",

View File

@ -310,6 +310,20 @@ func (client *DockerClient) StopAllMonitorStats() {
atomic.StoreInt32(&client.monitorStats, 0)
}
func (client *DockerClient) TagImage(nameOrID string, repo string, tag string, force bool) error {
v := url.Values{}
v.Set("repo", repo)
v.Set("tag", tag)
if force {
v.Set("force", "1")
}
uri := fmt.Sprintf("/%s/images/%s/tag?%s", APIVersion, nameOrID, v.Encode())
if _, err := client.doRequest("POST", uri, nil, nil); err != nil {
return err
}
return nil
}
func (client *DockerClient) Version() (*Version, error) {
uri := fmt.Sprintf("/%s/version", APIVersion)
data, err := client.doRequest("GET", uri, nil, nil)
@ -444,3 +458,9 @@ func (client *DockerClient) Exec(config *ExecConfig) (string, error) {
}
return createExecResp.Id, nil
}
func (client *DockerClient) RenameContainer(oldName string, newName string) error {
uri := fmt.Sprintf("/containers/%s/rename?name=%s", oldName, newName)
_, err := client.doRequest("POST", uri, nil, nil)
return err
}

View File

@ -24,6 +24,7 @@ type Client interface {
StopAllMonitorEvents()
StartMonitorStats(id string, cb StatCallback, ec chan error, args ...interface{})
StopAllMonitorStats()
TagImage(nameOrID string, repo string, tag string, force bool) error
Version() (*Version, error)
PullImage(name string, auth *AuthConfig) error
LoadImage(reader io.Reader) error
@ -32,4 +33,5 @@ type Client interface {
RemoveImage(name string) ([]*ImageDelete, error)
PauseContainer(name string) error
UnpauseContainer(name string) error
RenameContainer(oldName string, newName string) error
}

View File

@ -73,6 +73,11 @@ func (client *MockClient) StopAllMonitorEvents() {
client.Mock.Called()
}
func (client *MockClient) TagImage(nameOrID string, repo string, tag string, force bool) error {
args := client.Mock.Called(nameOrID, repo, tag, force)
return args.Error(0)
}
func (client *MockClient) StartMonitorStats(id string, cb dockerclient.StatCallback, ec chan error, args ...interface{}) {
client.Mock.Called(id, cb, ec, args)
}
@ -125,3 +130,8 @@ func (client *MockClient) Exec(config *dockerclient.ExecConfig) (string, error)
args := client.Mock.Called(config)
return args.String(0), args.Error(1)
}
func (client *MockClient) RenameContainer(oldName string, newName string) error {
args := client.Mock.Called(oldName, newName)
return args.Error(0)
}

View File

@ -130,6 +130,7 @@ type Container struct {
Ports []Port
SizeRw int64
SizeRootFs int64
Labels map[string]string
}
type Event struct {

View File

@ -489,6 +489,34 @@ func postCommit(c *context, w http.ResponseWriter, r *http.Request) {
}
}
// POST /containers/{name:.*}/rename
func postRename(c *context, w http.ResponseWriter, r *http.Request) {
container, err := getContainerFromVars(c, mux.Vars(r))
if err != nil {
httpError(w, err.Error(), http.StatusNotFound)
return
}
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
// check new name whether avaliable
newName := r.Form.Get("name")
if conflictContainer := c.cluster.Container(newName); conflictContainer != nil {
httpError(w, fmt.Sprintf("Conflict, The new name %s is already assigned to %s. ", newName, conflictContainer.Id), http.StatusConflict)
return
}
// call cluster rename
err = c.cluster.Rename(container, newName)
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
}
}
// Proxy a hijack request to the right node
func proxyHijack(c *context, w http.ResponseWriter, r *http.Request) {
container, err := getContainerFromVars(c, mux.Vars(r))

View File

@ -55,7 +55,7 @@ var routes = map[string]map[string]handler{
"/containers/{name:.*}/kill": proxyContainer,
"/containers/{name:.*}/pause": proxyContainer,
"/containers/{name:.*}/unpause": proxyContainer,
"/containers/{name:.*}/rename": proxyContainer,
"/containers/{name:.*}/rename": postRename,
"/containers/{name:.*}/restart": proxyContainer,
"/containers/{name:.*}/start": proxyContainer,
"/containers/{name:.*}/stop": proxyContainer,

View File

@ -43,4 +43,7 @@ type Cluster interface {
// FIXME: remove this method
// Return a random engine
RANDOMENGINE() (*Engine, error)
// Rename a container
Rename(container *Container, newName string) error
}

View File

@ -555,3 +555,16 @@ func (e *Engine) cleanupContainers() {
e.containers = make(map[string]*Container)
e.Unlock()
}
// Rename a container
func (e *Engine) Rename(container *Container, newName string) error {
// send rename request
err := e.client.RenameContainer(container.Id, newName)
if err != nil {
return err
}
// refresh container
err = e.refreshContainer(container.Id, true)
return err
}

View File

@ -333,3 +333,24 @@ func (c *Cluster) RANDOMENGINE() (*cluster.Engine, error) {
}
return nil, nil
}
// Rename a container
func (c *Cluster) Rename(container *cluster.Container, newName string) error {
c.RLock()
defer c.RUnlock()
// call engine rename
err := container.Engine.Rename(container, newName)
if err != nil {
return err
}
// update container name in store
st, err := c.store.Get(container.Id)
if err != nil {
return err
}
st.Name = newName
err = c.store.Replace(container.Id, st)
return err
}

View File

@ -352,9 +352,27 @@ function teardown() {
skip
}
# FIXME
@test "docker rename" {
skip
start_docker 3
swarm_manage
run docker_swarm run -d --name test_container busybox sleep 500
[ "$status" -eq 0 ]
# make sure container exist
run docker_swarm ps -l
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[*]}" == *"test_container"* ]]
[[ "${lines[*]}" != *"rename_container"* ]]
# rename container
run docker_swarm rename test_container rename_container
[ "$status" -eq 0 ]
# verify after rename
run docker_swarm ps -l
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[*]}" == *"rename_container"* ]]
}
@test "docker restart" {