Пример #1
0
    def test_rotate(self, mock_subprocess):
        mock_subprocess.return_value = 0
        expected = dict(rotate=dict(status='ok',
                                    actions=[
                                        dict(host=self.testhost,
                                             location='short.0',
                                             prev=Constants.SYNC_PATH,
                                             savesets=1)
                                    ]))

        saveset = Saveset(location='short.0',
                          saveset='testrotate',
                          host_id=self.testhost_id,
                          backup_host_id=self.testhost_id)

        self.maxDiff = None
        self.session.add(saveset)
        self.session.commit()

        shutil.copytree(
            self.testdata_path,
            os.path.join(self.snapshot_root, 'short.0', self.testhost))
        obj = Actions(self.cli, db_engine=self.engine, db_session=self.session)
        obj.inject(self.testhost, self.volume,
                   os.path.join(self.snapshot_root, 'short.0'), saveset.id)
        ret = obj.rotate('short')
        self.assertEqual(ret, expected)
        mock_subprocess.assert_called_once_with(
            ['rsnapshot', '-c', self.rsnapshot_conf, 'short'])

        record = self.session.query(Saveset).filter(
            Saveset.saveset == 'testrotate').one()
        self.assertEqual(record.location, 'short.1')
Пример #2
0
def main():
    opts = Config().docopt_convert(docopt.docopt(__doc__))
    Syslog.logger = Syslog(opts)
    obj = Actions(opts)

    result = {}
    status = 'ok'

    if (opts['list-hosts']):
        result = obj.list_hosts()
    elif (opts['list-savesets']):
        result = obj.list_savesets()
    elif (opts['list-volumes']):
        result = obj.list_volumes()
    elif (opts['verify']):
        result = obj.verify(opts['verify'])
    elif (opts['version']):
        result = dict(version=[dict(name='secondshot %s' % __version__)])
    elif (opts['action'] == 'start'):
        result = obj.start(obj.hosts, obj.volume)
        status = result['start']['status']
    elif (opts['action'] == 'rotate'):
        result = obj.rotate(opts['interval'])
    elif (opts['action'] == 'schema-update'):
        result = obj.schema_update()
        status = result['status']
    else:
        sys.exit('Unknown action: %s' % opts['action'])

    if (opts['format'] == 'json'):
        sys.stdout.write(json.dumps(result) + '\n')
    elif (opts['format'] == 'text' and result and next(iter(result.keys())) in
          ['hosts', 'savesets', 'schema-update', 'version', 'volumes']):
        for item in result[next(iter(result.keys()))]:
            sys.stdout.write(item['name'] + '\n')
    if (status != 'ok'):
        exit(1)