#!/usr/bin/python3
#
# Copyright 2018 ScyllaDB
#

#
# This file is part of Scylla.
#
# Scylla is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Scylla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Scylla.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import argparse
from scylla_util import *

if __name__ == '__main__':
    if os.getuid() > 0:
        print('Requires root permission.')
        sys.exit(1)
    parser = argparse.ArgumentParser(description='Configure cpuset configuration for Scylla.')
    parser.add_argument('--cpuset',
                        help='CPUs to use (in cpuset(7) format; default: all))')
    parser.add_argument('--smp',
                        help='number of threads (default: one per CPU)')
    args = parser.parse_args()
    if not args.cpuset and not args.smp:
        parser.print_help()
        sys.exit(1)

    cfg = sysconfig_parser('/etc/scylla.d/cpuset.conf')
    cfg.set('CPUSET', '{cpuset}{smp}'.format( \
            cpuset='--cpuset {} '.format(args.cpuset) if args.cpuset else '', \
            smp='--smp {} '.format(args.smp) if args.smp else '' \
            ))
    cfg.commit()

    if os.path.exists('/etc/scylla.d/perftune.yaml'):
        os.remove('/etc/scylla.d/perftune.yaml')
