def admin_upgrade(): """ Display any available upgrades and option to upgrade """ if not utils_general.user_has_permission('edit_settings'): return redirect(url_for('routes_general.home')) if not internet(): flash( gettext("Upgrade functionality is disabled because an internet " "connection was unable to be detected"), "error") return render_template('admin/upgrade.html', is_internet=False) # Read from the upgrade status file created by the upgrade script # to indicate if the upgrade is running. try: with open(UPGRADE_INIT_FILE) as f: upgrade = int(f.read(1)) except IOError: try: with open(UPGRADE_INIT_FILE, 'w') as f: f.write('0') finally: upgrade = 0 if upgrade: if upgrade == 2: flash( gettext("There was an error encountered during the upgrade" " process. Check the upgrade log for details."), "error") return render_template('admin/upgrade.html', upgrade=upgrade) form_backup = forms_misc.Backup() form_upgrade = forms_misc.Upgrade() is_internet = True upgrade_available = False # Check for any new Mycodo releases on github releases = [] try: current_maj_version = int(MYCODO_VERSION.split('.')[0]) releases = github_releases(current_maj_version) except Exception as err: flash( gettext("Could not determine local mycodo version or " "online release versions: {err}".format(err=err)), "error") if len(releases): current_latest_release = github_latest_release() current_latest_major_version = current_latest_release.split('.')[0] current_major_release = releases[0] current_releases = [] releases_behind = None for index, each_release in enumerate(releases): if parse_version(each_release) >= parse_version(MYCODO_VERSION): current_releases.append(each_release) if parse_version(each_release) == parse_version(MYCODO_VERSION): releases_behind = index if (parse_version(releases[0]) > parse_version(MYCODO_VERSION) or parse_version(current_latest_release[0]) > parse_version(MYCODO_VERSION)): upgrade_available = True else: current_releases = [] current_latest_release = '0.0.0' current_latest_major_version = '0' current_major_release = '0.0.0' releases_behind = 0 # Update database to reflect the current upgrade status mod_misc = Misc.query.first() if mod_misc.mycodo_upgrade_available != upgrade_available: mod_misc.mycodo_upgrade_available = upgrade_available db.session.commit() def not_enough_space_upgrade(): backup_size, free_before, free_after = can_perform_backup() if free_after / 1000000 < 50: flash( "A backup must be performed during an upgrade and there is " "not enough free space to perform a backup. A backup " "requires {size_bu:.1f} MB but there is only {size_free:.1f} " "MB available, which would leave {size_after:.1f} MB after " "the backup. If the free space after a backup is less than 50" " MB, the backup cannot proceed. Free up space by deleting " "current backups.".format(size_bu=backup_size / 1000000, size_free=free_before / 1000000, size_after=free_after / 1000000), 'error') return True else: return False if request.method == 'POST': if (form_upgrade.upgrade.data and (upgrade_available or FORCE_UPGRADE_MASTER)): if not_enough_space_upgrade(): pass elif FORCE_UPGRADE_MASTER: cmd = "{pth}/mycodo/scripts/mycodo_wrapper upgrade-master" \ " | ts '[%Y-%m-%d %H:%M:%S]'" \ " >> {log} 2>&1".format(pth=INSTALL_DIRECTORY, log=UPGRADE_LOG_FILE) subprocess.Popen(cmd, shell=True) upgrade = 1 flash(gettext("The upgrade (from master branch) has started"), "success") else: cmd = "{pth}/mycodo/scripts/mycodo_wrapper upgrade" \ " | ts '[%Y-%m-%d %H:%M:%S]'" \ " >> {log} 2>&1".format(pth=INSTALL_DIRECTORY, log=UPGRADE_LOG_FILE) subprocess.Popen(cmd, shell=True) upgrade = 1 mod_misc = Misc.query.first() mod_misc.mycodo_upgrade_available = False db.session.commit() flash(gettext("The upgrade has started"), "success") elif (form_upgrade.upgrade_next_major_version.data and upgrade_available): if not not_enough_space_upgrade(): cmd = "{pth}/mycodo/scripts/mycodo_wrapper upgrade-release-major {ver}" \ " | ts '[%Y-%m-%d %H:%M:%S]'" \ " >> {log} 2>&1".format(pth=INSTALL_DIRECTORY, ver=current_latest_major_version, log=UPGRADE_LOG_FILE) subprocess.Popen(cmd, shell=True) upgrade = 1 mod_misc = Misc.query.first() mod_misc.mycodo_upgrade_available = False db.session.commit() flash(gettext("The major version upgrade has started"), "success") else: flash(gettext("You cannot upgrade if an upgrade is not available"), "error") return render_template( 'admin/upgrade.html', final_releases=FINAL_RELEASES, force_upgrade_master=FORCE_UPGRADE_MASTER, form_backup=form_backup, form_upgrade=form_upgrade, current_release=MYCODO_VERSION, current_releases=current_releases, current_major_release=current_major_release, current_latest_release=current_latest_release, current_latest_major_version=current_latest_major_version, releases_behind=releases_behind, upgrade_available=upgrade_available, upgrade=upgrade, is_internet=is_internet)
def admin_upgrade(): """ Display any available upgrades and option to upgrade """ if not utils_general.user_has_permission('edit_settings'): return redirect(url_for('general_routes.home')) if not internet(): flash( gettext(u"Upgrade functionality is disabled because an internet " u"connection was unable to be detected"), "error") return render_template('admin/upgrade.html', is_internet=False) # Read from the upgrade status file created by the upgrade script # to indicate if the upgrade is running. try: with open(UPGRADE_INIT_FILE) as f: upgrade = int(f.read(1)) except IOError: try: with open(UPGRADE_INIT_FILE, 'w') as f: f.write('0') finally: upgrade = 0 if upgrade: if upgrade == 2: flash( gettext(u"There was an error encountered during the upgrade " u"process. Check the upgrade log for details."), "error") return render_template('admin/upgrade.html', upgrade=upgrade) form_backup = forms_misc.Backup() form_upgrade = forms_misc.Upgrade() is_internet = True upgrade_available = False # Check for any new Mycodo releases on github releases = [] try: maj_version = int(MYCODO_VERSION.split('.')[0]) releases = github_releases(maj_version) except Exception: flash( gettext(u"Could not determine local mycodo version or " u"online release versions"), "error") if len(releases): latest_release = releases[0] current_releases = [] releases_behind = None for index, each_release in enumerate(releases): if parse_version(each_release) >= parse_version(MYCODO_VERSION): current_releases.append(each_release) if parse_version(each_release) == parse_version(MYCODO_VERSION): releases_behind = index if parse_version(releases[0]) > parse_version(MYCODO_VERSION): upgrade_available = True else: current_releases = [] latest_release = '0.0.0' releases_behind = 0 # Update database to reflect the current upgrade status mod_misc = Misc.query.first() if mod_misc.mycodo_upgrade_available != upgrade_available: mod_misc.mycodo_upgrade_available = upgrade_available db.session.commit() if request.method == 'POST': if form_upgrade.upgrade.data and upgrade_available: cmd = "{pth}/mycodo/scripts/mycodo_wrapper upgrade" \ " | ts '[%Y-%m-%d %H:%M:%S]'" \ " >> {log} 2>&1".format(pth=INSTALL_DIRECTORY, log=UPGRADE_LOG_FILE) subprocess.Popen(cmd, shell=True) upgrade = 1 mod_misc = Misc.query.first() mod_misc.mycodo_upgrade_available = False db.session.commit() flash(gettext(u"The upgrade has started"), "success") else: flash( gettext(u"You cannot upgrade if an upgrade is not available"), "error") return render_template('admin/upgrade.html', form_backup=form_backup, form_upgrade=form_upgrade, current_release=MYCODO_VERSION, current_releases=current_releases, latest_release=latest_release, releases_behind=releases_behind, upgrade_available=upgrade_available, upgrade=upgrade, is_internet=is_internet)