Пример #1
0
def kubernetes_health_precheck(helms_to_update):
    import uHelm
    try:
        uHelm.clusterHealthPrecheck(helms_to_update)
    except uHelm.notEnoughResourcesException, e:
        uUtil.logLastException()
        raise PrecheckFailed(
            reason=e.message,
            what_to_do='Add more resources to Kubernetes cluster')
Пример #2
0
def readTablesFromDBManifests(con):
    progress.do("gathering schema information from manifests (from database)")
    cur = con.cursor()
    cur.execute("""SELECT sc.name, data FROM package_body p
	JOIN components c ON (c.pkg_id = p.pkg_id)
	JOIN sc_instances si ON (si.component_id = c.component_id)
	JOIN service_classes sc ON (sc.sc_id = si.sc_id)
	WHERE sc.name NOT IN ('pleskd', 'vzpemagent')""")

    manifest_tables = dict()
    for row in cur.fetchall():
        data = row[1]
        data = str(data)
        xml_text = uManifestParser.unsign_package(cStringIO.StringIO(data))
        for table in uPDLDBSchema.getSchemaFromString(xml_text):
            manifest_tables[table.name] = table

    # Add tables from ejb's reduced descriptor
    try:
        earFiles = []
        dummy, _mn_plesk_root = uPEM.getMNInfo()
        from u import bootstrap
        for root, subFolders, files in os.walk(
                bootstrap.getJBossDir(_mn_plesk_root) +
                '/standalone/data/content/'):
            for file in files:
                earFiles.append(os.path.join(root, file))

        wDir = tempfile.mkdtemp()
        for earFile in earFiles:
            progress.do("unzip ear file " + earFile + " to " + wDir)
            unzipFile(earFile, wDir)
            progress.done()

        jarFiles = [f for f in os.listdir(wDir) if f.endswith(".jar")]
        for jarFile in jarFiles:
            wDirJar = tempfile.mkdtemp()
            progress.do("unzip jar file " + jarFile + " to " + wDirJar)
            unzipFile(os.path.join(wDir, jarFile), wDirJar)
            progress.done()
            rdFilePath = os.path.join(wDirJar, 'rd.xml')
            if not os.path.isfile(rdFilePath):
                continue
            rdFile = open(rdFilePath, 'r')
            rdContent = rdFile.read()
            rdFile.close()
            for table in uPDLDBSchema.getSchemaFromString(rdContent):
                manifest_tables[table.name] = table

            shutil.rmtree(wDirJar)

        shutil.rmtree(wDir)

    except Exception, e:
        uUtil.logLastException()
        pass
Пример #3
0
def print_precheck_message_to_report(report_file, action_id, action_owner,
                                     message, counter):
    try:
        lines = message.splitlines()
        if len(lines) > 1:
            report_file.write(
                " 1.%-3s %s [%s]%s\n" %
                (str(counter) + '.', action_id, action_owner, ':'))
            for line in lines:
                report_file.write("\t" + line.strip() + "\n")
        else:
            report_file.write(
                " 1.%-3s %s [%s]: %s\n" %
                (str(counter) + '.', action_id, action_owner, lines[0]))
        report_file.write('\n')
    except:
        uUtil.logLastException()
        report_file.write('\n')
Пример #4
0
def checkURLAccessFromServiceNodes(hosts_to_check, url, in_precheck=False, proxy=""):
    problem_hosts = []

    for host in hosts_to_check:
        try:
            proxy_arg = ""
            if proxy:
                proxy_arg = "--proxy %s" % proxy
            request = uHCL.Request(host.host_id, user='******', group='root')
            # HTTP 301 redirect for requesting folders w/o trailing slash
            request.command(
                "curl -o /dev/null --silent --head -L --write-out '%%{http_code}' %s %s" % (url, proxy_arg), stdout='stdout', stderr='stderr', valid_exit_codes=range(0, 100))
            output = request.performCompat()
            if output['stdout'] != "200":
                uLogging.err('URL "%s" is not accessible from host "%s": HTTP response code is "%s"' % (
                    url, host, output['stdout']))
                problem_hosts += [(host, 'HTTP response code for %s is %s' % (url, output['stdout']))]
        except Exception, e:
            uUtil.logLastException()
            problem_hosts += [(host, str(e))]
        except:
Пример #5
0
            if proxy:
                proxy_arg = "--proxy %s" % proxy
            request = uHCL.Request(host.host_id, user='******', group='root')
            # HTTP 301 redirect for requesting folders w/o trailing slash
            request.command(
                "curl -o /dev/null --silent --head -L --write-out '%%{http_code}' %s %s" % (url, proxy_arg), stdout='stdout', stderr='stderr', valid_exit_codes=range(0, 100))
            output = request.performCompat()
            if output['stdout'] != "200":
                uLogging.err('URL "%s" is not accessible from host "%s": HTTP response code is "%s"' % (
                    url, host, output['stdout']))
                problem_hosts += [(host, 'HTTP response code for %s is %s' % (url, output['stdout']))]
        except Exception, e:
            uUtil.logLastException()
            problem_hosts += [(host, str(e))]
        except:
            uUtil.logLastException()
            problem_hosts += [(host, 'General error')]

    if problem_hosts:
        table = uTextRender.Table()
        table.setHeader(["host", "url", "issue"])
        for issue in problem_hosts:
            host, error = issue
            table.addRow([str(host), url, error])
        message = "URL %s is expected to be available from all managed hosts with pleskd RPM installed. " \
                  "Some of them listed bellow have access problems:\n%s" % (url, table)
        recommendation = "Fix accessibility problems."
        raise uPrecheck.PrecheckFailed(message, recommendation)


def getPleskdProps():
Пример #6
0
def performActions(actions, precheck=False, save_to_db=True):
    for action in actions:
        if action.id:
            con = uSysDB.connect()
            cur = con.cursor()
            cur.execute(
                "SELECT 1 FROM updater_atomic_actions WHERE action_id = %s",
                action.id)
            performed = cur.fetchone()
            cur.close()

            if performed:
                uLogging.info("action %s is already performed, skipping it",
                              action.id)
                continue

        started = time.time()
        progress.do("executing %s %s owner %s", action.kind(), action.id,
                    action.owner)

        success = False

        if not precheck:
            try:
                action_result = retriable(action.execute, True,
                                          globals())(readonly=False,
                                                     precheck=False)
                if action_result:
                    uLogging.info("Action %s result is: %s" %
                                  (action.id, action_result))
                success = True
            except ActionIgnored:
                uLogging.warn("Action %s of %s has been ignored", action.id,
                              action.owner)
        else:  # if precheck
            try:
                record = cStringIO.StringIO()
                uLogging.start_recording(record)
                try:
                    action.execute(readonly=True, precheck=True)
                    uPrecheck.precheck_results.append(
                        (action.id, action.owner, record.getvalue().strip()))
                except Exception, e:
                    uPrecheck.precheck_results.append(
                        (action.id, action.owner, e))
                    uUtil.logLastException()
            finally:
                uLogging.stop_recording()

        done = time.time()

        actions_timing.append((action.id, started, done))

        progress.done()
        if save_to_db and not precheck and success and action.id:
            to_insert = None
            if not action_result is None:
                to_insert = pickle.dumps(action_result)
                to_insert = uSysDB.toRaw(to_insert)

            con = uSysDB.connect()
            cur = con.cursor()
            cur.execute(
                "INSERT INTO updater_atomic_actions (action_id, action_output) VALUES(%s, %s)",
                (action.id, to_insert))
            con.commit()
Пример #7
0
    def fn(*args, **kwds):
        default_tried = False
        while True:
            try:
                return fun(*args, **kwds)
            except (KeyboardInterrupt, Exception), e:
                print_trace = True
                if isinstance(e, KeyboardInterrupt):
                    err_msg = "Interrupted"
                    print_trace = False
                elif not len(e.args):
                    err_msg = e.__class__.__name__
                else:
                    err_msg = ' '.join([str(x) for x in e.args])
                if print_trace:
                    uUtil.logLastException()

                progress_definition = ""

                if progress.what():
                    progress_definition = 'in "{progress_name}"'.format(
                        progress_name=progress.what())

                uLogging.debug(
                    "\n\n{exc_name} occurred {progress_definition}: {err_msg}".
                    format(exc_name=e.__class__.__name__,
                           progress_definition=progress_definition,
                           err_msg=err_msg))
                answer = None
                question = "An error has occurred, please select next action:\n(r)etry/abort/ignore"
                if allow_console:
                    question += "/(c)onsole"
                question += ": \n\n"
                while not answer:
                    if default_error_action and not default_tried:
                        answer = default_error_action
                        default_tried = True
                    else:
                        answer = uDialog.input(question)
                    if abort_pattern.match(answer):
                        uLogging.warn(
                            "Action {progress_definition} has been ABORTED".
                            format(progress_definition=progress_definition))
                        raise
                    elif ignore_pattern.match(answer):
                        uLogging.warn(
                            "Action {progress_definition} has been IGNORED".
                            format(progress_definition=progress_definition))
                        if raise_on_ignore:
                            raise ActionIgnored()
                        else:
                            return None
                    elif retry_pattern.match(answer):
                        uLogging.warn(
                            "Retrying action {progress_definition} ...".format(
                                progress_definition=progress_definition))
                        pass
                    elif allow_console and console_pattern.match(answer):
                        console = code.InteractiveConsole(allow_console)
                        answer = None
                        try:
                            console.interact(
                                "Type sys.exit() when you're done")
                        except:
                            pass

                    else:
                        answer = None
            except:
Пример #8
0
def process_precheck_report(build_info, version, poa_version, config):

    errors = []
    messages = []
    now = time.localtime()
    report_filename = time.strftime("precheck-report-%Y-%m-%d-%H%M%S.txt", now)
    report_filename = os.path.abspath(
        os.path.join(os.path.dirname(config.log_file), report_filename))
    report_file = codecs.open(report_filename, encoding='utf-8', mode='w+')

    for action_id, action_owner, result in precheck_results:
        if not result:
            message = "OK\n"
            messages.append((action_id, action_owner, result, message))
        elif isinstance(result, Exception):
            message = ""
            if isinstance(result, PrecheckFailed):
                if not result.reason.endswith('\n'):
                    result.reason += '\n'
                message += "%s" % result.reason
                if result.what_to_do:
                    message += "You should: %s\n" % result.what_to_do
                else:
                    message += "\n"
            else:
                message += " UNEXPECTED ERROR. See precheck log for details. Failed to complete precheck action %s [%s]: %s\n" % (
                    action_id, action_owner, result)

            errors.append((action_id, action_owner, result, message))
        else:
            message = "%s\n" % result
            messages.append((action_id, action_owner, result, message))

    con = uSysDB.connect()
    cur = con.cursor()
    cur.execute("SELECT company_name FROM accounts WHERE account_id = 1")
    company_name, = cur.fetchone()
    # TODO rewrite on PEMVersion.getCurrentVersion
    cur.execute("SELECT build FROM version_history ORDER BY install_date DESC")
    build_id, = cur.fetchone()
    cur.execute("SELECT name FROM hotfixes ORDER BY install_date DESC")
    hotfixes = cur.fetchone()

    report_file.write(
        "Operation Automation Upgrade Precheck Report for '%s'\n\n" %
        company_name)
    report_file.write("Current domain:            %s\n" % socket.getfqdn())
    report_file.write("Date of report:            %s\n" %
                      (time.strftime("%Y-%m-%d %H:%M:%S", now)))
    if not hotfixes:
        hotfix = ""
    else:
        hotfix = " (%s)" % hotfixes[0]
    report_file.write("Current Operation Automation build:         %s%s\n" %
                      (build_id, hotfix))
    target_build_id = ""
    for ver in build_info.version_list:
        name, build_ver, built, kind = ver
        target_build_id += "%s " % build_ver
    report_file.write("Target Operation Automation build:          %s\n" %
                      target_build_id)

    report_file.write("Precheck version:          %s\n" % version)
    report_file.write("Operation Automation version:               %s\n" %
                      poa_version)

    if errors:
        report_file.write(
            "\n%2s. Following errors need to be fixed before upgrade can be continued (refer also to '3. Additional information')\n\n"
            % '1')
        counter = 1
        for action_id, action_owner, result, message in errors:
            print_precheck_message_to_report(report_file, action_id,
                                             action_owner, message, counter)
            counter += 1
        report_file.write(
            "\n%2s. Following checks have passed without errors\n" % '2')
    else:
        report_file.write(
            "%2s. Success\n%6sNo issues preventing upgrade were found: you may continue with upgrade, though before that, please, check results below.\n\n"
            % ('1', ' '))

    skipped_table = uTextRender.Table(indent=2)
    skipped_table.setHeader(["Owner", "Skipped actions"])

    results_skipped = {}
    results_fine = ''
    results_fine_order = {}
    succeeded, skipped, other = 0, 0, 0
    other_results = ''
    for action_id, action_owner, result, message in messages:
        try:
            if result and ', skipping' in result:
                skipped += 1
                if not results_skipped.has_key(result):
                    results_skipped[result] = ''
                results_skipped[result] += "%s\n" % action_id
            elif result and ', skipping' not in result:
                other += 1
                other_results += ' %s.%-3s %s [%s]:\n       %s\n' % (
                    '3', str(other) + '.', action_id, action_owner,
                    result.replace('\n', '\n       '))
            else:
                succeeded += 1
                if not results_fine_order.has_key(result):
                    results_fine_order[result] = 0
                if results_fine_order[result] < 1:
                    results_fine += "  %-85s" % ("%s [%s]" %
                                                 (action_id, action_owner))
                    results_fine_order[result] += 1
                else:
                    results_fine += "  %s\n" % ("%s [%s]" %
                                                (action_id, action_owner))
                    results_fine_order[result] = 0
        except Exception, e:
            other_results += ' %s.%-3s *** Processing of action %s FAILED: %s. Check %s\n' % (
                '3', str(other) + '.', action_id, e, config.log_file)
            uUtil.logLastException()
        except: