def __init__(self, config_path, relaunch_options):
        self.config_path = config_path
        self.init_config(relaunch_options)
        self.master = Master(host=self.host,
                             port=self.port,
                             logfile_path=self.log_path)
        self.relaunch_container = []
        self.stopped = False
        self.activated = False

        # Parsing arguments takes precedence over config file options
        self.relaunch_live_activities = True
        self.relaunch_controllers = False
        self.relaunch_master = False

        if relaunch_options['no_live_activities']:
            print "Performing relaunch without relaunching live activities"
            self.relaunch_live_activities = False
        else:
            self.relaunch_live_activities = True

        if relaunch_options['full_relaunch'] or relaunch_options['full']:
            print colored("Performing full relaunch.", 'white', attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = True

        if relaunch_options['master_only']:
            print colored("Performing relaunch of master only", 'white', attrs=['bold'])
            self.relaunch_controllers = False
            self.relaunch_master = True
            self.relaunch_live_activities = False

        if relaunch_options['controllers_only']:
            print colored("Performing relaunch of controllers only", 'white', attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = False
            self.relaunch_live_activities = False

        if relaunch_options['live_activity_groups']:
            self.relaunch_sequence = relaunch_options['live_activity_groups'].split(',')
            if len(self.relaunch_sequence) == 0:
                print colored("Relaunch sequence is empty")
                sys.exit(1)
            print colored("Live activity groups to be relaunched: %s" % self.relaunch_sequence, 'white', attrs=['bold'])

        if relaunch_options['status']:
            print colored("Getting status of IS stack", 'white', attrs=['bold'])
        else:
            print colored("This is what's going to be launched:", 'white', attrs=['bold'])
            print "Controllers: %s" % self.relaunch_controllers
            print "Master: %s" % self.relaunch_master
            print "Live activities: %s" % self.relaunch_live_activities
            if self.relaunch_live_activities:
                print "Live activity groups: " + colored("%s" % (',').join(self.relaunch_sequence), 'magenta')
    def __init__(self, config_path, relaunch_options):
        self.config_path = config_path
        self.init_config(relaunch_options)
        self.master = Master(host=self.host,
                             port=self.port,
                             logfile_path=self.log_path)
        self.relaunch_container = []
        self.stopped = False
        self.activated = False

        # Parsing arguments takes precedence over config file options
        self.relaunch_live_activities = True
        self.relaunch_controllers = False
        self.relaunch_master = False

        if relaunch_options['no_live_activities']:
            print "Performing relaunch without relaunching live activities"
            self.relaunch_live_activities = False
        else:
            self.relaunch_live_activities = True

        if relaunch_options['full_relaunch'] or relaunch_options['full']:
            print colored("Performing full relaunch.", 'white', attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = True

        if relaunch_options['master_only']:
            print colored("Performing relaunch of master only",
                          'white',
                          attrs=['bold'])
            self.relaunch_controllers = False
            self.relaunch_master = True
            self.relaunch_live_activities = False

        if relaunch_options['controllers_only']:
            print colored("Performing relaunch of controllers only",
                          'white',
                          attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = False
            self.relaunch_live_activities = False

        if relaunch_options['live_activity_groups']:
            self.relaunch_sequence = relaunch_options[
                'live_activity_groups'].split(',')
            if len(self.relaunch_sequence) == 0:
                print colored("Relaunch sequence is empty")
                sys.exit(1)
            print colored("Live activity groups to be relaunched: %s" %
                          self.relaunch_sequence,
                          'white',
                          attrs=['bold'])

        if relaunch_options['status']:
            print colored("Getting status of IS stack",
                          'white',
                          attrs=['bold'])
        else:
            print colored("This is what's going to be launched:",
                          'white',
                          attrs=['bold'])
            print "Controllers: %s" % self.relaunch_controllers
            print "Master: %s" % self.relaunch_master
            print "Live activities: %s" % self.relaunch_live_activities
            if self.relaunch_live_activities:
                print "Live activity groups: " + colored(
                    "%s" % (',').join(self.relaunch_sequence), 'magenta')
class InteractiveSpacesRelaunch(object):
    @debug
    def __init__(self, config_path, relaunch_options):
        self.config_path = config_path
        self.init_config(relaunch_options)
        self.master = Master(host=self.host,
                             port=self.port,
                             logfile_path=self.log_path)
        self.relaunch_container = []
        self.stopped = False
        self.activated = False

        # Parsing arguments takes precedence over config file options
        self.relaunch_live_activities = True
        self.relaunch_controllers = False
        self.relaunch_master = False

        if relaunch_options['no_live_activities']:
            print "Performing relaunch without relaunching live activities"
            self.relaunch_live_activities = False
        else:
            self.relaunch_live_activities = True

        if relaunch_options['full_relaunch'] or relaunch_options['full']:
            print colored("Performing full relaunch.", 'white', attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = True

        if relaunch_options['master_only']:
            print colored("Performing relaunch of master only",
                          'white',
                          attrs=['bold'])
            self.relaunch_controllers = False
            self.relaunch_master = True
            self.relaunch_live_activities = False

        if relaunch_options['controllers_only']:
            print colored("Performing relaunch of controllers only",
                          'white',
                          attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = False
            self.relaunch_live_activities = False

        if relaunch_options['live_activity_groups']:
            self.relaunch_sequence = relaunch_options[
                'live_activity_groups'].split(',')
            if len(self.relaunch_sequence) == 0:
                print colored("Relaunch sequence is empty")
                sys.exit(1)
            print colored("Live activity groups to be relaunched: %s" %
                          self.relaunch_sequence,
                          'white',
                          attrs=['bold'])

        if relaunch_options['status']:
            print colored("Getting status of IS stack",
                          'white',
                          attrs=['bold'])
        else:
            print colored("This is what's going to be launched:",
                          'white',
                          attrs=['bold'])
            print "Controllers: %s" % self.relaunch_controllers
            print "Master: %s" % self.relaunch_master
            print "Live activities: %s" % self.relaunch_live_activities
            if self.relaunch_live_activities:
                print "Live activity groups: " + colored(
                    "%s" % (',').join(self.relaunch_sequence), 'magenta')

    @debug
    def init_config(self, relaunch_options):
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.config_path)
        self.host = self.config.get('master', 'host')
        self.port = self.config.get('master', 'port')
        self.shutdown_attempts = self.config.getint('relaunch',
                                                    'shutdown_attempts')
        self.startup_attempts = self.config.getint('relaunch',
                                                   'startup_attempts')
        self.relaunch_sequence = self.config.get(
            'relaunch', 'relaunch_sequence').split(',')
        self.interval_between_attempts = self.config.getint(
            'relaunch', 'interval_between_attempts')
        self.relaunch_controllers = self.config.getint('relaunch',
                                                       'relaunch_controllers')
        self.relaunch_master = self.config.getint('relaunch',
                                                  'relaunch_master')
        self.master_stop_command = self.config.get('master', 'stop_command')
        self.master_launch_command = self.config.get('master',
                                                     'launch_command')
        self.master_destroy_tmux_command = self.config.get(
            'master', 'destroy_tmux_command')
        self.log_path = self.config.get('global', 'logfile_path')
        self.ssh_command = self.config.get('global', 'ssh_command')

        self.pp = pprint.PrettyPrinter(indent=4)

        self.controllers_data = self.init_controllers_config(relaunch_options)

    @debug
    def create_uri_for(self, location):
        return "http://" + self.host + ":" + self.port + location

    @debug
    def init_controllers_config(self, relaunch_options):
        config = {}
        if relaunch_options['controllers']:
            controllers_list = relaunch_options['controllers'].split(',')
        else:
            controllers_list = self.config.get('global',
                                               'controllers_list').split(',')

        print colored(
            "Controller(s) to relaunch: %s" % (',').join(controllers_list),
            'green')

        for controller_name in controllers_list:
            config[controller_name] = {}
            config[controller_name]['name'] = self.config.get(
                controller_name, 'name')
            config[controller_name]['hostname'] = self.config.get(
                controller_name, 'hostname')
            config[controller_name]['stop_command'] = self.config.get(
                controller_name, 'stop_command')
            config[controller_name]['launch_command'] = self.config.get(
                controller_name, 'launch_command')
            config[controller_name]['pid_command'] = self.config.get(
                controller_name, 'pid_command')
            config[controller_name]['destroy_tmux_command'] = self.config.get(
                controller_name, 'destroy_tmux_command')
        return config

    @debug
    def stop_controller(self, controller_name):
        """
        @summary: stops a remote controller process
        @rtype: string containing output of stop command
        """
        command = "%s %s '%s'" % (
            self.ssh_command, controller_name,
            self.controllers_data[controller_name]['stop_command'])
        cmd_process = subprocess.Popen(command,
                                       shell=True,
                                       stdout=subprocess.PIPE)
        output = cmd_process.communicate()[0].replace('\n', '').split(' ')
        return output

    @debug
    def destroy_tmux_session(self, controller_name):
        """
        @summary: destroys tmux session of a controller
        @rtype: string with command output
        """
        command = "%s %s '%s'" % (
            self.ssh_command, controller_name,
            self.controllers_data[controller_name]['destroy_tmux_command'])
        cmd_process = subprocess.Popen(command,
                                       shell=True,
                                       stdout=subprocess.PIPE)
        output = cmd_process.communicate()[0].replace('\n', '').split(' ')
        return output

    @debug
    def start_controller(self, controller_name):
        """
        @summary: starts controller (most likely a tmux session)
        @rtype: string containing start output
        """
        command = "%s %s '%s'" % (
            self.ssh_command, controller_name,
            self.controllers_data[controller_name]['launch_command'])
        cmd_process = subprocess.Popen(command,
                                       shell=True,
                                       stdout=subprocess.PIPE)
        output = cmd_process.communicate()[0].replace('\n', '').split(' ')
        return output

    @debug
    def controller_connected(self, controller_name):
        """
        @summary: We always return False because ispaces controllers are tricky
        """
        try:
            controller = self.master.get_space_controller({
                'space_controller_name':
                controller_name,
                'space_controller_mode':
                'ENABLED',
                'space_controller_state':
                'RUNNING'
            })
            return True
        except ControllerNotFoundException, e:
            return False
        except MasterException, e:
            return False
class InteractiveSpacesRelaunch(object):
    @debug
    def __init__(self, config_path, relaunch_options):
        self.config_path = config_path
        self.init_config(relaunch_options)
        self.master = Master(host=self.host,
                             port=self.port,
                             logfile_path=self.log_path)
        self.relaunch_container = []
        self.stopped = False
        self.activated = False

        # Parsing arguments takes precedence over config file options
        self.relaunch_live_activities = True
        self.relaunch_controllers = False
        self.relaunch_master = False

        if relaunch_options['no_live_activities']:
            print "Performing relaunch without relaunching live activities"
            self.relaunch_live_activities = False
        else:
            self.relaunch_live_activities = True

        if relaunch_options['full_relaunch'] or relaunch_options['full']:
            print colored("Performing full relaunch.", 'white', attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = True

        if relaunch_options['master_only']:
            print colored("Performing relaunch of master only", 'white', attrs=['bold'])
            self.relaunch_controllers = False
            self.relaunch_master = True
            self.relaunch_live_activities = False

        if relaunch_options['controllers_only']:
            print colored("Performing relaunch of controllers only", 'white', attrs=['bold'])
            self.relaunch_controllers = True
            self.relaunch_master = False
            self.relaunch_live_activities = False

        if relaunch_options['live_activity_groups']:
            self.relaunch_sequence = relaunch_options['live_activity_groups'].split(',')
            if len(self.relaunch_sequence) == 0:
                print colored("Relaunch sequence is empty")
                sys.exit(1)
            print colored("Live activity groups to be relaunched: %s" % self.relaunch_sequence, 'white', attrs=['bold'])

        if relaunch_options['status']:
            print colored("Getting status of IS stack", 'white', attrs=['bold'])
        else:
            print colored("This is what's going to be launched:", 'white', attrs=['bold'])
            print "Controllers: %s" % self.relaunch_controllers
            print "Master: %s" % self.relaunch_master
            print "Live activities: %s" % self.relaunch_live_activities
            if self.relaunch_live_activities:
                print "Live activity groups: " + colored("%s" % (',').join(self.relaunch_sequence), 'magenta')

    @debug
    def init_config(self, relaunch_options):
        self.config = ConfigParser.RawConfigParser()
        self.config.read(self.config_path)
        self.host = self.config.get('master', 'host')
        self.port = self.config.get('master', 'port')
        self.shutdown_attempts = self.config.getint('relaunch','shutdown_attempts')
        self.startup_attempts = self.config.getint('relaunch','startup_attempts')
        self.relaunch_sequence = self.config.get('relaunch','relaunch_sequence').split(',')
        self.interval_between_attempts = self.config.getint('relaunch','interval_between_attempts')
        self.relaunch_controllers = self.config.getint('relaunch','relaunch_controllers')
        self.relaunch_master = self.config.getint('relaunch', 'relaunch_master')
        self.master_stop_command = self.config.get('master', 'stop_command')
        self.master_launch_command = self.config.get('master', 'launch_command')
        self.master_destroy_tmux_command = self.config.get('master', 'destroy_tmux_command')
        self.log_path = self.config.get('global', 'logfile_path')
        self.ssh_command = self.config.get('global', 'ssh_command')

        self.pp = pprint.PrettyPrinter(indent=4)

        self.controllers_data = self.init_controllers_config(relaunch_options)


    @debug
    def create_uri_for(self, location):
        return "http://" + self.host + ":" + self.port + location

    @debug
    def init_controllers_config(self, relaunch_options):
        config = {}
        if relaunch_options['controllers']:
            controllers_list = relaunch_options['controllers'].split(',')
        else:
            controllers_list = self.config.get('global', 'controllers_list').split(',')

        print colored("Controller(s) to relaunch: %s" % (',').join(controllers_list), 'green')

        for controller_name in controllers_list:
            config[controller_name] = {}
            config[controller_name]['name'] = self.config.get(controller_name, 'name')
            config[controller_name]['hostname'] = self.config.get(controller_name, 'hostname')
            config[controller_name]['stop_command'] = self.config.get(controller_name, 'stop_command')
            config[controller_name]['launch_command'] = self.config.get(controller_name, 'launch_command')
            config[controller_name]['pid_command'] = self.config.get(controller_name, 'pid_command')
            config[controller_name]['destroy_tmux_command'] = self.config.get(controller_name, 'destroy_tmux_command')
        return config

    @debug
    def stop_controller(self, controller_name):
        """
        @summary: stops a remote controller process
        @rtype: string containing output of stop command
        """
        command = "%s %s '%s'" % (self.ssh_command, controller_name, self.controllers_data[controller_name]['stop_command'])
        cmd_process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        output = cmd_process.communicate()[0].replace('\n', '').split(' ')
        return output

    @debug
    def destroy_tmux_session(self, controller_name):
        """
        @summary: destroys tmux session of a controller
        @rtype: string with command output
        """
        command = "%s %s '%s'" % (self.ssh_command, controller_name, self.controllers_data[controller_name]['destroy_tmux_command'])
        cmd_process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        output = cmd_process.communicate()[0].replace('\n', '').split(' ')
        return output

    @debug
    def start_controller(self, controller_name):
        """
        @summary: starts controller (most likely a tmux session)
        @rtype: string containing start output
        """
        command = "%s %s '%s'" % (self.ssh_command, controller_name, self.controllers_data[controller_name]['launch_command'])
        cmd_process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        output = cmd_process.communicate()[0].replace('\n', '').split(' ')
        return output

    @debug
    def controller_connected(self, controller_name):
        """
        @summary: We always return False because ispaces controllers are tricky
        """
        try:
            controller = self.master.get_space_controller({'space_controller_name' : controller_name,
                                                           'space_controller_mode' : 'ENABLED',
                                                           'space_controller_state': 'RUNNING'})
            return True
        except ControllerNotFoundException, e :
            return False
        except MasterException, e:
            return False