def workspaceDeleteStatus(self, workspaceId): print('Starting check for deleted workspace status.....') 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.get('{}/api/workspace/{}'.format( cheApiAddress, workspaceId), headers=headers) respJson = r.json() # print (respJson) exceptionMessage = respJson["message"] print('The deleted workspace exception messsage is {}'.format( exceptionMessage)) testString = "Workspace with id '" + workspaceId + "' doesn't exist" if testString in exceptionMessage: return True else: return False except Exception as e: print('Unexpected workspace get status found: {}'.format(e)) print('Raw text of request/response: [{}]'.format(r.text)) return False
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 cleanTenant(self): # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)" theToken = helpers.get_user_tokens().split(";")[0] print('Starting cleaning tenant.....') serverAddress = os.getenv("WIT_API") authHeader = 'Bearer {}'.format(theToken) headers = {'Accept': 'application/json', 'Authorization': authHeader, 'X-App': 'osio', 'X-Git-Provider': 'GitHub', 'Content-Type': 'application/json'} r = requests.delete( '{}/api/user/services'.format(serverAddress), headers=headers ) assert r.status_code == 200 r = requests.patch( '{}/api/user/services'.format(serverAddress), headers=headers ) assert r.status_code == 200 print('Tenant is cleaned.....')
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))
def createWorkspaceForSpace(self, spaceID): print('Creating 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' } print('Looking for codebases of space: {}'.format(spaceID)) r = requests.get('{}/api/spaces/{}/codebases'.format( apiAddress, spaceID), headers=headers) r.status_code | should.be(200).desc( "Status code for response to get space codebases is 200.") cbId = r.json()["data"][0]["id"] try: r = requests.post('{}/api/codebases/{}/create'.format( apiAddress, cbId), data="", headers=headers) respJson = r.json() # print (respJson) workspaceLink = respJson["links"]["open"] print('Workspace created at {}'.format(workspaceLink)) print('Looking for workspaces of codebase: {}'.format(cbId)) r = requests.get('{}/api/codebases/{}/workspaces'.format( apiAddress, cbId), headers=headers) r.status_code | should.be(200).desc( "Status code for response to get codebase workspaces is 200.") ws = r.json()["data"][0] wsId = ws["attributes"]["id"] wsName = ws["attributes"]["name"] print("Workspace ID (name): {} ({})".format(wsId, wsName)) print("Starting workspace") r = requests.post( '{}/api/workspace/{}/runtime?environment=default'.format( cheApiAddress, wsId), data='{}', headers=headers) print('Starting workspace response: [{}]'.format(r.text)) return wsId except Exception as e: print('Unexpected error found: {}'.format(e)) return None
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))
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 createSpace(self, spaceName): # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)" theToken = helpers.get_user_tokens().split(";")[0] print('Starting test.....') serverAddress = os.getenv("SERVER_ADDRESS") authHeader = 'Bearer {}'.format(theToken) headers = { 'Accept': 'application/json', 'Authorization': authHeader, 'X-App': 'OSIO', 'X-Git-Provider': 'GitHub', 'Content-Type': 'application/json' } data = '{{\ "data": {{\ "attributes": {{\ "description": "This is the osioperf collaboration space",\ "name": "{}"\ }},\ "type": "spaces"\ }}\ }}'.format(spaceName) print('Making request to create a new space "{}"...'.format(spaceName)) try: r = requests.post('{}/api/spaces'.format(serverAddress), headers=headers, data=data) # print 'request results = {}'.format(r.content) try: respJson = r.json() spaceID = respJson["data"]["id"] print('The spaceID is: {}'.format(spaceID)) return spaceID except ValueError: return None except Exception as e: print('Unexpected space creation 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
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))
def workspaceStatus(self, workspaceId, maxAttempts, expectedStatus): print('Starting check for workspace status.....') 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' } for i in range(1, int(maxAttempts) + 1): time.sleep(10) try: r = requests.get('{}/api/workspace/{}'.format( cheApiAddress, workspaceId), headers=headers) respJson = r.json() # print (respJson) # Commented out as this displays the machine token: # helpers.printToJson('Query the Che workspace status request response', r) workspaceStatus = respJson["status"] print('The new workspace status is: {}, looking for {}'.format( workspaceStatus, expectedStatus)) if workspaceStatus == expectedStatus: return True except Exception as e: print('Unexpected workspace get status found: {}'.format(e)) print('Raw text of request/response: [{}]'.format(r.text)) return False
def removeSpaces(self): # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)" theToken = helpers.get_user_tokens().split(";")[0] print('Starting removing spaces.....') spacesIDs = self.getSpaces() print('Number of spaces before removing: {}'.format(len(spacesIDs))) serverAddress = os.getenv("WIT_API") authHeader = 'Bearer {}'.format(theToken) headers = { 'Accept': 'application/json', 'Authorization': authHeader, 'X-App': 'OSIO', 'X-Git-Provider': 'GitHub', 'Content-Type': 'application/json' } # delete spaces for spaceID in spacesIDs: print('Deleting space {}'.format(spaceID)) r = requests.delete('{}/api/spaces/{}'.format( serverAddress, spaceID), headers=headers) assert r.status_code == 200 if r.status_code != 200: print( "ERROR - Request to delete space {} failed - error code = {}" .format(spaceID, r.status_code)) print('Number of spaces after removing: {}'.format( len(self.getSpaces()))) print('Spaces removed!')
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))
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))
def removeSpaces(self): # Tokens are stored in a form of "<access_token>;<refresh_token>(;<username>)" theToken = helpers.get_user_tokens().split(";")[0] print('Starting removing spaces.....') spacesIDs = self.getSpaces() print('Number of spaces before removing: {}'.format(len(spacesIDs))) serverAddress = os.getenv("WIT_API") cheAddress = os.getenv("CHE_API") authHeader = 'Bearer {}'.format(theToken) headers = { 'Accept': 'application/json', 'Authorization': authHeader, 'X-App': 'OSIO', 'X-Git-Provider': 'GitHub', 'Content-Type': 'application/json' } # delete spaces for spaceID in spacesIDs: print('Looking for codebases of space: {}'.format(spaceID)) r = requests.get('{}/api/spaces/{}/codebases'.format( serverAddress, spaceID), headers=headers) r.status_code | should.be(200).desc( "Status code for response to get space codebases is 200.") codebases = r.json()["data"] if codebases is not None: for cb in codebases: cbID = cb["id"] print( 'Looking for workspaces of codebase: {}'.format(cbID)) r = requests.get('{}/api/codebases/{}/workspaces'.format( serverAddress, cbID), headers=headers) r.status_code | should.be(200).desc( "Status code for response to get codebase workspaces is 200." ) workspaces = r.json()["data"] if workspaces is not None: for ws in workspaces: wsID = ws["attributes"]["id"] wsName = ws["attributes"]["name"] print('Deleting workspace: {} ({})'.format( wsID, wsName)) r = requests.delete('{}/api/workspace/{}'.format( cheAddress, wsID), headers=headers) r.status_code | should.be(204).desc( "Status code for response to delete workspace is 204." ) else: print("No workspaces found.") else: print("No codebases found.") print('Deleting space {}'.format(spaceID)) r = requests.delete('{}/api/spaces/{}'.format( serverAddress, spaceID), headers=headers) r.status_code | should.be(200).desc( "Status code for response to delete space is 200.") if r.status_code != 200: print( "ERROR - Request to delete space {} failed - error code = {}" .format(spaceID, r.status_code)) print('Number of spaces after removing: {}'.format( len(self.getSpaces()))) print('Spaces removed!')
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))