Exemplo n.º 1
0
def api_backup_list():
    # Doc: https://bitbucket.org/mineboxgmbh/minebox-client-tools/src/master/doc/mb-ui-gateway-api.md#markdown-header-get-backuplist
    if not check_login():
        return jsonify(
            message="Unauthorized access, please log into the main UI."), 401
    metalist = backupinfo.get_list(True)
    return jsonify(metalist), 200
Exemplo n.º 2
0
def api_backup_all_status():
    # Doc: https://bitbucket.org/mineboxgmbh/minebox-client-tools/src/master/doc/mb-ui-gateway-api.md#markdown-header-get-backupallstatus
    if not check_login():
        return jsonify(message="Unauthorized access, please log into the main UI."), 401
    backuplist = backupinfo.get_list(True)

    statuslist = []
    for backupname in backuplist:
        backupstatus, status_code = backupinfo.get_status(backupname, True)
        statuslist.append(backupstatus)

    return jsonify(statuslist), 200
Exemplo n.º 3
0
def api_ping():
    # This can be called to just have the service run something.
    # For example, we need to do this early after booting to restart backups
    # if needed (via @app.before_first_request).

    if not os.path.isfile(MACHINE_AUTH_FILE):
        app.logger.info(
            "Submit machine authentication to Minebox admin service.")
        success, errmsg = submit_machine_auth()
        if not success:
            app.logger.error(errmsg)

    # Look if we need to run some system maintenance tasks.
    # Do this here so it runs even if Sia and upper storage are down.
    # Note that in the case of updates being available for backup-service,
    # this results in a restart and the rest of the ping will not be executed.
    success, errmsg = system_maintenance()
    if not success:
        app.logger.error(errmsg)

    # Check for synced sia consensus as a prerequisite to everything else.
    success, errmsg = check_sia_sync()
    if not success:
        # Return early, we need a synced consensus to do anything.
        app.logger.debug(errmsg)
        app.logger.info(
            "Exiting because sia is not ready, let's check again on next ping."
        )
        return "", 204

    if not os.path.ismount(MINEBD_STORAGE_PATH):
        current_app.logger.info(
            "Upper storage is not mounted (yet), let's check again on next ping."
        )
        return "", 204

    # See if sia is fully set up and do init tasks if needed.
    # Setting up hosting is the last step, so if that is not active, we still
    # need to do something.
    walletdata, wallet_status_code = get_from_sia('wallet')
    hostdata, host_status_code = get_from_sia('host')
    if wallet_status_code == 200 and host_status_code == 200:
        if not hostdata["internalsettings"]["acceptingcontracts"]:
            # We need to seed the wallet, set up allowances and hosting, etc.
            setup_sia_system(walletdata, hostdata)
        elif not walletdata["unlocked"]:
            # We should unlock the wallet so new contracts can be made.
            unlock_sia_wallet()

    # Trigger a backup if the latest is older than 24h.
    timenow = int(time.time())
    latestbackup = get_latest()
    timelatest = int(latestbackup) if latestbackup else 0
    if timelatest < timenow - 24 * 3600:
        success, errmsg = check_backup_prerequisites()
        if success:
            bthread = start_backup_thread()

    # If no backup is active but the most recent one is not finished,
    # perform a restart of backups.
    active_backups = get_running_backups()
    if not active_backups:
        snapname = get_latest()
        if snapname:
            if not is_finished(snapname):
                restart_backups()
    else:
        # If the upload step is stuck (taking longer than 30 minutes),
        # we should restart the sia service.
        # See https://github.com/NebulousLabs/Sia/issues/1605
        for tname in threadstatus:
            if (threadstatus[tname]["snapname"] in active_backups
                    and threadstatus[tname]["step"] == "initiate uploads"
                    and threadstatus[tname]["starttime_step"] <
                    time.time() - 30 * 60):
                # This would return True for success but already logs errors.
                restart_sia()
        # If the list of unfinished backups is significantly larger than active
        # backups, we very probably have quite a few backups hanging around
        # that we need to cleanup but don't get to routine cleanup (which
        # happens only when a backup finishes).
        unfinished_backups = get_list()
        if len(unfinished_backups) > len(active_backups) + 3:
            app.logger.info(
                "We have %s unfinished backups but only %s active ones, let's clean up."
                % (len(unfinished_backups), len(active_backups)))
            start_cleanup_thread()

    # Update Sia config if more than 10% off.
    update_sia_config()

    # See if we need to rebalance the disk space.
    rebalance_diskspace()

    return "", 204