コード例 #1
0
ファイル: node.go プロジェクト: ninglipeng/server
func (this *NodeState) NodeDown(ip string, resp *model.NodeResponse) error {
	if ip == "" {
		resp.Code = 1
		return nil
	}

	g.DeleteNode(ip)
	g.RealState.DeleteByIp(ip)
	return nil
}
コード例 #2
0
ファイル: compare.go プロジェクト: ninglipeng/server
func DockerRun(app *model.App, ip string) {
	if g.Config().Debug {
		log.Printf("create container. app:%s, ip:%s\n", app.Name, ip)
	}

	envVars, err := g.LoadEnvVarsOf(app.Name)
	if err != nil {
		log.Println("[ERROR] load env fail:", err)
		return
	}

	envVars["APP_NAME"] = app.Name
	envVars["HOST_IP"] = ip
	if g.Config().Scribe.Ip != "" {
		envVars["SCRIBE_IP"] = g.Config().Scribe.Ip
	} else {
		envVars["SCRIBE_IP"] = ip
	}
	envVars["SCRIBE_PORT"] = fmt.Sprintf("%d", g.Config().Scribe.Port)

	addr := fmt.Sprintf("http://%s:%d", ip, g.Config().DockerPort)

	client, err := docker.NewClient(addr)
	if err != nil {
		log.Println("[ERROR] docker.NewClient fail:", err)
		return
	}

	opts := docker.CreateContainerOptions{
		Config: &docker.Config{
			Memory: int64(app.Memory * 1024 * 1024),
			ExposedPorts: map[docker.Port]struct{}{
				docker.Port("8080/tcp"): {},
			},
			Image:        app.Image,
			AttachStdin:  false,
			AttachStdout: false,
			AttachStderr: false,
			Env:          BuildEnvArray(envVars),
		},
	}

	container, err := client.CreateContainer(opts)

	if err != nil {
		if err == docker.ErrNoSuchImage {
			repos, tag := ParseRepositoryTag(app.Image)
			e := client.PullImage(docker.PullImageOptions{Repository: repos, Tag: tag}, docker.AuthConfiguration{})
			if e != nil {
				log.Println("[ERROR] pull image", app.Image, "fail:", e)
				return
			}

			// retry
			container, err = client.CreateContainer(opts)
			if err != nil {
				log.Println("[ERROR] retry create container fail:", err, "ip:", ip)
				g.UpdateAppStatus(app, model.AppStatus_CreateContainerFail)
				return
			}
		} else {
			log.Println("[ERROR] create container fail:", err, "ip:", ip)
			if err != nil && strings.Contains(err.Error(), "cannot connect") {
				g.DeleteNode(ip)
				g.RealState.DeleteByIp(ip)
				return
			}
			g.UpdateAppStatus(app, model.AppStatus_CreateContainerFail)
			return
		}
	}

	err = client.StartContainer(container.ID, &docker.HostConfig{
		PortBindings: map[docker.Port][]docker.PortBinding{
			"8080/tcp": []docker.PortBinding{docker.PortBinding{}},
		},
	})

	if err != nil {
		log.Println("[ERROR] docker.StartContainer fail:", err)
		g.UpdateAppStatus(app, model.AppStatus_StartContainerFail)
		return
	}

	if g.Config().Debug {
		log.Println("start container success:-)")
	}

}