john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file replace string

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python
# 2013-02-11 johnpfeiffer


from sys import argv, exit
import base64
import esutil       # utility for parsing command line parameters
import getopt
import os
import shutil
import tempfile


class Activator:

    app_properties_dictionary = dict()
    ssl_certificate_dictionary = dict()

    def __init__(self):
        try:
            keys=["help", "ibmusername=", "ibmpassword=", "ibmappname=", "ibmappkey=", "sgwoid=", "sgwhash=", "domainname=", "sslcertificate=", "sslkey=", "sslintermediate="]

            argmap = esutil.parse_cmd_args( argv, "hv", keys )

        except getopt.GetoptError, err:
            print str( err )
            self._usage()
            exit(2)

        if argmap.has_key( "-v" ):
            verbose = True

        elif argmap.has_key( "-h" ) or argmap.has_key( "--help" ):
            self._usage()
            exit()


        self.app_properties_dictionary[ 'storagegateway.storageGatewayOid=' ] = argmap.get( "--sgwoid" )
        self.app_properties_dictionary[ 'storagegateway.storageGatewaySignedHash=' ] = argmap.get( "--sgwhash" )
        download_link_list = [ 'https://' , argmap.get( "--domainname" ) , '/storagegateway/download' ]
        self.app_properties_dictionary[ 'storagegateway.downloadWeblinkPrefixURL=' ] = ''.join( download_link_list )
        upload_link_list = [ 'https://' , argmap.get( "--domainname" ) , '/storagegateway/upload' ]
        self.app_properties_dictionary[ 'storagegateway.directUploadPrefixUrl=' ] = ''.join( upload_link_list )
        self.app_properties_dictionary[ 'storagegateway.nirvanixrest.username=' ] = argmap.get( "--ibmusername" )
        self.app_properties_dictionary[ 'storagegateway.nirvanixrest.password=' ] = argmap.get( "--ibmpassword" )
        self.app_properties_dictionary[ 'storagegateway.nirvanixrest.appName=' ] = argmap.get( "--ibmappname" )
        self.app_properties_dictionary[ 'storagegateway.nirvanixrest.appKey=' ] = argmap.get( "--ibmappkey" )

        self.ssl_certificate_dictionary[ 'storagegateway.ssl.certificate=' ] = argmap.get( "--sslcertificate" )
        self.ssl_certificate_dictionary[ 'storagegateway.ssl.key=' ] = argmap.get( "--sslkey" )
        self.ssl_certificate_dictionary[ 'storagegateway.ssl.intermediate=' ] = argmap.get( "--sslintermediate" )

    def _usage(self):
        print  """\nUSAGE:\n AS/ibm-sgw/""" + os.path.basename(argv[0]) +  """    --ibmusername ibmuser  --ibmpassword ibmpass  --ibmappname ibmappname  --ibmappkey ibmappkey  --sgwoid sgwoid  --sgwhash sgwhash  --domainname domain.name  --sslcertificate sslcertificate  --sslkey sslkey  --sslintermediate sslintermediate"""



#    @staticmethod
#    def config_log( message ):
#        mylog = open( '/tmp/activate.log' , 'a' )
#        mylog.write( '%s \n' % message )
#        mylog.close()


    @staticmethod
    def get_property( identifier , filename ):
        """ get the value of the last occurrence of a line starting with identifier """
        property = None
        source = open( filename )
        for line in source:
            line = line.strip()
            if line.startswith( identifier ):
                oid_line_list = line.split( '=' )
                if len( oid_line_list ) > 1:
                    property = oid_line_list[ 1 ]
        return property


    @staticmethod
    def write_to_file( line_list , filename ):
        target = open( filename , 'w' )
        for line in line_list:
            target.write( line  )
        target.close()


    @staticmethod
    def set_property( identifier , new_value , filename ):
        source = open( filename )
        line_list = list()
        for line in source:
            line = line.strip()
            if not line:
                continue    # skip empty lines
            else:
                if line.startswith( identifier ):
                    line_list.append( '%s%s\n' % ( identifier , new_value ) )
                else:
                   line_list.append( '%s\n' % line )
        source.close()

        Activator.write_to_file( line_list , filename )


    def action(self):
        filename = '/var/lib/tomcat6/webapps/storagegateway/WEB-INF/app.properties'
        filename_path = os.path.join( filename )
        if not os.path.exists( filename_path ):
            print "ERROR: could not locate target configuration file " + filename
            exit( 2 )
        else:
#            Activator.config_log( str( self.app_properties_dictionary ) )
#            is_writable = os.access( filename , os.W_OK )
#            Activator.config_log( '%s is writable = %s ' % ( filename , is_writable ) )

            for key , value in self.app_properties_dictionary.iteritems():
                Activator.set_property( key , value , filename )

            ssl_certificate_list = [  self.ssl_certificate_dictionary[ 'storagegateway.ssl.certificate=' ] ]
            Activator.write_to_file( ssl_certificate_list , '/var/lib/ssl/cert.crt' )

            ssl_key_list = [  self.ssl_certificate_dictionary[ 'storagegateway.ssl.key=' ] ]
            Activator.write_to_file( ssl_key_list , '/var/lib/ssl/cert.key' )

            ssl_intermediate_list = [  self.ssl_certificate_dictionary[ 'storagegateway.ssl.intermediate=' ] ]
            Activator.write_to_file( ssl_intermediate_list , '/var/lib/ssl/intermediate.crt' )

         return 0



def main():
    activator = Activator()
    activator.action()


if __name__ == "__main__":
    main()

  • « Centos5 yum install rpm reinstall yum security updates only
  • Kvm create virt clone install centos6 edit ram »

Published

Feb 12, 2013

Category

python

~477 words

Tags

  • file 92
  • python 180
  • replace 7
  • string 18