有没有直接的方法从 Go 中的 unix 套接字获得 html 响应(就像 curl 一样)?
Is there a straight way to get html response from a unix socket in Go (like curl does)?
我需要创建一个 docker (1.13) 容器,它将 运行 作为 docker 群中的一项服务来安排作业(就像一个群范围的 crontab,它执行'docker exec' 在需要的地方)。
我是一个相当系统管理员的人,而不是真正的编码员,所以我开始使用 bash、curl 和 jq。
有效,但肯定还有改进的余地。
为了让您了解我正在处理的 mumbo-jumpo,这里有一些我传递给 docker 套接字的调用片段,以找出服务的位置 运行:
# Get local docker node ID:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/info | jq -r '.Name + " " + .Swarm.NodeID'
# List swarm node ID's:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/nodes | jq -r '.[] | .Description.Hostname + " " + .ID'
# List swarm services:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/nodes | jq -r '.[] | .Description.Hostname + " " + .ID'
# List running tasks:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/tasks | jq -r '.[] | select( .Status.State | contains("running")) | .NodeID + " " + .ServiceID + " " + .ID + " " + .Status.State'
我想用 golang 做得更好。
我了解 http.Get 在与 TCP 套接字通信时如何工作:
res, err := http.Get(url)
body, err := ioutil.ReadAll(res.Body)
err = json.Unmarshal(body, &p)
// p is then populated with the content of my request
但是我该如何处理 unix 文件套接字 (/var/run/docker.sock)?我假设我必须使用 net.DialUnix 但到目前为止无法让它工作。
欢迎任何想法或建议。
试试这个:
package main
import (
"fmt"
"net"
"log"
"bufio"
)
func main() {
conn, err := net.Dial("unix", "/var/run/docker.sock")
if err != nil {
panic(err)
}
fmt.Fprintf(conn, "GET /images/json HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\t')
if err != nil {
log.Println(err)
}
fmt.Print("Message from server: "+status)
}
另一种方法:
使用 docker SDK,可用于 python、go 和其他一些语言。
我需要创建一个 docker (1.13) 容器,它将 运行 作为 docker 群中的一项服务来安排作业(就像一个群范围的 crontab,它执行'docker exec' 在需要的地方)。 我是一个相当系统管理员的人,而不是真正的编码员,所以我开始使用 bash、curl 和 jq。 有效,但肯定还有改进的余地。
为了让您了解我正在处理的 mumbo-jumpo,这里有一些我传递给 docker 套接字的调用片段,以找出服务的位置 运行:
# Get local docker node ID:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/info | jq -r '.Name + " " + .Swarm.NodeID'
# List swarm node ID's:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/nodes | jq -r '.[] | .Description.Hostname + " " + .ID'
# List swarm services:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/nodes | jq -r '.[] | .Description.Hostname + " " + .ID'
# List running tasks:
curl -s --unix-socket /var/run/docker.sock http:/v1.26/tasks | jq -r '.[] | select( .Status.State | contains("running")) | .NodeID + " " + .ServiceID + " " + .ID + " " + .Status.State'
我想用 golang 做得更好。
我了解 http.Get 在与 TCP 套接字通信时如何工作:
res, err := http.Get(url)
body, err := ioutil.ReadAll(res.Body)
err = json.Unmarshal(body, &p)
// p is then populated with the content of my request
但是我该如何处理 unix 文件套接字 (/var/run/docker.sock)?我假设我必须使用 net.DialUnix 但到目前为止无法让它工作。
欢迎任何想法或建议。
试试这个:
package main
import (
"fmt"
"net"
"log"
"bufio"
)
func main() {
conn, err := net.Dial("unix", "/var/run/docker.sock")
if err != nil {
panic(err)
}
fmt.Fprintf(conn, "GET /images/json HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\t')
if err != nil {
log.Println(err)
}
fmt.Print("Message from server: "+status)
}
另一种方法:
使用 docker SDK,可用于 python、go 和其他一些语言。