PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/Microsoft/WindowsAzure/CommandLine/Package.php

https://bitbucket.org/ktos/tinyshare
PHP | 181 lines | 96 code | 12 blank | 73 comment | 22 complexity | e5c61acaf86f1813226a18c0d7509c2f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2011, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_Console
  30. * @subpackage Exception
  31. * @version $Id: Exception.php 55733 2011-01-03 09:17:16Z unknown $
  32. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  33. * @license http://phpazure.codeplex.com/license
  34. */
  35. /**
  36. * @see Microsoft_AutoLoader
  37. */
  38. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  39. /**
  40. * Package commands
  41. *
  42. * @category Microsoft
  43. * @package Microsoft_WindowsAzure_CommandLine
  44. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  45. * @license http://phpazure.codeplex.com/license
  46. *
  47. * @command-handler package
  48. * @command-handler-description Windows Azure Package commands
  49. * @command-handler-header Windows Azure SDK for PHP
  50. * @command-handler-header Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  51. * @command-handler-footer
  52. * @command-handler-footer All commands support the --ConfigurationFile or -F parameter.
  53. * @command-handler-footer The parameter file is a simple INI file carrying one parameter
  54. * @command-handler-footer value per line. It accepts the same parameters as one can
  55. * @command-handler-footer use from the command line command.
  56. */
  57. class Microsoft_WindowsAzure_CommandLine_Package
  58. extends Microsoft_Console_Command
  59. {
  60. /**
  61. * Packages a Windows Azure project structure.
  62. *
  63. * @command-name Create
  64. * @command-description Packages a Windows Azure project structure.
  65. *
  66. * @command-parameter-for $path Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile|Microsoft_Console_Command_ParameterSource_StdIn --InputPath|-in Required. The path to package.
  67. * @command-parameter-for $runDevFabric Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --RunDevFabric|-dev Required. Switch. Run and deploy to the Windows Azure development fabric.
  68. * @command-parameter-for $outputPath Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --OutputPath|-out Optional. The output path for the resulting package.
  69. */
  70. public function createPackageCommand($path, $runDevFabric, $outputPath)
  71. {
  72. // See if the $path variable requires some cleaning due to piping commands
  73. if (strpos($path, 'finished at location: ') !== false) {
  74. $path = explode('finished at location: ', $path);
  75. $path = rtrim($path[1]);
  76. }
  77. // Create output paths
  78. if (is_null($outputPath) || $outputPath == '') {
  79. $outputPath = realpath($path . '/../');
  80. }
  81. $packageOut = $outputPath . '/' . basename($path) . '.cspkg';
  82. if (!file_exists($outputPath)) {
  83. if (@mkdir($outputPath) === false) {
  84. throw new Microsoft_Console_Exception('The path ' . $outputPath . ' could not be created.');
  85. }
  86. }
  87. // Find Windows Azure SDK bin folders
  88. $csPackFolderCandidates = array_merge(
  89. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  90. isset($_SERVER['ProgramFiles(x86)']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  91. isset($_SERVER['ProgramW6432']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array()
  92. );
  93. if (count($csPackFolderCandidates) == 0) {
  94. throw new Microsoft_Console_Exception('Could not locate the Windows Azure SDK. Download the tools from www.azure.com or using the Web Platform Installer.');
  95. }
  96. $cspack = '"' . $csPackFolderCandidates[count($csPackFolderCandidates) - 1] . '\cspack.exe' . '"';
  97. $csRunFolderCandidates = array_merge(
  98. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  99. isset($_SERVER['ProgramFiles(x86)']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  100. isset($_SERVER['ProgramW6432']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  101. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure Emulator\emulator', GLOB_NOSORT) : array(),
  102. isset($_SERVER['ProgramFiles(x86)']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure Emulator\emulator', GLOB_NOSORT) : array(),
  103. isset($_SERVER['ProgramW6432']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure Emulator\emulator', GLOB_NOSORT) : array()
  104. );
  105. if (count($csRunFolderCandidates) == 0) {
  106. throw new Microsoft_Console_Exception('Could not locate the Windows Azure SDK. Download the tools from www.azure.com or using the Web Platform Installer.');
  107. }
  108. $csrun = '"' . $csRunFolderCandidates[count($csRunFolderCandidates) - 1] . '\csrun.exe' . '"';
  109. // Open the ServiceDefinition.csdef file and check for role paths
  110. $serviceDefinitionFile = $path . '/ServiceDefinition.csdef';
  111. if (!file_exists($serviceDefinitionFile)) {
  112. throw new Microsoft_Console_Exception('Could not locate ServiceDefinition.csdef at ' . $serviceDefinitionFile . '.');
  113. }
  114. $serviceDefinition = simplexml_load_file($serviceDefinitionFile);
  115. $xmlRoles = array();
  116. if ($serviceDefinition->WebRole) {
  117. if (count($serviceDefinition->WebRole) > 1) {
  118. foreach ($serviceDefinition->WebRole as $role) {
  119. $xmlRoles[] = $role;
  120. }
  121. } else {
  122. $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WebRole));
  123. }
  124. }
  125. if ($serviceDefinition->WorkerRole) {
  126. if (count($serviceDefinition->WorkerRole) > 1) {
  127. foreach ($serviceDefinition->WorkerRole as $role) {
  128. $xmlRoles[] = $role;
  129. }
  130. } else {
  131. $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WorkerRole));
  132. }
  133. }
  134. // Build '/role:' command parameter
  135. $roleArgs = array();
  136. foreach ($xmlRoles as $xmlRole) {
  137. if ($xmlRole["name"]) {
  138. $roleArgs[] = '/role:' . $xmlRole["name"] . ';' . realpath($path . '/' . $xmlRole["name"]);
  139. }
  140. }
  141. // Build command
  142. $command = $cspack;
  143. $args = array(
  144. $path . '\ServiceDefinition.csdef',
  145. implode(' ', $roleArgs),
  146. '/out:' . $packageOut
  147. );
  148. if ($runDevFabric) {
  149. $args[] = '/copyOnly';
  150. }
  151. passthru($command . ' ' . implode(' ', $args));
  152. // Can we copy a configuration file?
  153. $serviceConfigurationFile = $path . '/ServiceConfiguration.cscfg';
  154. $serviceConfigurationFileOut = $outputPath . '/ServiceConfiguration.cscfg';
  155. if (file_exists($serviceConfigurationFile) && !file_exists($serviceConfigurationFileOut)) {
  156. copy($serviceConfigurationFile, $serviceConfigurationFileOut);
  157. }
  158. // Do we have to start the development fabric?
  159. if ($runDevFabric) {
  160. passthru($csrun . ' /devstore:start');
  161. passthru($csrun . ' /devfabric:start');
  162. passthru($csrun . ' /removeAll');
  163. passthru($csrun . ' /run:"' . $packageOut . ';' . $serviceConfigurationFileOut . '" /launchBrowser');
  164. }
  165. // Echo package
  166. echo $packageOut;
  167. }
  168. }
  169. Microsoft_Console_Command::bootstrap($_SERVER['argv']);