Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add "pouch rm" #214

Merged
merged 1 commit into from Dec 8, 2017
Merged

feature: add "pouch rm" #214

merged 1 commit into from Dec 8, 2017

Conversation

skoowoo
Copy link
Contributor

@skoowoo skoowoo commented Dec 4, 2017

"pouch rm" is used to remove a container which is stopped or created,
if a container is running, you can use "pouch rm -f" to force to remove
the container.

Signed-off-by: skoo87 marckywu@gmail.com

cli/rm.go Outdated
r.cli = c
r.cmd = &cobra.Command{
Use: "rm container",
Short: "remove a container",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this command supports removing multiple containers. Could we change Short message to Remove one or more containers to be more precise?

defer ensureCloseReader(resp)

if resp.StatusCode != http.StatusNoContent {
body, err := ioutil.ReadAll(resp.Body)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any necessary for us to add a new struct in swagger.yml, for exmaple ContainerRmResponse or something like that? And maybe this will make API data transferring more structed.

Copy link
Contributor Author

@skoowoo skoowoo Dec 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if remove success, there is no content in body as same as docker. Only ocurring error, the body contain the error message.
like this:
{"message":"container: 26ecc8f964f2831a4106d79808af2995a3517f0277bcd398a92c590cba0aeee7 is not stopped, can't remove it"}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if string(body) is empty string, this function will return an error. Is it reasonable?
Actually if resp.StatusCode != http.StatusNoContent returns true, it means this is a non-2xx response. Then the error message will not be in resp, I think it is in err of resp, err := client.delete("/images/"+name, q). This is guaranteed by HTTP framework. So I am wondering if this block is reasonable. @HusterWan @skoo87

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not as you said, if resp.StatusCode != http.StatusNoContent returns true, it means pouchd returns non-204 response. pouchd server remove container success, will returns 204 status code. if not 204, that is remove container failed. you should look at the server api code. the action is same as docker, it's reasonable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid I was wrong. I search moby's client code, in its cli, when requesting, he will do like this:

func (cli *Client) sendRequest(ctx context.Context, method, path string, query url.Values, body io.Reader, headers headers) (serverResponse, error) {
	req, err := cli.buildRequest(method, cli.getAPIPath(path, query), body, headers)
	if err != nil {
		return serverResponse{}, err
	}
	resp, err := cli.doRequest(ctx, req)
	if err != nil {
		return resp, err
	}
        //  !!!!!!!! this part is important, transfer error in body to the second return parameter of func
	if err := cli.checkResponseErr(resp); err != nil {
		return resp, err
	}
	return resp, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. I realized it's a problem after I answer you yesterday.


option := &mgr.ContainerRemoveOption{
Force: httputils.BoolValue(req, "force"),
Volume: httputils.BoolValue(req, "v"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we have implemented remove flags v and l.
Can we remove both two, or mark this and implement them in the future?

If the answer is latter one, I think we had better track this in an issue or mark that in code comments.

@allencloud
Copy link
Collaborator

Shall we add this API in swagger.yml ?

@skoowoo
Copy link
Contributor Author

skoowoo commented Dec 6, 2017

there is not structure what be needed to define in swagger.

@allencloud
Copy link
Collaborator

I agree with you that no struct needs added. While I am afraid we need to the api things like:

paths:
  /_ping:
    get:
      summary: ""
      description: ""
      responses:
        200:
          description: "no error"
          schema:
            type: "string"
            example: "OK"
        500:
          description: "server error"
          schema:
            $ref: '#/definitions/Error'

After this, we can generate API doc with tools. If not, this API will not be generated in API docs.

Auto-generation of API documents I have discussed with @CodeJuan, and will work it out soon.

@skoowoo
Copy link
Contributor Author

skoowoo commented Dec 6, 2017

I agree with you, I updated. @allencloud

@skoowoo
Copy link
Contributor Author

skoowoo commented Dec 7, 2017

please review it, thanks @allencloud

apis/swagger.yml Outdated

/containers/{name}:
post:
summary: "Remove one or more containers"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This api only removes one container. So if Remove one container is better?

cli/rm.go Outdated
r.cmd = &cobra.Command{
Use: "rm container",
Short: "Remove one or more containers",
Args: cobra.MinimumNArgs(1),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Long and Example in command definition like https://github.com/alibaba/pouch/blob/master/cli/create.go#L28-L33 ?
Then we can automatically generate docs? How about that?

func (mgr *ContainerManager) Remove(ctx context.Context, name string, option *ContainerRemoveOption) error {
c, err := mgr.container(name)
Copy link
Collaborator

@allencloud allencloud Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, I think we need to classify a StatusNotFound code here, since we may not find container by input name.
We need to do this, in follow-up pull requests.

defer c.Unlock()

if !c.IsStopped() && !c.IsCreated() && !option.Force {
return fmt.Errorf("container: %s is not stopped, can't remove it", c.ID())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When removing a running container without flag force, I think pouchd would return an error container: %s is not stopped, can't remove it.
I suggest using fmt.Errorf("container %s is not stopped, cannot remove it without flag force", c.ID()) to tell others to use force flag.

apis/swagger.yml Outdated
@@ -298,7 +298,24 @@ paths:
schema:
$ref: "#/definitions/Error"
tags: ["Container"]

/containers/{name}:
post:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part should be delete, not post.

cli/rm.go Outdated
Remove a container object in Pouchd.
If a container is stopped or created, you can remove it.
If the container is running, you can also remove it with flag force.
When the container be removed, the all resource of the container will
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/be/is

"pouch rm" is used to remove a container which is stopped or created,
if a container is running, you can use "pouch rm -f" to force to remove
the container.

Signed-off-by: skoo87 <marckywu@gmail.com>
@skoowoo
Copy link
Contributor Author

skoowoo commented Dec 8, 2017

updated @allencloud

@allencloud
Copy link
Collaborator

LGTM

@pouchrobot pouchrobot added the LGTM one maintainer or community participant agrees to merge the pull reuqest. label Dec 8, 2017
@allencloud allencloud merged commit 2d31984 into AliyunContainerService:master Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature LGTM one maintainer or community participant agrees to merge the pull reuqest. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants