Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Query docker stats via luvit
-- env from luvi
local env = require('env')
-- some modules form luvit
local pathJoin = require('path').join
local urlParse = require('url').parse
local jsonParse = require('json').parse
local httpCodec = require('http-codec')
-- Some third-party modules
-- To get these run:
-- lit install creationix/coro-wrapper creationix/coro-tcp creationix/coro-tls creationix/coro-fs
--
local connect = require('coro-tcp').connect
local fs = require('coro-fs')
local tlsWrap = require('coro-tls').wrap
local wrapper = require('coro-wrapper')
-- Enter your container ID
local id = "df321ebcd929"
-- Make sure DOCKER_HOST is set with tcp host and path
-- Test by polling the stats for a container
local config = urlParse(env.get("DOCKER_HOST"))
coroutine.wrap(function ()
local read, write = assert(connect(config.host, tonumber(config.port)))
read, write = tlsWrap(read, write, {
ca = fs.readFile(pathJoin(env.get("DOCKER_CERT_PATH"), "ca.pem")),
key = fs.readFile(pathJoin(env.get("DOCKER_CERT_PATH"), "key.pem")),
cert = fs.readFile(pathJoin(env.get("DOCKER_CERT_PATH"), "cert.pem")),
})
read = wrapper.reader(read, httpCodec.decoder())
write = wrapper.writer(write, httpCodec.encoder())
write({
method = "GET",
path = "/containers/" .. id .. "/stats"
})
local res = read()
p(res)
for data in read do
p(jsonParse(data))
end
write()
end)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.