PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/WindowsAzure/CommandLine/Package.php

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