#!/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
sys.path.append('/opt/scylladb/scripts')
from scylla_util import *
from subprocess import run

MSG_HEADER = '''

   _____            _ _       _____  ____  
  / ____|          | | |     |  __ \|  _ \ 
 | (___   ___ _   _| | | __ _| |  | | |_) |
  \___ \ / __| | | | | |/ _` | |  | |  _ < 
  ____) | (__| |_| | | | (_| | |__| | |_) |
 |_____/ \___|\__, |_|_|\__,_|_____/|____/ 
               __/ |                       
              |___/                        

Version:
       {scylla_version}
Nodetool:
	nodetool help
CQL Shell:
	cqlsh
More documentation available at: 
	http://www.scylladb.com/doc/
By default, Scylla sends certain information about this node to a data collection server. For information, see http://www.scylladb.com/privacy/

'''[1:-1]
MSG_UNSUPPORTED_INSTANCE_TYPE = '''
    {red}{type} is not eligible for optimized automatic tuning!{nocolor}
To continue startup ScyllaDB on this instance, run 'sudo scylla_io_setup' then 'systemctl start scylla-server'.
For a list of optimized instance types and more EC2 instructions see '%s'"

'''[1:-1]
MSG_UNSUPPORTED_INSTANCE_TYPE_NO_DISKS = '''
    {red}{type} is not eligible for optimized automatic tuning!{nocolor}
To continue startup ScyllaDB on this instance, you need to attach additional disks, then run 'sudo scylla_io_setup' then 'systemctl start scylla-server'.
For a list of optimized instance types and more EC2 instructions see '%s'"

'''[1:-1]
MSG_SETUP_ACTIVATING = '''
    {green}Constructing RAID volume...{nocolor}

Please wait for setup. To see status, run 
 'systemctl status scylla-image-setup'

After setup finished, scylla-server service will launch.
To see status of scylla-server, run 
 'systemctl status scylla-server'

'''[1:-1]
MSG_SETUP_FAILED = '''
    {red}Initial image configuration failed!{nocolor}

To see status, run 
 'systemctl status scylla-image-setup'

'''[1:-1]
MSG_SCYLLA_ACTIVATING = '''
    {green}ScyllaDB is starting...{nocolor}

Please wait for start. To see status, run 
 'systemctl status scylla-server'

'''[1:-1]
MSG_SCYLLA_FAILED = '''
    {red}ScyllaDB is not started!{nocolor}
Please wait for startup. To see status of ScyllaDB, run 
 'systemctl status scylla-server'

'''[1:-1]
MSG_SCYLLA_MOUNT_FAILED = '''
    {red}Failed mounting RAID volume!{nocolor}

ScyllaDB aborted startup because of RAID volume missing.
To see status, run
 'systemctl status scylla-server'

'''[1:-1]
MSG_SCYLLA_UNKNOWN = '''
    {red}ScyllaDB is not started!{nocolor}

To see status of ScyllaDB, run
 'systemctl status scylla-server'

'''[1:-1]
MSG_SCYLLA_ACTIVE = '''
    {green}ScyllaDB is active.{nocolor}

$ nodetool status

'''[1:-1]

if __name__ == '__main__':
    colorprint(MSG_HEADER.format(scylla_version=run("scylla --version", shell=True, check=True, capture_output=True, encoding='utf-8').stdout.strip()))
    cloud_instance = get_cloud_instance()
    if not cloud_instance.is_supported_instance_class():
        if len(cloud_instance.non_root_disks()) == 0:
            colorprint(MSG_UNSUPPORTED_INSTANCE_TYPE_NO_DISKS % cloud_instance.GETTING_STARTED_URL,
                type=cloud_instance.instance_class())

        else:
            colorprint(MSG_UNSUPPORTED_INSTANCE_TYPE % cloud_instance.GETTING_STARTED_URL,
                type=cloud_instance.instance_class())
    else:
        skip_scylla_server = False
        if not os.path.exists('/etc/scylla/machine_image_configured'):
            setup = systemd_unit('scylla-image-setup.service')
            res = setup.is_active()
            if res == 'activating':
                colorprint(MSG_SETUP_ACTIVATING)
                skip_scylla_server = True
            elif res == 'failed':
                colorprint(MSG_SETUP_FAILED)
                skip_scylla_server = True
        if not skip_scylla_server:
            server = systemd_unit('scylla-server.service')
            res = server.is_active()
            if res == 'activating':
                colorprint(MSG_SCYLLA_ACTIVATING)
            elif res == 'failed':
                colorprint(MSG_SCYLLA_FAILED)
            elif res == 'inactive':
                if os.path.exists('/etc/systemd/system/scylla-server.service.d/mounts.conf'):
                    colorprint(MSG_SCYLLA_MOUNT_FAILED)
                else:
                    colorprint(MSG_SCYLLA_UNKNOWN)
            else:
                colorprint(MSG_SCYLLA_ACTIVE)
                run('nodetool status', shell=True)
        cloud_instance.check()
        print('\n', end='')
