func()

in cache-config/t3c-apply/torequest/torequest.go [921:1059]


func (r *TrafficOpsReq) ProcessPackages() error {
	log.Infoln("Calling ProcessPackages")
	// get the package list for this cache from Traffic Ops.
	pkgs, err := getPackages(r.Cfg)
	if err != nil {
		return errors.New("getting packages: " + err.Error())
	}
	log.Infof("ProcessPackages got %+v\n", pkgs)

	var install []string   // install package list.
	var uninstall []string // uninstall package list
	// loop through the package list to build an install and uninstall list.
	for ii := range pkgs {
		var instpkg string // installed package
		var reqpkg string  // required package
		log.Infof("Processing package %s-%s\n", pkgs[ii].Name, pkgs[ii].Version)
		// check to see if any package by name is installed.
		arr, err := util.PackageInfo("pkg-query", pkgs[ii].Name)
		if err != nil {
			return errors.New("PackgeInfo pkg-query: " + err.Error())
		}
		// go needs the ternary operator :)
		if len(arr) == 1 {
			instpkg = arr[0]
		} else {
			instpkg = ""
		}
		// check if the full package version is installed
		fullPackage := pkgs[ii].Name + "-" + pkgs[ii].Version

		if r.Cfg.InstallPackages {
			if instpkg == fullPackage {
				log.Infof("%s Currently installed and not marked for removal\n", reqpkg)
				r.Pkgs[fullPackage] = true
				continue
			} else if instpkg != "" { // the installed package needs upgrading.
				log.Infof("%s Currently installed and marked for removal\n", instpkg)
				uninstall = append(uninstall, instpkg)
				// the required package needs installing.
				log.Infof("%s is Not installed and is marked for installation.\n", fullPackage)
				install = append(install, fullPackage)
				// get a list of packages that depend on this one and mark dependencies
				// for deletion.
				arr, err = util.PackageInfo("pkg-requires", instpkg)
				if err != nil {
					return errors.New("PackgeInfo pkg-requires: " + err.Error())
				}
				if len(arr) > 0 {
					for jj := range arr {
						log.Infof("%s is Currently installed and depends on %s and needs to be removed.", arr[jj], instpkg)
						uninstall = append(uninstall, arr[jj])
					}
				}
			} else {
				// the required package needs installing.
				log.Infof("%s is Not installed and is marked for installation.\n", fullPackage)
				log.Errorf("%s is Not installed and is marked for installation.\n", fullPackage)
				install = append(install, fullPackage)
			}
		} else {
			// Only check if packages exist and complain if they are wrong.
			if instpkg == fullPackage {
				log.Infof("%s Currently installed.\n", reqpkg)
				r.Pkgs[fullPackage] = true
				continue
			} else if instpkg != "" { // the installed package needs upgrading.
				log.Errorf("%s Wrong version currently installed.\n", instpkg)
				r.Pkgs[instpkg] = true
			} else {
				// the required package needs installing.
				log.Errorf("%s is Not installed.\n", fullPackage)
			}
		}
	}

	log.Debugf("number of packages requiring installation: %d\n", len(install))
	if r.Cfg.ReportOnly {
		log.Errorf("number of packages requiring installation: %d\n", len(install))
	}
	log.Debugf("number of packages requiring removal: %d\n", len(uninstall))
	if r.Cfg.ReportOnly {
		log.Errorf("number of packages requiring removal: %d\n", len(uninstall))
	}

	if r.Cfg.InstallPackages {
		log.Debugf("number of packages requiring installation: %d\n", len(install))
		if r.Cfg.ReportOnly {
			log.Errorf("number of packages requiring installation: %d\n", len(install))
		}
		log.Debugf("number of packages requiring removal: %d\n", len(uninstall))
		if r.Cfg.ReportOnly {
			log.Errorf("number of packages requiring removal: %d\n", len(uninstall))
		}

		if len(install) > 0 {
			for ii := range install {
				result, err := util.PackageAction("info", install[ii])
				if err != nil || result != true {
					return errors.New("Package " + install[ii] + " is not available to install: " + err.Error())
				}
			}
			log.Infoln("All packages available.. proceding..")

			// uninstall packages marked for removal
			if len(install) > 0 && r.Cfg.InstallPackages {
				for jj := range uninstall {
					log.Infof("Uninstalling %s\n", uninstall[jj])
					r, err := util.PackageAction("remove", uninstall[jj])
					if err != nil {
						return errors.New("Unable to uninstall " + uninstall[jj] + " : " + err.Error())
					} else if r == true {
						log.Infof("Package %s was uninstalled\n", uninstall[jj])
					}
				}

				// install the required packages
				for jj := range install {
					pkg := install[jj]
					log.Infof("Installing %s\n", pkg)
					result, err := util.PackageAction("install", pkg)
					if err != nil {
						return errors.New("Unable to install " + pkg + " : " + err.Error())
					} else if result == true {
						r.Pkgs[pkg] = true
						r.installedPkgs[pkg] = struct{}{}
						log.Infof("Package %s was installed\n", pkg)
					}
				}
			}
		}
		if r.Cfg.ReportOnly && len(install) > 0 {
			for ii := range install {
				log.Errorf("\nIn Report mode and %s needs installation.\n", install[ii])
				return errors.New("In Report mode and packages need installation")
			}
		}
	}
	return nil
}