def _setupDockerRepo(self): uLogging.info('Setup Docker repository host: %s' % self.k8s_docker_repo_host) self._setSystemProperty('kubernetes_docker_repository_host', self.k8s_docker_repo_host) command = self._K8S.get_image_pull_secrets_patch_cmd() uUtil.readCmd(command)
def _upgradeHelmPackagesGroup(self, helms_to_update, deployedReleases, group): uLogging.info('Upgrade Helm packages group: %s' % group) pkgs = helms_to_update.get(group) if not pkgs: return for pkg in self._getNewestPackages(pkgs, deployedReleases, group == HelmGroup.OPTIONAL): uAction.retriable(self._upgradeHelmPackageEx)(pkg)
def mockUser(self, question=None, default=None): # consider 'yes' answer to every question in batch mode if default: uLogging.debug("Ignoring default value in batch mode. Value = %s" % default) uLogging.info(question) uLogging.info("Yes") return True
def _loadsLatestCharts(self, repoName): command = '%s search %s/' % (self._getHelmCommand(), repoName) uLogging.info('Loads latest charts: %s' % command) charts = list( csv.reader(StringIO(uUtil.readCmd(command)), delimiter='\t')) charts = [list(map(str.strip, x)) for x in charts] charts[0] = list(map(lambda x: x.title().replace(' ', ''), charts[0])) return list(map(lambda x: dict(zip(charts[0], x)), charts[1:]))
def install(self, helms_to_update, repo_version): if not self.isHelmInstalled(): return uAction.retriable(self._setupK8sApi)() uAction.retriable(self._setupRepos)(repo_version) uAction.retriable(self._updateHelmRepo)() groups_to_install = [HelmGroup.FOUNDATION, HelmGroup.REQUIRED] uAction.retriable(self._upgradeHelmPackages)(helms_to_update, groups_to_install) uLogging.info('Helm install completed')
def _checkHelm(self): try: command = '%s help' % self._getHelmCommand() uLogging.info('Check Helm: %s' % command) uUtil.execCommand(command) return True except: uLogging.warn('Helm not found.') return False
def upgrade(self, helms_to_update, repo_version): if not self.isHelmInstalled(): return uAction.retriable(self._upgradeHelmRepo)(repo_version) uAction.retriable(self._updateHelmRepo)() groups_to_update = [ HelmGroup.FOUNDATION, HelmGroup.REQUIRED, HelmGroup.OPTIONAL ] uAction.retriable(self._upgradeHelmPackages)(helms_to_update, groups_to_update) uLogging.info('Helm upgrade completed')
def _setupK8sApiCertificate(self): cert = '/usr/local/pem/kubernetes/certs/kubernetesApi.pem' uLogging.info('Setup Kubernetes API certificate: %s' % cert) cmds = [] cmds.append("mkdir -p /usr/local/pem/kubernetes/certs/") cmds.append( "grep 'certificate-authority-data' /root/.kube/config | awk '{print $2}' | base64 -d > %s" % cert) cmds.append("chown jboss:jboss %s" % cert) cmds.append("chmod 0400 %s" % cert) map(uUtil.readCmd, cmds)
def update(localesIds): if CspLocales.exists(): uLogging.info('Updating CSP locales: %s' % localesIds) cspPath = CspLocales.getDirectory() out = subprocess.check_output( 'cd %s && (echo "import pba_upgrade_helper"; echo "pba_upgrade_helper.update_csp_locales(%s)") | python' % (cspPath, localesIds), shell=True) uLogging.debug(out) else: uLogging.debug('CSP locales was not found.')
def __init__(self, config, opts): self.config = config dev_action_opts = '--dev-actions-run' in opts if config.batch and not dev_action_opts: uLogging.info("Setting batch mode.") uAction.retriable = self.mockRetriable uDialog.askYesNo = self.mockUser else: uLogging.info( "Config.batch = %s , --dev-actions-run in opts = %s" % (config.batch, dev_action_opts))
def upgrade(self, upgrade_instructions, newest_packages): con = self._getConnection() billing_packages = [] hosts_ids = self._getBillingHosts().values() uLogging.info('Upgrade packages on hosts: %s' % hosts_ids) for host_id in hosts_ids: billing_packages.extend( uRoutines.getPkgsToInstall(con, host_id, upgrade_instructions, newest_packages)) if billing_packages: self._updateBilling()
def install(self, hostId): if uPackaging.pkg_installed(hostId, (self.getCtype(), self.getName())): uLogging.info('Reinstalling locale: "%s" (host_id=%d)' % (self.getName(), hostId)) uPackaging.reinstallPackageToHost(hostId, self.getName(), self.getCtype()) else: uLogging.info('Installing locale: "%s" (host_id=%d)' % (self.getName(), hostId)) uPackaging.installPackageToHostAPI(hostId, name=self.getName(), ctype=self.getCtype())
def tuneJBoss(config): uLogging.debug("tuneJBoss started, scale_down: %s" % config.scale_down) if not Const.isWindows() and config.scale_down: from u import bootstrap jbossdir = bootstrap.getJBossDir(config.rootpath) uLogging.info("Tuning JBoss connection pool") bootstrap.execCLI( jbossdir, 'embed-server --server-config=%s,' % bootstrap.serverConfig + '/subsystem=datasources/data-source=pauds:write-attribute(name="max-pool-size", value="80")') # jboss restart required, performed after PUI deployment else: uLogging.debug("nothing done")
def _upgradeHelmPackage(self, pkg, reuse_values_file_path): command = '%s upgrade %s %s --wait' % (self._getHelmCommand(), pkg.name, pkg.chart) if pkg.group and (pkg.group == HelmGroup.FOUNDATION or pkg.group == HelmGroup.REQUIRED): command = '%s --install' % command if pkg.version and pkg.version != 'latest': command = '%s --version %s' % (command, pkg.version) command = '%s -f %s' % (command, reuse_values_file_path) dockerRepo = self._getKubernetesDockerRepositoryHost() if dockerRepo: command = '%s --set dockerrepo=%s' % (command, dockerRepo) with tempfile.NamedTemporaryFile() as values_file: command = '%s -f %s' % (command, values_file.name) self._fillHelmPackageValuesFile(pkg, values_file) uLogging.info('Upgrade Helm package: %s (%s)' % (command, pkg.script)) uUtil.readCmd(command) return command
def install(localesIds, batchMode): try: oaHostId = Locales.__getOaHostId() baHostId = Locales.__getBaHostId() Locales.checkAvailability(localesIds) Locales.checkLimit(localesIds, oaHostId, baHostId) if baHostId: uLogging.info( 'Stores will be synchronized during locales update process.' ) uLogging.info( 'Billing services will be restarted during locales update process and may be unavailable for some time.' ) if not batchMode and not uDialog.askYesNo( 'Do you wish to continue?'): uLogging.info('Locales update was rejected.') return oaLocales = OaLocale.getAvailableLocales(oaHostId) baLocales = BaLocale.getAvailableLocales(baHostId) baNeedConfigure = False for localeId in localesIds: if localeId in oaLocales: for hostId in uPEM.getAllUiHosts(): uiLocales = OaLocale.getAvailableLocales(hostId) if localeId in uiLocales: uiLocales[localeId].install(hostId) else: uLogging.err( 'OA locale "%s" was not found (hostId=%s)' % (localeId, hostId)) else: uLogging.err('OA locale "%s" was not found (hostId=%s)' % (localeId, oaHostId)) continue if not baHostId: continue if localeId in baLocales: baLocales[localeId].install(baHostId) baNeedConfigure = True else: uLogging.err('BA locale "%s" was not found (hostId=%s)' % (localeId, baHostId)) continue if baHostId and baNeedConfigure: Locales.__configureBa(localesIds) except Exception as e: uLogging.debug(e)
def checkLimit(newLocalesIds, oaHostId, baHostId): oldLocales = Locales.getIds(None, oaHostId, baHostId) newLocales = Locales.getIds(newLocalesIds, None, None) allLocales = Locales.getIds(newLocalesIds, oaHostId, baHostId) uLogging.info('%d locale(s) already installed %s' % (len(oldLocales), oldLocales)) uLogging.info('%d locale(s) will be installed %s' % (len(newLocales), newLocales)) uLogging.info('%d locale(s) will be in total %s' % (len(allLocales), allLocales)) localesLimit = Locales.getInstalledLocalesLimit() if len(allLocales) > localesLimit: uLogging.err( 'Impossible to have in total more than %d locales (including English)' % localesLimit) raise Exception( 'Locales update was rejected: locales limit is reached (%d)' % localesLimit)
def _checkUpgradeHelmPackages(self, helm_packages): if not helm_packages: return deployedReleases = self._listDeployedReleases() if deployedReleases is None: return pkgs_to_upgrade = list(map(lambda x: x.name, helm_packages)) #required_cpu, required_memory, pkg_name_cpu, pkg_name_memory = self._getUpgradeRequirements(deployedReleases, pkgs_to_upgrade) pkgRequirements = self._getUpgradeRequirements(deployedReleases, pkgs_to_upgrade) nodesCapacity = self._K8S.getNodesAvailableResources() for pkgName in pkgRequirements: uLogging.info("Package: %s" % pkgName) (reqCpu, reqMem) = pkgRequirements[pkgName] uLogging.info("Requires: %s cpu, %s mem" % (reqCpu, reqMem)) fitNode = None fitCpu = fitMemory = True for nodeCapacity in nodesCapacity: (nodeCpu, nodeMem) = nodeCapacity uLogging.info("Node resources: %s cpu, %s mem" % (nodeCpu, nodeMem)) if reqCpu < nodeCpu and reqMem < nodeMem: # pod fits in the node fitNode = nodeCapacity fitCpu = fitMemory = True break if reqCpu < nodeCpu: fitMemory = False else: fitCpu = False if fitNode is None: if not fitCpu: raise notEnoughResourcesException('cpu', pkgName) if not fitMemory: raise notEnoughResourcesException('memory', pkgName) pendingPods = self._K8S.getPendingPods() if pendingPods: uLogging.warn( "The following pods are in Pending state in Kubernetes cluster:\n %s This may indicate lack of resources of other problems with the cluster!" % pendingPods)
def _log_cmd(msg): uLogging.info('\x1b[32m%s\x1b[0m' % msg)
sys.setdefaultencoding('utf-8') import getopt try: opts, args = getopt.getopt(sys.argv[1:], '', dict(_long_options).keys()) opts = dict(opts) except getopt.GetoptError, err: print str(err) _print_usage() sys.exit(2) if opts and '--help' in opts: _print_usage() sys.exit(0) try: k8s_node, repo = _init(opts) _main(k8s_node, repo) except KeyboardInterrupt: _save_traceback() uLogging.debug("Keybord interrupted") sys.exit(3) except Exception, e: uLogging.err("%s", e) _save_traceback() if uLogging.logfile: uLogging.info("See additional info at %s" % uLogging.logfile.name) exit(1)
def _log_stdout(msg): uLogging.info(msg)
def _loadsDeployedReleases(self): command = '%s list --output json' % self._getHelmCommand() uLogging.info('Loads deployed releases: %s' % command) output = uUtil.readCmd(command) if not output: output = '{}' return json.loads(output).get('Releases', [])
def install(self, repo): self.deploy_master() self.setup_mn(repo) self.setup_oa() uLogging.info('Kubernetes is successfully registered')
def _log_stderr(msg): uLogging.info('\x1b[1m%s\x1b[0m', msg)
def checkCapacityForUpgrade(self, helms_to_update): if not self.isHelmInstalled(): return self._checkUpgradeHelmPackages(helms_to_update) uLogging.info('Kubernetes cluster has enough resources for upgrade')
def _updateHelmRepo(self): command = ' %s repo update' % self._getHelmCommand() uLogging.info('Update Helm repository: %s' % command) uUtil.readCmd(command)
def _setupK8sApiUrl(self): api_url = uUtil.readCmd( 'kubectl config view -o jsonpath="{.clusters[*].cluster.server}"') uLogging.info('Setup Kubernetes API URL: %s' % api_url) self._setSystemProperty('kubernetes_api', api_url)
def _upgradeHelmRepo(self, version): uLogging.info('Upgrade Helm repository to version: %s' % version) repo = self._K8S.get_helm_repo() if not repo.is_upgradable_url(): return command = self._K8S.get_custom_helm_repo_add_cmd(repo, version) uUtil.readCmd(command)
def _setupHelmRepo(self, version): repo = uK8s.Repo(prefix=self.k8s_repo_url) repo_url = repo.custom_url(version) uLogging.info('Setup Helm repository: %s' % repo_url) command = self._K8S.get_custom_helm_repo_add_cmd(repo, version) uUtil.readCmd(command)