douhengdao4499 2016-11-10 19:34
浏览 45
已采纳

关联phing中的列表属性

I have an automated deployment system working using Phing. We are adding a second server and I need to modify the deployment target in order to handle this. My issue is that the paths to deploy to between the two servers are not the same, so I'm not sure how to associate the current deploy server with the path I need to deploy to.

In my main phing.xml file, I set up properties:

<property name="deploy.servers.production"    value="server1.com,server2.com" />
<property name="deploy.path.production"       value="/path/on/server1,/path/on/server2" />

During the build, I just have a foreach element that iterates over the different servers and calls an upload task setting them as a property:

<foreach list="${deploy.servers.production}" param="deploy.server" target="deploy.deliver" />

The problem I have is that I can't find a way to associate the path that I need to deliver to with the server that I'm currently deploying to. What I would like to avoid is having to have separate properties for each server, something like this:

<property name="deploy.servers.production1"    value="server1.com" />
<property name="deploy.servers.production2"    value="server2.com" />
<property name="deploy.path.production1"       value="/path/on/server1" />
<property name="deploy.path.production2"       value="/path/on/server2" />

Is there a way to have two separate list properties in phing that correlate with one another?

  • 写回答

1条回答 默认 最新

  • doudihuang7642 2016-11-11 19:38
    关注

    I wound up having to write two custom phing tasks for this. One of them was basically just an adding of feature to the foreach task, which I added as a gist on github here. The other one is a much simpler task that is used to retrieve an item in a delimited list property by index, which I also added as a gist here. Importing these tasks works like this:

    <includepath    classpath="${base.dir}${ds}Tasks${ds}" />
    <taskdef name="foreachkey"      classname="ForeachKeyTask" />
    <taskdef name="listitembykey"   classname="ListItemByKeyTask" />
    
    <property name="deploy.targets.production"          value="server1,server2,server3" />
    <property name="deploy.users.production"            value="user1,user2,user3" />
    <property name="deploy.privateKeys.production"      value=".ssh/key1_rsa,.ssh/key2_rsa,.ssh/key3_rsa" />
    <property name="deploy.publicKeys.production"       value=".ssh/key1_rsa.pub,.ssh/key2_rsa.pub,.ssh/key3_rsa.pub" />
    <property name="deploy.paths.production"            value="/path/on/server1,/path/on/server2,/path/on/server3" />
    

    The list of servers is iterated over similarly to using the <foreach> task, with the minor addition of adding a property name to keep track of the current index:

    <foreachkey list="deploy.targets" key="currentIndex" param="deploy.server" target="install" />
    

    The callee then has two properties that it can then access, deploy.server to get the current value in the list of target servers, and currentIndex that references the current index in the list. Using this, I can now get the other associated values:

    <-- The properties above get aliased to these properties when the target environment is selected -->
    <listitembykey key="currentIndex" property="deploy.users" returnval="user" />
    <listitembykey key="currentIndex" property="deploy.privateKeys" returnval="privateKey" />
    <listitembykey key="currentIndex" property="deploy.publicKeys" returnval="publicKey" />
    <listitembykey key="currentIndex" property="deploy.paths" returnval="path" />
    
    <!-- Properties now available that hold the current credential information related to 
         the current deployment target:
            user, privateKey, publicKey, passphrase, path -->
    
    <!-- And they can be used to execute SSH commands... -->
    
    <netssh username="${user}"
        pubkeyfile="${publicKey}"
        privkeyfile="${privateKey}"
        host="${deploy.server}"
        sshlib="${deploy.sshlib}"
        command="mkdir -p ${path}${ds}${deploy.releasedir}${ds}${deploy.ts}" />
    

    The downside to this approach is that you have to keep the associated data in the same indices. Even if the username (or keys, or paths) between server1 and server2 is the same, you still have to list it twice (in my case they are different).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效