这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 71 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ app.all('/container/:containerId', function (req, res) {
}
});

/**
* List all of the containers
*/
app.get('/containers', function (req, res) {
docker.listContainers({ all: true }, function (err, containers) {
if (err) {
res.status(500);
res.send(err);
return;
}
res.status(200);
res.send(containers);
});
});


/**
* Restart the container by the ID specified
*/
Expand All @@ -165,6 +181,58 @@ app.get('/container/:containerId/restart', function (req, res) {
})
});


/**
* Start the container by the ID specified
*/
app.get('/container/:containerId/start', function (req, res) {
var containerId = req.params.containerId;
console.log("Start " + containerId);

getContainer(containerId, function (container) {
docker.getContainer(container.Id).start(function (err, data) {
if (err) {
res.status(500);
res.send(err);
return;
}
res.status(200); //We found the container! This reponse can be trusted
res.send(data);
});
}, function (status, message) {
res.status(status);
if (message) {
res.send(message);
}
})
});


/**
* Stop the container by the ID specified
*/
app.get('/container/:containerId/stop', function (req, res) {
var containerId = req.params.containerId;
console.log("Stop " + containerId);

getContainer(containerId, function (container) {
docker.getContainer(container.Id).stop(function (err, data) {
if (err) {
res.status(500);
res.send(err);
return;
}
res.status(200); //We found the container! This reponse can be trusted
res.send(data);
});
}, function (status, message) {
res.status(status);
if (message) {
res.send(message);
}
})
});

app.post('/container/:containerId/exec', function(req, res) {
var containerId = req.params.containerId;
console.log("Exec " + containerId);
Expand All @@ -178,7 +246,7 @@ app.post('/container/:containerId/exec', function(req, res) {
res.status(400);
return;
}

getContainer(containerId, function (container) {
if (config.get("debug"))
console.log("Attempting to execute command in container " + container.Id);
Expand Down Expand Up @@ -283,14 +351,14 @@ function getContainer(name, cb, error)
if (err) {
if (typeof error == "function")
return error(500, err);

return;
}

if (containers.length < 1) {
if (typeof error == "function")
return error(404, "container not found");

return;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ha-dockermon",
"version": "0.0.2",
"version": "0.0.3",
"description": "A RESTful HTTP endpoint which can return the status of Docker containers for Home Automation controllers like Home Assistant",
"main": "index.js",
"scripts": {
Expand Down
70 changes: 70 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,78 @@ curl --request POST \
--data '{"command": "ls -a"}'
```

### GET /container/{container name}/start

Allows you to send a simple HTTP request to start a Docker container.

The response will be a json object with a `result` key containing the output from the command executed.

*Warning:* There is no confirmation for the command to be executed. Going to the URL in your browser will start the container.

### GET /container/{container name}/stop

Allows you to send a simple HTTP request to stop a Docker container.

The response will be a json object with a `result` key containing the output from the command executed.

*Warning:* There is no confirmation for the command to be executed. Going to the URL in your browser will stop the container.

### GET /containers

Outputs a list of all stopped and started containers on the host.

This is the same as performing a `docker ps -a` command on the host machine.

The response will be a json object, with each container in its own key. An example response is below.

```json
[{
"Id": "2096eaf1a58f1730234d2e30c982021c196192eae9f41c6abf8fa26aad348477",
"Names": ["/hadockermon"],
"Image": "hadockermon",
"ImageID": "sha256:e7352295ec274a441f691a8c83f8823137654f5d4df5fb187d9f1cee1f4711d6",
"Command": "/bin/sh -c 'npm start'",
"Created": 1523522864,
"Ports": [{
"IP": "0.0.0.0",
"PrivatePort": 8126,
"PublicPort": 8126,
"Type": "tcp"
}],
"Labels": {},
"State": "running",
"Status": "Up 19 seconds",
"HostConfig": {
"NetworkMode": "default"
},
"NetworkSettings": {
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ed342d9b95ab77f57172ca3fdd2dc87682ee7e0c3f94db7bb3a83ba81a5f2135",
"EndpointID": "bfdec2f98a2521093e1210c1cc5135e3a788be5b80b8409d8652915a5ee38224",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
},
"Mounts": [{
"Source": "/var/run/docker.sock",
"Destination": "/var/run/docker.sock",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}]
}]
```

### Home Assistant RESTful Switch

You can use this service as a [RESTful switch](https://home-assistant.io/components/switch.rest/) inside Home Assistant.
Expand Down