cloudify文件管理和操作

使用正则表达式替换文件中的字符串

下面的例子被用在tomcat服务配方安装处理程序中

/* Groovy Usage:

     The following example replaces:

      the 1st occurrence of "James" in data.txt with "user1",

      the 2nd occurrence of "James" in data.txt with "user2",

      the 3rd occurrence of "James" in data.txt with "user3",

      ..

      the 9th occurrence of "James" in data.txt with "user9".

*/

 

def myFile = new File("data.txt")

def fileText = myFile.text

for ( index in 1..9 ) {

       fileText = (fileText =~ /James/).replaceFirst("user${index}")

}

myFile.write(fileText)

 

/* Usage in Cloudify:

     The following code snippet replaces all the occurrences of 8080

     with the current actual http port and all the occurrences of 8009

     with the current actual AJP port.

*/

 

def config = new ConfigSlurper().parse(new File("tomcat-service.properties").toURL())

def context = ServiceContextFactory.getServiceContext()

def home = "${context.serviceDirectory}/${config.name}"

portIncrement = 0

def serverXmlFile = new File("${home}/conf/server.xml")

def serverXmlText = serverXmlFile.text  

portReplacementStr = "port=\"${config.port + portIncrement}\""

ajpPortReplacementStr = "port=\"${config.ajpPort + portIncrement}\""

serverXmlText = serverXmlText.replace("port=\"8080\"", portReplacementStr)

serverXmlText = serverXmlText.replace("port=\"8009\"", ajpPortReplacementStr)

serverXmlFile.write(serverXmlText)

查找一个文件夹中所有groovy文件中的注释

/* Groovy Usage:

     The following code snippet finds and prints all the lines

     in which there's a groovy/java comment:

*/

def myRootFolder="c:\\scripts"

def p = ~/.*\.groovy/

new File( "${myRootFolder}" ).eachFileMatch(p) { f ->

    // imports list

    def allLines = []

    f.eachLine { ln ->

      if ( ln =~ '^.*\\/+\\*+.*$' ) {

        allLines << "${f} : ${ln}"

      }

      if ( ln =~ '^.*\\*+\\/+.*$' ) {

        allLines << "${f} : ${ln}"

      }      

    }

    // print all the lines

    if ( ! allLines.empty ) {

        allLines.each{ println "   $it" }

    }

}

Groovy不使用Ant下载文件

/* Groovy Usage:

     The following code snippet downloads a file

*/

 

def download(fileUrl)

{

    def file = new FileOutputStream(fileUrl.tokenize("/")[-1])

    def out = new BufferedOutputStream(file)

    out << new URL(fileUrl).openStream()

    out.close()

}

 

/* Replace the fileUrl value with a full URL path to a file */

def fileUrl="http://.../myFile"

 

download(fileUrl)

Groovy下载文件

下面的例子被用在apacheLB服务配方安装处理程序中

/* Groovy Usage:

     This code snippet downloads the file from http://www.mysite.com/myFile.zip to the /tmp folder.

     If myFile.zip already exists in /tmp, this file will NOT be downloaded.

*/

builder = new AntBuilder()

builder.get(src:"http://www.mysite.com/myFile.zip", dest:"/tmp", skipexisting:true)

 

/* Usage in Cloudify:

     The following code snippet reads the properties file first.

     Then, the file is downloaded from the path which is specified in the property named downloadUrl.

     The file will be downloaded to a location which is calculated

     and stored in a variable named downloadFile.

     If the file already exists in the destination folder, it will be overridden.

*/

 

config = new ConfigSlurper().parse(new File("apacheLB-service.properties").toURL())

downloadFile="${config.downloadFolder}/{$config.zipName}"

builder = new AntBuilder()

builder.get(src:"${config.downloadUrl}", dest:"${downloadFile}", skipexisting:false)

Groovy压缩/解压文件

下面的例子被用在mongod服务配方安装后处理程序中

Groovy复制/移动文件

下面的例子被用在jboss服务配方安装处理程序中

/* Groovy Usage:

     This code snippet copies myFile.txt from the /tmp/ folder

     to the /usr/bin folder and changes its name to myNewFile.ini.

     If myNewFile.ini already exists in /usr/bin,

     then this file will NOT be copied.

     After that, myNewFile.ini will be moved to the /myApp folder.

*/

builder = new AntBuilder()

builder.sequential {

  copy(tofile: "/usr/bin/myNewFile.ini", file:"/tmp/myFile.txt", overwrite:false)

  move(file:"/usr/bin/myNewFile.ini", tofile:"/myApp/myNewFile.ini")

}

 

/* Usage in Cloudify:

     The following code snippet copies standalone.xml from the service folder to the full path which is specified in jboss-service.properties.

     If standalone.xml already exists in the destination folder, it will be overridden.

*/

 

jbossConfig = new ConfigSlurper().parse(new File("jboss-service.properties").toURL())

serviceContext = ServiceContextFactory.getServiceContext()

builder = new AntBuilder()

builder.copy(tofile: "${jbossConfig.standaloneXmlFile}", file:"${serviceContext.serviceDirectory}/standalone.xml", overwrite:true)

view rawcopyMove.groovyThis Gist brought to you by GitHub.

Groovy改变文件夹权限

下面的例子被用在apacheLB服务配方安装处理程序中

/* Groovy Usage:

     This code snippet changes the permissions

     of all the files inside the /usr/bin/myapp/data folder.

     In this case, all the files in /usr/bin/myapp/data

     will be writable after invoking this command.

*/

 

builder = new AntBuilder()

builder.chmod(dir:"/usr/bin/myapp/data", perm:'+w', includes:"*")

 

/* Usage in Cloudify:

     The following code snippet changes the permissions

     of all the shell files in the service recipe folder.

     In this case, all these *.sh files will be executable.

     This code snippet also writes a (debug) message prior to invoking the chmod.

*/

 

context = ServiceContextFactory.getServiceContext()

builder = new AntBuilder()

builder.sequential {

  echo(message:"apacheLB_install.groovy: Chmodding +x ${context.serviceDirectory} ...")

  chmod(dir:"${context.serviceDirectory}", perm:"+x", includes:"*.sh")

}

Groovy执行系统命令和可执行脚本

下面的例子被用在tomcat服务配方启动处理程序中

/* Groovy Usage:

     This code snippet executes the myScript.sh script only on linux.

*/

 

builder = new AntBuilder()

builder.exec(executable:"myScript.sh", osfamily:"unix")

 

/* Usage in Cloudify:

     The following code snippet starts the tomcat on Windows

     and passes arguments and environments variables to it.

*/

 

config=new ConfigSlurper().parse(new File("tomcat-service.properties").toURL())

serviceContext = ServiceContextFactory.getServiceContext()

script= serviceContext.attributes.thisInstance["script"]

builder = new AntBuilder()

builder.sequential {

  exec(executable:"${script}.bat", osfamily:"windows") {

    env(key:"CATALINA_HOME", value: "${home}")

    env(key:"CATALINA_BASE", value: "${home}")

    env(key:"CATALINA_TMPDIR", value: "${home}/temp")

    env(key:"CATALINA_OPTS", value:"-Dcom.sun.management.jmxremote.port=${currJmxPort} -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false")

    env(key:"${config.dbHostVarName}", value: "${dbServiceHost}")

    env(key:"${config.dbPortVarName}", value: "${dbServicePort}")  

    arg(value:"run")

  }

}

 

你可能感兴趣的:(cloudify文件管理和操作)