1 | import configparser |
||
2 | import os |
||
3 | import sqlite3 |
||
4 | |||
5 | from backuppc_clone.command.BaseCommand import BaseCommand |
||
6 | from backuppc_clone.exception.BackupPcCloneException import BackupPcCloneException |
||
7 | |||
8 | |||
9 | View Code Duplication | class InitCloneCommand(BaseCommand): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
10 | """ |
||
11 | Creates the configuration file for a clone |
||
12 | |||
13 | init-clone |
||
14 | """ |
||
15 | parameters = [('SCHEMA_VERSION', 'schema version', '1'), |
||
16 | ('LAST_POOL_SYNC', 'timestamp of last original pool scan', '-1')] |
||
17 | |||
18 | # ------------------------------------------------------------------------------------------------------------------ |
||
19 | def __create_dirs(self, top_dir_clone: str) -> None: |
||
20 | """ |
||
21 | Creates required directories under the top dir of the clone. |
||
22 | |||
23 | @param str top_dir_clone: The top directory of the clone. |
||
24 | """ |
||
25 | os.chmod(top_dir_clone, 0o770) |
||
26 | |||
27 | self._io.writeln(' Creating directories') |
||
28 | |||
29 | dir_names = ['cpool', 'pool', 'etc', 'pc', 'tmp', 'trash'] |
||
30 | for dir_name in dir_names: |
||
31 | path = os.path.join(top_dir_clone, dir_name) |
||
32 | if not os.path.isdir(path): |
||
33 | self._io.log_verbose('Creating directory <fso>{}</fso>'.format(dir_name)) |
||
34 | os.mkdir(path, 0o700) |
||
35 | |||
36 | # ------------------------------------------------------------------------------------------------------------------ |
||
37 | def __create_database(self, db_path: str) -> None: |
||
38 | """ |
||
39 | Creates the metadata database. |
||
40 | |||
41 | @param str db_path: The path to the SQLite database. |
||
42 | """ |
||
43 | sql_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
||
44 | '..', |
||
45 | 'lib', |
||
46 | 'ddl', |
||
47 | '0100_create_tables.sql') |
||
48 | with open(sql_path) as file: |
||
49 | sql = file.read() |
||
50 | |||
51 | self._io.section('Creating metadata database') |
||
52 | |||
53 | self._io.writeln(' Initializing <fso>{}</fso>'.format(db_path)) |
||
54 | connection = sqlite3.connect(db_path) |
||
55 | cursor = connection.cursor() |
||
56 | cursor.executescript(sql) |
||
57 | |||
58 | for parameter in self.parameters: |
||
59 | cursor.execute('insert into BKC_PARAMETER(prm_code, prm_description, prm_value) values(?, ?, ?)', |
||
60 | parameter) |
||
61 | |||
62 | connection.commit() |
||
63 | connection.close() |
||
64 | |||
65 | # ------------------------------------------------------------------------------------------------------------------ |
||
66 | def __writing_config_clone(self, |
||
67 | config_filename_clone: str, |
||
68 | config_filename_original: str, |
||
69 | name_clone: str, |
||
70 | name_master: str) -> None: |
||
71 | """ |
||
72 | Creates the config file for the clone. |
||
73 | |||
74 | @param str config_filename_clone: The path to the config file of the clone. |
||
75 | @param str config_filename_original: The path to the config file of the original. |
||
76 | @param str name_clone: The name of the clone. |
||
77 | @param str name_master: The name of the master. |
||
78 | """ |
||
79 | self._io.writeln(' Writing <fso>{}</fso>'.format(config_filename_clone)) |
||
80 | |||
81 | config = configparser.ConfigParser() |
||
82 | config['BackupPC Clone'] = {'role': 'clone', |
||
83 | 'name': name_clone} |
||
84 | config['Original'] = {'config': config_filename_original, |
||
85 | 'name': name_master} |
||
86 | with open(config_filename_clone, 'w') as config_file: |
||
87 | config.write(config_file) |
||
88 | |||
89 | # ------------------------------------------------------------------------------------------------------------------ |
||
90 | def _init_singletons(self) -> None: |
||
91 | """ |
||
92 | Omits the creating of singleton objects. |
||
93 | """ |
||
94 | pass |
||
95 | |||
96 | # ------------------------------------------------------------------------------------------------------------------ |
||
97 | def _handle_command(self) -> None: |
||
98 | """ |
||
99 | Executes the command. |
||
100 | """ |
||
101 | self._io.title('Initializing Clone') |
||
102 | |||
103 | self._io.section('Creating configuration file') |
||
104 | |||
105 | top_dir_original = self.ask('top dir of the original', '/var/lib/BackupPC') |
||
106 | config_filename_original = os.path.join(top_dir_original, 'original.cfg') |
||
107 | |||
108 | if not os.path.isfile(config_filename_original): |
||
109 | raise BackupPcCloneException( |
||
110 | 'Configuration file {} of original not found'.format(config_filename_original)) |
||
111 | |||
112 | config_original = configparser.ConfigParser() |
||
113 | config_original.read(config_filename_original) |
||
114 | |||
115 | top_dir_clone = None |
||
116 | while top_dir_clone is None or not os.path.isdir(top_dir_clone): |
||
117 | top_dir_clone = self.ask('top dir of the clone') |
||
118 | |||
119 | name_clone = self.ask('name of clone', config_original['BackupPC Clone']['name'] + '-clone') |
||
120 | |||
121 | config_filename_clone = os.path.join(top_dir_clone, 'clone.cfg') |
||
122 | |||
123 | if os.path.isfile(config_filename_clone): |
||
124 | create = self.confirm('Overwrite {}'.format(config_filename_clone), False) |
||
125 | else: |
||
126 | create = True |
||
127 | |||
128 | if create: |
||
129 | self.__writing_config_clone(config_filename_clone, |
||
130 | config_filename_original, |
||
131 | name_clone, |
||
132 | config_original['BackupPC Clone']['name']) |
||
133 | |||
134 | self.__create_dirs(top_dir_clone) |
||
135 | |||
136 | self._io.writeln('') |
||
137 | |||
138 | self.__create_database(os.path.join(top_dir_clone, 'clone.db')) |
||
139 | else: |
||
140 | self._io.warning('No configuration file created') |
||
141 | |||
142 | # ---------------------------------------------------------------------------------------------------------------------- |
||
143 |