#!/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
import glob
import shutil
import io
import stat
from scylla_util import *

interactive = False

def interactive_ask_service(msg1, msg2, default = None):
    print(msg1)
    print(msg2)
    while True:
        if default == 'yes':
            prompt = '[YES/no]'
        elif default == 'no':
            prompt = '[yes/NO]'
        else:
            prompt = '[yes/no]'
        ans = input(prompt).lower()
        if len(ans) == 0:
            ans = default
        if ans == 'yes' or ans == 'y':
            return True
        elif ans == 'no' or ans =='n':
            return False

def interactive_choose_nic():
    nics = [os.path.basename(n) for n in glob.glob('/sys/class/net/*') if n != '/sys/class/net/lo']
    if len(nics) == 0:
        print('A NIC was not found.')
        sys.exit(1)
    elif len(nics) == 1:
        return nics[0]
    else:
        print('Please select a NIC from the following list:')
        while True:
            print(nics)
            n = input('> ')
            if is_valid_nic(n):
                return n

def do_verify_package(pkg):
    if is_debian_variant():
        res = run('dpkg -s {}'.format(pkg), silent=True, exception=False)
    elif is_redhat_variant():
        res = run('rpm -q {}'.format(pkg), silent=True, exception=False)
    elif is_gentoo_variant():
        res = 1 if len(glob.glob('/var/db/pkg/*/{}-*'.format(pkg))) else 0
    if res != 0:
        print('{} package is not installed.'.format(pkg))
        sys.exit(1)

def list_block_devices():
    s = out('lsblk --help')
    if re.match(r'\s*-p', s, flags=re.MULTILINE):
        s = out('lsblk -pnr -e 7,252')
        devices = re.findall(r'^(\S+) ', s, flags=re.MULTILINE)
    else:
        devices = []
        for p in ['/dev/sd*', '/dev/hd*', '/dev/xvd*', '/dev/nvme*', '/dev/mapper/*']:
            devices.extend([d for d in glob.glob(p) if d != '/dev/mapper/control'])
    return devices

def get_unused_disks():
    unused = []
    for dev in list_block_devices():
        # dev contains partitions
        if len(glob.glob('/sys/class/block/{dev}/{dev}*'.format(dev=dev.replace('/dev/','')))) > 0:
            continue
        # dev is used
        if not is_unused_disk(dev):
            continue
        unused.append(dev)
    return unused

def run_setup_script(name, script):
    global interactive
    res = run(script, exception=False)
    if res != 0:
        if interactive:
            colorprint('{red}{name} setup failed. Press any key to continue...{nocolor}', name=name)
            input()
        else:
            print('{} setup failed.'.format(name))
            sys.exit(1)
    return res

if __name__ == '__main__':
    if os.getuid() > 0:
        print('Requires root permission.')
        sys.exit(1)
    parser = argparse.ArgumentParser(description='Configure environment for Scylla.')
    parser.add_argument('--disks',
                        help='specify disks for RAID')
    parser.add_argument('--nic', default='eth0',
                        help='specify NIC')
    parser.add_argument('--ntp-domain',
                        help='specify NTP domain')
    parser.add_argument('--ami', action='store_true', default=False,
                        help='setup AMI instance')
    parser.add_argument('--setup-nic-and-disks', action='store_true', default=False,
                        help='optimize NIC and disks')
    parser.add_argument('--developer-mode', action='store_true', default=False,
                        help='enable developer mode')
    parser.add_argument('--no-ec2-check', action='store_true', default=False,
                        help='skip EC2 configuration check')
    parser.add_argument('--no-kernel-check', action='store_true', default=False,
                        help='skip kernel version check')
    parser.add_argument('--no-verify-package', action='store_true', default=False,
                        help='skip verifying packages')
    parser.add_argument('--no-enable-service', action='store_true', default=False,
                        help='skip enabling service')
    if is_redhat_variant():
        parser.add_argument('--no-selinux-setup', action='store_true', default=False,
                            help='skip selinux setup')
    parser.add_argument('--no-bootparam-setup', action='store_true', default=False,
                        help='skip bootparam setup')
    parser.add_argument('--no-ntp-setup', action='store_true', default=False,
                        help='skip ntp setup')
    parser.add_argument('--no-raid-setup', action='store_true', default=False,
                        help='skip raid setup')
    parser.add_argument('--no-coredump-setup', action='store_true', default=False,
                        help='skip coredump setup')
    parser.add_argument('--no-sysconfig-setup', action='store_true', default=False,
                        help='skip sysconfig setup')
    parser.add_argument('--no-io-setup', action='store_true', default=False,
                        help='skip IO configuration setup')
    parser.add_argument('--no-version-check', action='store_true', default=False,
                        help='skip daily version check')
    parser.add_argument('--no-node-exporter', action='store_true', default=False,
                        help='do not install the node exporter')
    parser.add_argument('--no-cpuscaling-setup', action='store_true', default=False,
                        help='skip cpu scaling setup')
    parser.add_argument('--no-fstrim-setup', action='store_true', default=False,
                        help='skip fstrim setup')
    args = parser.parse_args()

    if len(sys.argv) == 1:
        interactive = True

    if not interactive:
        if not args.no_raid_setup and not args.disks:
            parser.print_help()
            sys.exit(1)
        if not args.no_sysconfig_setup or (is_ec2() and not args.no_ec2_check):
            if not is_valid_nic(args.nic):
                print('NIC {} doesn\'t exist.'.format(args.nic))
                sys.exit(1)

    disks = args.disks
    nic = args.nic
    set_nic_and_disks = args.setup_nic_and_disks
    ec2_check = not args.no_ec2_check
    kernel_check = not args.no_kernel_check
    verify_package = not args.no_verify_package
    enable_service = not args.no_enable_service
    if is_redhat_variant():
        selinux_setup = not args.no_selinux_setup
    bootparam_setup = not args.no_bootparam_setup
    ntp_setup = not args.no_ntp_setup
    raid_setup = not args.no_raid_setup
    coredump_setup = not args.no_coredump_setup
    sysconfig_setup = not args.no_sysconfig_setup
    io_setup = not args.no_io_setup
    version_check = not args.no_version_check
    node_exporter = not args.no_node_exporter
    cpuscaling_setup = not args.no_cpuscaling_setup
    fstrim_setup = not args.no_fstrim_setup
    selinux_reboot_required = False

    if interactive:
        colorprint('{green}Skip any of the following steps by answering \'no\'{nocolor}')

    if is_ec2():
        if interactive:
            ec2_check = interactive_ask_service('Do you want to run Amazon EC2 configuration check?', 'Yes - runs a script to verify that this instance is optimized for running Scylla. No - skips the configuration check.', 'yes')
            if ec2_check:
                nic = interactive_choose_nic()
        if ec2_check:
            run('/usr/lib/scylla/scylla_ec2_check --nic {}'.format(nic))

    if interactive:
        kernel_check = interactive_ask_service('Do you want to run check your kernel version?', 'Yes - runs a  script to verify that the kernel for this instance qualifies to run Scylla. No - skips the kernel check.', 'yes')
    if kernel_check:
        run('/usr/lib/scylla/scylla_kernel_check')

    if interactive:
        verify_package = interactive_ask_service('Do you want to verify the ScyllaDB packages are installed?', 'Yes - runs a script to confirm that ScyllaDB is installed. No - skips the installation check.', 'yes')
    if verify_package:
        do_verify_package('scylla-jmx')
        do_verify_package('scylla-tools')

    if interactive:
        enable_service = interactive_ask_service('Do you want the Scylla server service to automatically start when the Scylla node boots?', 'Yes - Scylla server service automatically starts on Scylla node boot. No - skips this step. Note you will have to start the Scylla Server service manually.', 'yes')
    if enable_service:
        if is_systemd():
            systemd_unit('scylla-server.service').enable()
        elif is_gentoo_variant():
            run('rc-update add scylla-server default')

    if interactive and not os.path.exists('/etc/scylla.d/housekeeping.cfg'):
        version_check = interactive_ask_service('Do you want to enable Scylla to check if there is a newer version of Scylla available?', 'Yes - start the Scylla-housekeeping service to check for a newer version. This check runs periodically. No - skips this step.', 'yes')
    if version_check:
        with open('/etc/scylla.d/housekeeping.cfg', 'w') as f:
            f.write('[housekeeping]\ncheck-version: True\n')
        if is_systemd():
            systemd_unit('scylla-housekeeping-daily.timer').unmask()
            systemd_unit('scylla-housekeeping-restart.timer').unmask()
    else:
        with open('/etc/scylla.d/housekeeping.cfg', 'w') as f:
            f.write('[housekeeping]\ncheck-version: False\n')
        if is_systemd():
            hk_daily = systemd_unit('scylla-housekeeping-daily.timer')
            hk_daily.mask()
            hk_daily.stop()
            hk_restart = systemd_unit('scylla-housekeeping-restart.timer')
            hk_restart.mask()
            hk_restart.stop()

    cur_version=out('scylla --version', exception=False)
    if len(cur_version) > 0:
        if is_debian_variant():
            new_version=out('sudo -u scylla /usr/lib/scylla/scylla-housekeeping --uuid-file /var/lib/scylla-housekeeping/housekeeping.uuid --repo-files \'/etc/apt/sources.list.d/scylla*.list\' version --version {} --mode i'.format(cur_version), shell=True, exception=False)
        else:
            new_version=out('sudo -u scylla /usr/lib/scylla/scylla-housekeeping --uuid-file /var/lib/scylla-housekeeping/housekeeping.uuid --repo-files \'/etc/yum.repos.d/scylla*.repo\' version --version {} --mode i'.format(cur_version), shell=True, exception=False)
        if len(new_version) > 0:
            print(new_version)
    else:
        if is_debian_variant():
            new_version=out('sudo -u scylla /usr/lib/scylla/scylla-housekeeping --uuid-file /var/lib/scylla-housekeeping/housekeeping.uuid --repo-files \'/etc/apt/sources.list.d/scylla*.list\' version --version unknown --mode u', shell=True, exception=False)
        else:
            new_version=out('sudo -u scylla /usr/lib/scylla/scylla-housekeeping --uuid-file /var/lib/scylla-housekeeping/housekeeping.uuid --repo-files \'/etc/yum.repos.d/scylla*.repo\' version --version unknown --mode u', shell=True, exception=False)
        print('A Scylla executable was not found, please check your installation {}'.format(new_version))

    if is_redhat_variant():
        if interactive:
            selinux_setup = interactive_ask_service('Do you want to disable SELinux?', 'Yes - disables SELinux. Choosing Yes greatly improves performance. No - keeps SELinux activated.', 'yes')
        if selinux_setup:
            res = run_setup_script('SELinux', '/usr/lib/scylla/scylla_selinux_setup')
            if res != 0:
                selinux_reboot_required=True

    if args.ami:
        if interactive:
            bootparam_setup = interactive_ask_service('Do you want add hugepages capability to the bootloader options?', 'Yes - enable hugepages at boot time. Choosing yes greatly improves performance. No - skips this step.', 'yes')
        if bootparam_setup:
            run_setup_script('boot parameter', '/usr/lib/scylla/scylla_bootparam_setup --ami')

    if interactive:
        ntp_setup = interactive_ask_service('Do you want to setup Network Time Protocol(NTP) to auto-synchronize the current time on the node?', 'Yes - enables time-synchronization. This keeps the correct time on the node. No - skips this step.', 'yes')
    if ntp_setup:
        if args.ntp_domain:
            run_setup_script('NTP', '/usr/lib/scylla/scylla_ntp_setup --subdomain {}'.format(args.ntp_domain))

        else:
            run_setup_script('NTP', '/usr/lib/scylla/scylla_ntp_setup')

    if interactive:
        res = interactive_ask_service('Do you want to setup RAID0 and XFS?', 'It is recommended to use RAID0 and XFS for Scylla data. If you select yes, you will be prompted to choose the unmounted disks to use for Scylla data. Selected disks are formatted as part of the process.\nYes - choose a disk/disks to format and setup for RAID0 and XFS. No - skip this step.', 'yes')
        if res:
            raid_setup = interactive_ask_service('Are you sure you want to setup RAID0 and XFS?', 'If you choose Yes, the selected drive will be reformated, erasing all existing data in the process.', 'yes')
        else:
            raid_setup = False
        if raid_setup:
            devices = get_unused_disks()
            if len(devices) == 0:
                print('No free disks were detected, abort RAID/XFS setup. Disks must be unmounted before proceeding.\n')
                raid_setup = False
            else:
                print('Please select unmounted disks from the following list: {}'.format(devices))
            selected = []
            dsklist = []
            while True:
                print('type \'cancel\' to cancel RAID/XFS setup.')
                print('type \'done\' to finish selection. Selected: {}'.format(selected))
                if len(dsklist) > 0:
                    dsk = dsklist.pop(0)
                else:
                    dsk = input('> ')
                if dsk == 'cancel':
                    raid_setup = 0
                    break
                if dsk == 'done':
                    if len(selected) == 0:
                        continue
                    break
                if dsk == '':
                    continue
                if dsk.find(',') > 0:
                    dsklist = dsk.split(',')
                    continue
                if not os.path.exists(dsk):
                    print('{} not found'.format(dsk))
                    continue
                if not stat.S_ISBLK(os.stat(dsk).st_mode):
                    print('{} is not block device'.format(dsk))
                    continue
                selected.append(dsk)
                devices.remove(dsk)
            disks = ','.join(selected)
    if raid_setup:
        run_setup_script('RAID', '/usr/lib/scylla/scylla_raid_setup --disks {} --update-fstab'.format(disks))

    if interactive:
        coredump_setup = interactive_ask_service('Do you want to enable coredumps?', 'Yes - sets up coredump to allow a post-mortem analysis of the Scylla state just prior to a crash. No - skips this step.', 'yes')
    if coredump_setup:
        if disks:
            run_setup_script('coredump', '/usr/lib/scylla/scylla_coredump_setup --dump-to-raiddir')
        else:
            run_setup_script('coredump', '/usr/lib/scylla/scylla_coredump_setup')

    if interactive:
        sysconfig_setup = interactive_ask_service('Do you want to setup a system-wide customized configuration for Scylla?', 'Yes - setup the sysconfig file. No - skips this step.', 'yes')
    if sysconfig_setup:
        if interactive:
            nic = interactive_choose_nic()
            set_nic_and_disks = interactive_ask_service('Do you want to enable Network Interface Card (NIC) and disk(s) optimization?', 'Yes - optimize the NIC queue and disks settings. Selecting Yes greatly improves performance. No - skip this step.', 'yes')
    if sysconfig_setup:
        setup_args = '--setup-nic-and-disks' if set_nic_and_disks else ''
        run_setup_script('NIC queue', '/usr/lib/scylla/scylla_sysconfig_setup --nic {nic} {setup_args}'.format(nic=nic, setup_args=setup_args))

    if interactive:
        io_setup = interactive_ask_service('Do you want iotune to study your disks IO profile and adapt Scylla to it?', 'Yes - let iotune study my disk(s). Note that this action will take a few minutes. No - skip this step.', 'yes')
    if io_setup:
        run_setup_script('IO configuration', '/usr/lib/scylla/scylla_io_setup')

    if interactive:
        node_exporter = interactive_ask_service('Do you want to install node exporter to export Prometheus data from the node? Note that the Scylla monitoring stack uses this data', 'Yes - install node exporter. No - skip this  step.', 'yes')
    if node_exporter:
        run_setup_script('node exporter', '/usr/lib/scylla/node_exporter_install')

    if args.developer_mode:
        run_setup_script('developer mode', '/usr/lib/scylla/scylla_dev_mode_setup --developer-mode 1')

    if interactive:
        cpuscaling_setup = interactive_ask_service('Do you want to set the CPU scaling governor to Performance level on boot?', 'Yes - sets the CPU scaling governor to performance level. No - skip this step.', 'yes')
    if cpuscaling_setup:
        run_setup_script('CPU scaling', '/usr/lib/scylla/scylla_cpuscaling_setup')

    if interactive:
        fstrim_setup = interactive_ask_service('Do you want to enable fstrim service?', 'Yes - runs fstrim on your SSD. No - skip this step.', 'yes')
    if fstrim_setup:
        run_setup_script('fstrim', '/usr/lib/scylla/scylla_fstrim_setup')

    print('ScyllaDB setup finished.')
    if selinux_reboot_required:
        print('Please restart your machine before using ScyllaDB, as you have disabled')
        print(' SELinux.')


