web/lib/Zend/Service/WindowsAzure/CommandLine/Package.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     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-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * Package commands
       
    26  * 
       
    27  * @category   Zend
       
    28  * @package    Zend_Service_WindowsAzure_CommandLine
       
    29  * @copyright  Copyright (c) 2005-2012 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 		
       
    62 		// Verify scaffolder
       
    63 		if (!is_file($scaffolder)) {
       
    64 			require_once 'Zend/Service/Console/Exception.php';
       
    65 			throw new Zend_Service_Console_Exception('Could not locate the given scaffolder: ' . $scaffolder);
       
    66 		}
       
    67 		
       
    68 		// Include scaffolder
       
    69 		$archive = new Phar($scaffolder);
       
    70 		include $scaffolder;
       
    71 		if (!class_exists('Scaffolder')) {
       
    72 			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.');
       
    74 		}
       
    75 		
       
    76 		// Cleanup $argv
       
    77 		$options = array();
       
    78 		foreach ($argv as $arg) {
       
    79 			list($key, $value) = explode(':', $arg, 2);
       
    80 			while (substr($key, 0, 1) == '-') {
       
    81 				$key = substr($key, 1);
       
    82 			}
       
    83 			$options[$key] = $value;
       
    84 		}
       
    85 		
       
    86 		// Run scaffolder
       
    87 		$scaffolderInstance = new Scaffolder();
       
    88 		$scaffolderInstance->invoke($archive, $path, $options);
       
    89 	}
       
    90 	
       
    91 
       
    92 	/**
       
    93 	 * Packages a Windows Azure project structure.
       
    94 	 * 
       
    95 	 * @command-name Create
       
    96 	 * @command-description Packages a Windows Azure project structure.
       
    97 	 * 
       
    98 	 * @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.
       
   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. 
       
   101 	 */
       
   102 	public function createPackageCommand($path, $runDevFabric, $outputPath)
       
   103 	{
       
   104 		// Create output paths
       
   105 		if ($outputPath == '') {
       
   106 			$outputPath = realpath($path . '/../');
       
   107 		}
       
   108 		$packageOut = $outputPath . '/' . basename($path) . '.cspkg';
       
   109 
       
   110 		// Find Windows Azure SDK bin folder
       
   111 		$windowsAzureSdkFolderCandidates = array_merge(
       
   112 			isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
       
   113 			isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
       
   114 			isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array()
       
   115 		);
       
   116 		if (count($windowsAzureSdkFolderCandidates) == 0) {
       
   117 			throw new Zend_Service_Console_Exception('Could not locate Windows Azure SDK for PHP.');
       
   118 		}
       
   119 		$cspack = '"' . $windowsAzureSdkFolderCandidates[0] . '\cspack.exe' . '"';
       
   120 		$csrun = '"' . $windowsAzureSdkFolderCandidates[0] . '\csrun.exe' . '"';
       
   121 		
       
   122 		// Open the ServiceDefinition.csdef file and check for role paths
       
   123 		$serviceDefinitionFile = $path . '/ServiceDefinition.csdef';
       
   124 		if (!file_exists($serviceDefinitionFile)) {
       
   125 			require_once 'Zend/Service/Console/Exception.php';
       
   126 			throw new Zend_Service_Console_Exception('Could not locate ServiceDefinition.csdef at ' . $serviceDefinitionFile . '.');
       
   127 		}
       
   128 		$serviceDefinition = simplexml_load_file($serviceDefinitionFile);
       
   129 		$xmlRoles = array();
       
   130 		if ($serviceDefinition->WebRole) {
       
   131 			if (count($serviceDefinition->WebRole) > 1) {
       
   132 	    		$xmlRoles = array_merge($xmlRoles, $serviceDefinition->WebRole);
       
   133 			} else {
       
   134 	    		$xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WebRole));
       
   135 	    	}
       
   136 		}
       
   137 		if ($serviceDefinition->WorkerRole) {
       
   138 			if (count($serviceDefinition->WorkerRole) > 1) {
       
   139 	    		$xmlRoles = array_merge($xmlRoles, $serviceDefinition->WorkerRole);
       
   140 			} else {
       
   141 	    		$xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WorkerRole));
       
   142 	    	}
       
   143 		}
       
   144     		
       
   145 		// Build '/role:' command parameter
       
   146 		$roleArgs = array();
       
   147 		foreach ($xmlRoles as $xmlRole) {
       
   148 			if ($xmlRole["name"]) {
       
   149 				$roleArgs[] = '/role:' . $xmlRole["name"] . ';' . realpath($path . '/' . $xmlRole["name"]);
       
   150 			}
       
   151 		}
       
   152 		
       
   153 		// Build command
       
   154 		$command = $cspack;
       
   155 		$args = array(
       
   156 			$path . '\ServiceDefinition.csdef',
       
   157 			implode(' ', $roleArgs),
       
   158 			'/out:' . $packageOut
       
   159 		);
       
   160 		if ($runDevFabric) {
       
   161 			$args[] = '/copyOnly';
       
   162 		}
       
   163 		passthru($command . ' ' . implode(' ', $args));
       
   164 		
       
   165 		// Can we copy a configuration file?
       
   166 		$serviceConfigurationFile = $path . '/ServiceConfiguration.cscfg';
       
   167 		$serviceConfigurationFileOut = $outputPath . '/ServiceConfiguration.cscfg';
       
   168 		if (file_exists($serviceConfigurationFile) && !file_exists($serviceConfigurationFileOut)) {
       
   169 			copy($serviceConfigurationFile, $serviceConfigurationFileOut);
       
   170 		}
       
   171 		
       
   172 		// Do we have to start the development fabric?
       
   173 		if ($runDevFabric) {
       
   174 			passthru($csrun . ' /devstore:start /devfabric:start');
       
   175 			passthru($csrun . ' /removeAll');
       
   176 			passthru($csrun . ' /run:"' . $packageOut . ';' . $serviceConfigurationFileOut . '" /launchBrowser');
       
   177 		}
       
   178 	}
       
   179 	
       
   180 	/**
       
   181 	 * Creates a scaffolder from a given path.
       
   182 	 * 
       
   183 	 * @command-name CreateScaffolder
       
   184 	 * @command-description Creates a scaffolder from a given path.
       
   185 	 * 
       
   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.
       
   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.
       
   188 	 */
       
   189 	public function createScaffolderCommand($rootPath, $scaffolderFile)
       
   190 	{
       
   191 		$archive = new Phar($scaffolderFile);
       
   192 		$archive->buildFromIterator(
       
   193 			new RecursiveIteratorIterator(
       
   194 				new RecursiveDirectoryIterator(realpath($rootPath))),
       
   195 		realpath($rootPath));
       
   196 	}
       
   197 }
       
   198 Zend_Service_Console_Command::bootstrap($_SERVER['argv']);