//If image is running, stop and refresh with latest version from docker registry. //Otherwise, start latest version from docker registry //Ensure started container is healthy func refresh(appNames string, pathToYml string, airplaneMode bool) { fmt.Println("Fetching latest versions of applications from docker hub") apps := splitCommas(appNames) internalApplications := getInternalApplications(pathToYml) registeredApps := filterAppsAndExpandDepends(apps, internalApplications, airplaneMode) var appsInStartOrder []Application for i := len(registeredApps) - 1; i >= 0; i-- { appsInStartOrder = append(appsInStartOrder, registeredApps[i]) } healthChecker := healthcheck.HealthChecker{1, 1, 30} for _, app := range appsInStartOrder { var start = true latestVersion := getLatestVersion(app.ImageName, airplaneMode) if docker.IsImageRunning(app.ImageName) { fmt.Println(" checking") currentVersion := docker.GetImageVersion(app.ImageName) image := fmt.Sprintf("%s:%s", currentVersion, latestVersion) fmt.Printf("Running Version [%s:%s], Latest Version: [%s:%s]\n", app.ImageName, currentVersion, app.ImageName, latestVersion) if currentVersion != latestVersion { fmt.Printf("Stopping Version [%s:%s]\n", app.ImageName, currentVersion) docker.Stop([]docker.Container{docker.GetContainerIdFromImageName(fmt.Sprintf("%s:%s", app.ImageName, currentVersion))}) } else { if healthcheck.IsHealthy(app.HealthCheck, healthChecker) { fmt.Println(" - healthy") start = false } else { fmt.Println(" unhealthy. Restarting") docker.Stop([]docker.Container{docker.GetContainerIdFromImageName(image)}) } } } else { fmt.Println(" starting") } if start { newImage := fmt.Sprintf("%s:%s", app.ImageName, latestVersion) docker.Start(app.Service, newImage, app.Ports) fmt.Printf(" - Healthcheck: %s\n ", app.HealthCheck.Description()) if !healthcheck.AttemptHealthCheck(app.HealthCheck, healthChecker) { os.Exit(1) } } fmt.Println("") } }
//If image is running, ping healthcheck. // If not healthy, get latest version from docker registry, stop old and start new //Otherwise, start latest version from docker registry //Ensure started container is healthy func up(applications []Application, airplaneMode bool) { healthChecker := healthcheck.HealthChecker{1, 1, 60} for _, app := range applications { fmt.Printf("%s", app.ImageName) var start = true if docker.IsImageRunning(app.ImageName) { fmt.Println(" checking") if healthcheck.IsHealthy(app.HealthCheck, healthChecker) { fmt.Println(" - healthy") start = false } else { fmt.Println(" unhealthy. Restarting") latestVersion := getLatestVersion(app.ImageName, airplaneMode) image := fmt.Sprintf("%s:%s", strings.Split(app.ImageName, ":")[0], latestVersion) docker.Stop([]docker.Container{docker.GetContainerIdFromImageName(image)}) } } else { fmt.Println(" starting") } if start { image := fmt.Sprintf("%s:%s", strings.Split(app.ImageName, ":")[0], getLatestVersion(app.ImageName, airplaneMode)) docker.Start(app.Service, image, app.Ports) fmt.Printf(" - Healthcheck: %s\n ", app.HealthCheck.Description()) if !healthcheck.AttemptHealthCheck(app.HealthCheck, healthChecker) { os.Exit(1) } } fmt.Println("") } }
func getContainersForDownstreamDeps(deps []string) []docker.Container { var containers []docker.Container for _, dep := range deps { containers = append(containers, docker.GetContainerIdFromImageName(dep)) } return containers }
func down(appNames string, pathToYml string, airplaneMode bool) { var allAppsToStop []docker.Container internalApplications := getInternalApplications(pathToYml) apps := splitCommas(appNames) registeredApps := filterApps(apps, internalApplications, airplaneMode) running := docker.BuildListOfRunningContainers() depends := make(map[string][]Application) runningVersions := make(map[string]string) //create two (2) maps: //1) running application's dependencies to its parent application //2) an image to its currently running version //this will be used to find downstream dependencies and stop the correct containers in the next step for _, container := range running { //map image names to locally running version runningVersions[container.Image.Base] = container.Image.Version regApp := getApplication(container.Image.Base, internalApplications, airplaneMode) for _, dependency := range regApp.Dependencies { if !strings.Contains(dependency, dockerHubOrgName(dependency)) { dependency = fmt.Sprintf("%s/%s", dockerHubOrgName(dependency), dependency) } depends[dependency] = append(depends[dependency], regApp) } } //applications to stop consist of itself and any downstream dependencies for _, app := range registeredApps { downStreamDeps := getDownstreamDepends(app.ImageName, depends, runningVersions) allAppsToStop = append(allAppsToStop, getContainersForDownstreamDeps(downStreamDeps)...) image := fmt.Sprintf("%s:%s", app.ImageName, runningVersions[app.ImageName]) allAppsToStop = append(allAppsToStop, docker.GetContainerIdFromImageName(image)) } docker.Stop(allAppsToStop) }