Ejemplo n.º 1
0
 def test_process_raw_inventory(self):
     result = inventory.process_inventory(self.uuid)
     # get inventory via job:
     # print(Job.objects.get(uuid=self.uuid).inv.get_queryset())
     # get inventory via inventory:
     # print(Inventory.objects.filter(job__uuid=self.uuid))
     self.assertTrue(result)
Ejemplo n.º 2
0
 def setUp(self):
     self.uuid = "7c32d60d-6312-42f6-a73a-22da56b07372"
     self.job = Job.objects.get(uuid=self.uuid)
     self.testUsername = '******'
     self.testPassword = '******'
     self.email = "*****@*****.**"
     self.first_name = "test"
     self.last_name = "user"
     # create a test user
     self.user = User.objects.create_user(username=self.testUsername,
                                          password=self.testPassword,
                                          email=self.email,
                                          first_name=self.first_name,
                                          last_name=self.last_name)
     inventory.process_inventory(self.uuid)
     self.inventory = Inventory.objects.filter(job__uuid=self.uuid)
     self.checkpoint = checkpoint.Checkpoint(self.uuid)
Ejemplo n.º 3
0
    def test_get_inventory_view(self):
        client = APIClient()
        token = AuthToken.objects.create(self.user)
        result = inventory.process_inventory(self.uuid)
        response = client.get('/api/v1/inventory/{}'.format(self.uuid),
                              format='json',
                              HTTP_AUTHORIZATION="Token %s" % token)

        # for item in response.json():
        #     print(json.dumps(item, indent=4, sort_keys=True))

        self.assertEqual(response.status_code, 200)
Ejemplo n.º 4
0
def manageJenkinsJob(uuid):
    iterations = 0
    obj = Job.objects.get(uuid=uuid)
    job = obj.job
    parameters = {
        'timer': '5',
        'username': obj.username,
        'tags': ','.join(map(lambda x: x.name, obj.tags.all()))
    }
    lastBuildNumber = jenkins.getLastBuildNumber(job)
    jenkins.startJobWithParameters(job, parameters)
    time.sleep(0.2)
    build = jenkins.getLastBuild(job)
    obj.jobNumber = lastBuildNumber + 1
    obj.save()
    if build['number'] != lastBuildNumber + 1:
        logger.info(
            'JOBS:WARNING:JOBNUMBER_INCONSISTENT:FOUND:{}:---EXPECTED:{}'.
            format(build['number'], lastBuildNumber + 1))
        logger.info('JOBS:FINISH:JOB:PARAMETERS:{}'.format(
            build['parameters']))

    job_number = lastBuildNumber + 1
    meta_obj = JobMetadata(job=obj)
    meta_obj.save()
    while True:
        obj = Job.objects.get(uuid=uuid)
        if obj.status == "FINISHED":
            # TODO: add fetching of job post-deploy files and add to database
            # TODO: change this to obj.username from default 'demo'
            # s3_output = jenkins.getJobCompletionContent(job, "{}/s3_credentials.json".format(obj.username))
            # inventory = jenkins.getJobCompletionContent(job, "{}/inventory.json".format(obj.username))
            s3_output = jenkins.getJobCompletionContent(
                job, "{}/s3_credentials.json".format('demo'))
            inventory = jenkins.getJobCompletionContent(
                job, "{}/inventory.json".format('demo'))
            try:
                logger.info('JOBS:FINISH:S3:{}'.format(s3_output.json()))
                meta_obj.s3 = s3_output.json()
            except Exception as e:
                logger.info(
                    'JOBS:FINISH:EXCEPTION_PROCESSING_S3_JSON:E:{}'.format(e))
                meta_obj.s3 = "ERROR_PROCESSING"
            try:
                # logger.info('JOBS:FINISH:INVENTORY:{}'.format(inventory.json()))
                clean_inventory = dict(
                    filter(lambda x: 'mgmt.pants.net' in x[0],
                           inventory.json().items()))
                meta_obj.inventory = clean_inventory

            except Exception as e:
                logger.info(
                    'JOBS:FINISH:EXCEPTION_PROCESSING_INVENTORY_JSON:E:{}'.
                    format(e))
                meta_obj.inventory = "ERROR_PROCESSING"
            meta_obj.save()
            inventory_processed = process_inventory(uuid)
            if inventory_processed:
                logger.info('JOBS:FINISH:PROCESS:INVENTORY:SUCCESS')
            else:
                logger.info('JOBS:FINISH:PROCESS:INVENTORY:FAILURE')
            if job != 'network-example':
                kafkaPoller.delay(uuid)
            else:
                logger.info('JOBS:FINISH:NO_KAFKA_POLLER_REQUIRED')
            # if job == 'pipeline_demo':
            #     kafkaPoller.delay(uuid)
            return "FINISHED"
        if iterations > 700:
            return "TIMED_OUT"
        stdout = jenkins.getJobStdout(job, job_number)
        if job == 'job-example-project':
            status = getExamplePipelineStatus(stdout.text)
        elif job == 'pipeline_demo':
            status = getPipelineDemoStatus(stdout.text)
        elif job == 'network-example':
            status = getNetworkSDNExampleStatus(stdout.text)
        logger.info('JOBS:ITERATION:{}'.format(iterations))
        logger.info('JOBS:STATUS:{}'.format(status))
        logger.info('JOBS:JOB_NUMBER:{}'.format(job_number))
        if status:
            obj.status = status['status']
            obj.progress = status['progress']
            meta_obj.stdout = stdout.text
            meta_obj.save()
            obj.save()
        iterations += 1
        time.sleep(4)