Example #1
0
    def saveCredentials(self, credentials, database=None):
        """ Save the Credentials to the datastore. """
        logging.debug('Saving EC2 credentials...')
        service = backendservices(self.user_data)
        params = {}
        params['credentials'] = credentials
        params["infrastructure"] = AgentTypes.EC2
        
        # Check if the supplied credentials are valid of not
        if service.validateCredentials(params):
            self.user_data.valid_credentials = True
            result = {'credentials_status': True, 'credentials_msg': 'Credentials saved successfully! The EC2 keys have been validated.'}
            # See if the amazon db table is intitalized
            if not self.user_data.isTable():
                db_credentials = self.user_data.getCredentials()
                # Set the environmental variables
                #                    os.environ["AWS_ACCESS_KEY_ID"] = credentials['EC2_ACCESS_KEY']
                #                    os.environ["AWS_SECRET_ACCESS_KEY"] = credentials['EC2_SECRET_KEY']

                try:
                    if not database:
                        database = DynamoDB(access_key=credentials['EC2_ACCESS_KEY'],
                                            secret_key=credentials['EC2_SECRET_KEY'])
                            
                    database.createtable(JobDatabaseConfig.TABLE_NAME)
                    database.createtable(JobDatabaseConfig.COST_ANALYSIS_TABLE_NAME)

                    self.user_data.is_amazon_db_table = True

                except Exception,e:
                    logging.error('Failed to create database tables', str(e))
def get_all_jobs_time_cost(uuid, access_key, secret_key):
    database = DynamoDB(access_key, secret_key)
    results = database.getEntry(attribute_name='uuid', attribute_value=uuid,
                                table_name=JobDatabaseConfig.COST_ANALYSIS_TABLE_NAME)

    jobs = []
    if results is None:
        return jobs
    
    try:
        seen_instance_types = {} # so we don't get duplicate entries
        for result in results:
            logging.info('result = {0}'.format(result))
            job = {}
            job['agent'] = result['agent']
            job['instance_type'] = result['instance_type']
            if job['instance_type'] in seen_instance_types:
                continue
            else:
                seen_instance_types[job['instance_type']] = 1
            job['status'] = result['status']
            if 'time_taken' not in result:
                time = 0
            else:
                logging.info("time taken: {0}".format(result['time_taken']))
                time = result['time_taken']
                if time:
                    time = time.partition(' ')[0]#seconds
                else:
                    time = 0
                
            job['time'] = str(float(time)/60.00)
            
            cost_per_hour = Price.COST_TABLE_PER_HOUR['ec2'][job['instance_type']]
            job['cost'] = str( cost_per_hour * float(time)/3600.00)
            time_by_hour = int(float(time))/3600 + 1
            job['cost_by_hour'] = str(cost_per_hour * time_by_hour)
            
            logging.info('agent: '+job['agent']+', instance_type: '+job['instance_type']+', time: '+job['time']+', cost: '+job['cost']+', cost per hour: '+str(cost_per_hour)+', cost by hour: '+ job['cost_by_hour'])
            jobs.append(job)
        return jobs
    except Exception, e:
        logging.error("{0}".format(e))
        return {}
Example #3
0
    def saveCredentials(self, credentials, database=None):
        """ Save the Credentials to the datastore. """
        logging.debug('Saving EC2 credentials...')
        service = backendservices(self.user_data)
        params = {}
        params['credentials'] = credentials
        params["infrastructure"] = AgentTypes.EC2

        currentCredentials = self.user_data.getCredentials()

        if len(credentials['EC2_ACCESS_KEY']) > 0 and len(
                credentials['EC2_SECRET_KEY']) > 0:
            uniqAccessLetters = list(set(credentials['EC2_ACCESS_KEY']))
            uniqSecretLetters = list(set(credentials['EC2_SECRET_KEY']))

            if uniqAccessLetters[0] == '*' and len(uniqAccessLetters) == 1:
                params['credentials']['EC2_ACCESS_KEY'] = currentCredentials[
                    'EC2_ACCESS_KEY']

            if uniqSecretLetters[0] == '*' and len(uniqSecretLetters) == 1:
                params['credentials']['EC2_SECRET_KEY'] = currentCredentials[
                    'EC2_SECRET_KEY']

        # Check if the supplied credentials are valid of not
        if service.validateCredentials(params):
            self.user_data.valid_credentials = True
            result = {
                'credentials_status':
                True,
                'credentials_msg':
                'Credentials saved successfully! The EC2 keys have been validated.'
            }
            # See if the amazon db table is intitalized
            if not self.user_data.isTable():
                db_credentials = self.user_data.getCredentials()
                # Set the environmental variables
                #                    os.environ["AWS_ACCESS_KEY_ID"] = credentials['EC2_ACCESS_KEY']
                #                    os.environ["AWS_SECRET_ACCESS_KEY"] = credentials['EC2_SECRET_KEY']

                try:
                    if not database:
                        database = DynamoDB(
                            access_key=credentials['EC2_ACCESS_KEY'],
                            secret_key=credentials['EC2_SECRET_KEY'])

                    database.createtable(JobDatabaseConfig.TABLE_NAME)
                    database.createtable(
                        JobDatabaseConfig.COST_ANALYSIS_TABLE_NAME)

                    self.user_data.is_amazon_db_table = True

                except Exception, e:
                    logging.error('Failed to create database tables', str(e))