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