Esempio n. 1
0
func main() {
	docker, err := dockerclient.NewDockerClient(os.Getenv("DOCKER_HOST"), nil)
	if err != nil {
		log.Fatal(err)
	}

	docker.StartMonitorEvents(eventCallback, nil)

	waitForInterrupt()
}
Esempio n. 2
0
func psAdapterPostHook(d *Daemon, reqParams adapterRequest) (postResp *adapterPostResponse) {
	postResp = &adapterPostResponse{}
	postResp.PowerstripProtocolVersion = reqParams.PowerstripProtocolVersion
	postResp.ModifiedServerResponse.ContentType = "application/json"
	postResp.ModifiedServerResponse.Body = reqParams.ServerResponse.Body
	postResp.ModifiedServerResponse.Code = reqParams.ServerResponse.Code

	if reqParams.ClientRequest.Method != "POST" &&
		reqParams.ClientRequest.Method != "DELETE" {
		fmt.Println("Invalid method: ", reqParams.ClientRequest.Method)
		postResp.ModifiedServerResponse.Code = 500
		return
	}

	if reqParams.ClientRequest.Request != "" {
		// start api looks like this /<version>/containers/<cid>/start
		s := regexp.MustCompile("/").Split(reqParams.ClientRequest.Request, 5)
		var cid string

		if strings.HasPrefix(reqParams.ClientRequest.Request, "/v") {
			// start api looks like this /<version>/containers/<cid>/start
			cid = s[3]

		} else {
			// start api looks like this /containers/<cid>/start for the fsouza/go-dockerclient without api version
			cid = s[2]
		}

		var cfg = &Connection{}
		var op = ConnectionAdd

		switch reqParams.ClientRequest.Method {
		case "POST":
			docker, _ := dockerclient.NewDockerClient(
				"unix:///var/run/docker.sock", nil)
			info, err := docker.InspectContainer(cid)
			if err != nil {
				fmt.Println("InspectContainer failed", err)
				postResp.ModifiedServerResponse.Code = 404
				return
			}

			cfg.ContainerID = string(cid)
			cfg.ContainerName = info.Name
			cfg.ContainerPID = strconv.Itoa(info.State.Pid)
			cfg.Network = DefaultNetworkName
			for _, env := range info.Config.Env {
				val := regexp.MustCompile("=").Split(env, 3)
				if val[0] == "SP_NETWORK" {
					cfg.Network = strings.Trim(val[1], " ")
				}
			}

			op = ConnectionAdd
		case "DELETE":
			var ok bool
			if cfg, ok = d.Connections[cid]; !ok {
				fmt.Println("Couldn't find the connection", cid)
				postResp.ModifiedServerResponse.Code = 500
				return
			}

			op = ConnectionDelete
		}

		context := &ConnectionContext{
			op,
			cfg,
			make(chan *Connection),
		}

		d.cC <- context

		<-context.Result
	}

	return
}