Ejemplo n.º 1
0
//监听到容器启动时做的操作
func Start(client *docker.Client, event *docker.APIEvents) {
	fmt.Println("Received start event %s for container %s", event.Status, event.ID[:12])
	container, err := client.InspectContainer(event.ID[:12])
	common.ErrorHandle(err)
	ipAddress := container.NetworkSettings.IPAddress
	include := common.GetConfig("Section", "include")
	if include == "*" || strings.Contains(","+include+",", ","+common.SubstrAfter(container.Name, 0)+",") {
		common.AppendFile(common.GetConfig("Section", "hostFile"), ipAddress+"  "+getDomainName(container.Name))
		restartDns()
	}
}
Ejemplo n.º 2
0
//监听到容器消亡时做的操作
func Die(client *docker.Client, event *docker.APIEvents) {
	fmt.Println("Received die event %s for container %s", event.Status, event.ID[:12])
	container, err := client.InspectContainer(event.ID[:12])
	common.ErrorHandle(err)
	include := common.GetConfig("Section", "include")
	if include == "*" || strings.Contains(","+include+",", ","+common.SubstrAfter(container.Name, 0)+",") {
		strData := common.ReadFile(common.GetConfig("Section", "hostFile"))
		arrData := strings.Split(strData, "\n")
		strData = ""
		for i := 0; i < len(arrData); i++ {
			if strings.Index(arrData[i], getDomainName(container.Name)) >= 0 {
				continue
			}
			if strData == "" {
				strData = arrData[i]
			} else {
				strData += "\n" + arrData[i]
			}
		}
		common.SaveFile(common.GetConfig("Section", "hostFile"), strData)
		restartDns()
	}
}
Ejemplo n.º 3
0
//重启dnsmasq
func restartDns() {
	sh := exec.Command("/sbin/service", "dnsmasq", " restart")
	_, err := sh.CombinedOutput()
	common.ErrorHandle(err)
}