func (c *Client) CreateDockerContainer(con *disco.Container) (*disco.Container, error) { var updated disco.Container cj, err := con.Marshal() reply, err := c.do("/disco/api/docker/create/" + con.Name + "\n" + string(cj)) if err != nil { return &updated, err } replySplit := strings.Split(string(reply), "\n") if replySplit[0] != "success" { return &updated, errors.New(string(reply)) } err = json.Unmarshal([]byte(replySplit[1]), &updated) if err != nil { return &updated, err } return &updated, nil }
func (c *Client) RegisterContainer(con *disco.Container) error { cJson, err := con.Marshal() if err != nil { return err } reply, err := c.do(fmt.Sprintf("/disco/api/add_container/%s\n%s", con.Name, cJson)) if err != nil { return err } if string(reply) != "success" { return errors.New(string(reply)) } return nil }
func (d *DiscoAPI) createDockerContainer(p string, payload []byte) { var con disco.Container err := json.Unmarshal(payload, &con) if err != nil { d.Reply([]byte(err.Error())) return } create := docker.CreateContainerOptions{ Name: con.Name, } config := &docker.Config{ Image: con.Image, Env: con.Env, NetworkDisabled: false, AttachStdin: false, AttachStdout: false, AttachStderr: false, } create.Config = config c, err := d.docker.CreateContainer(create) if err != nil { d.Reply([]byte(err.Error())) return } hc := &docker.HostConfig{ NetworkMode: "bridge", PublishAllPorts: true, } hc.LxcConf = make([]docker.KeyValuePair, 0) hc.PortBindings = make(map[docker.Port][]docker.PortBinding) err = d.docker.StartContainer(c.ID, hc) if err != nil { d.Reply([]byte(err.Error())) return } con.Id = c.ID conj, err := con.Marshal() if err != nil { d.Reply([]byte(err.Error())) return } reply := []byte("success\n") reply = append(reply, conj...) d.Reply(reply) }
func updateContainer(fromDocker, fromDisco *disco.Container) { if fromDisco.HasLinks() { fromDocker.Links = fromDisco.Links } dockerHash, err := fromDocker.Hash() discoHash, err := fromDisco.Hash() if err != nil { log.Print("Update error:" + err.Error()) return } if dockerHash != discoHash { log.Print("Container [", fromDisco.Name, "] exists but has been updated") if err := dc.RegisterContainer(fromDocker); err != nil { log.Print("Error: ", err) } } }