【问题标题】:Correlating list properties in phing关联 phing 中的列表属性
【发布时间】:2017-03-24 22:54:20
【问题描述】:

我有一个使用 Phing 的自动部署系统。我们正在添加第二台服务器,我需要修改部署目标以处理此问题。我的问题是两个服务器之间的部署路径不一样,所以我不确定如何将当前部署服务器与我需要部署到的路径关联起来。

在我的主要phing.xml 文件中,我设置了属性:

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

在构建期间,我只有一个 foreach 元素,它遍历不同的服务器并调用上传任务,将它们设置为属性:

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

我遇到的问题是我找不到a way to associate 我需要通过当前部署到的服务器 传送到的路径。我想避免的是每个服务器都必须有单独的属性,如下所示:

<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" />

有没有办法让 phing 中的两个单独的列表属性相互关联?

【问题讨论】:

    标签: php foreach properties phing


    【解决方案1】:

    我最终不得不为此编写两个自定义 phing 任务。其中一个基本上只是为 foreach 任务添加了一个功能,我在 github here 上将其添加为要点。另一个是一个更简单的任务,用于按索引检索分隔列表属性中的项目,我也将其添加为要点here。导入这些任务的工作方式如下:

    <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" />
    

    服务器列表的迭代与使用&lt;foreach&gt; 任务类似,只是添加了一个属性名称以跟踪当前索引:

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

    然后,被调用者有两个可以访问的属性,deploy.server 获取目标服务器列表中的当前值,currentIndex 引用列表中的当前索引。使用它,我现在可以获得其他关联值:

    <-- 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}" />
    

    这种方法的缺点是您必须将相关数据保存在相同的索引中。即使 server1 和 server2 之间的用户名(或密钥或路径)相同,您仍然需要列出两次(在我的情况下它们是不同的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      相关资源
      最近更新 更多