Package.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Console
  17. * @subpackage Exception
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** @see Zend_Xml_Security */
  23. require_once 'Zend/Xml/Security.php';
  24. /**
  25. * @see Zend_Service_Console_Command
  26. */
  27. require_once 'Zend/Service/Console/Command.php';
  28. /**
  29. * Package commands
  30. *
  31. * @category Zend
  32. * @package Zend_Service_WindowsAzure_CommandLine
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. *
  36. * @command-handler package
  37. * @command-handler-description Windows Azure Package commands
  38. * @command-handler-header Windows Azure SDK for PHP
  39. * @command-handler-header Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  40. * @command-handler-footer
  41. * @command-handler-footer All commands support the --ConfigurationFile or -F parameter.
  42. * @command-handler-footer The parameter file is a simple INI file carrying one parameter
  43. * @command-handler-footer value per line. It accepts the same parameters as one can
  44. * @command-handler-footer use from the command line command.
  45. */
  46. class Zend_Service_WindowsAzure_CommandLine_Package
  47. extends Zend_Service_Console_Command
  48. {
  49. /**
  50. * Scaffolds a Windows Azure project structure which can be customized before packaging.
  51. *
  52. * @command-name Scaffold
  53. * @command-description Scaffolds a Windows Azure project structure which can be customized before packaging.
  54. *
  55. * @command-parameter-for $path Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to create the Windows Azure project structure.
  56. * @command-parameter-for $scaffolder Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile|Zend_Service_Console_Command_ParameterSource_Env --Scaffolder|-s Optional. The path to the scaffolder to use. Defaults to Scaffolders/DefaultScaffolder.phar
  57. */
  58. public function scaffoldCommand($path, $scaffolder, $argv)
  59. {
  60. // Default parameter value
  61. if ($scaffolder == '') {
  62. $scaffolder = dirname(__FILE__) . '/Scaffolders/DefaultScaffolder.phar';
  63. }
  64. $scaffolder = realpath($scaffolder);
  65. // Verify scaffolder
  66. if (!is_file($scaffolder)) {
  67. require_once 'Zend/Service/Console/Exception.php';
  68. throw new Zend_Service_Console_Exception('Could not locate the given scaffolder: ' . $scaffolder);
  69. }
  70. // Include scaffolder
  71. $archive = new Phar($scaffolder);
  72. include $scaffolder;
  73. if (!class_exists('Scaffolder')) {
  74. require_once 'Zend/Service/Console/Exception.php';
  75. throw new Zend_Service_Console_Exception('Could not locate a class named Scaffolder in the given scaffolder: ' . $scaffolder . '. Make sure the scaffolder package contains a file named index.php and contains a class named Scaffolder.');
  76. }
  77. // Cleanup $argv
  78. $options = array();
  79. foreach ($argv as $arg) {
  80. list($key, $value) = explode(':', $arg, 2);
  81. while (substr($key, 0, 1) == '-') {
  82. $key = substr($key, 1);
  83. }
  84. $options[$key] = $value;
  85. }
  86. // Run scaffolder
  87. $scaffolderInstance = new Scaffolder();
  88. $scaffolderInstance->invoke($archive, $path, $options);
  89. }
  90. /**
  91. * Packages a Windows Azure project structure.
  92. *
  93. * @command-name Create
  94. * @command-description Packages a Windows Azure project structure.
  95. *
  96. * @command-parameter-for $path Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to package.
  97. * @command-parameter-for $runDevFabric Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --RunDevFabric|-dev Required. Switch. Run and deploy to the Windows Azure development fabric.
  98. * @command-parameter-for $outputPath Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --OutputPath|-out Optional. The output path for the resulting package.
  99. */
  100. public function createPackageCommand($path, $runDevFabric, $outputPath)
  101. {
  102. // Create output paths
  103. if ($outputPath == '') {
  104. $outputPath = realpath($path . '/../');
  105. }
  106. $packageOut = $outputPath . '/' . basename($path) . '.cspkg';
  107. // Find Windows Azure SDK bin folder
  108. $windowsAzureSdkFolderCandidates = array_merge(
  109. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  110. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  111. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array()
  112. );
  113. if (count($windowsAzureSdkFolderCandidates) == 0) {
  114. throw new Zend_Service_Console_Exception('Could not locate Windows Azure SDK for PHP.');
  115. }
  116. $cspack = '"' . $windowsAzureSdkFolderCandidates[0] . '\cspack.exe' . '"';
  117. $csrun = '"' . $windowsAzureSdkFolderCandidates[0] . '\csrun.exe' . '"';
  118. // Open the ServiceDefinition.csdef file and check for role paths
  119. $serviceDefinitionFile = $path . '/ServiceDefinition.csdef';
  120. if (!file_exists($serviceDefinitionFile)) {
  121. require_once 'Zend/Service/Console/Exception.php';
  122. throw new Zend_Service_Console_Exception('Could not locate ServiceDefinition.csdef at ' . $serviceDefinitionFile . '.');
  123. }
  124. $serviceDefinition = Zend_Xml_Security::scanFile($serviceDefinitionFile);
  125. $xmlRoles = array();
  126. if ($serviceDefinition->WebRole) {
  127. if (count($serviceDefinition->WebRole) > 1) {
  128. $xmlRoles = array_merge($xmlRoles, $serviceDefinition->WebRole);
  129. } else {
  130. $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WebRole));
  131. }
  132. }
  133. if ($serviceDefinition->WorkerRole) {
  134. if (count($serviceDefinition->WorkerRole) > 1) {
  135. $xmlRoles = array_merge($xmlRoles, $serviceDefinition->WorkerRole);
  136. } else {
  137. $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WorkerRole));
  138. }
  139. }
  140. // Build '/role:' command parameter
  141. $roleArgs = array();
  142. foreach ($xmlRoles as $xmlRole) {
  143. if ($xmlRole["name"]) {
  144. $roleArgs[] = '/role:' . $xmlRole["name"] . ';' . realpath($path . '/' . $xmlRole["name"]);
  145. }
  146. }
  147. // Build command
  148. $command = $cspack;
  149. $args = array(
  150. $path . '\ServiceDefinition.csdef',
  151. implode(' ', $roleArgs),
  152. '/out:' . $packageOut
  153. );
  154. if ($runDevFabric) {
  155. $args[] = '/copyOnly';
  156. }
  157. passthru($command . ' ' . implode(' ', $args));
  158. // Can we copy a configuration file?
  159. $serviceConfigurationFile = $path . '/ServiceConfiguration.cscfg';
  160. $serviceConfigurationFileOut = $outputPath . '/ServiceConfiguration.cscfg';
  161. if (file_exists($serviceConfigurationFile) && !file_exists($serviceConfigurationFileOut)) {
  162. copy($serviceConfigurationFile, $serviceConfigurationFileOut);
  163. }
  164. // Do we have to start the development fabric?
  165. if ($runDevFabric) {
  166. passthru($csrun . ' /devstore:start /devfabric:start');
  167. passthru($csrun . ' /removeAll');
  168. passthru($csrun . ' /run:"' . $packageOut . ';' . $serviceConfigurationFileOut . '" /launchBrowser');
  169. }
  170. }
  171. /**
  172. * Creates a scaffolder from a given path.
  173. *
  174. * @command-name CreateScaffolder
  175. * @command-description Creates a scaffolder from a given path.
  176. *
  177. * @command-parameter-for $rootPath Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to package into a scaffolder.
  178. * @command-parameter-for $scaffolderFile Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --OutFile|-out Required. The filename of the scaffolder.
  179. */
  180. public function createScaffolderCommand($rootPath, $scaffolderFile)
  181. {
  182. $archive = new Phar($scaffolderFile);
  183. $archive->buildFromIterator(
  184. new RecursiveIteratorIterator(
  185. new RecursiveDirectoryIterator(realpath($rootPath))),
  186. realpath($rootPath));
  187. }
  188. }
  189. Zend_Service_Console_Command::bootstrap($_SERVER['argv']);