Skip to content

Commit

Permalink
Fix inability to properly import settings backup
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed Dec 13, 2023
1 parent 0150f93 commit f7825ba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Features

- Fix inability to import settings backup
- Fix inability to properly import settings backup
- Add ability to use Actions in Custom Functions
- Add Input Action: Execute Python 3 Code ([#1334](https://github.com/kizniche/Mycodo/issues/1334))
- Add Function: Adafruit Neokey (Key Press Executes Actions) ([#1353](https://github.com/kizniche/Mycodo/issues/1353))
Expand Down
14 changes: 7 additions & 7 deletions mycodo/mycodo_flask/routes_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,25 +646,25 @@ def get_raspi_config_settings():
'spi_enabled': None,
'hostname': None
}
i2c_status, _, _ = cmd_output("raspi-config nonint get_i2c")
i2c_status, _, _ = cmd_output("raspi-config nonint get_i2c", user="root")
if i2c_status:
settings['i2c_enabled'] = not bool(int(i2c_status))
ssh_status, _, _ = cmd_output("raspi-config nonint get_ssh")
ssh_status, _, _ = cmd_output("raspi-config nonint get_ssh", user="root")
if ssh_status:
settings['ssh_enabled'] = not bool(int(ssh_status))
cam_status, _, _ = cmd_output("raspi-config nonint get_camera")
cam_status, _, _ = cmd_output("raspi-config nonint get_camera", user="root")
if cam_status:
settings['pi_camera_enabled'] = not bool(int(cam_status))
one_wire_status, _, _ = cmd_output("raspi-config nonint get_onewire")
one_wire_status, _, _ = cmd_output("raspi-config nonint get_onewire", user="root")
if one_wire_status:
settings['one_wire_enabled'] = not bool(int(one_wire_status))
serial_status, _, _ = cmd_output("raspi-config nonint get_serial")
serial_status, _, _ = cmd_output("raspi-config nonint get_serial", user="root")
if serial_status:
settings['serial_enabled'] = not bool(int(serial_status))
spi_status, _, _ = cmd_output("raspi-config nonint get_spi")
spi_status, _, _ = cmd_output("raspi-config nonint get_spi", user="root")
if spi_status:
settings['spi_enabled'] = not bool(int(spi_status))
hostname_out, _, _ = cmd_output("raspi-config nonint get_hostname")
hostname_out, _, _ = cmd_output("raspi-config nonint get_hostname", user="root")
if hostname_out:
settings['hostname'] = hostname_out.decode("utf-8")
return settings
27 changes: 17 additions & 10 deletions mycodo/mycodo_flask/utils/utils_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
PATH_HTML_USER, PATH_INPUTS_CUSTOM,
PATH_OUTPUTS_CUSTOM, PATH_PYTHON_CODE_USER,
PATH_USER_SCRIPTS, PATH_WIDGETS_CUSTOM,
SQL_DATABASE_MYCODO)
SQL_DATABASE_MYCODO, DATABASE_PATH)
from mycodo.config_translations import TRANSLATIONS
from mycodo.mycodo_flask.utils.utils_general import (flash_form_errors,
flash_success_errors)
Expand Down Expand Up @@ -148,32 +148,32 @@ def thread_import_settings(tmp_folder):
try:
# Initialize
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper initialize | ts '[%Y-%m-%d %H:%M:%S]' >> {IMPORT_LOG_FILE} 2>&1"
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

# Upgrade database
append_to_log(IMPORT_LOG_FILE, f"\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Upgrading database\n")
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper upgrade_database | ts '[%Y-%m-%d %H:%M:%S]' >> {IMPORT_LOG_FILE} 2>&1"
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

# Install/update dependencies (could take a while)
append_to_log(IMPORT_LOG_FILE, f"\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Installing dependencies (this can take a while)...\n")
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper update_dependencies | ts '[%Y-%m-%d %H:%M:%S]' >> {IMPORT_LOG_FILE} 2>&1"
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

# Generate widget HTML
generate_widget_html()

# Initialize
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper initialize | ts '[%Y-%m-%d %H:%M:%S]' >> {IMPORT_LOG_FILE} 2>&1"
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

# Start Mycodo daemon (backend)
append_to_log(IMPORT_LOG_FILE, f"\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Restarting backend")
if DOCKER_CONTAINER:
subprocess.Popen('docker start mycodo_daemon 2>&1', shell=True)
else:
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper daemon_restart | ts '[%Y-%m-%d %H:%M:%S]' >> {IMPORT_LOG_FILE} 2>&1"
a, b, c = cmd_output(cmd)
a, b, c = cmd_output(cmd, user="root")

# Delete tmp directory if it exists
if os.path.isdir(tmp_folder):
Expand All @@ -185,7 +185,7 @@ def thread_import_settings(tmp_folder):
subprocess.Popen('docker start mycodo_flask 2>&1', shell=True)
else:
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper frontend_reload | ts '[%Y-%m-%d %H:%M:%S]' >> {IMPORT_LOG_FILE} 2>&1"
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")
except:
logger.exception("thread_import_settings()")

Expand All @@ -197,7 +197,7 @@ def import_settings(form):
"""
Receive a zip file containing a Mycodo settings database that was
exported with export_settings(), then back up the current Mycodo settings
database and implement the one form the zip in its's place.
database and implement the one form the zip in its place.
"""
action = '{action} {controller}'.format(
action=TRANSLATIONS['import']['title'],
Expand Down Expand Up @@ -255,6 +255,7 @@ def import_settings(form):
full_path = os.path.join(tmp_folder, filename)
assure_path_exists(upload_folder)
assure_path_exists(tmp_folder)
append_to_log(IMPORT_LOG_FILE, f"\n\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Saving {filename} to {tmp_folder}")
form.settings_import_file.data.save(
os.path.join(tmp_folder, filename))

Expand All @@ -273,6 +274,7 @@ def import_settings(form):
try:
assure_path_exists(tmp_folder)
zip_ref = zipfile.ZipFile(full_path, 'r')
append_to_log(IMPORT_LOG_FILE, f"\n\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Extracting {full_path} to {tmp_folder}")
zip_ref.extractall(tmp_folder)
zip_ref.close()
except Exception as err:
Expand All @@ -287,13 +289,18 @@ def import_settings(form):
else:
# Stop Mycodo daemon (backend)
cmd = f"{INSTALL_DIRECTORY}/mycodo/scripts/mycodo_wrapper daemon_stop"
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

# Backup current database and replace with extracted mycodo.db
imported_database = os.path.join(tmp_folder, DATABASE_NAME)
backup_name = f"{SQL_DATABASE_MYCODO}.backup_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}"
full_path_backup = os.path.join(DATABASE_PATH, backup_name)

os.rename(SQL_DATABASE_MYCODO, backup_name) # rename current database to backup name
append_to_log(IMPORT_LOG_FILE,
f"\n\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Renaming {SQL_DATABASE_MYCODO} to {full_path_backup}")
os.rename(SQL_DATABASE_MYCODO, full_path_backup) # rename current database to backup name
append_to_log(IMPORT_LOG_FILE,
f"\n\n[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Moving {imported_database} to {SQL_DATABASE_MYCODO}")
shutil.move(imported_database, SQL_DATABASE_MYCODO) # move unzipped database to Mycodo

delete_directories = [
Expand Down
6 changes: 3 additions & 3 deletions mycodo/mycodo_flask/utils/utils_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,19 +1959,19 @@ def install_dependencies():
" | ts '[%Y-%m-%d %H:%M:%S]' >> {log} 2>&1".format(
pth=INSTALL_DIRECTORY,
log=DEPENDENCY_LOG_FILE)
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

cmd = "{pth}/mycodo/scripts/mycodo_wrapper frontend_restart" \
" | ts '[%Y-%m-%d %H:%M:%S]' >> {log} 2>&1".format(
pth=INSTALL_DIRECTORY,
log=DEPENDENCY_LOG_FILE)
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

cmd = "{pth}/mycodo/scripts/mycodo_wrapper daemon_restart" \
" | ts '[%Y-%m-%d %H:%M:%S]' >> {log} 2>&1".format(
pth=INSTALL_DIRECTORY,
log=DEPENDENCY_LOG_FILE)
_, _, _ = cmd_output(cmd)
_, _, _ = cmd_output(cmd, user="root")

install_deps = threading.Thread(target=install_dependencies)
install_deps.start()
Expand Down
2 changes: 1 addition & 1 deletion mycodo/utils/system_pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def dpkg_package_exists(package_name):
start = "dpkg-query -W -f='${Status}'"
end = '2>/dev/null | grep -c "ok installed"'
cmd = "{} {} {}".format(start, package_name, end)
_, _, stat = cmd_output(cmd)
_, _, stat = cmd_output(cmd, user="root")
if not stat:
return True

Expand Down

0 comments on commit f7825ba

Please sign in to comment.