#!/usr/bin/env python
from __future__ import print_function

import uuid
import glob
import os, yaml
import sys
import time
import xbow
from xbow.instances import get_by_name, ConnectedInstance


def portal_config():
    """
    Configures xbow portal
    """

    cfg_file = os.path.join(xbow.XBOW_CONFIGDIR, "settings.yml")

    with open(cfg_file, 'r') as ymlfile:
        cfg = yaml.safe_load(ymlfile)

    instances = get_by_name(cfg['scheduler_name'])
    if len(instances) == 0:
        raise ValueError('Error - no such instance')
    elif len(instances) > 1:
        raise ValueError('Error - more than one instance has that name')
    instance = instances[0]

    ci = ConnectedInstance(instance) 
    
    print('Configuring Xbow-Portal')
    ci.exec_command('sudo xport-config')
    ci.exec_command('sudo /etc/init.d/nginx stop')
    ci.exec_command('sudo /etc/init.d/nginx start')

def get_hostname():
    """
    Returns the url of the cluster scheduler node.
    """
    cfg_file = os.path.join(xbow.XBOW_CONFIGDIR, "settings.yml")

    with open(cfg_file, 'r') as ymlfile:
        cfg = yaml.safe_load(ymlfile)

    instances = get_by_name(cfg['scheduler_name'])
    if len(instances) == 0:
        raise ValueError('Error - no such instance')
    elif len(instances) > 1:
        raise ValueError('Error - more than one instance has that name')
    instance = instances[0]
    print('Below is your Xbow-Portal Web Address. Please copy and paste it into your web browser')
    print(instance.public_dns_name)

if __name__ == '__main__':
    portal_config()
    get_hostname()

