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).

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

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法