2017-07-09 07:33:51 +02:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
import os
|
|
|
|
from subprocess import CalledProcessError
|
|
|
|
import sys
|
2017-07-10 19:13:57 +02:00
|
|
|
import textwrap
|
2017-07-09 07:33:51 +02:00
|
|
|
|
|
|
|
from ruamel import yaml
|
|
|
|
|
|
|
|
from borgmatic.config import convert, generate, legacy, validate
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_SOURCE_CONFIG_FILENAME = '/etc/borgmatic/config'
|
|
|
|
DEFAULT_SOURCE_EXCLUDES_FILENAME = '/etc/borgmatic/excludes'
|
|
|
|
DEFAULT_DESTINATION_CONFIG_FILENAME = '/etc/borgmatic/config.yaml'
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments(*arguments):
|
|
|
|
'''
|
|
|
|
Given command-line arguments with which this script was invoked, parse the arguments and return
|
|
|
|
them as an ArgumentParser instance.
|
|
|
|
'''
|
2017-07-10 18:43:25 +02:00
|
|
|
parser = ArgumentParser(
|
|
|
|
description='''
|
|
|
|
Convert legacy INI-style borgmatic configuration and excludes files to a single YAML
|
|
|
|
configuration file. Note that this replaces any comments from the source files.
|
|
|
|
'''
|
|
|
|
)
|
2017-07-09 07:33:51 +02:00
|
|
|
parser.add_argument(
|
2017-07-10 18:43:25 +02:00
|
|
|
'-s', '--source-config',
|
|
|
|
dest='source_config_filename',
|
2017-07-09 07:33:51 +02:00
|
|
|
default=DEFAULT_SOURCE_CONFIG_FILENAME,
|
|
|
|
help='Source INI-style configuration filename. Default: {}'.format(DEFAULT_SOURCE_CONFIG_FILENAME),
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-07-10 18:43:25 +02:00
|
|
|
'-e', '--source-excludes',
|
|
|
|
dest='source_excludes_filename',
|
|
|
|
default=DEFAULT_SOURCE_EXCLUDES_FILENAME if os.path.exists(DEFAULT_SOURCE_EXCLUDES_FILENAME) else None,
|
|
|
|
help='Excludes filename',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--destination-config',
|
|
|
|
dest='destination_config_filename',
|
2017-07-09 07:33:51 +02:00
|
|
|
default=DEFAULT_DESTINATION_CONFIG_FILENAME,
|
|
|
|
help='Destination YAML configuration filename. Default: {}'.format(DEFAULT_DESTINATION_CONFIG_FILENAME),
|
|
|
|
)
|
|
|
|
|
|
|
|
return parser.parse_args(arguments)
|
|
|
|
|
|
|
|
|
2017-07-10 19:13:57 +02:00
|
|
|
TEXT_WRAP_CHARACTERS = 80
|
|
|
|
|
|
|
|
|
|
|
|
def display_result(args): # pragma: no cover
|
|
|
|
result_lines = textwrap.wrap(
|
|
|
|
'Your borgmatic configuration has been upgraded. Please review the result in {}.'.format(
|
|
|
|
args.destination_config_filename
|
|
|
|
),
|
|
|
|
TEXT_WRAP_CHARACTERS,
|
|
|
|
)
|
|
|
|
|
|
|
|
delete_lines = textwrap.wrap(
|
|
|
|
'Once you are satisfied, you can safely delete {}{}.'.format(
|
|
|
|
args.source_config_filename,
|
|
|
|
' and {}'.format(args.source_excludes_filename) if args.source_excludes_filename else '',
|
|
|
|
),
|
|
|
|
TEXT_WRAP_CHARACTERS,
|
|
|
|
)
|
|
|
|
|
|
|
|
print('\n'.join(result_lines))
|
|
|
|
print()
|
|
|
|
print('\n'.join(delete_lines))
|
|
|
|
|
|
|
|
|
2017-07-10 02:03:45 +02:00
|
|
|
def main(): # pragma: no cover
|
2017-07-09 07:33:51 +02:00
|
|
|
try:
|
|
|
|
args = parse_arguments(*sys.argv[1:])
|
|
|
|
schema = yaml.round_trip_load(open(validate.schema_filename()).read())
|
2017-07-10 18:43:25 +02:00
|
|
|
source_config = legacy.parse_configuration(args.source_config_filename, legacy.CONFIG_FORMAT)
|
2017-07-10 19:37:11 +02:00
|
|
|
source_config_file_mode = os.stat(args.source_config_filename).st_mode
|
2017-07-10 18:43:25 +02:00
|
|
|
source_excludes = (
|
|
|
|
open(args.source_excludes_filename).read().splitlines()
|
|
|
|
if args.source_excludes_filename
|
|
|
|
else []
|
|
|
|
)
|
2017-07-09 07:33:51 +02:00
|
|
|
|
2017-07-10 18:43:25 +02:00
|
|
|
destination_config = convert.convert_legacy_parsed_config(source_config, source_excludes, schema)
|
2017-07-09 07:33:51 +02:00
|
|
|
|
2017-07-11 00:20:50 +02:00
|
|
|
generate.write_configuration(
|
|
|
|
args.destination_config_filename,
|
|
|
|
destination_config,
|
|
|
|
mode=source_config_file_mode,
|
|
|
|
)
|
2017-07-09 07:33:51 +02:00
|
|
|
|
2017-07-10 19:13:57 +02:00
|
|
|
display_result(args)
|
2017-07-09 07:33:51 +02:00
|
|
|
except (ValueError, OSError) as error:
|
|
|
|
print(error, file=sys.stderr)
|
|
|
|
sys.exit(1)
|