def server_diff(hostA, hostB, source_file, remote_path): global OverthereHostSession global OperatingSystemFamily global LocalConnectionOptions global ObertherHostSession global StringUtils global Diff #context.logOutput(remote_path) localOpts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX) local_session = OverthereHostSession(OverthereHost(localOpts)) sessionA = OverthereHostSession(hostA) sessionB = OverthereHostSession(hostB) try: d1 = sessionA.remote_file(remote_path) local_d1 = local_session.work_dir_file("hostA") local_session.copy_to(d1, local_d1) myFile = "%s" % (local_d1) local_f1 = myFile.split(':')[1] d2 = sessionB.remote_file(remote_path) local_d2 = local_session.work_dir_file("hostB") local_session.copy_to(d2, local_d2) myFile = "%s" % (local_d2) local_f2 = myFile.split(':')[1] response = local_session.execute("diff %s %s" % (local_f1, local_f2), check_success=False) finally: sessionA.close_conn() sessionB.close_conn() local_session.close_conn() # End try return response.stdout
def docker_machine_inspect(machine_name): print "Inspect docker '{0}' machine ".format(machine_name) command_line = "docker-machine inspect {0}".format(machine_name) local_opts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX) host = OverthereHost(local_opts) session = OverthereHostSession(host) print "Executing '{0}'....".format(command_line) try: response = session.execute(command_line, check_success=False) out = "" for s in response.stdout: out = "%s %s\n" % (out, s) print out for s in response.stderr: print "ERR", s if response.rc != 0: sys.exit(response.rc) return json.loads(out) except: tb = traceback.format_exc() print "Error" print tb finally: session.close_conn()
def test_with_support(self): s = OverthereHostSession(self._linuxhost) with s: work_dir = s.work_dir().path eq_(os.path.exists(work_dir), True) eq_(os.path.exists(work_dir), False)
def preview(self,deployed): try: session = OverthereHostSession(self.helmclient.host,stream_command_output=False) command_line = self.command_line(session,deployed) print command_line finally: session.close_conn()
def content_to_file(input_content): localOpts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX) host = OverthereHost(localOpts) session = OverthereHostSession(host) input_file = session.get_conn().getTempFile("a_json_file", ".json") session.copy_text_to_file(input_content, input_file) print("input_file is {0}".format(input_file)) return input_file
def _action(self, verb, data): session = OverthereHostSession(self.cluster.kubectlHost) data_str = self.data_to_string(data) print data_str remote_file = session.upload_text_content_to_work_dir( data_str, 'k8s_{0}_resource.json'.format(verb)) command_line = "{0} {1} -f {2}".format(self.get_kubectl_command(), verb, remote_file.path) print command_line self._execute(session, command_line)
def get_associated_pods(ci): pods = [] session = OverthereHostSession(target_host) command_line = "{0} get {1} -l=component={2} -o json".format(get_kubectl_command(ci.container), 'pods', ci.name) print command_line response = session.execute(command_line) if response.rc == 0: data = json.loads(" ".join(response.stdout)) for item in data['items']: pods.append(item['metadata']['name']) return pods
def __init__(self, server, dirname="tmp"): self.sshOpts = SshConnectionOptions(server['host'], username=server['username'], password=server['password']) self.host = OverthereHost(self.sshOpts) self.session = OverthereHostSession(self.host) self.WorkDirBase = server['filesystemLocation'] self.WorkDirTimeStamp = int(time.time()) self.WorkDirName = dirname self.workDirectory = None self.RtcClient = server['pathToClientSoftware']
def get_pod_events(ci, pod_name): events = [] session = OverthereHostSession(target_host) command_line = "{0} get {1} --field-selector involvedObject.name={2} -o json".format( get_kubectl_command(ci.container), 'event', pod_name) response = session.execute(command_line) if response.rc == 0: data = json.loads(" ".join(response.stdout)) for item in data['items']: events.append("{type} {message}".format(**item)) return events
def execute(self,deployed): try: session = OverthereHostSession(self.helmclient.host,stream_command_output=False) command_line = self.command_line(session,deployed) print command_line uploaded_runner = session.upload_text_content_to_work_dir(command_line,'xldeploy_helm.sh',executable=True) print uploaded_runner.path response = session.execute(command_line,check_success=False) print "\n ".join(response.stdout) print "\n ".join(response.stderr) rc = response.rc if response.rc > 0: sys.exit(rc) finally: session.close_conn()
def gke_describe(name, target): command_line = "gcloud container clusters describe {0} --format=json".format( name) print command_line host = target.container session = OverthereHostSession(host) print "Executing '{0}'....".format(command_line) try: response = session.execute(command_line) return json.loads(" ".join(response.stdout)) except: tb = traceback.format_exc() print "Error" print tb finally: session.close_conn()
def test_execution_ctx_logging(self): class ExecutionContext(object): def __init__(self): self.output_success = False self.error_success = False def logOutput(self, msg): self.output_success = True def logError(self, msg): self.error_success = True exec_ctx = ExecutionContext() session = OverthereHostSession(self._linuxhost, execution_context=exec_ctx) session.logger.info("Check") eq_(exec_ctx.output_success, True) session.logger.error("Check") eq_(exec_ctx.error_success, True)
def docker_machine_env(machine_name): print "Env docker '{0}' machine ".format(machine_name) command_line = "docker-machine env {0}".format(machine_name) local_opts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX) host = OverthereHost(local_opts) session = OverthereHostSession(host) print "Executing '{0}'....".format(command_line) try: response = session.execute(command_line) return to_map(response.stdout) except: tb = traceback.format_exc() print "Error" print tb finally: session.close_conn()
def execute_docker_compose(context, composed, application, command, options=''): session = OverthereHostSession(composed.container.host, stream_command_output=True, execution_context=context) runtime = { 'application': application, 'command': command, 'options': options, 'uploaded_compose_file': session.upload_file_to_work_dir(composed.file), 'dockerHost': composed.container.dockerHost, 'cert_pem': session.upload_text_content_to_work_dir(composed.container.certPem, "cert.pem"), 'ca_pem': session.upload_text_content_to_work_dir(composed.container.caPem, "ca.pem"), 'key_pem': session.upload_text_content_to_work_dir(composed.container.keyPem, "key.pem") } print "docker-compose {0}....".format(runtime['command']) command_line = "docker-compose --file {r[uploaded_compose_file].path} --project-name {r[application]} --tlscacert {r[ca_pem].path} --tlscert {r[cert_pem].path} --tlskey {r[key_pem].path} --tlsverify -H {r[dockerHost]} --skip-hostname-check {r[command]} {r[options]} ".format( r=runtime) print '---------------------------' print command_line print '---------------------------' response = session.execute(command_line) if response.rc != 0: sys.exit(response.rc)
def __init__(self, server): self.retryCount = server['retryCount'] if self.retryCount == 0: self.retryCount = 1 self.retryBaseInterval = 60 self.sshOpts = SshConnectionOptions(server['host'], username=server['username'], privateKeyFile=server['key_file'], password=server['password'], port=server['port']) self.host = OverthereHost(self.sshOpts) self.session = OverthereHostSession(self.host) tries = 0 while tries < self.retryCount: tries += 1 try: self.zipBinary = server['pathToZipExecutable'] self.workingDirectory = server['workingDirectory'] self.create_directory(self.workingDirectory) workDir = self.session.remote_file(self.workingDirectory) self.session.get_conn().setWorkingDirectory(workDir) break except: if tries > 3: print sys.exc_info()[0] print sys.exc_info()[1] if tries > self.retryCount: print sys.exc_info()[2].format_stack() print "we were unable to setup a connection to server: %s after %i retries" % ( server['host'], tries) sys.exit(2) sleeptime = self.retryBaseInterval * tries print "retrying to setup a connection to server %s in %i seconds" % ( server['host'], sleeptime) time.sleep(sleeptime)
def apply(self, content, values): try: localOpts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX) host = OverthereHost(localOpts) self.session = OverthereHostSession(host, stream_command_output=False) command_line = self.command_line(content, values) #print command_line uploaded_runner = self.session.upload_text_content_to_work_dir( command_line, 'xldeploy_xl_runner.sh', executable=True) #print uploaded_runner.path response = self.session.execute(command_line, check_success=False) print "\n```" print "\n ".join(response.stdout) print "\n ".join(response.stderr) print "```\n " rc = response.rc if response.rc > 0: sys.exit(rc) finally: if not self.session == None: self.session.close_conn()
def run(self, verb, parameters, json_output=False, raise_on_fail=False): session = OverthereHostSession(self.cluster.kubectlHost) command_line = "{0} {1} {2}".format(self.get_kubectl_command(), verb, parameters) if json_output: command_line = command_line + " -o json" print command_line response = session.execute(command_line, suppress_streaming_output=True, check_success=False) if response.rc == 0: stdout = "\n".join(response.stdout) if json_output: return json.loads(stdout) else: return stdout else: if raise_on_fail: raise Exception("Kubectl Error when running '{0}':{1}".format( command_line, '\n'.join(response.stderr))) else: return '\n'.join(response.stderr)
def execute(self, xmlaccessscript, config_uri=None): session = OverthereHostSession(self.host, stream_command_output=False, execution_context=self.exec_context) with session: request_file = session.upload_text_content_to_work_dir( xmlaccessscript, "request.xml") response_file = session.work_dir_file("response.xml") fs = session.os.fileSeparator executable = "%s%sbin%sxmlaccess%s" % (self.wp_home, fs, fs, session.os.scriptExtension) wp_url = self.resolve_config_url(config_uri) cmd_line = [ executable, "-user", self.wp_user, "-password", self.wp_password, "-url", wp_url ] cmd_line.extend( ["-in", request_file.path, "-out", response_file.path]) resp = session.execute(cmd_line, check_success=False) response_xml = "" if response_file.exists(): response_xml = session.read_file(response_file.path) if resp.rc != 0: self.exec_context.logError("XML Access failed!!!") self.log_with_header("XML Access response:", response_xml, True) self.log_with_header("Standard out", StringUtils.concat(resp.stdout)) self.log_with_header("Standard error", StringUtils.concat(resp.stderr)) cmd_line[4] = '******' # Password is 5th element in array self.log_with_header("Executed command line", StringUtils.concat(cmd_line, " ")) sys.exit(1) return response_xml
def TEST(deployed): logger.error("======== TEST Diffs ===========") ci = repositoryService.read(deployed) for d in ci.deployeds: dtype = str(d.type) logger.error("dtype = " + dtype) if dtype == "file.DeployedFile" or dtype == "file.Folder": logger.error("Found a file object we can diff") if dtype == "file.DeployedFile": logger.error("Deal with files") targetPath = str(d.targetPath) logger.error("TargetPath = " + targetPath) fileName = str(d.name) logger.error("FileName = " + fileName) logger.error("%s/%s " % (targetPath, fileName)) remote_path = "%s/%s" % (targetPath, fileName) else: logger.error("Deal with directories") targetPath = repositoryService.read(d).targetPath print "%s/ " % (targetPath) remote_path = targetPath # End if logger.error("Connect to overthere host @ " + str(d.container)) with OverthereHostSession(d.container) as session: diff_lines = remote_diff(session, fileName, remote_path) logger.error("+--------------------------------------------") logger.error("| %s on %s" % (d.name, d.container)) logger.error("+--------------------------------------------") if len(diff_lines) > 0: print "%s" % (diff_lines) logger.error(StringUtils.concat(diff_lines)) print StringUtils.concat(diff_lines) # End if logger.error("+--------------------------------------------")
# # Copyright 2021 XEBIALABS # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # from overtherepy import OverthereHostSession session = OverthereHostSession(container.ansibleController.host) target = session.remote_file(target_path) print('Delete {0}'.format(target)) target.delete()
import sys import traceback from com.xebialabs.overthere import OperatingSystemFamily from overtherepy import LocalConnectionOptions, OverthereHost, OverthereHostSession machine_name = thisCi.machineName if machine_name is None: raise ValueError("the 'Machine Name' property is empty") print "Start docker '{0}' machine ".format(machine_name) command_line = "docker-machine start {0}".format(machine_name) localOpts = LocalConnectionOptions(os=OperatingSystemFamily.UNIX) host = OverthereHost(localOpts) session = OverthereHostSession(host) print "Executing '{0}'....".format(command_line) try: response = session.execute(command_line, check_success=False) for s in response.stdout: print s for s in response.stderr: print s except: tb = traceback.format_exc() print "Error" print tb finally: session.close_conn()
def get_value_context(name): key = "wait_replicates_{0}".format(name) return context.getAttribute(key) result = "" stack_status = "" command_line = "{0} cloudformation describe-stacks --stack-name {1} --region {2}".format( 'aws', stack_name, deployed.container.region) if get_value_context(stack_name) == None: print command_line session = OverthereHostSession(target_host) while True: try: response = session.execute(command_line, check_success=False) rc = response.rc if rc != 0: print "Non zero Exit Code {0}".format(rc) raise Exception("Non zero Exit Code {0}".format(rc)) else: data = json.loads(" ".join(response.stdout)) if get_value_context(stack_name) == None: stack_id = data['Stacks'][0]['StackId'] print stack_id new_stack_status = data['Stacks'][0]['StackStatus'] if new_stack_status == stack_status:
except: return -1 attempts = 10 deployment_name = podname if deployed.namespace is not None: command_line = "{2} get deployment {0} --namespace={1} -o=json".format( deployment_name, deployed.namespace, deployed.container.command) else: command_line = "{2} get deployment {0} -o=json".format( deployment_name, deployed.namespace, deployed.container.command) print command_line try: session = OverthereHostSession(deployed.container.host) response = session.execute(command_line) data = json.loads(" ".join(response.stdout)) condition = data['status']['conditions'][0] print "Status {status} {reason}: {message}".format(**condition) availableReplicas = get_available_replicas(data) print "availableReplicas {0}/{1}".format(availableReplicas, deployed.replicas) if condition['status'] == "True": print "Status Ok" elif availableReplicas == deployed.replicas: print "DONE replicas" else: inc_context()
response = CommandResponse(rc=rc, stdout=capture_so_handler.outputLines, stderr=capture_se_handler.outputLines) if response.rc != 0: session.logger.error(StringUtils.concat(response.stdout)) session.logger.error(StringUtils.concat(response.stderr)) session.logger.error("Exit code {0}".format(response.rc)) sys.exit(response.rc) return response ansible_controler = repositoryService.read(ansible_controller_id) remote_session = OverthereHostSession(ansibleController.host) remote_yaml_file = remote_session.remote_file(yaml_file) print("remote_yaml_file {0}".format(remote_yaml_file)) operating_system = System.getProperty("os.name").lower() print("running " + operating_system + " OS on localhost") if operating_system.startswith("win"): os = OperatingSystemFamily.WINDOWS else: os = OperatingSystemFamily.UNIX local_opts = LocalConnectionOptions(os=os) local_host = OverthereHost(local_opts) local_session = OverthereHostSession(local_host, stream_command_output=True) local_yaml_file = local_session.work_dir_file(remote_yaml_file.getName())
def setUp(self): self._linuxhost = OverthereHost( LocalConnectionOptions(os=OperatingSystemFamily.UNIX)) self._session = OverthereHostSession(self._linuxhost) self._session.logger = OverthereSessionLogger(capture=True)
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS # FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS. # from overtherepy import LocalConnectionOptions, OverthereHost, OverthereHostSession from com.xebialabs.overthere import OperatingSystemFamily import sys import re def to_map(stdout): data = {} for s in response.stdout: if 'export' in s: info = s.replace('export ', ' ').replace('"', ' ').strip().split('=') data[str(info[0])] = str(info[1]) return data session = OverthereHostSession(target.container.host) response = session.execute("docker-machine env %s " % target.machineName) data = to_map(response.stdout) ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', data['DOCKER_HOST']) if len(ip) == 1: data['address'] = ip[0] print "IP Address is %s " % data['address'] print data session.close_conn() context.setAttribute(context_key, data)