#!/usr/bin/python3
#
# Copyright 2020 ScyllaDB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import pathlib
sys.path.append('/opt/scylladb/scripts')
from scylla_util import *
from subprocess import run

if __name__ == '__main__':
    # On Ubuntu, we configure CPU scaling while AMI building time
    if is_redhat_variant():
        run('/opt/scylladb/scripts/scylla_cpuscaling_setup', shell=True, check=True)
    cloud_instance = get_cloud_instance()
    run('/opt/scylladb/scylla-machine-image/scylla_configure.py', shell=True, check=True)
    if not os.path.ismount('/var/lib/scylla'):
        print('Failed to initialize RAID volume, exiting setup')
        sys.exit(1)

    run('/opt/scylladb/scripts/scylla_sysconfig_setup --nic eth0 --setup-nic --ami', shell=True, check=True)
    if cloud_instance.is_supported_instance_class():
        cloud_instance.io_setup()
    if os.path.ismount('/var/lib/scylla'):
        run('/opt/scylladb/scripts/scylla_coredump_setup --dump-to-raiddir', shell=True, check=True)
    else:
        run('/opt/scylladb/scripts/scylla_coredump_setup', shell=True, check=True)
    run('/opt/scylladb/scripts/scylla_swap_setup --swap-directory /', shell=True, check=True)
    pathlib.Path('/etc/scylla/machine_image_configured').touch()
