azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Management of debmirror |
| 4 | ======================= |
| 5 | |
| 6 | Create debian mirrors, using debmirror |
| 7 | -------------------------------------- |
| 8 | |
| 9 | .. code-block:: yaml |
| 10 | debmirror_test1_present: |
| 11 | debmirror.mirror_present: |
| 12 | - name: test1 |
| 13 | |
| 14 | """ |
| 15 | import logging |
| 16 | import os |
| 17 | from functools import wraps |
| 18 | from salt.exceptions import CommandExecutionError, SaltInvocationError |
| 19 | log = logging.getLogger(__name__) |
| 20 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 21 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 22 | def __virtual__(): |
| 23 | ''' |
| 24 | Only load this module if debmirror is installed with deps. |
| 25 | ''' |
| 26 | return 'debmirror' |
| 27 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 28 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 29 | def _test_call(method): |
| 30 | (resource, functionality) = method.func_name.split('_') |
| 31 | if functionality == 'present': |
| 32 | functionality = 'updated' |
| 33 | else: |
| 34 | functionality = 'removed' |
| 35 | |
| 36 | @wraps(method) |
| 37 | def check_for_testing(name, *args, **kwargs): |
| 38 | if __opts__.get('test', None): |
| 39 | return _no_change(name, resource, test=functionality) |
| 40 | return method(name, *args, **kwargs) |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 41 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 42 | return check_for_testing |
| 43 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 44 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 45 | def _created(name, resource, resource_definition={}): |
| 46 | changes_dict = {'name': name, |
| 47 | 'changes': resource_definition, |
| 48 | 'result': True, |
| 49 | 'comment': '{0} {1} created'.format(resource, name)} |
| 50 | return changes_dict |
| 51 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 52 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 53 | def _failed(name, resource, resource_definition={}): |
| 54 | changes_dict = {'name': name, |
| 55 | 'changes': resource_definition, |
| 56 | 'result': False, |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 57 | 'comment': '{0} {1} failed to create'.format(resource, |
| 58 | name)} |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 59 | return changes_dict |
| 60 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 61 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 62 | def _no_change(name, resource, test=False): |
| 63 | changes_dict = {'name': name, |
| 64 | 'changes': {}, |
| 65 | 'result': True} |
| 66 | if test: |
| 67 | changes_dict['comment'] = \ |
| 68 | '{0} {1} will be {2}'.format(resource, name, test) |
| 69 | else: |
| 70 | changes_dict['comment'] = \ |
| 71 | '{0} {1} is in correct state'.format(resource, name) |
| 72 | return changes_dict |
| 73 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 74 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 75 | def _check_state(name, tgt): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 76 | lock_file = _get_target_path(name, tgt)['lock_file'] |
| 77 | if os.path.isfile(lock_file) and not tgt.get('force', False): |
azvyagintsev | b80d622 | 2018-02-19 15:53:40 +0200 | [diff] [blame] | 78 | return _no_change(name, '{} exist=>repo locked.'.format(lock_file)) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 79 | return False |
| 80 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 81 | |
| 82 | def _get_target_path(name, tgt): |
| 83 | if not tgt.get('target_dir', False): |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 84 | raise SaltInvocationError('Argument "target_dir" is mandatory! ') |
| 85 | return {'target_dir': tgt.get('target_dir'), |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 86 | 'lock_file': tgt.get('lock_file', |
| 87 | os.path.join(tgt.get('target_dir'), |
| 88 | '.lockmirror')), |
| 89 | 'log_file': tgt.get('log_file', '/var/log/debmirror_'.join(name))} |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 90 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 91 | |
| 92 | def _get_env(tgt): |
| 93 | env = {} |
| 94 | for k in ['http_proxy', 'https_proxy', 'ftp_proxy', 'rsync_proxy']: |
| 95 | if tgt.get(k, False): |
| 96 | env[k] = tgt[k] |
| 97 | env[k.upper()] = env[k] |
| 98 | if tgt.get('no_proxy', False): |
| 99 | env['no_proxy'] = ','.join(str(x) for x in tgt['no_proxy']) |
| 100 | env['no_proxy'.upper()] = env['no_proxy'] |
| 101 | return env |
| 102 | |
| 103 | |
| 104 | def _get_cmdline(name, tgt): |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 105 | cmdline = " debmirror " |
| 106 | if tgt.get('extra_flags'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 107 | cmdline += ' '.join(tgt['extra_flags']) |
Denis Egorenko | 39db2a6 | 2018-10-30 14:37:17 +0400 | [diff] [blame] | 108 | if tgt.get('rsync_options'): |
| 109 | rsync_options = tgt.get('rsync_options') |
| 110 | if tgt.get('rsync_options_defaults', True): |
| 111 | rsync_options = ['-aL', '--partial'] + rsync_options |
| 112 | cmdline += " --rsync-options='" + ' '.join(rsync_options) + "'" |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 113 | if tgt.get('dist'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 114 | cmdline += ' --dist=' + ",".join(tgt['dist']) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 115 | if tgt.get('section'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 116 | cmdline += ' --section=' + ",".join(tgt['section']) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 117 | if tgt.get('method'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 118 | cmdline += ' --method=' + tgt.get('method') |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 119 | if tgt.get('mirror_host'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 120 | cmdline += ' --host="{}"'.format(tgt.get('mirror_host')) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 121 | if tgt.get('mirror_root'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 122 | cmdline += ' --root="{}"'.format(tgt.get('mirror_root')) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 123 | if tgt.get('arch', 'amd64'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 124 | cmdline += ' --arch=' + ','.join(tgt.get('arch')) |
azvyagintsev | 6e410f9 | 2018-03-07 23:35:05 +0200 | [diff] [blame] | 125 | if tgt.get('exclude_deb_section'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 126 | for section in tgt['exclude_deb_section']: |
| 127 | cmdline += " --exclude-deb-section='" + section + "'" |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 128 | if tgt.get('filter'): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 129 | for key, value in enumerate(sorted(tgt['filter'])): |
| 130 | cmdline += " " + tgt['filter'][value] |
| 131 | if tgt.get('target_dir', False): |
| 132 | cmdline += ' ' + _get_target_path(name, tgt)['target_dir'] |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 133 | return cmdline |
| 134 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 135 | |
| 136 | def _update_mirror(name, tgt): |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 137 | # Remove old lock file, is was. |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 138 | lock_file = _get_target_path(name, tgt)['lock_file'] |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 139 | if os.path.isfile(lock_file): |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 140 | log.debug("Removing lockfile:{} for mirror{}".format(lock_file, name)) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 141 | __states__['file.absent'](lock_file) |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 142 | cmdline = _get_cmdline(name, tgt) |
| 143 | # fetch ENV params for proxy |
| 144 | env_vars = _get_env(tgt) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 145 | # init file logger |
| 146 | l_dir = os.path.dirname(tgt['log_file']) |
| 147 | if not os.path.isdir(l_dir): |
azvyagintsev | f5fc653 | 2018-02-21 22:17:02 +0200 | [diff] [blame] | 148 | __salt__['file.makedirs'](l_dir + '/') |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 149 | fh = logging.FileHandler( |
| 150 | "{0}".format(_get_target_path(name, tgt)['log_file'])) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 151 | fh.setLevel(logging.DEBUG) |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 152 | fh_format = logging.Formatter( |
| 153 | '%(asctime)s - %(lineno)d - %(levelname)-8s - %(message)s') |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 154 | fh.setFormatter(fh_format) |
| 155 | log2file = logging.getLogger("debmirror") |
| 156 | log2file.addHandler(fh) |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 157 | result = __salt__['cmd.run_all'](cmdline, redirect_stderr=True, |
| 158 | env=env_vars) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 159 | log2file.debug(result['stdout']) |
| 160 | # destroy file logger |
| 161 | for i in list(log2file.handlers): |
| 162 | log2file.removeHandler(i) |
| 163 | i.flush() |
| 164 | i.close() |
| 165 | if result['retcode'] != 0: |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 166 | # raise CommandExecutionError(result['stderr']) |
| 167 | return _failed(name, |
| 168 | "debmirror failed.Reason {0}".format(result['stderr'])) |
| 169 | if tgt.get('lock_target', None): |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 170 | __states__['file.touch'](lock_file) |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 171 | return _created(name, "Mirror {0} created.".format(name)) |
| 172 | |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 173 | |
| 174 | @_test_call |
| 175 | def mirror_present(name, **kwargs): |
| 176 | ''' |
| 177 | |
| 178 | :param name: mirror key name |
| 179 | ''' |
| 180 | try: |
| 181 | tgt = __salt__['config.get']('debmirror')['client']['mirrors'][name] |
| 182 | except KeyError: |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 183 | comment = 'Mirror "{0}" not exist in configurathion,skipping..'.format( |
| 184 | name) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 185 | return _no_change(name, comment) |
| 186 | |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 187 | current_state = _check_state(name, tgt) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 188 | if not current_state: |
azvyagintsev | 73720db | 2018-06-20 17:28:09 +0300 | [diff] [blame] | 189 | return _update_mirror(name, tgt) |
azvyagintsev | e5c4e83 | 2018-01-25 20:02:46 +0200 | [diff] [blame] | 190 | return current_state |