Beispiel #1
0
    def checkCodebases(self, maxAttempts=10):
        serverUrl = os.getenv("SERVER_ADDRESS")
        spaceId = helpers.getSpaceID()
        codebasesUrl = '{}/api/spaces/{}/codebases'.format(serverUrl, spaceId)

        theToken = helpers.get_user_tokens().split(";")[0]
        authHeader = 'Bearer {}'.format(theToken)
        headers = {'Accept': 'application/json',
                   'Authorization': authHeader,
                   'X-App': 'osio',
                   'X-Git-Provider': 'GitHub',
                   'Content-Type': 'application/x-www-form-urlencoded'}

        global boosterImported
        for i in range(1, int(maxAttempts) + 1):
            r = requests.get(
                codebasesUrl,
                headers=headers
            )
            helpers.printToJson('Attempt to get codebases #{}:'.format(i), r)
            responseJson = r.json()
            data = responseJson['data']
            cbCount = len(data)
            print('Codebases found: {}'.format(cbCount))
            if cbCount > 0:
                boosterImported = True
                return True
            time.sleep(1)
        boosterImported = False
        return False
    def getStackReport(self, reportKey):

        print('Getting Stack Analyses report.....')

        theToken = helpers.get_user_tokens().split(";")[0]
        authHeader = 'Bearer {}'.format(theToken)

        # TODO - Convert to environment variables
        _url = 'https://recommender.api.openshift.io'
        _endpoint = '/api/v1/stack-analyses'

        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        try:
            r = requests.get(urljoin(_url, _endpoint + '/' + reportKey),
                             headers=headers)
            print(r.text)
            helpers.printToJson('Obtain the stack analyses report', r)
            return r.text

        except Exception as e:
            print('Unexpected stack analyses report: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
Beispiel #3
0
    def buildStatus(self, sleepTimer, maxTries, expectedBuildStatus,
                    expectedAnnotation=None):
        """
        Function to query builds, wait/retry for expected build status
        """
        print('Look for build, expected build status: {}'.format(expectedBuildStatus))

        counter = 0
        requestFailed = True

        while (requestFailed and (counter < maxTries)):
            counter = counter + 1

            try:
                print('Making request to check for the build status...')
                r = requests.get(self.urlString, headers=self.headers)

                helpers.printToJson('Build status response', r)

                respJson = r.json()
                actualBuildStatus = respJson['items'][0]['status']['phase']

                expectedAnnotationFound = False
                if expectedAnnotation is not None:
                    expectedAnnotationFound = (
                        expectedAnnotation in respJson['items'][0]['metadata']['annotations']
                    )

                if re.search(expectedBuildStatus, actualBuildStatus):
                    print('Expected build status {} found'.format(expectedBuildStatus))
                    if expectedAnnotation is not None:
                        if expectedAnnotationFound:
                            print('Expected annotation "{}" found'.format(expectedAnnotation))
                            requestFailed = False
                            break
                        else:
                            print(
                                'Expected annotation "{}" not found, retrying...'
                                .format(expectedAnnotation)
                            )
                            time.sleep(sleepTimer)
                            continue
                    requestFailed = False
                    break
                else:
                    print('Expected build status not found, retrying - expected: "{}" actual: "{}" '
                          .format(expectedBuildStatus, actualBuildStatus)
                          )
                    time.sleep(sleepTimer)

            except IndexError as e:
                print('Unexpected error found: {}'.format(e))
                print('attempt {} failed - retrying...'.format(counter))
                time.sleep(sleepTimer)

        # print('The value of requestFailed = {}'.format(requestFailed))

        return not requestFailed
Beispiel #4
0
    def runTest(self, theString):
        print(theString)

        ###############################################
        # Initialize variables from Environment setting

        osoUsername = os.environ.get('OSO_USERNAME')
        clusterAddress = os.environ.get('OSO_CLUSTER_ADDRESS')

        oso_token = os.environ.get('OSO_TOKEN')
        temp = 'Bearer ' + oso_token
        headers = {
            'Authorization': temp,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub'
        }

        # Generate a request to find the routes
        urlString = '{}/oapi/v1/namespaces/{}-stage/routes'.format(
            clusterAddress, osoUsername)

        r = requests.get(urlString, headers=headers)
        # r = requests.get(
        #   'https://api.starter-us-east-2.openshift.com:443/oapi/v1/namespaces/ldimaggi-stage/routes',
        #   headers=headers
        # )
        # print r.text

        respJson = r.json()
        # print respJson

        routeString = respJson['items'][0]['status']['ingress'][0]['host']
        # print routeString

        urlString = 'http://{}'.format(routeString)

        # Example staged app endpoint:
        # http://test123-ldimaggi-stage.8a09.starter-us-east-2.openshiftapps.com

        print('Starting Stage test.....')

        # Wait to ensure that deploy to stage has time to complete
        time.sleep(60)

        r = requests.get(urlString)
        print('stage request results = {}'.format(r.text))

        result = r.text
        helpers.printToJson('Promote to Stage response', r)
        global stageDeployed
        if re.search('Using the greeting service', result):
            stageDeployed = True
            return 'Success'
        else:
            stageDeployed = False
            return 'Fail'
Beispiel #5
0
    def getReportKey(self, codebaseUrl):
        print('Getting Stack Analyses report ID.....')

        theToken = helpers.get_user_tokens().split(";")[0]
        authHeader = 'Bearer {}'.format(theToken)

        # TODO - Convert to environment variables
        _url = 'https://recommender.api.openshift.io'
        _endpoint = '/api/v1/stack-analyses'

        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'osio',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        # print ('the target URL is:' + codebaseUrl)

        # Remove the ".git" suffix from the URL
        shortenedUrl = codebaseUrl

        if shortenedUrl.endswith(".git"):
            shortenedUrl = codebaseUrl[:-4]
        # print ('the shortened URL is:' + shortenedUrl)

        # TODO - The call is failing to all repos other than booster repos
        #shortenedUrl = 'https://github.com/wildfly-swarm-openshiftio-boosters/wfswarm-rest-http'

        payload = {
            'github_url': shortenedUrl,
            'source': 'osio',
            'github_ref': 13
        }

        try:
            r = requests.post(urljoin(_url, _endpoint),
                              headers=headers,
                              data=payload)

            # print (r.text)
            respJson = r.json()
            helpers.printToJson('Obtain the stack analyses key', r)
            theID = respJson["id"]
            print('Stack analyses key  {}'.format(theID))
            return theID

        except Exception as e:
            print('Unexpected stack analyses key: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
Beispiel #6
0
    def importGithubRepo(self, gitRepo):

        ###############################################
        # Environment variables
        #
        # Note: Pipelines = https://forge.api.openshift.io/api/services/jenkins/pipelines
        # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)"
        theToken = helpers.get_user_tokens().split(";")[0]
        projectName = os.getenv('PROJECT_NAME')
        pipeline = os.getenv('PIPELINE')
        spaceId = helpers.getSpaceID()
        authHeader = 'Bearer {}'.format(theToken)

        print('Starting test.....')

        ###############################################
        # Import the booster
        headers = {'Accept': 'application/json',
                   'Authorization': authHeader,
                   'X-App': 'osio',
                   'X-Git-Provider': 'GitHub',
                   'Content-Type': 'application/x-www-form-urlencoded'}
        data = {'gitRepository': gitRepo,
                'projectName': projectName,
                'pipeline': pipeline,
                'space': spaceId}

        forgeApi = os.getenv("FORGE_API")

        print('Making request to import...')

        try:
            r = requests.post(
                '{}/api/osio/import'.format(forgeApi),
                headers=headers,
                data=data
            )
            # print('request results = {}'.format(r.text))
            helpers.printToJson('Import booster request response', r)

            result = r.text
            if re.search('uuid', result):
                return 'Success'
            else:
                return 'Fail'

        except Exception as e:
            print('Unexpected booster import exception found: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
    def getCodebaseUrl(self, maxAttempts=10):
        serverUrl = os.getenv("SERVER_ADDRESS")
        spaceId = helpers.getSpaceID()
        codebasesUrl = '{}/api/spaces/{}/codebases'.format(serverUrl, spaceId)

        theToken = helpers.get_user_tokens().split(";")[0]
        authHeader = 'Bearer {}'.format(theToken)
        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        for i in range(1, 2):
            r = requests.get(codebasesUrl, headers=headers)
            helpers.printToJson('Attempt to get codebases #{}:'.format(i), r)
            responseJson = r.json()
            data = responseJson['data'][0]['attributes']['url']
            time.sleep(5)
        return data
Beispiel #8
0
    def promoteBuild(self):
        """
        Promote build from stage to run
        """
        forgeApi = os.getenv("FORGE_API")
        osoUsername = os.getenv("OSO_USERNAME")
        githubUsername = os.getenv("GITHUB_USERNAME")
        githubRepo = helpers.getGithubRepo()

        theToken = helpers.get_user_tokens().split(";")[0]
        headers = {"Authorization": "Bearer {}".format(theToken),
                   'X-App': 'OSIO',
                   'X-Git-Provider': 'GitHub'}

        promoteUrl = "{}/api/openshift/services/jenkins/{}-jenkins/job/{}/job/{}/job/{}".format(
            forgeApi,
            osoUsername,
            githubUsername,
            githubRepo,
            "master/lastBuild/input/Proceed/proceedEmpty"
        )

        print("Promote URL: {}".format(promoteUrl))
        print("Making request to promote build from Stage to Run...")

        try:
            r = requests.post(promoteUrl, headers=headers)
            helpers.printToJson('Promote response', r)

            if r.status_code == 200:
                print("SUCCESS - Successful promotion")
                return True
            else:
                print("ERROR - Request failed to promote - error code = {}".format(r.status_code))
                return False

        except Exception as e:
            print('Unexpected booster promote exception found: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
Beispiel #9
0
    def workspaceStop(self, workspaceId):
        print('Stopping workspace.....')

        theToken = helpers.get_user_tokens().split(";")[0]
        authHeader = 'Bearer {}'.format(theToken)

        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/json'
        }
        try:
            r = requests.delete('{}/api/workspace/{}/runtime'.format(
                cheApiAddress, workspaceId),
                                headers=headers)
            # respJson = r.json()
            # print (respJson)
            helpers.printToJson('Stop the Che workspace request response', r)

        except Exception as e:
            print('Unexpected workspace stop status found: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
    def getReportKey(self, codebaseUrl):
        print('Getting Stack Analyses report ID.....')

        githubUsername = os.getenv("GITHUB_USERNAME")
        githubPassword = os.getenv("GITHUB_PASSWORD")

        theToken = helpers.get_user_tokens().split(";")[0]
        authHeader = 'Bearer {}'.format(theToken)

        _url = os.getenv("ANALYSES_API")
        _endpoint = '/api/v1/stack-analyses'

        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        print('the target URL is:' + codebaseUrl)

        # Remove the ".git" suffix from the URL
        shortenedUrl = codebaseUrl

        if shortenedUrl.endswith(".git"):
            shortenedUrl = codebaseUrl[:-4]
        print('the shortened URL is: {}'.format(shortenedUrl))

        spaceName = helpers.getSpaceName()
        projectName = os.getenv("PROJECT_NAME")
        gitRepo = spaceName + "-" + projectName
        print('the gitRepo name is: {}'.format(gitRepo))

        cloneUrl = codebaseUrl.replace(
            'https://github.com/',
            'https://{}:{}@github.com/'.format(githubUsername, githubPassword))
        # print('the cloneUrl = {}'.format(cloneUrl))

        # Invoke shell script to create and upload to master effective pom
        try:
            testOutput = subprocess.check_output(
                ['./test.sh', cloneUrl, gitRepo, githubUsername])
            print('Output = {}'.format(testOutput))
        except Exception as e:
            print('Unexpected subprocess exception: {}'.format(e))

        print('the shortened URL is: {}'.format(shortenedUrl))

        # TODO - The call is failing to all repos other than booster repos
        # shortenedUrl = 'https://github.com/wildfly-swarm-openshiftio-boosters/wfswarm-rest-http'

        # payload = {
        #    'github_url': shortenedUrl,
        #    'source': 'osio',
        #    'github_ref': 13
        # }
        filePathList = './tempDir/' + gitRepo
        payload = {
            # 'filePath': filePathList,
            # 'manifest': manifestList
            'filePath': [filePathList],
            'manifest': ['pom.xml']
        }

        try:
            r = requests.post(urljoin(_url, _endpoint),
                              headers=headers,
                              data=payload)

            print('response text = {}'.format(r.text))
            respJson = r.json()
            helpers.printToJson('Obtain the stack analyses key', r)
            theID = respJson["id"]
            print('Stack analyses key  {}'.format(theID))
            return theID

        except Exception as e:
            print('Unexpected stack analyses key: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
Beispiel #11
0
    def createWorkspace(self, workspaceName):
        # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)"
        theToken = helpers.get_user_tokens().split(";")[0]
        print('Creating space now.....')

        # serverAddress = 'https://che.openshift.io/api/workspace'

        authHeader = 'Bearer {}'.format(theToken)

        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/json'
        }

        # Extended JSON from Ilya expressed as string
        payloadString2 = '{\
              "projects": [\
                {\
                  "projectType": "maven",\
                  "projects": [],\
                          "commands": [\
                            {\
                              "commandLine": "mvn -f ${current.project.path} clean install",\
                              "name": "console-java-simple:build",\
                      "type": "mvn",\
                      "attributes": {\
                        "previewUrl": "",\
                        "goal": "Build"\
                      }\
                    },\
                    {\
                      "commandLine": "mvn -f ${current.project.path} clean install NEW_LINE java -jar ${current.project.path}/target/*.jar",\
                      "name": "console-java-simple:run",\
                      "type": "mvn",\
                      "attributes": {\
                        "previewUrl": "",\
                        "goal": "Run"\
                      }\
                    }\
                  ],\
                  "mixins": [],\
                  "problems": [],\
                  "links": [],\
                  "category": "Samples",\
                  "options": {},\
                  "source": {\
                    "location": "GITHUB_REPO_NAME",\
                    "type": "git",\
                    "parameters": {}\
                  },\
                  "description": "A hello world Java application.",\
                  "displayName": "console-java-simple",\
                  "tags": [\
                    "java",\
                    "maven"\
                  ],\
                  "name": "console-java-simple",\
                  "path": "/console-java-simple",\
                  "attributes": {\
                    "language": [\
                       "java"\
                    ]\
                  },\
                  "type": "maven"\
                }\
              ],\
              "commands": [\
                {\
                  "commandLine": "mvn clean install -f ${current.project.path}",\
                  "name": "build",\
                  "type": "mvn",\
                  "attributes": {\
                    "goal": "Build",\
                    "previewUrl": ""\
                  }\
                },\
                {\
                  "commandLine": "mvn -f ${current.project.path} clean install",\
                  "name": "console-java-simple:build",\
                  "type": "mvn",\
                  "attributes": {\
                    "previewUrl": "",\
                    "goal": "Build"\
                  }\
                },\
               {\
                  "commandLine": "mvn -f ${current.project.path} clean install NEW_LINE java -jar ${current.project.path}/target/*.jar",\
                  "name": "console-java-simple:run",\
                  "type": "mvn",\
                  "attributes": {\
                    "previewUrl": "",\
                    "goal": "Run"\
                  }\
                }\
              ],\
              "defaultEnv": "default",\
              "environments": {\
                "default": {\
                  "recipe": {\
                    "type": "dockerimage",\
                    "content": "registry.devshift.net/che/centos_jdk8"\
                  },\
                  "machines": {\
                    "dev-machine": {\
                      "volumes": {},\
                      "servers": {\
                        "tomcat8": {\
                          "attributes": {},\
                          "protocol": "http",\
                          "port": "8080"\
                        }\
                      },\
                      "installers": [\
                        "com.redhat.oc-login",\
                        "com.redhat.bayesian.lsp",\
                        "org.eclipse.che.ws-agent",\
                        "org.eclipse.che.terminal",\
                        "org.eclipse.che.exec"\
                      ],\
                      "env": {},\
                      "attributes": {\
                        "memoryLimitBytes": 2147483648\
                      }\
                    }\
                  }\
                }\
              },\
              "name": "WORKSPACE_NAME",\
              "attributes": {},\
              "links": []\
        }'

        # Replace strings in the payloadString

        # Replace NEW_LINE with \n (json.loads fails on \n)
        tempString1 = payloadString2.replace("NEW_LINE", "\\n")
        # print tempString1

        # Replace workspace name placeholder with actual intended workspace name
        tempString2 = tempString1.replace("WORKSPACE_NAME", workspaceName)
        # print tempString2

        # Replace github repo name placeholder
        tempString3 = tempString2.replace("GITHUB_REPO_NAME", githubRepoUrl)
        # print (tempString3)

        d = json.loads(tempString3)
        # print (d)
        print('Making request to create a new workspace "{}"...'.format(
            workspaceName))

        try:
            r = requests.post(
                '{}/api/workspace?start-after-create=true'.format(
                    cheApiAddress),
                headers=headers,
                json=d)
            # print 'request results = {}'.format(r.content)
            try:
                respJson = r.json()
                # print(respJson)
                helpers.printToJson(
                    'Create the Che workspace request response', r)

                workspaceUrl = respJson["links"]["ide"]
                print('The new workspace name is: {}'.format(workspaceUrl))

                workspaceId = respJson["id"]
                print('The new workspace id is: {}'.format(workspaceId))

                return workspaceId
            except ValueError:
                return None

        except Exception as e:
            print(
                'Unexpected workspace creation exception found: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
Beispiel #12
0
    def launch(self,
               projectName,
               mission="rest-http",
               runtime="vert.x",
               version="redhat",
               pipeline="maven-releasestageapproveandpromote",
               blankBooster="false"):

        ###############################################
        # Environment variables
        # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)"
        theToken = helpers.get_user_tokens().split(";")[0]
        spaceId = helpers.getSpaceID()
        spaceName = helpers.getSpaceName()
        authHeader = 'Bearer {}'.format(theToken)

        gitRepo = '{}-{}'.format(spaceName, projectName)
        helpers.setGithubRepo(gitRepo)

        print('Starting test.....')

        ###############################################
        # Launch the booster
        headers = {
            'Accept': 'application/json',
            'Authorization': authHeader,
            'X-App': 'OSIO',
            'X-Git-Provider': 'GitHub',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'emptyGitRepository': blankBooster,  # true for blank booster
            'mission': mission,
            'runtime': runtime,
            'runtimeVersion': version,
            'pipeline': pipeline,
            'projectName': projectName,
            'projectVersion': '1.0.0',
            'gitRepository': gitRepo,
            'groupId': 'io.openshift.booster',
            'artifactId': projectName,
            'spacePath': spaceName,
            'space': spaceId
        }

        forgeApi = os.getenv("FORGE_API")

        print('Making request to launch...')

        try:
            r = requests.post('{}/api/osio/launch'.format(forgeApi),
                              headers=headers,
                              data=data)
            # print('request results = {}'.format(r.text))
            helpers.printToJson('Launch booster request response', r)

            result = r.text
            if re.search('GITHUB_PUSHED', result):
                return 'Success'
            else:
                return 'Fail'

        except Exception as e:
            print('Unexpected booster launch exception found: {}'.format(e))
            print('Raw text of request/response: [{}]'.format(r.text))
Beispiel #13
0
    def buildStatus(self,
                    sleepTimer,
                    maxTries,
                    expectedBuildStatuses,
                    expectedAnnotation=None):
        """
        Function to query builds, wait/retry for expected build status
        """
        print('Look for build, expected build status - one of {}'.format(
            expectedBuildStatuses))
        if isinstance(expectedBuildStatuses, str):
            expectedBuildStatuses = [expectedBuildStatuses]

        counter = 0
        requestFailed = True

        while (requestFailed and (counter < maxTries)):
            counter = counter + 1

            print('Making request to check for the build status...')
            r = requests.get(self.urlString, headers=self.headers)

            helpers.printToJson('Build status response', r)

            respJson = r.json()
            currentBuild = None
            for item in respJson['items']:
                if re.search('{}-\\d+'.format(self.projectName),
                             item['metadata']['name']):
                    currentBuild = item

            if currentBuild is None:
                print(
                    'Attempt {} failed - current build not found - retrying...'
                    .format(counter))
                time.sleep(sleepTimer)
                continue

            actualBuildStatus = currentBuild['status']['phase']

            expectedAnnotationFound = False
            if expectedAnnotation is not None:
                expectedAnnotationFound = (
                    expectedAnnotation
                    in currentBuild['metadata']['annotations'])
            ebsFound = None
            for ebs in expectedBuildStatuses:
                if re.search(ebs, actualBuildStatus):
                    ebsFound = ebs
                    break
            if ebsFound is not None:
                print('Expected build status {} found'.format(ebsFound))
                if expectedAnnotation is not None:
                    if expectedAnnotationFound:
                        print('Expected annotation "{}" found'.format(
                            expectedAnnotation))
                        requestFailed = False
                        break
                    else:
                        print(
                            'Expected annotation "{}" not found, retrying...'.
                            format(expectedAnnotation))
                        time.sleep(sleepTimer)
                        continue
                requestFailed = False
                break
            else:
                if actualBuildStatus in ('Failed', 'Aborted'):
                    print('Actual build status is {} - Fail fast triggered...'.
                          format(actualBuildStatus))
                    return False
                print(
                    'Expected build status not found, retrying - expected: "{}" actual: "{}" '
                    .format(expectedBuildStatuses, actualBuildStatus))
                time.sleep(sleepTimer)

        # print('The value of requestFailed = {}'.format(requestFailed))

        return not requestFailed