npbackup/bin/NPBackupInstaller.py

167 lines
5.5 KiB
Python
Raw Normal View History

2023-01-26 08:13:07 +08:00
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of npbackup
__intname__ = "npbackup.installer"
__author__ = "Orsiris de Jong"
__copyright__ = "Copyright (C) 2023 NetInvent"
__license__ = "GPL-3.0-only"
__build__ = "2023012901"
__version__ = "1.1.7"
2023-01-26 08:13:07 +08:00
import sys
import os
import shutil
from distutils.dir_util import copy_tree
from command_runner import command_runner
import ofunctions.logger_utils
2023-01-29 01:07:45 +08:00
# Insert parent dir as path se we get to use npbackup as package
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(__file__), "..")))
2023-01-27 18:30:06 +08:00
from npbackup.customization import PROGRAM_NAME, PROGRAM_DIRECTORY
from npbackup.path_helper import CURRENT_DIR
2023-01-26 08:13:07 +08:00
2023-01-29 01:07:45 +08:00
del sys.path[0]
2023-01-26 08:13:07 +08:00
2023-01-26 08:26:08 +08:00
_DEBUG = os.environ.get("_DEBUG", False)
LOG_FILE = os.path.join(CURRENT_DIR, __intname__ + ".log")
2023-01-26 08:13:07 +08:00
BASEDIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
2023-01-26 08:13:07 +08:00
2023-01-26 08:26:08 +08:00
INSTALL_TO = "{}{}{}".format(
os.environ.get("PROGRAMFILES", None), os.sep, PROGRAM_DIRECTORY
)
NPBACKUP_EXECUTABLE = "npbackup.exe" if os.name == "nt" else "npbackup"
2023-01-26 08:13:07 +08:00
NPBACKUP_INSTALLED_EXECUTABLE = os.path.join(INSTALL_TO, NPBACKUP_EXECUTABLE)
FILES_TO_COPY = [NPBACKUP_EXECUTABLE]
2023-01-26 08:26:08 +08:00
DIRS_TO_COPY = ["excludes"]
CONF_FILE = "npbackup.conf.dist"
2023-01-26 08:13:07 +08:00
logger = ofunctions.logger_utils.logger_get_logger(LOG_FILE, debug=_DEBUG)
def install(config_file=None):
logger.info("Running {} {}".format(__intname__, __version__))
# We need to stop / disable current npbackup tasks so we don't get race conditions
2023-01-26 08:26:08 +08:00
exit_code, output = command_runner(
'schtasks /END /TN "{}"'.format(PROGRAM_NAME),
valid_exit_codes=[0, 1],
windows_no_window=True,
encoding="cp437",
)
2023-01-26 08:13:07 +08:00
if exit_code != 0:
2023-01-26 08:26:08 +08:00
logger.error(
"Could not terminate currant scheduled task {}:\{}".format(
PROGRAM_NAME, output
)
)
2023-01-26 08:13:07 +08:00
# Create destination directory
if not os.path.isdir(INSTALL_TO):
2023-01-26 08:26:08 +08:00
try:
os.makedirs(INSTALL_TO)
except OSError as exc:
logger.error("Could not create directory {}: {}".format(INSTALL_TO, exc))
return False
2023-01-26 08:13:07 +08:00
# Copy files
for file in FILES_TO_COPY:
source = os.path.join(BASEDIR, file)
destination = os.path.join(INSTALL_TO, file)
try:
logger.info("Copying {} to {}".format(source, destination))
shutil.copy2(source, destination)
except OSError as exc:
2023-01-26 08:26:08 +08:00
logger.error(
"Could not copy file {} to {}: {}".format(source, destination, exc)
)
2023-01-26 08:13:07 +08:00
return False
# Copy dirs
for dir in DIRS_TO_COPY:
source = os.path.join(BASEDIR, dir)
destination = os.path.join(INSTALL_TO, dir)
try:
logger.info("Copying {} to {}".format(source, destination))
copy_tree(source, destination)
except OSError as exc:
2023-01-26 08:26:08 +08:00
logger.error(
"Could not copy directory {} to {}: {}".format(source, destination, exc)
)
2023-01-26 08:13:07 +08:00
return False
# Copy distribution config file if none given, never overwrite existing conf file
if not config_file:
# Execute config file modification if needed
source = os.path.join(BASEDIR, CONF_FILE)
destination = os.path.join(INSTALL_TO, CONF_FILE.replace(".dist", ""))
2023-01-26 08:13:07 +08:00
config_file = destination
if os.path.isfile(destination):
2023-01-26 08:26:08 +08:00
logger.info("Keeping in place configuration file.")
destination = os.path.join(INSTALL_TO, CONF_FILE)
2023-01-26 08:13:07 +08:00
try:
logger.info("Copying {} to {}".format(source, destination))
shutil.copy2(source, destination)
except OSError as exc:
2023-01-26 08:26:08 +08:00
logger.error(
"Could not copy file {} to {}: {}".format(source, destination, exc)
)
2023-01-26 08:13:07 +08:00
return False
2023-01-26 08:26:08 +08:00
logger.info(
"Running configuration from {}".format(NPBACKUP_INSTALLED_EXECUTABLE)
)
exit_code, output = command_runner(
'"{}" --config-gui --config-file "{}"'.format(
NPBACKUP_INSTALLED_EXECUTABLE, config_file
),
shell=False,
timeout=3600,
)
2023-01-26 08:13:07 +08:00
if exit_code != 0:
logger.error("Could not run config gui:\n{}".format(output))
return False
2023-01-26 08:26:08 +08:00
2023-01-26 08:13:07 +08:00
else:
destination = os.path.join(INSTALL_TO, os.path.basename(config_file))
try:
logger.info("Copying {} to {}".format(source, destination))
shutil.copy2(config_file, destination)
except OSError as exc:
2023-01-26 08:26:08 +08:00
logger.error(
"Could not copy file {} to {}: {}".format(source, destination, exc)
)
2023-01-26 08:13:07 +08:00
return False
# Create task
2023-01-26 08:26:08 +08:00
exit_code, output = command_runner(
'"{}" --create-scheduled-task 15'.format(
NPBACKUP_INSTALLED_EXECUTABLE, shell=False, timeout=300
)
)
2023-01-26 08:13:07 +08:00
if exit_code != 0:
2023-01-26 08:26:08 +08:00
logger.info("Could not create scheduled task:\n".format(output))
2023-01-26 08:13:07 +08:00
else:
2023-01-26 08:26:08 +08:00
logger.info("Created scheduled task.")
2023-01-26 08:13:07 +08:00
logger.info("Installation succesful.")
return True
2023-01-26 08:26:08 +08:00
if __name__ == "__main__":
2023-01-26 08:13:07 +08:00
try:
result = install(sys.argv[1:])
if not result:
sys.exit(12)
sys.exit(0)
except KeyboardInterrupt as exc:
2023-01-26 08:26:08 +08:00
logger.error("Program interrupted by keyboard. {}".format(exc))
logger.info("Trace:", exc_info=True)
2023-01-26 08:13:07 +08:00
sys.exit(200)
except Exception as exc:
2023-01-26 08:26:08 +08:00
logger.error("Program interrupted by error. {}".format(exc))
logger.info("Trace", exc_info=True)
sys.exit(201)