Example #1
0
def read_aws_credential_file(location, parameter_pool, func_matrix, source, quiet = False):
    try:
        env_name = parameter_pool.get_value(ParameterName.EnvironmentName) \
            if parameter_pool.has(ParameterName.EnvironmentName) else ''
        
        log.info('Reading AWS credential from file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        parser.read(location)

        for name, from_file_func in func_matrix:
            if name == ParameterName.RdsMasterPassword:
                key_name = rds_utils.password_key_name(env_name)
            else:
                key_name = AwsCredentialFileDefault.KeyName[name]
                
            if parser.has_option(key_name):
                value = parser.get(key_name)
                value = from_file_func(value) if from_file_func is not None else value
                parameter_pool.put(Parameter(name, value, source))
        log.info('Finished reading AWS credential from file.')
                
    except BaseException as ex:
        log.error('Failed to retrieve AWS credential from file "{0}", because: "{1}"'.\
                  format(location, ex))
        if not quiet:
            msg = CredentialFileErrorMessage.ReadError.format(location)
            prompt.error(msg)
            raise EBSCliException(msg)
        else:          
            return False # if failed, just skip 
Example #2
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info(u'Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = []
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                cos = ConfigurationOptionSetting()
                cos._namespace = misc.to_unicode(section)
                cos._option_name = misc.to_unicode(option)
                cos._value = misc.to_unicode(value)
                option_settings.append(cos) 
        
        log.debug(u'Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error(u'Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return []
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
Example #3
0
def trim_aws_credential_file(location, param_list, quiet = False):
    try:
        log.info(u'Trimming AWS credential file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        try:
            parser.read(location)
        except IOError as ex:
            return # File not exists
        
        for name in param_list:
            parser.remove_option(name)
        
        parser.write(location)
        log.info(u'Finished trimming AWS credential file.')
                
        # Set access permission
        set_access_permission(location, False)
        log.info(u'Set AWS credential file access permission.')
        
    except BaseException as ex:
        log.error(u'Failed to trim AWS credential file at "{0}", because: "{1}"'.\
                  format(location, ex))
        msg = CredentialFileErrorMessage.WriteError.format(location)
        prompt.error(msg)
        if not quiet:
            raise EBSCliException(msg)
        else:          
            return False # if failed, just skip 
Example #4
0
    def execute(self, pool):

        # Test if git local repo exists
        if not os.path.isdir(os.path.join(os.getcwd(),
                                          DevToolsConfigFile.Path)):
            prompt.error(DevToolsMessage.GitRepoNotExist)
            #            raise EBSCliException()
            return

        try:
            self.run_dev_tools_script()

            location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name
            config_file.set_access_permission(location, True)

        except (OSError, IOError, subprocess.CalledProcessError) as ex:
            log.error(
                "Encountered error when updating AWS Dev Tools settings: {0}.".
                format(ex))
            message = DevToolsMessage.ExecutionError.format(
                DevToolsConfigFile.InitHelpUrl)
            prompt.error(message)


#            raise EBSCliException()

        ret_result = OperationResult(self, None, None, None)
        return ret_result
Example #5
0
def load_eb_config_file(location, parameter_pool, quiet = False):
    try:
        log.info('Reading EB configuration from file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        parser.read(location)

        for (name, from_file, _) in ConfigFileParameters:
            if parser.has_option(name):
                value = parser.get(name)
                if from_file is not None:
                    value = from_file(value)
                parameter_pool.put(Parameter(name, value, ParameterSource.ConfigFile))

        # Add original solution stack infos
        if parser.has_option(ParameterName.SolutionStack):
            parameter_pool.put(Parameter(ParameterName.OriginalSolutionStack, 
                                         parser.get(ParameterName.SolutionStack), 
                                         ParameterSource.ConfigFile))
            

        log.info('Finished reading from EB configuration file.')
        
    except BaseException as ex:
        log.error('Failed to parse EB configuration from file, because: "{0}"'.format(ex))
        if not quiet:
            if (isinstance(ex, OSError) or isinstance(ex, IOError)) and\
                ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                raise EBConfigFileNotExistError(ex)
            else:
                msg = ConfigFileErrorMessage.ReadError.format(location)
                prompt.error(msg)
                raise EBConfigFileNotExistError(msg)
        else:    
            pass # if failed, just skip     
def save_env_option_setting_file(location, option_settings):
    log.info(u'Try loading option settings file from {0}'.format(location))
#    old_option_settings = load_env_option_setting_file(location, quiet = True)
    
    log.info(u'Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.iteritems()):
                if not _is_whitelisted_option(namespace, option):
#                    and (namespace not in old_option_settings \
#                         or option not in old_option_settings[namespace]):
                    continue
                
                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                # Add option setting to new file
                if value is None:
                    value = u''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error(u'Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
Example #7
0
    def execute(self, parameter_pool):
        command = parameter_pool.get_value(PName.Command, False)
        if command == CommandType.INIT:
            sanitize = False
            for name, ori_name in EbConfigFile.BranchResetParameters.iteritems():
                if parameter_pool.has(ori_name) and \
                    parameter_pool.get_value(name, False) != parameter_pool.get_value(ori_name, False):
                    sanitize = True
                    break

            blast = False
            if sanitize:
                if parameter_pool.has(PName.Branches):
                    parameter_pool.remove(PName.Branches)
                    blast = True
                if parameter_pool.has(PName.BranchMapping):
                    parameter_pool.remove(PName.BranchMapping)
                    blast = True
                
                if blast:
                    prompt.error(ConfigFileMessage.BranchResetWarning);

            ret_result = OperationResult(self,
                                         None,
                                         u'Need clean: {0}. Removed branch: {1}'.format(sanitize, blast),
                                         None)
            return ret_result
Example #8
0
 def execute(self, parameter_pool):
     current_branch, branch_count = shell_utils.get_working_branch(True)
     parameter_pool.put(
         Parameter(ParameterName.CurrentBranch, current_branch,
                   ParameterSource.ConfigFile))
     if current_branch:
         log.info('Current working branch is "{0}".'.format(current_branch))
         branches = parameter_pool.get_value(ParameterName.Branches)\
             if parameter_pool.has(ParameterName.Branches) else None
         if branches and current_branch in list(branches.keys()):
             log.info(
                 'Found registered environment for branch "{0}".'.format(
                     current_branch))
             for key, value in branches[current_branch].items():
                 parameter_pool.put(
                     Parameter(key, value, ParameterSource.ConfigFile))
         else:
             if branch_count == 1:
                 log.info(
                     'Only one unregistered branch found. Using default settings.'
                 )
                 pass
             else:
                 msg = TerminalMessage.FallBackToDefaultBranch.format(
                     current_branch)
                 log.error(msg)
                 prompt.error(msg)
     else:
         # local repository does not have more than one branch, using default
         pass
Example #9
0
def save_env_option_setting_file(location, option_settings):
    log.info(u'Try loading option settings file from {0}'.format(location))
#    old_option_settings = load_env_option_setting_file(location, quiet = True)
    
    log.info(u'Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.iteritems()):
                if not _is_whitelisted_option(namespace, option):
#                    and (namespace not in old_option_settings \
#                         or option not in old_option_settings[namespace]):
                    continue
                
                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                # Add option setting to new file
                if value is None:
                    value = u''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error(u'Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
Example #10
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info('Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = dict()
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value) 
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value 
        
        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error('Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
Example #11
0
def read_aws_credential_file(location,
                             parameter_pool,
                             func_matrix,
                             source,
                             quiet=False):
    try:
        env_name = parameter_pool.get_value(ParameterName.EnvironmentName) \
            if parameter_pool.has(ParameterName.EnvironmentName) else u''

        log.info(u'Reading AWS credential from file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        parser.read(location)

        for name, from_file_func in func_matrix:
            if name == ParameterName.RdsMasterPassword:
                key_name = rds_utils.password_key_name(env_name)
            else:
                key_name = AwsCredentialFileDefault.KeyName[name]

            if parser.has_option(key_name):
                value = parser.get(key_name)
                value = from_file_func(
                    value) if from_file_func is not None else value
                parameter_pool.put(Parameter(name, value, source))
        log.info(u'Finished reading AWS credential from file.')

    except BaseException as ex:
        log.error(u'Failed to retrieve AWS credential from file "{0}", because: "{1}"'.\
                  format(location, ex))
        if not quiet:
            msg = CredentialFileErrorMessage.ReadError.format(location)
            prompt.error(msg)
            raise EBSCliException(msg)
        else:
            return False  # if failed, just skip
 def _validate_change(self, parameter_pool, eb_client, app_name, env_name, 
                      option_settings, option_remove, template_spec):
     response = eb_client.validate_configuration_settings(app_name, option_settings, 
                                                          environment_name = env_name,
                                                          option_remove = option_remove,
                                                          template_specification = template_spec)
     warning_count = 0
     error_count = 0
     for message in response.result:
         if misc.string_equal_ignore_case(message.severity, ValidationSeverity.SeverityError):
             error_count = error_count + 1
         else:
             warning_count = warning_count + 1
         prompt.error(ValidationMessage.ValidateSettingError.format\
                      (message.severity, message.namespace, message.option_name, message.message))
         
     if error_count > 0:            
         log.info('Validating configuration setting failed. Abort command.')
         raise EBSCliException()
     elif warning_count > 0:
         if parameter_pool.get_value(ParameterName.Force) == ServiceDefault.ENABLED:
             pass
         elif not TerminalBase.ask_confirmation(UpdateEnvironmentOptionSettingOpMessage.Continue):
             log.info('User cancelled command.')
             raise EBSCliException()
     else:
         log.info('Validating configuration setting passed.')
Example #13
0
    def run_dev_tools_script(self):

        log.info('Running Dev Tools initialization script.')
        current_path = os.getcwd()

        try:
            if misc.is_os_windows():
                path = shell_utils.climb_dir_tree(
                    shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth)
                #TODO: replace current workaround for WindowsModuleScript
                current_path = os.getcwd()
                script_path = os.path.join(path,
                                           OSSpecific.WindowsModuleScriptPath)
                log.debug('Changing path to {0}.'.format(script_path))
                os.chdir(script_path)

                log.info('Running script "{0}".'.format(
                    OSSpecific.WindowsModuleScriptName))
                shell_utils.call([OSSpecific.WindowsModuleScriptName])

                log.debug('Changing path to {0}.'.format(current_path))
                os.chdir(current_path)

                log.info('Running script "{0}".'.format(
                    OSSpecific.WindowsRepoScript))
                fullpath = os.path.join(path, OSSpecific.WindowsRepoScript)
                shell_utils.call([fullpath])
            else:
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(),
                                                  OSSpecific.LinuxClimbUpDepth)
                log.info('Running script "{0}" at {1}.'.format(
                    OSSpecific.LinuxRepoScript, path))
                fullpath = os.path.join(path, OSSpecific.LinuxRepoScript)
                shell_utils.call([fullpath])

        except subprocess.CalledProcessError as ex:
            # Git returned with an error code
            log.error(
                'Dev Tools initialiation script report an error, because "{0}".'
                .format(ex))
            prompt.error(DevToolsMessage.InitError)
            raise

        except (OSError, IOError) as ex:
            log.error(
                'Failed to call Dev Tools initialiation script, because "{0}".'
                .format(ex))
            # Cannot find or run script
            if ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                prompt.error(DevToolsMessage.FileMissingError.format(fullpath))
            raise
    def execute(self, parameter_pool):
        eb_client = self._get_eb_client(parameter_pool)
        app_name = parameter_pool.get_value(ParameterName.ApplicationName, False)
        env_name = parameter_pool.get_value(ParameterName.EnvironmentName, False)

        response = eb_client.describe_environments(app_name, env_name, include_deleted=False)
        if len(response.result) > 0:
            shell_utils.git_aws_push(False)
        else:
            prompt.error(PushApplicationVersionOpMessage.EnvNotExist.format(env_name))

        ret_result = OperationResult(self, None, None, None)

        return ret_result
Example #15
0
 def _ask_for_specific_profile(cls, parameter_pool, profile_list):
     nameset = set()
     arnset = set()
     for profile in profile_list:
         nameset.add(profile.name)
         arnset.add(profile.arn) 
     
     value = None
     while value is None:
         value = cls.ask_value(parameter_pool, PName.InstanceProfileName)
         if not value in nameset and not value in arnset:
             prompt.error(IamTerminalMessage.ProfileNotExist.format(value))
             value = None  
             
     return value
Example #16
0
 def _ask_for_specific_profile(cls, parameter_pool, profile_list):
     nameset = set()
     arnset = set()
     for profile in profile_list:
         nameset.add(profile.name)
         arnset.add(profile.arn) 
     
     value = None
     while value is None:
         value = cls.ask_value(parameter_pool, PName.InstanceProfileName)
         if not value in nameset and not value in arnset:
             prompt.error(IamTerminalMessage.ProfileNotExist.format(value))
             value = None  
             
     return value
Example #17
0
def write_aws_credential_file(location,
                              parameter_pool,
                              func_matrix,
                              quiet=False):
    try:
        log.info(u'Writing AWS credential to file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        try:
            parser.read(location)
        except IOError as ex:
            pass  # No existing file

        for branch, name, to_file_func in func_matrix:
            if branch:
                value = parameter_pool.get_value(ParameterName.Branches,
                                                 False)[branch][name]
                env_name = parameter_pool.get_value(ParameterName.Branches, False)[branch]\
                    [ParameterName.EnvironmentName]
            else:
                value = parameter_pool.get_value(name, False)
                env_name = parameter_pool.get_value(
                    ParameterName.EnvironmentName, False)

            if to_file_func:
                value = to_file_func(value)

            if name == ParameterName.RdsMasterPassword:
                key_name = rds_utils.password_key_name(env_name)
            else:
                key_name = AwsCredentialFileDefault.KeyName[name]
            parser.set(key_name, value)

        parser.write(location)
        log.info(u'Finished writing AWS credential to file.')

        # Set access permission
        set_access_permission(location, False)
        log.info(u'Set AWS credential file access permission.')

    except BaseException as ex:
        log.error(u'Failed to update AWS credential file at "{0}", because: "{1}"'.\
                  format(location, ex))
        msg = CredentialFileErrorMessage.WriteError.format(location)
        prompt.error(msg)
        if not quiet:
            raise EBSCliException(msg)
        else:
            return False  # if failed, just skip
Example #18
0
 def ask_profile_creation(cls, parameter_pool):
     try:
         iam_client = api_wrapper.create_iam_client(parameter_pool)
         
         original_value = parameter_pool.get_value(PName.InstanceProfileName)
 
         if original_value is None or len(original_value) < 1:
             append_message = TerminalMessage.CurrentValue.format(IamTerminalMessage.CreateProfile)
         else:
             append_message = TerminalMessage.CurrentValue.format(original_value)
         print(IamTerminalMessage.ProfileNameSelect.format(append_message))
         
         profiles = iam_client.list_instance_profiles().result
         sorted_profiles = cls._sort_instance_profile_by_time(profiles)
         
         profile_list = [IamTerminalMessage.CreateProfile];
         for i in range(0, min(len(sorted_profiles), TerminalConstant.IamProfileListNumber)):
             profile_list.append(sorted_profiles[i].name)
         profile_list.append(IamTerminalMessage.OtherProfile)
 
         #profile_index = cls.single_choice(choice_list = profile_list,
         #                                  can_return_none = True)
         profile_index = 1 # I define here to use an IAM profile that I already configured using AWS console - IAM section
         val = profile_index + 1
         print "Using profile aws-elasticbeanstalk-ec2-role %d" % val
 
         if profile_index == 0:
             # Create profile instance from scratch
             value = None
         elif profile_index == len(profile_list) - 1:
             # Name not in list
             value = cls._ask_for_specific_profile(parameter_pool, sorted_profiles)
         else:
             value = profile_list[profile_index] if profile_index is not None else original_value
         
         if value is None or len(value) < 1:
             value = cls._create_default_profile(iam_client, parameter_pool)
     except AccessDeniedException as ex:
         prompt.error(IamTerminalMessage.AccessDeniedMessage.format(ex.message))
         if cls.ask_confirmation(IamTerminalMessage.ContinueWithoutRole):
             value = u''
         else:
             raise EBSCliException()
             
     profile = Parameter(PName.InstanceProfileName, value, PSource.Terminal)
     parameter_pool.put(profile, True)
Example #19
0
def write_aws_credential_file(location, parameter_pool, 
                              func_matrix,
                              quiet = False):
    try:
        log.info(u'Writing AWS credential to file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        try:
            parser.read(location)
        except IOError as ex:
            pass # No existing file
        
        for branch, name, to_file_func in func_matrix:
            if branch:
                value = parameter_pool.get_value(ParameterName.Branches)[branch][name]
                env_name = parameter_pool.get_value(ParameterName.Branches)[branch]\
                    [ParameterName.EnvironmentName]       
            else:
                value = parameter_pool.get_value(name)
                env_name = parameter_pool.get_value(ParameterName.EnvironmentName)       
            
            if to_file_func:
                value = to_file_func(value) 

            if name == ParameterName.RdsMasterPassword:
                key_name = rds_utils.password_key_name(env_name)
            else:
                key_name = AwsCredentialFileDefault.KeyName[name]
            parser.set(key_name, value)
        
        parser.write(location)
        log.info(u'Finished writing AWS credential to file.')
                
        # Set access permission
        set_access_permission(location, False)
        log.info(u'Set AWS credential file access permission.')
        
    except BaseException as ex:
        log.error(u'Failed to update AWS credential file at "{0}", because: "{1}"'.\
                  format(location, ex))
        msg = CredentialFileErrorMessage.WriteError.format(location)
        prompt.error(msg)
        if not quiet:
            raise EBSCliException(msg)
        else:          
            return False # if failed, just skip 
Example #20
0
def rotate_file(location, max_retry=FileDefaultParameter.RotationMaxRetry):
    """ Rotate a file by adding a incremental postfix to filename"""
    if not os.path.exists(location):
        return

    filename = os.path.basename(location)
    path = os.path.dirname(location)
    for i in range(1, max_retry):
        new_location = os.path.join(path, (filename + "_{0}".format(i)))
        if not os.path.exists(new_location):
            log.info('Renamed file "{0}" to "{1}".'.format(location, new_location))
            prompt.info(GeneralFileMessage.RenameFile.format(location, new_location))
            os.rename(location, new_location)
            return
    else:
        log.error("Cannot rotate file {0} because all available names are used.".format(location))
        prompt.error(GeneralFileMessage.RotationNameNotAvailable.format(location))
        return
Example #21
0
    def execute(self, pool):

        # Test if git local repo exists
        if not os.path.isdir(os.path.join(os.getcwd(),
                                          DevToolsConfigFile.Path)):
            prompt.error(
                DevToolsMessage.GitRepoNotExist.format(
                    CommandType.INIT.lower()))
            #            raise EBSCliException()
            return

        region_id = ServiceRegionId[pool.get_value(ParameterName.Region)]
        try:
            self.run_dev_tools_script()

            self._call(DevToolsConfigFile.SetAccessKey,
                       pool.get_value(ParameterName.AwsAccessKeyId))
            self._call(DevToolsConfigFile.SetSecretKey,
                       pool.get_value(ParameterName.AwsSecretAccessKey))
            self._call(DevToolsConfigFile.SetRegion, region_id)
            self._call(DevToolsConfigFile.SetServicePoint,
                       DevToolsConfigFile.Endpoint.format(region_id))
            self._call(DevToolsConfigFile.SetApplicationName,
                       pool.get_value(ParameterName.ApplicationName))
            self._call(DevToolsConfigFile.SetEnvironmentName,
                       pool.get_value(ParameterName.EnvironmentName))

            location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name
            config_file.set_access_permission(location, True)

        except (OSError, IOError, _subprocess.CalledProcessError) as ex:
            log.error(
                "Encountered error when updating AWS Dev Tools settings: {0}.".
                format(ex))
            message = DevToolsMessage.ExecutionError.format(
                DevToolsConfigFile.InitHelpUrl)
            prompt.error(message)


#            raise EBSCliException()

        ret_result = OperationResult(self, None, None, None)
        return ret_result
Example #22
0
 def ask_profile_creation(cls, parameter_pool):
     try:
         iam_client = api_wrapper.create_iam_client(parameter_pool)
         
         original_value = parameter_pool.get_value(PName.InstanceProfileName)
 
         if original_value is None or len(original_value) < 1:
             append_message = TerminalMessage.CurrentValue.format(IamTerminalMessage.CreateProfile)
         else:
             append_message = TerminalMessage.CurrentValue.format(original_value)
         print(IamTerminalMessage.ProfileNameSelect.format(append_message))
         
         profiles = iam_client.list_instance_profiles().result
         sorted_profiles = cls._sort_instance_profile_by_time(profiles)
         
         profile_list = [IamTerminalMessage.CreateProfile];
         for i in range(0, min(len(sorted_profiles), TerminalConstant.IamProfileListNumber)):
             profile_list.append(sorted_profiles[i].name)
         profile_list.append(IamTerminalMessage.OtherProfile)
 
         profile_index = cls.single_choice(choice_list = profile_list,
                                           can_return_none = True)
 
         if profile_index == 0:
             # Create profile instance from scratch
             value = None
         elif profile_index == len(profile_list) - 1:
             # Name not in list
             value = cls._ask_for_specific_profile(parameter_pool, sorted_profiles)
         else:
             value = profile_list[profile_index] if profile_index is not None else original_value
         
         if value is None or len(value) < 1:
             value = cls._create_default_profile(iam_client, parameter_pool)
     except AccessDeniedException as ex:
         prompt.error(IamTerminalMessage.AccessDeniedMessage.format(ex.message))
         if cls.ask_confirmation(IamTerminalMessage.ContinueWithoutRole):
             value = u''
         else:
             raise EBSCliException()
             
     profile = Parameter(PName.InstanceProfileName, value, PSource.Terminal)
     parameter_pool.put(profile, True)
Example #23
0
def rotate_file(location, max_retry = FileDefaultParameter.RotationMaxRetry):
    ''' Rotate a file by adding a incremental postfix to filename'''
    if not os.path.exists(location):
        return
     
    filename = os.path.basename(location)
    path = os.path.dirname(location)
    for i in range(1, max_retry):
        new_location = os.path.join(path, (filename + u'_{0}'.format(i)))
        if not os.path.exists(new_location):
            log.info(u'Renamed file "{0}" to "{1}".'.format(location, new_location))
            prompt.info(GeneralFileMessage.RenameFile.format(location, new_location))
            os.rename(location, new_location)
            return
    else:
        log.error(u'Cannot rotate file {0} because all available names are used.'.\
                  format(location))
        prompt.error(GeneralFileMessage.RotationNameNotAvailable.format(location))
        return
Example #24
0
def save_eb_config_file(location, parameter_pool, quiet = False):
    try:
        log.info('Writing EB configuration to file: "{0}"'.format(location))
        parser = NoSectionConfigParser()

        for (name, _, to_file) in ConfigFileParameters:
            if parameter_pool.has(name):
                value = parameter_pool.get_value(name)
                if to_file is not None:
                    value = to_file(value)
                parser.set(name, value)

        parser.write(location)
        log.info('Finished writing EB configuration file.')
        
    except BaseException as ex:
        log.error('Failed to save EB configuration file, because: "{0}"'.format(ex))
        prompt.error(ConfigFileErrorMessage.WriteError.format(location))        
        raise
Example #25
0
def save_env_option_setting_file(location, option_settings):
    log.info(u'Writing environment option settings to file at "{0}".'.format(
        location))
    try:
        parser = SectionedConfigParser()

        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue

            for option, value in sorted(options.iteritems()):

                if namespace.startswith(OptionSettingContainerPrefix):
                    pass
                elif namespace.startswith(
                        OptionSettingApplicationEnvironment.Namespace):
                    if option in OptionSettingApplicationEnvironment.IgnoreOptionNames:
                        continue
                    else:
                        pass
                # Skip if option setting is on in local option setting list
                elif namespace not in LocalOptionSettings \
                    or option not in LocalOptionSettings[namespace]:
                    continue

                if not parser.has_section(namespace):
                    parser.add_section(namespace)

                if value is None:
                    value = u''
                parser.set(namespace, option, value)

        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        set_access_permission(location, True)
    except BaseException as ex:
        log.error(
            u'Failed to save environment option setting file, because: "{0}"'.
            format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))
        raise
Example #26
0
    def execute(self, parameter_pool):
        eb_client = self._get_eb_client(parameter_pool)
        app_name = parameter_pool.get_value(ParameterName.ApplicationName,
                                            False)
        env_name = parameter_pool.get_value(ParameterName.EnvironmentName,
                                            False)

        response = eb_client.describe_environments(app_name,
                                                   env_name,
                                                   include_deleted=False)
        if len(response.result) > 0:
            shell_utils.git_aws_push(False)
        else:
            prompt.error(
                PushApplicationVersionOpMessage.EnvNotExist.format(env_name))

        ret_result = OperationResult(self, None, None, None)

        return ret_result
    def run_dev_tools_script(self):

        log.info('Running Dev Tools initialization script.')
        current_path = os.getcwd()
        
        try:
            if misc.is_os_windows():
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth)
                #TODO: replace current workaround for WindowsModuleScript
                current_path = os.getcwd()
                script_path = os.path.join(path, OSSpecific.WindowsModuleScriptPath)
                log.debug('Changing path to {0}.'.format(script_path))
                os.chdir(script_path)

                log.info('Running script "{0}".'.format(OSSpecific.WindowsModuleScriptName))
                shell_utils.call([OSSpecific.WindowsModuleScriptName])
                
                log.debug('Changing path to {0}.'.format(current_path))
                os.chdir(current_path)
                
                log.info('Running script "{0}".'.format(OSSpecific.WindowsRepoScript))
                fullpath = os.path.join(path, OSSpecific.WindowsRepoScript)
                shell_utils.call([fullpath])
            else:
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.LinuxClimbUpDepth)
                log.info('Running script "{0}" at {1}.'.format(OSSpecific.LinuxRepoScript,
                                                                path))
                fullpath = os.path.join(path, OSSpecific.LinuxRepoScript)
                shell_utils.call([fullpath])
                
        except subprocess.CalledProcessError as ex:
            # Git returned with an error code
            log.error('Dev Tools initialiation script report an error, because "{0}".'.format(ex))
            prompt.error(DevToolsMessage.InitError)
            raise
        
        except (OSError, IOError) as ex:
            log.error('Failed to call Dev Tools initialiation script, because "{0}".'.format(ex))
            # Cannot find or run script
            if ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                prompt.error(DevToolsMessage.FileMissingError.format(fullpath))
            raise
Example #28
0
def save_eb_config_file(location, parameter_pool, quiet=False):
    try:
        log.info(u'Writing EB configuration to file: "{0}"'.format(location))
        parser = NoSectionConfigParser()

        for (name, _, to_file) in ConfigFileParameters:
            if parameter_pool.has(name):
                value = parameter_pool.get_value(name)
                if to_file is not None:
                    value = to_file(value)
                parser.set(name, value)

        parser.write(location)
        log.info(u'Finished writing EB configuration file.')

    except BaseException as ex:
        log.error(
            u'Failed to save EB configuration file, because: "{0}"'.format(ex))
        prompt.error(ConfigFileErrorMessage.WriteError.format(location))
        raise
    def execute(self, parameter_pool):
        command = parameter_pool.get_value(ParameterName.Command)
        if command == CommandType.INIT:
            sanitize = False
            for name, ori_name in EbConfigFile.BranchResetParameters.items():
                if parameter_pool.has(ori_name) and \
                    parameter_pool.get_value(name) != parameter_pool.get_value(ori_name):
                    sanitize = True
                    break

            blast = False
            if sanitize:
                if parameter_pool.has(ParameterName.Branches):
                    parameter_pool.remove(ParameterName.Branches)
                    blast = True
                if parameter_pool.has(ParameterName.BranchMapping):
                    parameter_pool.remove(ParameterName.BranchMapping)
                    blast = True
                
                if blast:
                    prompt.error(ConfigFileMessage.BranchResetWarning);
    def execute(self, parameter_pool):
        command = parameter_pool.get_value(ParameterName.Command)
        if command == CommandType.INIT:
            sanitize = False
            for name, ori_name in EbConfigFile.BranchResetParameters.iteritems():
                if parameter_pool.has(ori_name) and \
                    parameter_pool.get_value(name) != parameter_pool.get_value(ori_name):
                    sanitize = True
                    break

            blast = False
            if sanitize:
                if parameter_pool.has(ParameterName.Branches):
                    parameter_pool.remove(ParameterName.Branches)
                    blast = True
                if parameter_pool.has(ParameterName.BranchMapping):
                    parameter_pool.remove(ParameterName.BranchMapping)
                    blast = True
                
                if blast:
                    prompt.error(ConfigFileMessage.BranchResetWarning);
    def execute(self, parameter_pool):
        location = EbLocalDir.Path + os.path.sep + EbConfigFile.Name
        
        try:
            config_file.load_eb_config_file(location, parameter_pool, False)
            if config_file.check_access_permission(location) is False:
                message = ConfigFileErrorMessage.PermissionError.format(EbConfigFile.Name)
                log.info(message)
                prompt.error(message)
           
            #Post processing
            if not parameter_pool.has(ParameterName.RdsSnippetUrl)\
                and parameter_pool.has(ParameterName.Region):
                region = parameter_pool.get_value(ParameterName.Region)
                parameter_pool.put(Parameter(ParameterName.RdsSnippetUrl,
                                             RdsDefault.get_snippet_url(region),
                                             ParameterSource.ConfigFile))
            
        except EBConfigFileNotExistError:
            log.error('Configuration file "{0}" not exist.'.format(EbConfigFile.Name))
            prompt.error(ConfigFileMessage.CannotFind.format\
                        (EbConfigFile.Name, CommandType.INIT.lower()))
            raise EBSCliException()
            
        except BaseException as ex:
            log.error('Encountered error when load configuration file "{0}", becuase "{1}".'.\
                      format(EbConfigFile.Name, ex))
            prompt.error(ConfigFileMessage.CorrectionSuggestion.
                         format(location,CommandType.INIT.lower()))
            raise

        ret_result = OperationResult(self, None, None, None)
        return ret_result
Example #32
0
def save_env_option_setting_file(location, option_settings):
    log.info('Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.items()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.items()):

                if namespace.startswith(OptionSettingContainerPrefix):
                    pass
                elif namespace.startswith(OptionSettingApplicationEnvironment.Namespace):
                    if option in OptionSettingApplicationEnvironment.IgnoreOptionNames:
                        continue
                    else:
                        pass
                # Skip if option setting is on in local option setting list
                elif namespace not in LocalOptionSettings \
                    or option not in LocalOptionSettings[namespace]:
                    continue

                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                if value is None:
                    value = ''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug('Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error('Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
        
Example #33
0
    def ask_master_password(cls, parameter_pool):
        for _ in range(0, RdsDefault.PasswordMismatchThreshold):
            password = cls.ask_value(parameter_pool,
                                     PName.RdsMasterPassword,
                                     no_echo=True)
            if password is None:
                return
            else:
                if not ParameterValidator.validate_alphanumeric(
                        password, RdsDefault.PasswordMinSize,
                        RdsDefault.PasswordMaxSize):
                    prompt.error(RdsTerminalMessage.PasswordWrongFormat)
                    continue

                confirm = cls.line_input(
                    RdsTerminalMessage.PasswordConfirmation,
                    can_return_none=False,
                    no_echo=True)
                if confirm != password:
                    prompt.error(RdsTerminalMessage.PasswordNotMatch)
                else:
                    parameter_pool.put(
                        Parameter(PName.RdsMasterPassword, password,
                                  PSource.Terminal), True)
                    return
        else:
            prompt.error(RdsTerminalMessage.PasswordMatchFailure)
            raise EBSCliException()
Example #34
0
 def ask_master_password(cls, parameter_pool):
     for _ in range(0, RdsDefault.PasswordMismatchThreshold):
         password = cls.ask_value(parameter_pool, PName.RdsMasterPassword, no_echo = True)
         if password is None:
             return 
         else:
             if not ParameterValidator.validate_alphanumeric(password,
                                                             RdsDefault.PasswordMinSize,
                                                             RdsDefault.PasswordMaxSize):
                 prompt.error(RdsTerminalMessage.PasswordWrongFormat)
                 continue
             
             confirm = cls.line_input(RdsTerminalMessage.PasswordConfirmation, 
                                      can_return_none = False, no_echo = True)
             if confirm != password:
                 prompt.error(RdsTerminalMessage.PasswordNotMatch)
             else:
                 parameter_pool.put(Parameter(PName.RdsMasterPassword, 
                                              password, 
                                              PSource.Terminal), 
                                    True)
                 return                    
     else:
         prompt.error(RdsTerminalMessage.PasswordMatchFailure)
         raise EBSCliException()
Example #35
0
    def execute(self, parameter_pool):
        location = EbLocalDir.Path + os.path.sep + EbConfigFile.Name
        
        try:
            config_file.load_eb_config_file(location, parameter_pool, False)
            if config_file.check_access_permission(location) is False:
                message = ConfigFileErrorMessage.PermissionError.format(EbConfigFile.Name)
                log.info(message)
                prompt.error(message)
           
            #Post processing
            if not parameter_pool.has(ParameterName.RdsSnippetUrl)\
                and parameter_pool.has(ParameterName.Region):
                region = parameter_pool.get_value(ParameterName.Region, False)
                parameter_pool.put(Parameter(ParameterName.RdsSnippetUrl,
                                             RdsDefault.get_snippet_url(region),
                                             ParameterSource.ConfigFile))
            
        except EBConfigFileNotExistError:
            log.error(u'Configuration file "{0}" not exist.'.format(EbConfigFile.Name))
            prompt.error(ConfigFileMessage.CannotFind.format\
                        (EbConfigFile.Name, CommandType.INIT.lower()))
            raise EBSCliException()
            
        except BaseException as ex:
            log.error(u'Encountered error when load configuration file "{0}", becuase "{1}".'.\
                      format(EbConfigFile.Name, ex))
            prompt.error(ConfigFileMessage.CorrectionSuggestion.
                         format(location,CommandType.INIT.lower()))
            raise

        ret_result = OperationResult(self, None, None, None)
        return ret_result
    def execute(self, pool):
        
        # Test if git local repo exists
        if not os.path.isdir(os.path.join(os.getcwd(), DevToolsConfigFile.Path)):
            prompt.error(DevToolsMessage.GitRepoNotExist)
#            raise EBSCliException()
            return
        
        try:
            self.run_dev_tools_script()
            
            location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name        
            config_file.set_access_permission(location, True)
            
        except (OSError, IOError, subprocess.CalledProcessError) as ex:
            log.error("Encountered error when updating AWS Dev Tools settings: {0}.".format(ex))
            message = DevToolsMessage.ExecutionError.format(DevToolsConfigFile.InitHelpUrl)
            prompt.error(message)
#            raise EBSCliException()
        
        ret_result = OperationResult(self, None, None, None)
        return ret_result
 def execute(self, parameter_pool):
     current_branch, branch_count = shell_utils.get_working_branch(True)
     parameter_pool.put(Parameter(ParameterName.CurrentBranch, current_branch, ParameterSource.ConfigFile))
     if current_branch:
         log.info(u'Current working branch is "{0}".'.format(current_branch))
         branches = (
             parameter_pool.get_value(ParameterName.Branches) if parameter_pool.has(ParameterName.Branches) else None
         )
         if branches and current_branch in branches.keys():
             log.info(u'Found registered environment for branch "{0}".'.format(current_branch))
             for key, value in branches[current_branch].iteritems():
                 parameter_pool.put(Parameter(key, value, ParameterSource.ConfigFile))
         else:
             if branch_count == 1:
                 log.info(u"Only one unregistered branch found. Using default settings.")
                 pass
             else:
                 msg = TerminalMessage.FallBackToDefaultBranch.format(current_branch)
                 log.error(msg)
                 prompt.error(msg)
     else:
         # local repository does not have more than one branch, using default
         pass
Example #38
0
def load_eb_config_file(location, parameter_pool, quiet=False):
    try:
        log.info(u'Reading EB configuration from file: "{0}"'.format(location))
        parser = NoSectionConfigParser()
        parser.read(location)

        for (name, from_file, _) in ConfigFileParameters:
            if parser.has_option(name):
                value = parser.get(name)
                if from_file is not None:
                    value = from_file(value)
                parameter_pool.put(
                    Parameter(name, value, ParameterSource.ConfigFile))

        # Add original solution stack infos
        if parser.has_option(ParameterName.SolutionStack):
            parameter_pool.put(
                Parameter(ParameterName.OriginalSolutionStack,
                          parser.get(ParameterName.SolutionStack),
                          ParameterSource.ConfigFile))

        log.info(u'Finished reading from EB configuration file.')

    except BaseException as ex:
        log.error(
            u'Failed to parse EB configuration from file, because: "{0}"'.
            format(ex))
        if not quiet:
            if (isinstance(ex, OSError) or isinstance(ex, IOError)) and\
                ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                raise EBConfigFileNotExistError(ex)
            else:
                msg = ConfigFileErrorMessage.ReadError.format(location)
                prompt.error(msg)
                raise EBConfigFileNotExistError(msg)
        else:
            pass  # if failed, just skip
Example #39
0
    def execute(self, pool):
        
        # Test if git local repo exists
        if not os.path.isdir(os.path.join(os.getcwd(), DevToolsConfigFile.Path)):
            prompt.error(DevToolsMessage.GitRepoNotExist.format(CommandType.INIT.lower()))
#            raise EBSCliException()
            return
        
        region_id = ServiceRegionId[pool.get_value(ParameterName.Region)]
        try:
            self.run_dev_tools_script()
            
            self._call(DevToolsConfigFile.SetAccessKey, 
                       pool.get_value(ParameterName.AwsAccessKeyId))
            self._call(DevToolsConfigFile.SetSecretKey, 
                       pool.get_value(ParameterName.AwsSecretAccessKey))
            self._call(DevToolsConfigFile.SetRegion, 
                       region_id)
            self._call(DevToolsConfigFile.SetServicePoint, 
                       DevToolsConfigFile.Endpoint.format(region_id))
            self._call(DevToolsConfigFile.SetApplicationName, 
                       pool.get_value(ParameterName.ApplicationName))
            self._call(DevToolsConfigFile.SetEnvironmentName, 
                       pool.get_value(ParameterName.EnvironmentName))
            
            location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name        
            config_file.set_access_permission(location, True)
            
        except (OSError, IOError, _subprocess.CalledProcessError) as ex:
            log.error("Encountered error when updating AWS Dev Tools settings: {0}.".format(ex))
            message = DevToolsMessage.ExecutionError.format(DevToolsConfigFile.InitHelpUrl)
            prompt.error(message)
#            raise EBSCliException()
        
        ret_result = OperationResult(self, None, None, None)
        return ret_result
Example #40
0
def load_env_option_setting_file(location, option_settings=None, quiet=False):
    log.info('Reading environment option settings from file at "{0}".'.format(
        location))

    if option_settings is None:
        option_settings = dict()

    try:
        parser = SectionedConfigParser()
        parser.read(location)

        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value)
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value

        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        check_access_permission(location, True)
        return option_settings

    except BaseException as ex:
        log.error(
            'Failed to load environment option setting file, because: "{0}"'.
            format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(
                OptionSettingFileErrorMessage.ReadError.format(location))
            raise
    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)      
    def execute(self, pool):

        # Test if git local repo exists
        if not os.path.isdir(os.path.join(os.getcwdu(), DevToolsConfigFile.Path)):
            prompt.error(DevToolsMessage.GitRepoNotExist)
            return

        error = False
        current_path = os.getcwdu()
        try:
            log.info(u"Running Dev Tools initialization script.")
            if misc.is_os_windows():
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth)
                # TODO: replace current workaround for WindowsModuleScript
                current_path = os.getcwdu()
                script_path = os.path.join(path, OSSpecific.WindowsModuleScriptPath)
                log.debug(u"Changing path to {0}.".format(script_path))
                os.chdir(script_path)

                log.info(u'Running script "{0}".'.format(OSSpecific.WindowsModuleScriptName))
                shell_utils.call([OSSpecific.WindowsModuleScriptName])

                log.debug(u"Changing path to {0}.".format(current_path))
                os.chdir(current_path)

                log.info(u'Running script "{0}".'.format(OSSpecific.WindowsRepoScript))
                fullpath = os.path.join(path, OSSpecific.WindowsRepoScript)
                prompt.error(shell_utils.call([fullpath]))
            else:
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.LinuxClimbUpDepth)
                log.info(u'Running script "{0}" at {1}.'.format(OSSpecific.LinuxRepoScript, path))
                fullpath = os.path.join(path, OSSpecific.LinuxRepoScript)
                prompt.error(shell_utils.call([fullpath]))

            location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name
            config_file.set_access_permission(location, True)

        except subprocess.CalledProcessError as ex:
            # Git returned with an error code
            log.error(u'Dev Tools initialization script report an error, because "{0}".'.format(ex))
            error = True
            prompt.error(DevToolsMessage.InitError)

        except (OSError, IOError) as ex:
            log.error(u'Failed to call Dev Tools initialization script, because "{0}".'.format(ex))
            # Cannot find or run script
            error = True
            if ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                if fullpath:
                    prompt.error(DevToolsMessage.FileMissingError.format(fullpath))
                else:
                    prompt.error(ex)

        finally:
            if error:
                prompt.error(DevToolsMessage.ExecutionError.format(DevToolsConfigFile.InitHelpUrl))

        ret_result = OperationResult(self, None, None, None)
        return ret_result
Example #43
0
def save_eb_config_file(location, parameter_pool, quiet = False):
    log.info(u'Writing EB configuration to file: "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        parser.add_section(EbConfigFile.RootSectionName)    # Make root section the first

        # add known session
        for section_name, (condition, keys) in EbConfigFile.KnownSections.iteritems():
            if parameter_pool.has(condition) and parameter_pool.get_value(condition):
                log.debug(u'Create section "{0}" in config file.'.format(section_name))
                parser.add_section(section_name)
                for key in sorted(keys):
                    if parameter_pool.has(key):
                        value = parameter_pool.get_value(key)
                        if key in ConfigFileParameters:
                            _, to_file = ConfigFileParameters[key]
                            if to_file is not None:
                                value = to_file(value)
                        parser.set(section_name, key, value)

        # add branch mapping sections
        if parameter_pool.has(ParameterName.BranchMapping)\
            and len(parameter_pool.get_value(ParameterName.BranchMapping)) > 0:
            log.debug(u'Create section "{0}" in config file.'.format(EbConfigFile.BranchSectionName))
            parser.add_section(EbConfigFile.BranchSectionName)
            branch_map = parameter_pool.get_value(ParameterName.BranchMapping)
            for key in sorted(branch_map.keys()):
                parser.set(EbConfigFile.BranchSectionName, key, branch_map[key])

        # add branch environment sections
        if parameter_pool.has(ParameterName.Branches)\
            and len(parameter_pool.get_value(ParameterName.Branches)) > 0:
            branches = parameter_pool.get_value(ParameterName.Branches)
            for branch_name in sorted(branches.keys()):
                section_name = EbConfigFile.BranchSectionPrefix + branch_name
                log.debug(u'Create section "{0}" in config file.'.format(section_name))
                parser.add_section(section_name)
                branch = branches[branch_name]
                for key in sorted(EbConfigFile.BranchSectionKeys):
                    if key in branch:
                        value = branch[key]
                        if key in ConfigFileParameters:
                            _, to_file = ConfigFileParameters[key]
                            if to_file is not None:
                                value = to_file(value)
                        parser.set(section_name, key, value)
            
        # add else
        if parameter_pool.has(ParameterName.ConfigFileExtra): 
            extra_config = parameter_pool.get_value(ParameterName.ConfigFileExtra) 
            for section, pairs in sorted(extra_config.iteritems(), key=operator.itemgetter(0)):
                if not parser.has_section(section):
                    log.debug(u'Create section "{0}" in config file.'.format(section))
                    parser.add_section(section)
                for key, value in pairs.iteritems():
                    parser.set(section, key, value)

        parser.write(location)
        log.info(u'Finished writing EB configuration file.')
        
    except BaseException as ex:
        log.error(u'Failed to save EB configuration file, because: "{0}"'.format(ex))
        prompt.error(ConfigFileErrorMessage.WriteError.format(location))        
        raise
Example #44
0
def load_eb_config_file(location, parameter_pool, quiet = False):
    log.info(u'Reading EB configuration from file: "{0}"'.format(location))
    try:
        try: 
            parser = SectionedConfigParser()
            parser.read(location)

            log.debug(u'Found a sectioned config file.')
            extra_config = dict()
            extra_config[EbConfigFile.RootSectionName] = dict()
            branches = dict()
            
            for section in parser.sections():
                log.debug(u'Reading section "{0}" from config file.'.format(section))                
                # Known sections
                if section in EbConfigFile.KnownSections.keys(): 
                    for key, value in parser.items(section):
                        if ConfigFileParameters.has_key(key):
                            from_file, _ = ConfigFileParameters[key]
                            if from_file:
                                value = from_file(value)
                            parameter_pool.put(Parameter(key, value, ParameterSource.ConfigFile))
                        else:
                            extra_config[EbConfigFile.RootSectionName][key] = value
                            
                elif section == EbConfigFile.BranchSectionName:
                    #branch section
                    branch_mapping = dict()
                    for key, value in parser.items(section):
                        branch_mapping[key] = value
                    parameter_pool.put(Parameter(ParameterName.BranchMapping, 
                                                 branch_mapping, 
                                                 ParameterSource.ConfigFile))
                    
                elif section.startswith(EbConfigFile.BranchSectionPrefix):
                    #branch environment session
                    parsed = section.split(EbConfigFile.SectionNameDelimiter)
                    if len(parsed) != 2 or misc.is_blank_string(parsed[1]):
                        continue    # skip if no environment name
                    branch_name = parsed[1]
                    branches[branch_name] = dict()
                    for key, value in parser.items(section):
                        if ConfigFileParameters.has_key(key):
                            from_file, _ = ConfigFileParameters[key]
                            if from_file:
                                value = from_file(value)
                            branches[branch_name][key] = value
                        else:
                            if not extra_config.has_key(section):
                                extra_config[section] = dict()
                            extra_config[section][key] = value
                
                else:
                    # unknown sections
                    new_section = dict()
                    for key, value in parser.items(section):
                        new_section[key] = value 
                    extra_config[section] = new_section

            parameter_pool.put(Parameter(ParameterName.ConfigFileExtra, 
                                         extra_config, 
                                         ParameterSource.ConfigFile))
            parameter_pool.put(Parameter(ParameterName.Branches, 
                                         branches, 
                                         ParameterSource.ConfigFile))     
            
        except MissingSectionHeaderError as ex:
            # old format: sectionless config file
            log.debug(u'Found a section-less config file.')
            nosect_parser = NoSectionConfigParser()
            nosect_parser.read(location)

            for name, (from_file, _) in ConfigFileParameters.iteritems():
                if nosect_parser.has_option(name):
                    value = nosect_parser.get(name)
                    if from_file is not None:
                        value = from_file(value)
                    parameter_pool.put(Parameter(name, value, ParameterSource.ConfigFile))
        
        # Add original parameter infos
        for name, ori_name in EbConfigFile.BranchResetParameters.iteritems():
            if parameter_pool.has(name):
                parameter_pool.put(Parameter(ori_name, 
                                             parameter_pool.get_value(name), 
                                             ParameterSource.ConfigFile))

        log.info(u'Finished reading from EB configuration file.')
   
    except BaseException as ex:
        log.error(u'Failed to parse EB configuration from file, because: "{0}"'.format(ex))
        if not quiet:
            if (isinstance(ex, OSError) or isinstance(ex, IOError)) and\
                ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                raise EBConfigFileNotExistError(ex)
            else:
                msg = ConfigFileErrorMessage.ReadError.format(location)
                prompt.error(msg)
                raise EBConfigFileNotExistError(msg)
        else:    
            pass # if failed, just skip     
Example #45
0
    def execute(self, pool):
        
        # Test if git local repo exists
        if not os.path.isdir(os.path.join(os.getcwdu(), DevToolsConfigFile.Path)):
            prompt.error(DevToolsMessage.GitRepoNotExist.format(''))
            return

        error = False
        current_path = os.getcwdu()        
        try:
            log.info(u'Running Dev Tools initialization script.')
            if misc.is_os_windows():
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth)
                #TODO: replace current workaround for WindowsModuleScript
                current_path = os.getcwdu()
                script_path = os.path.join(path, OSSpecific.WindowsModuleScriptPath)
                log.debug(u'Changing path to {0}.'.format(script_path))
                os.chdir(script_path)

                log.info(u'Running script "{0}".'.format(OSSpecific.WindowsModuleScriptName))
                shell_utils.call([OSSpecific.WindowsModuleScriptName])
                
                log.debug(u'Changing path to {0}.'.format(current_path))
                os.chdir(current_path)
                
                log.info(u'Running script "{0}".'.format(OSSpecific.WindowsRepoScript))
                fullpath = os.path.join(path, OSSpecific.WindowsRepoScript)
                prompt.error(shell_utils.call([fullpath]))
            else:
                path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.LinuxClimbUpDepth)
                log.info(u'Running script "{0}" at {1}.'.format(OSSpecific.LinuxRepoScript,
                                                                path))
                fullpath = os.path.join(path, OSSpecific.LinuxRepoScript)
                prompt.error(shell_utils.call([fullpath]))
            
            location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name        
            config_file.set_access_permission(location, True)

        except subprocess.CalledProcessError as ex:
            # Git returned with an error code
            log.error(u'Dev Tools initialization script report an error, because "{0}".'.format(ex))
            error = True
            prompt.error(DevToolsMessage.InitError.format(ex.message))
        
        except (OSError, IOError) as ex:
            log.error(u'Failed to call Dev Tools initialization script, because "{0}".'.format(ex))
            # Cannot find or run script
            error = True
            if ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                if fullpath:
                    prompt.error(DevToolsMessage.FileMissingError.format(fullpath))
                else:
                    prompt.error(ex)

        finally:
            if error:            
                prompt.error(DevToolsMessage.ExecutionError.format(DevToolsConfigFile.InitHelpUrl))
        
        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)