def execute(self, parameter_pool):

        func_matrix = []
        # Loop over default settings
        for name, _, to_file in CredentialFileParameters:
            if not parameter_pool.has(name):
                continue
            elif ParameterSource.is_ahead(ParameterSource.Terminal,\
                                        parameter_pool.get_source(name)):
                continue
            else:
                func_matrix.append((None, name, to_file))

        # Loop over current branch settings
        cur_branch = parameter_pool.get_value(ParameterName.CurrentBranch)\
            if parameter_pool.has(ParameterName.CurrentBranch) else None
        if cur_branch and ParameterSource.is_ahead(
                parameter_pool.get_source(ParameterName.Branches),
                ParameterSource.ConfigFile):
            branch_setting = parameter_pool.get_value(
                ParameterName.Branches)[cur_branch]
            for name, _, to_file in CredentialFileParameters:
                if not name in branch_setting:
                    continue
                else:
                    func_matrix.append((cur_branch, name, to_file))

        if len(func_matrix) < 1:
            log.info(
                'Skipped updating credential file as credentials are not changed.'
            )
            return

        location = config_file.default_aws_credential_file_location()
        # Create directory if needed
        try:
            config_file.create_directory(
                config_file.default_aws_credential_file_path())
            config_file.write_aws_credential_file(location, parameter_pool,
                                                  func_matrix)
        except BaseException as ex:
            log.error('Encountered error when creating AWS Credential file at "{0}", because {1}.'.\
                      format(location, ex))
            return

        else:
            log.info(WriteAwsCredentialFileOpMessage.Succeed.format(location))
            prompt.result(
                WriteAwsCredentialFileOpMessage.Succeed.format(location))

            parameter_pool.put(
                Parameter(ParameterName.AwsCredentialFile, location,
                          ParameterSource.OperationOutput), True)

            ret_result = OperationResult(self, None, None, None)
            return ret_result
    def execute(self, parameter_pool):

        func_matrix = []
        # Loop over default settings
        for name, _, to_file in CredentialFileParameters:
            if not parameter_pool.has(name):
                continue
            elif ParameterSource.is_ahead(ParameterSource.Terminal, parameter_pool.get_source(name)):
                continue
            else:
                func_matrix.append((None, name, to_file))

        # Loop over current branch settings
        cur_branch = (
            parameter_pool.get_value(ParameterName.CurrentBranch)
            if parameter_pool.has(ParameterName.CurrentBranch)
            else None
        )
        if cur_branch and ParameterSource.is_ahead(
            parameter_pool.get_source(ParameterName.Branches), ParameterSource.ConfigFile
        ):
            branch_setting = parameter_pool.get_value(ParameterName.Branches)[cur_branch]
            for name, _, to_file in CredentialFileParameters:
                if not name in branch_setting:
                    continue
                else:
                    func_matrix.append((cur_branch, name, to_file))

        if len(func_matrix) < 1:
            log.info(u"Skipped updating credential file as credentials are not changed.")
            return

        location = config_file.default_aws_credential_file_location()
        # Create directory if needed
        try:
            config_file.create_directory(config_file.default_aws_credential_file_path())
            config_file.write_aws_credential_file(location, parameter_pool, func_matrix)
        except BaseException as ex:
            log.error(
                u'Encountered error when creating AWS Credential file at "{0}", because {1}.'.format(location, ex)
            )
            return

        else:
            log.info(WriteAwsCredentialFileOpMessage.Succeed.format(location))
            prompt.result(WriteAwsCredentialFileOpMessage.Succeed.format(location))

            parameter_pool.put(
                Parameter(ParameterName.AwsCredentialFile, location, ParameterSource.OperationOutput), True
            )

            ret_result = OperationResult(self, None, None, None)
            return ret_result
 def _generate_service_endpoint(self, pool):
     ''' 
     Generate EB service endpoint from region if not presents, or overwrite
     if specified region has higher priority.
     '''
     if pool.has(ParameterName.Region) and \
         (not pool.has(ParameterName.ServiceEndpoint) \
          or ParameterSource.is_ahead(pool.get_source(ParameterName.Region), 
                                      pool.get_source(ParameterName.ServiceEndpoint))\
          or not pool.has(ParameterName.DevToolsEndpoint) \
          or ParameterSource.is_ahead(pool.get_source(ParameterName.Region), 
                                      pool.get_source(ParameterName.DevToolsEndpoint))):
         
         region = pool.get(ParameterName.Region)
         eb_utils.generate_endpoint(pool, region.value, region.source)
    def _generate_service_endpoint(self, pool):
        ''' 
        Generate EB service endpoint from region if not presents, or overwrite
        if specified region has higher priority.
        '''
        if pool.has(ParameterName.Region) and \
            (not pool.has(ParameterName.ServiceEndpoint) \
             or ParameterSource.is_ahead(pool.get_source(ParameterName.Region),
                                         pool.get_source(ParameterName.ServiceEndpoint))\
             or not pool.has(ParameterName.DevToolsEndpoint) \
             or ParameterSource.is_ahead(pool.get_source(ParameterName.Region),
                                         pool.get_source(ParameterName.DevToolsEndpoint))):

            region = pool.get(ParameterName.Region)
            eb_utils.generate_endpoint(pool, region.value, region.source)
Beispiel #5
0
    def ask_environment_tier(cls, parameter_pool):
        # Skip if user supplies environment tier string as CLI arguments, or already by terminal
        if parameter_pool.has(ParameterName.EnvironmentTier) \
            and ParameterSource.is_ahead(parameter_pool.get_source(ParameterName.EnvironmentTier),
                                         ParameterSource.ConfigFile):
            serialized_tier = parameter_pool.get_value(
                ParameterName.EnvironmentTier, False).to_serialized_string()
            print(TerminalPromptSettingParameterMessage[ParameterName.EnvironmentTier].\
                  format(serialized_tier))
            return

        original_value = parameter_pool.get_value(
            ParameterName.EnvironmentTier)
        append_message = u'' if original_value is None \
            else TerminalMessage.CurrentValue.format(original_value.to_serialized_string())
        print(TerminalPromptAskingMessage[ParameterName.EnvironmentTier].\
              format(append_message))

        # TODO replace with call to list tiers API once it is created
        tiers = BeanstalkTerminal.ENVIRONMENT_TIERS
        serialized_tiers = [tier.to_serialized_string() for tier in tiers]
        tier_index = cls.single_choice(
            serialized_tiers, TerminalMessage.AvailableEnvironmentTier, None,
            original_value is not None)

        value = tiers[tier_index] if tier_index is not None else original_value
        tier = Parameter(ParameterName.EnvironmentTier, value,
                         ParameterSource.Terminal)
        parameter_pool.put(tier, True)
Beispiel #6
0
    def ask_solution_stack(cls, parameter_pool):

        # Skip if user supplies solution stack string as CLI arguments, or already by terminal
        if parameter_pool.has(ParameterName.SolutionStack) \
            and ParameterSource.is_ahead(parameter_pool.get_source(ParameterName.SolutionStack),
                                         ParameterSource.ConfigFile):
            print((TerminalPromptSettingParameterMessage[ParameterName.SolutionStack].\
                  format(parameter_pool.get_value(ParameterName.SolutionStack))))
            return

        original_value = parameter_pool.get_value(ParameterName.SolutionStack) \
            if parameter_pool.has(ParameterName.SolutionStack) else None
        append_message = '' if original_value is None \
            else TerminalMessage.CurrentValue.format(original_value)
        print((TerminalPromptAskingMessage[ParameterName.SolutionStack].\
              format(append_message)))

        stacks = api_wrapper.retrieve_solution_stacks(parameter_pool)
        stack_index = cls.single_choice(stacks,
                                        TerminalMessage.AvailableSolutionStack,
                                        None, original_value is not None)

        value = stacks[
            stack_index] if stack_index is not None else original_value
        stack = Parameter(ParameterName.SolutionStack, value,
                          ParameterSource.Terminal)
        parameter_pool.put(stack, True)
 def ask_environment_tier(cls, parameter_pool):
     # Skip if user supplies environment tier string as CLI arguments, or already by terminal
     if parameter_pool.has(ParameterName.EnvironmentTier) \
         and ParameterSource.is_ahead(parameter_pool.get_source(ParameterName.EnvironmentTier),
                                      ParameterSource.ConfigFile):
         serialized_tier = parameter_pool.get_value(ParameterName.EnvironmentTier, False).to_serialized_string()
         print(TerminalPromptSettingParameterMessage[ParameterName.EnvironmentTier].\
               format(serialized_tier))
         return
     
     original_value = parameter_pool.get_value(ParameterName.EnvironmentTier)
     append_message = u'' if original_value is None \
         else TerminalMessage.CurrentValue.format(original_value.to_serialized_string())
     print(TerminalPromptAskingMessage[ParameterName.EnvironmentTier].\
           format(append_message))
     
     # TODO replace with call to list tiers API once it is created
     tiers = BeanstalkTerminal.ENVIRONMENT_TIERS
     serialized_tiers = [tier.to_serialized_string() for tier in tiers]
     tier_index = cls.single_choice(serialized_tiers, 
                                     TerminalMessage.AvailableEnvironmentTier, None,
                                     original_value is not None)
     
     value = tiers[tier_index] if tier_index is not None else original_value
     tier = Parameter(ParameterName.EnvironmentTier, value, ParameterSource.Terminal)
     parameter_pool.put(tier, True)
 def ask_solution_stack(cls, parameter_pool):
     
     # Skip if user supplies solution stack string as CLI arguments, or already by terminal
     if parameter_pool.has(ParameterName.SolutionStack) \
         and ParameterSource.is_ahead(parameter_pool.get_source(ParameterName.SolutionStack),
                                      ParameterSource.ConfigFile):
         print(TerminalPromptSettingParameterMessage[ParameterName.SolutionStack].\
               format(parameter_pool.get_value(ParameterName.SolutionStack)))
         return            
     
     original_value = parameter_pool.get_value(ParameterName.SolutionStack) \
         if parameter_pool.has(ParameterName.SolutionStack) else None
     append_message = u'' if original_value is None \
         else TerminalMessage.CurrentValue.format(original_value)        
     print(TerminalPromptAskingMessage[ParameterName.SolutionStack].\
           format(append_message))
     
     stacks = api_wrapper.retrieve_solution_stacks(parameter_pool)
     stack_index = cls.single_choice(stacks, 
                                     TerminalMessage.AvailableSolutionStack, None,
                                     original_value is not None)
     
     value = stacks[stack_index] if stack_index is not None else original_value
     stack = Parameter(ParameterName.SolutionStack, value, ParameterSource.Terminal)
     parameter_pool.put(stack, True)
 def _set_parameter_value(cls, parameter_pool, name, value, source, force = False):
     # Set parameter value if not specified as before with higher priority
     if parameter_pool.has(name) \
             and Source.is_ahead(parameter_pool.get_source(name), Source.Terminal):
         value = parameter_pool.get_value(name, False)
     else:
         parameter_pool.put(Parameter(name, value, source), force)
     
     prompt.info(TerminalPromptSettingParameterMessage[name].format(value))        
Beispiel #10
0
    def _set_parameter_value(cls,
                             parameter_pool,
                             name,
                             value,
                             source,
                             force=False):
        # Set parameter value if not specified as before with higher priority
        if parameter_pool.has(name) \
                and Source.is_ahead(parameter_pool.get_source(name), Source.Terminal):
            value = parameter_pool.get_value(name)
        else:
            parameter_pool.put(Parameter(name, value, source), force)

        prompt.info(TerminalPromptSettingParameterMessage[name].format(value))
 def _generate_service_endpoint(self, pool):
     ''' 
     Generate EB service endpoint from region if not presents, or overwrite
     if specified region has higher priority.
     '''
     if pool.has(ParameterName.Region) and \
         (not pool.has(ParameterName.ServiceEndpoint) or \
          ParameterSource.is_ahead(pool.get_source(ParameterName.Region), 
                                  pool.get_source(ParameterName.ServiceEndpoint))):
         region = pool.get(ParameterName.Region)
         log.info(u'Generate service endpoint from region "{0}".'.format(region.value))
         pool.put(Parameter(ParameterName.ServiceEndpoint, 
                             ServiceEndpoint[region.value], 
                             region.source))        
Beispiel #12
0
 def put(self, param, force = False):
     ''' 
     Add new parameter to pool.
     When new parameter is not presented in pool or force is set to True, new
     parameter will be always added/updated to pool. Otherwise, it will be 
     only updated to pool when source of new parameter has higher priority 
     than the one in pool.          
     '''
     if not isinstance(param, Parameter):
         raise AttributeError("Cannot add item that's not instance of Parameter.")
     if param.name not in self._pool \
         or force\
         or ParameterSource.is_ahead(param.source, self._pool[param.name].source):
         self._pool[param.name] = param
Beispiel #13
0
 def _generate_service_endpoint(self, pool):
     ''' 
     Generate EB service endpoint from region if not presents, or overwrite
     if specified region has higher priority.
     '''
     if pool.has(ParameterName.Region) and \
         (not pool.has(ParameterName.ServiceEndpoint) or \
          ParameterSource.is_ahead(pool.get_source(ParameterName.Region),
                                  pool.get_source(ParameterName.ServiceEndpoint))):
         region = pool.get(ParameterName.Region)
         log.info(u'Generate service endpoint from region "{0}".'.format(
             region.value))
         pool.put(
             Parameter(ParameterName.ServiceEndpoint,
                       ServiceEndpoint[region.value], region.source))
Beispiel #14
0
 def put(self, param, force=False):
     ''' 
     Add new parameter to pool.
     When new parameter is not presented in pool or force is set to True, new
     parameter will be always added/updated to pool. Otherwise, it will be 
     only updated to pool when source of new parameter has higher priority 
     than the one in pool.          
     '''
     if not isinstance(param, Parameter):
         raise AttributeError(
             u"Cannot add item that's not instance of Parameter.")
     if not self._pool.has_key(param.name) \
         or force\
         or ParameterSource.is_ahead(param.source, self._pool[param.name].source):
         self._pool[param.name] = param
Beispiel #15
0
    def ask_region(cls, parameter_pool):
        original_value = parameter_pool.get_value(ParameterName.Region) \
            if parameter_pool.has(ParameterName.Region) else None
        
        original_source = parameter_pool.get_source(ParameterName.Region) \
            if parameter_pool.has(ParameterName.Region) else None
        
        if original_value is not None and \
            ParameterSource.is_ahead(original_source, ParameterSource.Terminal):
            print(TerminalPromptSettingParameterMessage[ParameterName.Region].\
                      format(ServiceRegionName[original_value]))            
            region_value = original_value
        else:
            append_message = u'' if original_value is None \
                else TerminalMessage.CurrentValue.format(ServiceRegionName[original_value])        
            print TerminalPromptAskingMessage[ParameterName.Region].format(append_message)
            
            region_name_list = list()
            for region in AvailableServiceRegion:
                region_name_list.append(ServiceRegionName[region])
            region_index = cls.single_choice(region_name_list, 
                                              TerminalMessage.AvailableRegion, 
                                              None,
                                              original_value is not None)
            
            region_value = AvailableServiceRegion[region_index] \
                if region_index is not None else original_value
            region = Parameter(ParameterName.Region, 
                               misc.to_unicode(region_value), 
                               ParameterSource.Terminal)
            parameter_pool.put(region, True)

        # Set service endpoint if not specified as CLI arguments
        cls._set_parameter_value(parameter_pool, 
                                 ParameterName.ServiceEndpoint, 
                                 misc.to_unicode(ServiceEndpoint[region_value]), 
                                 ParameterSource.Terminal)

            
        # Set RDS endpont and snippet if not specified as CLI arguments
        cls._set_parameter_value(parameter_pool, 
                                 ParameterName.RdsEndpoint, 
                                 RdsEndpoint[region_value], 
                                 ParameterSource.Terminal)
        cls._set_parameter_value(parameter_pool, 
                                 ParameterName.RdsSnippetUrl, 
                                 RdsDefault.get_snippet_url(region_value), 
                                 ParameterSource.Terminal)
    def ask_service_endpoint(cls, parameter_pool):
        endpoint_source = parameter_pool.get_source(ParameterName.ServiceEndpoint)\
            if parameter_pool.has(ParameterName.ServiceEndpoint) else None
        region_source = parameter_pool.get_source(ParameterName.Region)\
            if parameter_pool.has(ParameterName.Region) else None
        
        if region_source is None:
            cls.ask_region(parameter_pool)
        
        if endpoint_source is not None \
            and not ParameterSource.is_ahead(region_source, endpoint_source):
            return

        endpoint = Parameter(ParameterName.ServiceEndpoint, 
                             ServiceEndpoint[parameter_pool.get_value(ParameterName.Region, False)], 
                             region_source)
        parameter_pool.put(endpoint, False)
Beispiel #17
0
    def ask_service_endpoint(cls, parameter_pool):
        endpoint_source = parameter_pool.get_source(ParameterName.ServiceEndpoint)\
            if parameter_pool.has(ParameterName.ServiceEndpoint) else None
        region_source = parameter_pool.get_source(ParameterName.Region)\
            if parameter_pool.has(ParameterName.Region) else None
        
        if region_source is None:
            cls.ask_region(parameter_pool)
        
        if endpoint_source is not None \
            and not ParameterSource.is_ahead(region_source, endpoint_source):
            return

        endpoint = Parameter(ParameterName.ServiceEndpoint, 
                             ServiceEndpoint[parameter_pool.get_value(ParameterName.Region)], 
                             region_source)
        parameter_pool.put(endpoint, False)
            
Beispiel #18
0
    def ask_region(cls, parameter_pool):
        original_value = parameter_pool.get_value(ParameterName.Region)

        original_source = parameter_pool.get_source(ParameterName.Region) \
            if parameter_pool.has(ParameterName.Region) else None

        if original_value is not None and \
            ParameterSource.is_ahead(original_source, ParameterSource.Terminal):
            print((TerminalPromptSettingParameterMessage[ParameterName.Region].\
                      format(ServiceRegionName[original_value])))
            region_value = original_value
        else:
            append_message = '' if original_value is None \
                else TerminalMessage.CurrentValue.format(ServiceRegionName[original_value])
            print(TerminalPromptAskingMessage[ParameterName.Region].format(
                append_message))

            region_name_list = list()
            for region in AvailableServiceRegion:
                region_name_list.append(ServiceRegionName[region])
            region_index = cls.single_choice(
                choice_list=region_name_list,
                title=TerminalMessage.AvailableRegion,
                can_return_none=original_value is not None)

            region_value = AvailableServiceRegion[region_index] \
                if region_index is not None else original_value
            region = Parameter(ParameterName.Region,
                               misc.to_unicode(region_value),
                               ParameterSource.Terminal)
            parameter_pool.put(region, True)

        # Set RDS service endpoints if not specified as CLI arguments
        log.info(
            'Generate RDS endpoint from region "{0}".'.format(region_value))
        eb_utils.generate_endpoint(parameter_pool, region_value,
                                   ParameterSource.Terminal)
        # Set RDS endpont and snippet if not specified as CLI arguments
        log.info(
            'Generate RDS snippet URL from region "{0}".'.format(region_value))
        rds_utils.generate_endpoint(parameter_pool, region_value,
                                    ParameterSource.Terminal)
Beispiel #19
0
    def execute(self, parameter_pool):

        func_matrix = []
        for param in CredentialFileParameters:
            if not parameter_pool.has(param[0]):
                continue
            elif ParameterSource.is_ahead(ParameterSource.Terminal,\
                                        parameter_pool.get_source(param[0])):
                continue
            else:
                func_matrix.append((param[0], param[2]))

        if len(func_matrix) < 1:
            log.info(
                'Skipped updating credential file as credentials are not changed.'
            )
            return

        location = config_file.default_aws_credential_file_location()
        # Create directory if needed
        try:
            config_file.create_directory(
                config_file.default_aws_credential_file_path())
            config_file.write_aws_credential_file(location, parameter_pool,
                                                  func_matrix)
        except BaseException as ex:
            log.error('Encountered error when creating AWS Credential file at "{0}", because {1}.'.\
                      format(location, ex))
            return

        else:
            log.info(WriteAwsCredentialFileOpMessage.Succeed.format(location))
            prompt.result(
                WriteAwsCredentialFileOpMessage.Succeed.format(location))

            parameter_pool.put(
                Parameter(ParameterName.AwsCredentialFile, location,
                          ParameterSource.OperationOutput), True)

            ret_result = OperationResult(self, None, None, None)
            return ret_result
    def ask_region(cls, parameter_pool):
        original_value = parameter_pool.get_value(ParameterName.Region) \
            if parameter_pool.has(ParameterName.Region) else None
        
        original_source = parameter_pool.get_source(ParameterName.Region) \
            if parameter_pool.has(ParameterName.Region) else None
        
        if original_value is not None and \
            ParameterSource.is_ahead(original_source, ParameterSource.Terminal):
            print(TerminalPromptSettingParameterMessage[ParameterName.Region].\
                      format(ServiceRegionName[original_value]))            
            region_value = original_value
        else:
            append_message = u'' if original_value is None \
                else TerminalMessage.CurrentValue.format(ServiceRegionName[original_value])        
            print TerminalPromptAskingMessage[ParameterName.Region].format(append_message)
            
            region_name_list = list()
            for region in AvailableServiceRegion:
                region_name_list.append(ServiceRegionName[region])
            region_index = cls.single_choice(region_name_list, 
                                              TerminalMessage.AvailableRegion, 
                                              None,
                                              original_value is not None)
            
            region_value = AvailableServiceRegion[region_index] \
                if region_index is not None else original_value
            region = Parameter(ParameterName.Region, 
                               misc.to_unicode(region_value), 
                               ParameterSource.Terminal)
            parameter_pool.put(region, True)

        # Set RDS service endpoints if not specified as CLI arguments
        log.info(u'Generate RDS endpoint from region "{0}".'.format(region_value))
        eb_utils.generate_endpoint(parameter_pool, region_value, ParameterSource.Terminal)
        # Set RDS endpont and snippet if not specified as CLI arguments
        log.info(u'Generate RDS snippet URL from region "{0}".'.format(region_value))
        rds_utils.generate_endpoint(parameter_pool, region_value, ParameterSource.Terminal)
    def execute(self, parameter_pool):
        
        func_matrix = []        
        for param in CredentialFileParameters:
            if not parameter_pool.has(param[0]):
                continue
            elif ParameterSource.is_ahead(ParameterSource.Terminal,\
                                        parameter_pool.get_source(param[0])):
                continue
            else:
                func_matrix.append((param[0], param[2]))
        
        if len(func_matrix) < 1:
            log.info('Skipped updating credential file as credentials are not changed.')
            return 

        location = config_file.default_aws_credential_file_location()        
        # Create directory if needed
        try:
            config_file.create_directory(config_file.default_aws_credential_file_path())
            config_file.write_aws_credential_file(location, parameter_pool, func_matrix)
        except BaseException as ex:
            log.error('Encountered error when creating AWS Credential file at "{0}", because {1}.'.\
                      format(location, ex))
            return
        
        else:
            log.info(WriteAwsCredentialFileOpMessage.Succeed.format(location))
            prompt.result(WriteAwsCredentialFileOpMessage.Succeed.format(location))
            
            parameter_pool.put(Parameter(ParameterName.AwsCredentialFile,
                                         location,
                                         ParameterSource.OperationOutput),
                               True)
            
            ret_result = OperationResult(self, None, None, None)
            return ret_result
    def execute(self, parameter_pool):
        current_branch, _ = shell_utils.get_working_branch(False)
        parameter_pool.put(
            Parameter(ParameterName.CurrentBranch, current_branch,
                      ParameterSource.ConfigFile))
        if current_branch:
            log.info('Current working branch is "{0}".'.format(current_branch))
            branch_pool = _copy.deepcopy(parameter_pool)

            # Fill branch environment parameter values
            branches = parameter_pool.get_value(ParameterName.Branches)\
                if parameter_pool.has(ParameterName.Branches) else None
            for key in EbConfigFile.BranchSectionKeys | EbConfigFile.BranchSectionHiddenKeys:
                if branches and current_branch in list(branches.keys()) \
                    and key in list(branches[current_branch].keys()):
                    # Copy parameter if current branch has corresponding setting
                    branch_pool.put(
                        Parameter(key, branches[current_branch][key],
                                  ParameterSource.ConfigFile))
                else:
                    # TODO: we will leave following parameter if not presents in branch, since
                    # we are not asking for them for now but they are required in terminal
                    if not key in (ParameterName.ApplicationName,
                                   ParameterName.Region,
                                   ParameterName.ServiceEndpoint,
                                   ParameterName.DevToolsEndpoint):
                        branch_pool.remove(key)
            branch_pool.put(
                Parameter(
                    ParameterName.DefaultEnvironmentName,
                    parameter_pool.get_value(ParameterName.EnvironmentName),
                    ParameterSource.ConfigFile))

            # Call terminal
            copy = BeanstalkTerminal.ask_branch(branch_pool)

            # Create mapping and branch-environment section
            if branches is None:
                parameter_pool.put(
                    Parameter(ParameterName.Branches, dict(),
                              ParameterSource.Terminal))
            branches = parameter_pool.get_value(ParameterName.Branches)
            branches[current_branch] = dict()
            source = ParameterSource.ConfigFile
            for key in EbConfigFile.BranchSectionKeys | EbConfigFile.BranchSectionHiddenKeys:
                if branch_pool.has(key):
                    branches[current_branch][key] = branch_pool.get_value(key)
                    if ParameterSource.is_ahead(branch_pool.get_source(key),
                                                source):
                        source = branch_pool.get_source(key)
                else:
                    # Copy parameter if not exists in branch
                    if parameter_pool.has(key):
                        branches[current_branch][
                            key] = parameter_pool.get_value(key)
            parameter_pool.update(ParameterName.Branches, source=source)

            # Copy over optionsetting file
            if copy:
                default_option_file = parameter_pool.get_value(
                    ParameterName.OptionSettingFile)
                branch_option_file = branches[current_branch][
                    ParameterName.OptionSettingFile]
                log.debug('Copying config file from {0} to {1}.'.format(
                    default_option_file, branch_option_file))
                shell_utils.copy_file(default_option_file, branch_option_file,
                                      True)
                config_file.set_access_permission(branch_option_file, True)

            # Fill [branch] section
            if not parameter_pool.has(ParameterName.BranchMapping)\
                or parameter_pool.get_value(ParameterName.BranchMapping) is None:
                parameter_pool.put(
                    Parameter(ParameterName.BranchMapping, dict(),
                              ParameterSource.Terminal))
            branch_mapping = parameter_pool.get_value(
                ParameterName.BranchMapping)
            branch_mapping[current_branch] = branch_pool.get_value(
                ParameterName.EnvironmentName)

        else:
            # local repository does not have branch committed yet.
            msg = TerminalMessage.NoBranchToRegister
            log.error(msg)
            prompt.error(msg)
    def execute(self, parameter_pool):
        current_branch, _ = shell_utils.get_working_branch(False)
        parameter_pool.put(Parameter(ParameterName.CurrentBranch,
                                     current_branch,
                                     ParameterSource.ConfigFile))
        if current_branch:
            log.info(u'Current working branch is "{0}".'.format(current_branch))
            branch_pool = _copy.deepcopy(parameter_pool)
            
            # Fill branch environment parameter values
            branches = parameter_pool.get_value(ParameterName.Branches)
            for key in EbConfigFile.BranchSectionKeys | EbConfigFile.BranchSectionHiddenKeys:
                if branches and current_branch in branches.keys() \
                    and key in branches[current_branch].keys():
                    # Copy parameter if current branch has corresponding setting
                    branch_pool.put(Parameter(key,
                                                 branches[current_branch][key],
                                                 ParameterSource.ConfigFile))
                else:
                    # TODO: we will leave following parameter if not presents in branch, since
                    # we are not asking for them for now but they are required in terminal
                    if not key in (ParameterName.ApplicationName,
                                   ParameterName.Region,
                                   ParameterName.ServiceEndpoint,
                                   ParameterName.DevToolsEndpoint):  
                        branch_pool.remove(key)
            branch_pool.put(Parameter(ParameterName.DefaultEnvironmentName,
                                      parameter_pool.get_value(ParameterName.EnvironmentName, False),
                                      ParameterSource.ConfigFile))

            
            # Call terminal
            copy = BeanstalkTerminal.ask_branch(branch_pool)
            
            # Create mapping and branch-environment section
            if branches is None:
                parameter_pool.put(Parameter(ParameterName.Branches,
                                             dict(),
                                             ParameterSource.Terminal))
            branches = parameter_pool.get_value(ParameterName.Branches, False)
            branches[current_branch] = dict()
            source = ParameterSource.ConfigFile
            for key in EbConfigFile.BranchSectionKeys | EbConfigFile.BranchSectionHiddenKeys:
                if branch_pool.has(key):
                    branches[current_branch][key] = branch_pool.get_value(key, False)
                    if ParameterSource.is_ahead(branch_pool.get_source(key), source):
                        source = branch_pool.get_source(key)
                else:
                    # Copy parameter if not exists in branch
                    if parameter_pool.has(key): 
                        branches[current_branch][key] = parameter_pool.get_value(key, False)
            parameter_pool.update(ParameterName.Branches, source=source)
            
            # Copy over optionsetting file
            if copy:
                default_option_file = parameter_pool.get_value(ParameterName.OptionSettingFile, False)
                branch_option_file = branches[current_branch][ParameterName.OptionSettingFile]
                log.debug(u'Copying optionsettings file from {0} to {1}.'.format(default_option_file,
                                                                         branch_option_file))
                shell_utils.copy_file(default_option_file, branch_option_file, True)
                config_file.set_access_permission(branch_option_file, True)

            # Fill [branch] section
            if parameter_pool.get_value(ParameterName.BranchMapping) is None:
                parameter_pool.put(Parameter(ParameterName.BranchMapping,
                                             dict(),
                                             ParameterSource.Terminal))
            branch_mapping = parameter_pool.get_value(ParameterName.BranchMapping, False)
            branch_mapping[current_branch] = branch_pool.get_value(ParameterName.EnvironmentName, False)
            
        else:
            # local repository does not have branch committed yet.
            msg = TerminalMessage.NoBranchToRegister
            log.error(msg)
            prompt.error(msg)