示例#1
0
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
class UnixBuildServer(object):
    def __init__(self, server):
        self.sshOpts = SshConnectionOptions(server['host'], username=server['username'],password=server['password'])
        self.host = OverthereHost(self.sshOpts)
        self.session = OverthereHostSession(self.host)

        self.base_working_directory = server['BaseWorkingDirectory']
        self.working_directory = self.base_working_directory

        self.create_working_directory(self.base_working_directory)
        workDir = self.session.remote_file(self.base_working_directory)
        self.session.get_conn().setWorkingDirectory(workDir)

    @staticmethod
    def createServer(server):

        return UnixBuildServer(server)


    def set_working_directory(self, relative_directory_path):
        self.working_directory = "%s/%s" % (self.base_working_directory, relative_directory_path)
        self.create_working_directory(self.working_directory)
        workDir = self.session.remote_file(self.working_directory)
        self.session.get_conn().setWorkingDirectory(workDir)

    # main interface


    # session related methods

    def close_session(selfs):
        self.session.close()

    def create_working_directory(self, dir_full_path):
        response = self.session.execute("/bin/mkdir -p %s" % (dir_full_path))

    # File Handeling
    def read_file_in_workspace(self, file_name):
        print "not yet implemented"

    def write_file_in_workspace(self, file_name, content):
        print "not yet implemented"

    def create_directory(self, directory_name):
        self.execute_command(["/bin/mkdir -p %s" % (directory_name)])


    # command Execution
    def filename_generator(self, size=9, chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))

    def get_tmp_script_filename(self):
        return "%s.sh" % self.filename_generator()

    def execute_command(self, command, environment_variables=None):
        # get a BashScriptBuilder object
        command_object = BashScriptBuilder()

        # copy in the environment variables
        if environment_variables is not None:
            for key, value in environment_variables.items():
                command_object.set_env_var(key, value)

        # add the rest of the possible multiline command
        command_object.add_lines(command)

        executable_command = command_object.build()

        print "executing command: %s " % (executable_command)

        tmp_script_filename = self.get_tmp_script_filename()

        self.session.upload_text_content_to_work_dir(command_object.build(), tmp_script_filename, executable=True)
    #
        response = self.session.execute("%s/%s" % (self.working_directory, tmp_script_filename), check_success=False, suppress_streaming_output=False)

        if response.rc != 0:
            print response.stderr
            print response.stdout
            print "unable to execute command %s" % (command)
            raise Exception('unable to execute command')
        else:
            print "Response:", str(response.stdout)
            return response
示例#3
0
class DarBuildServer(object):
    def __init__(self, server):
        self.sshOpts = SshConnectionOptions(server['host'], username=server['username'],password=server['password'])
        self.host = OverthereHost(self.sshOpts)
        self.session = OverthereHostSession(self.host)
        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)

    def __del__(self):
        self.destroy_work_directory()
        self.session.close_conn() 


    @staticmethod
  
    def createServer(server): 
        return DarBuildServer(server)
    #    
  
    def init_dar(self, appName, appVersion):
        workspace_root = self.create_dar_directory(appName, appVersion) 
        manifest_filename = "%s/deployit-manifest.xml" % workspace_root
        self.write_dar_manifest(appName, appVersion, workspace_root) 

    def import_ear(self, appName, appVersion, deployable, url):   
        self.download_file_into_dar(appName, appVersion,deployable, str(url)) 
  
    def create_dar_directory(self, appName, appVersion):
        dirName = "%s/%s" % (appVersion, appName)

        # check if the directory exists .. if it does we should go do something else.
        # might want to do a better locking mechanism
        if self.dir_exists(dirName): 
          print "unable to create dar directory: %s/%s/%s" % (self.workingDirectory, appVersion, appName)  
          raise Exception('unable to create Dar Package Directory')
        else: 
          self.create_directory(dirName)

        return dirName

        
    def download_file_into_dar(self, appName, appVersion, deployable, url):
        #filename = url.split('/')[-1]
        filename = os.path.basename(url)
        outfile = "%s/%s/%s/%s" % (appVersion, appName,deployable, filename) 
        dirName =  "%s/%s/%s" % (appVersion, appName,deployable)
        if self.dir_exists(dirName): 
          print "output dir already exists: %s" % (dirName)
        else: 
          self.create_directory(dirName)

        self.execute_command("/usr/bin/curl -L -o %s %s" % (outfile, url))

    def read_manifest(self, appName, appVersion):
        file_name = "%s/%s/%s/deployit-manifest.xml" % (self.workingDirectory, appVersion, appName)
        return self.session.read_file(file_name, encoding="UTF-8")

    
    def write_manifest(self, appName, appVersion, content):
        file_name = "%s/%s/deployit-manifest.xml" % (appVersion, appName)
        self.write_to_file(file_name, content) 
         
    def create_directory(self, dirName):
        self.execute_command("/bin/mkdir -p %s" % (dirName)) 
	 
    def create_file(self, fileName, content=None):
        if content:
         self.write_to_file(fileName, str(content))
        else:
         self.execute_command("/bin/touch %s" % (fileName)) 
 

    def write_to_file(self, fileName, content):
        remoteFile = self.session.remote_file("%s/%s" % (self.workingDirectory, fileName)) 
        self.session.copy_text_to_file(str(content), remoteFile)
          
    def dir_exists(self, dirName): 
        print dirName
        command = "[ -d %s ]" % (dirName)
        response = self.session.execute(command, check_success=False, suppress_streaming_output=False)
        
        if response.rc == 0 :
          return True
        else: 
          return False


    def execute_command(self, command):
        print "executing command: %s " % (command)
        response = self.session.execute(command, check_success=False, suppress_streaming_output=False)


        if response.rc != 0:
          print response.stderr
          print "unable to execute command %s" % (command)
          raise Exception('unable to execute command ')
        else:
          print "Response:", str(response.stdout)

       # self.switch_working_directory()
        return response

    
    def write_dar_manifest(self, appName, appVersion, workspace_root):
       filename = "./%s/deployit-manifest.xml" % (workspace_root)
       file_content = self.basic_dar_manifest_template().substitute(appVersion=appVersion, appName=appName)
       self.create_file(filename, file_content)      

    def basic_dar_manifest_template(self):
       xml_template  = '<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n'
       xml_template += ' <udm.DeploymentPackage version=\"$appVersion\" application=\"$appName\"> \n'
       xml_template += '   <orchestrator /> \n'
       xml_template += '   <deployables/> \n' 
       xml_template += ' </udm.DeploymentPackage> \n'
       return Template(xml_template)

    def package_dar(self, appName, appVersion):
        command = "if [ -f %s_%s.dar ] \n"
        command += " then rm -rf %s_%s.dar \n"
        command += "fi \n"
        command += "cd %s/%s \n" % (appVersion, appName)
        command += "/usr/bin/zip -r %s/%s_%s.dar *" % (self.workingDirectory, appName, appVersion.replace('.','_'))
        self.execute_multiline_command(command)

    def write_exec_script(self, commands, script):
        self.session.upload_text_content_to_work_dir(self, commands, script, executable=False)

    def execute_multiline_command(self, command):

        tmp_filename = self.filename_generator()
        self.write_to_file(tmp_filename, command)
        self.execute_command("chmod +x %s" % (tmp_filename))
        self.execute_command("./%s" % (tmp_filename))


    def filename_generator(self, size=9, chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))

    def upload_dar_package(self, appName, appVersion, xldeployServer):
        dar_file_name = "%s_%s.dar" % (appName, appVersion.replace('.','_'))
        command = "/usr/bin/curl -u %s:%s -X POST -H \"content-type:multipart/form-data\" %s/package/upload/%s -F fileData=@./%s " % (xldeployServer['username'], xldeployServer['password'], xldeployServer['url'], dar_file_name, dar_file_name)
        print command
        self.execute_command(command)
class ConfigRTCServer(object):
    def __init__(self, server, dirname="tmp", work_directory=None):

        # if 'key_file' in server:
        #   self.sshOpts = SshConnectionOptions(server['host'], username=server['username'],privateKeyFile=server['key_file'])
        # else:
        #   self.sshOpts = SshConnectionOptions(server['host'], username=server['username'],password=server['password'])

        self.sshOpts = SshConnectionOptions(server['host'], username=server['username'],privateKeyFile=server['key_file'],password=server['password'] )

        self.host = OverthereHost(self.sshOpts)
        self.session = OverthereHostSession(self.host)
        self.WorkDirBase = server['filesystemLocation']
        self.session.execute("/bin/mkdir -p %s" % (self.WorkDirBase))
        workDir = self.session.remote_file(self.WorkDirBase)
        self.session.get_conn().setWorkingDirectory(workDir)

        if work_directory is None:
          self.WorkDirTimeStamp = int(time.time())
          self.WorkDirName = dirname
          self.workDirectory = None
        else:
          self.workDirectory = work_directory

        self.RtcClient = server['pathToClientSoftware']




    @staticmethod
    def createServer(server, dirname, work_directory=None): 
        return ConfigRTCServer(server, dirname, work_directory)


    def get_work_directory(self):
        return self.workDirectory
       
    def get_file_contents(self, file_name):
        response = self.session.read_file("%s/%s" % (self.getWorkDirectory(), file_name))
        return response
   
    def write_string_as_file(self, file_name, content):
        remote_file = self.session.remote_file("%s/%s" % (self.getWorkDirectory(), file_name))
        response = self.session.copy_text_to_file(str(content), remote_file)
	 
    def execute_lscm_command(self, command, run_in_workdirectory=False):
        #logger.info(command)
        command = self.sub_placeholders(command)
        lscm_command="%s %s" % (self.RtcClient, command)

        if run_in_workdirectory is False:
          response = self.execute_command(lscm_command)
        else:
          response = self.execute_command_in_workDirectory(lscm_command)
        return response

    def execute_command_in_workDirectory(self, command):
    #    #logger.info("switching overthere to workdirectory")
    #    workDir = self.session.remote_file(self.get_work_directory())
    #    self.session.get_conn().setWorkingDirectory(workDir)
    #    #logger.info("switched to workDirectory")
        command = "cd %s;%s" % (self.get_work_directory(), command) 
        return self.execute_command(command)
 
    #def execute_command(self, command):
    #    #logger.info("executing command: %s " % (command))
    #    response = self.session.execute(command, check_success=False, suppress_streaming_output=False)
    #    if response.rc != 0:
    #      #logger.info(response.stderr)
    #      #logger.info("unable to execute command %s" % (command))
    #      raise Exception('unable to execute command ')
    #    else:
    #      #logger.info("Response", str(response.stdout))
    #      return response

    def execute_command(self, command, retain_scripts=True):
        command_object = BashScriptBuilder()
        for command_line in command.split(';'):
	  command_object.add_line(command_line)
       
        executable_command = command_object.build() 

        tmp_script_file_name = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(9))
        #logger.info("uploading script %s" % tmp_script_file_name) 

        script = self.session.upload_text_content_to_work_dir(executable_command, tmp_script_file_name, executable=True)
        
        response = self.session.execute(script.getPath(), check_success=False, suppress_streaming_output=False)

        #logger.info("stdErr for command yielded: " + str(response.stderr))
        #logger.info("stdOut for command yielded: " + str(response.stdout))
        #logger.info("the returncode for the command was:" + str(response.rc)) 
        if response.rc != 0:
          #logger.info("unable to execute command %s" % (command))
          raise Exception('unable to execute command ')
        else:
          #logger.info("Response", str(response.stdout))
          return response

                 
        
    def getWorkDirectory(self):
        if self.workDirectory is None:
          self.session.execute("/bin/mkdir -p %s/%s/%s" % (self.WorkDirBase,self.WorkDirTimeStamp,self.WorkDirName))
          self.workDirectory =  "%s/%s/%s" % (self.WorkDirBase,self.WorkDirTimeStamp,self.WorkDirName)
        return self.workDirectory

    def destroy_work_directory(self):
        if self.workDirectory is not None:
          self.execute_command("/bin/rm -rf %s/%s" % (self.WorkDirBase, self.WorkDirName))
    
    def sub_placeholders(self, input):
        #logger.info(self.workDirectory)
        #logger.info(self.getWorkDirectory())
	output = input.replace('<work_dir>',  self.getWorkDirectory())
        return output
class DarBuildServer(object):
    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 __del__(self):
        self.destroy_work_directory()
        self.session.close_conn()

    def closeConnection(self):
        print "closing ssh connection"
        self.session.close_conn()

    @staticmethod
    def createServer(server):
        return DarBuildServer(server)

    #

    def init_dar(self, appName, appVersion):
        workspace_root = self.create_dar_directory(appName, appVersion)
        manifest_filename = "%s/deployit-manifest.xml" % workspace_root
        self.write_dar_manifest(appName, appVersion, workspace_root)

    def delete_workspace(self, appName, appVersion):
        dirName = "%s/%s" % (appVersion, appName)

        response = self.execute_command("/bin/rm -rf %s" % (dirName))
        response = self.execute_command("/bin/rm -rf %s" % (appVersion))

    def import_ear(self, appName, appVersion, deployable, url):
        self.download_file_into_dar(appName, appVersion, deployable, str(url))

    def create_dar_directory(self, appName, appVersion):
        dirName = "%s/%s" % (appVersion, appName)

        # check if the directory exists .. if it does we should go do something else.
        # might want to do a better locking mechanism
        if self.dir_exists(dirName):
            print "unable to create dar directory: %s/%s/%s" % (
                self.workingDirectory, appVersion, appName)
            raise Exception('unable to create Dar Package Directory')
        else:
            self.create_directory(dirName)

        return dirName

    def download_file_into_dar(self, appName, appVersion, deployable, url):
        #filename = url.split('/')[-1]
        filename = os.path.basename(url)
        outfile = "%s/%s/%s/%s" % (appVersion, appName, deployable, filename)
        dirName = "%s/%s/%s" % (appVersion, appName, deployable)
        if self.dir_exists(dirName):
            print "output dir already exists: %s" % (dirName)
        else:
            self.create_directory(dirName)

        self.execute_command(
            "/usr/bin/curl --retry 5 --retry-delay 2 -k -L -o %s %s" %
            (outfile, url))

    def read_manifest(self, appName, appVersion):
        file_name = "%s/%s/%s/deployit-manifest.xml" % (self.workingDirectory,
                                                        appVersion, appName)
        return self.session.read_file(file_name, encoding="UTF-8")

    def write_manifest(self, appName, appVersion, content):

        file_name = "%s/%s/deployit-manifest.xml" % (appVersion, appName)
        self.write_to_file(file_name, content)

    def create_directory(self, dirName):
        self.execute_command("/bin/mkdir -p %s" % (dirName))

    def create_file(self, fileName, content=None):
        if content:
            self.write_to_file(fileName, str(content))
        else:
            self.execute_command("/bin/touch %s" % (fileName))

    def write_to_file(self, fileName, content):
        remoteFile = self.session.remote_file(
            "%s/%s" % (self.workingDirectory, fileName))
        self.session.copy_text_to_file(str(content), remoteFile)

    def dir_exists(self, dirName):
        command = "[ -d %s ]" % (dirName)
        response = self.session.execute(command,
                                        check_success=False,
                                        suppress_streaming_output=False)

        if response.rc == 0:
            return True
        else:
            return False

    def execute_command(self, command):
        print "executing command: %s " % (command)
        response = self.session.execute(command,
                                        check_success=False,
                                        suppress_streaming_output=False)

        if response.rc != 0:
            print response.stderr
            print response.stdout
            print "unable to execute command %s" % (command)
            raise Exception('unable to execute command ')
        else:
            print "Response:", str(response.stdout)
            print "Errors:", str(response.stderr)

    # self.switch_working_directory()
        return response

    def write_dar_manifest(self, appName, appVersion, workspace_root):
        filename = "./%s/deployit-manifest.xml" % (workspace_root)
        file_content = self.basic_dar_manifest_template().substitute(
            appVersion=appVersion, appName=appName)
        self.create_file(filename, file_content)

    def basic_dar_manifest_template(self):
        xml_template = '<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n'
        xml_template += ' <udm.DeploymentPackage version=\"$appVersion\" application=\"$appName\"> \n'
        xml_template += '   <orchestrator /> \n'
        xml_template += '   <deployables/> \n'
        xml_template += ' </udm.DeploymentPackage> \n'
        return Template(xml_template)

    def package_dar(self, appName, appVersion):
        command = "set -x \n"
        command += "if [ -f %s_%s.dar ] \n"
        command += " then rm -rf %s_%s.dar \n"
        command += "fi \n"
        command += "cd %s/%s \n" % (appVersion, appName)
        command += "/usr/bin/zip -r %s/%s_%s.dar *" % (
            self.workingDirectory, appName.replace(
                '/', '_'), appVersion.replace('.', '_'))
        self.execute_multiline_command(command)

    def remove_dar(self, appName, appVersion):
        command = "set -x \n"
        command += "cd %s \n" % self.workingDirectory
        command += "if [ -f %s_%s.dar ] \n" % (appName.replace(
            '/', '_'), appVersion.replace('.', '_'))
        command += " then rm -rf %s_%s.dar \n" % (appName.replace(
            '/', '_'), appVersion.replace('.', '_'))
        command += " fi \n"
        self.execute_multiline_command(command)

    def write_exec_script(self, commands, script):
        self.session.upload_text_content_to_work_dir(self,
                                                     commands,
                                                     script,
                                                     executable=False)

    def execute_multiline_command(self, command):

        tmp_filename = self.filename_generator()
        self.write_to_file(tmp_filename, command)
        self.execute_command("chmod +x %s" % (tmp_filename))
        self.execute_command("./%s" % (tmp_filename))

    def filename_generator(self,
                           size=9,
                           chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))

    def upload_dar_package(self, appName, appVersion, xldeployServer):
        dar_file_name = "%s_%s.dar" % (appName.replace(
            '/', '_'), appVersion.replace('.', '_'))
        command = "/usr/bin/curl -k -u %s:%s -X POST -H \"content-type:multipart/form-data\" %s/package/upload/%s -F fileData=@./%s " % (
            xldeployServer['username'], xldeployServer['password'],
            xldeployServer['url'], dar_file_name, dar_file_name)
        self.execute_command(command)