mirror of https://github.com/hauke68/Magallanes
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.4 KiB
102 lines
3.4 KiB
13 years ago
|
<?php
|
||
11 years ago
|
/*
|
||
|
* This file is part of the Magallanes package.
|
||
|
*
|
||
|
* (c) Andrés Montañez <andres@andresmontanez.com>
|
||
|
*
|
||
|
* For the full copyright and license information, please view the LICENSE
|
||
|
* file that was distributed with this source code.
|
||
|
*/
|
||
|
|
||
11 years ago
|
namespace Mage\Command\BuiltIn;
|
||
|
|
||
|
use Mage\Command\AbstractCommand;
|
||
|
use Mage\Console;
|
||
|
|
||
|
use Exception;
|
||
|
|
||
|
/**
|
||
|
* Command for Adding elements to the Configuration.
|
||
|
* Currently elements allowed to add:
|
||
|
* - environments
|
||
|
*
|
||
|
* @author Andrés Montañez <andres@andresmontanez.com>
|
||
|
*/
|
||
|
class AddCommand extends AbstractCommand
|
||
13 years ago
|
{
|
||
11 years ago
|
/**
|
||
|
* Adds new Configuration Elements
|
||
|
* @see \Mage\Command\AbstractCommand::run()
|
||
|
* @throws Exception
|
||
|
*/
|
||
13 years ago
|
public function run()
|
||
13 years ago
|
{
|
||
13 years ago
|
$subCommand = $this->getConfig()->getArgument(1);
|
||
|
|
||
|
try {
|
||
|
switch ($subCommand) {
|
||
|
case 'environment':
|
||
11 years ago
|
$this->addEnvironment();
|
||
|
break;
|
||
|
|
||
|
default;
|
||
|
throw new Exception('The Type of Add is needed.');
|
||
13 years ago
|
break;
|
||
|
}
|
||
11 years ago
|
} catch (Exception $exception) {
|
||
|
Console::output('<red>' . $exception->getMessage() . '</red>', 1, 2);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Adds an Environment
|
||
|
*
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
protected function addEnvironment()
|
||
13 years ago
|
{
|
||
|
$withReleases = $this->getConfig()->getParameter('enableReleases', false);
|
||
|
$environmentName = strtolower($this->getConfig()->getParameter('name'));
|
||
|
|
||
|
if ($environmentName == '') {
|
||
|
throw new Exception('You must specify a name for the environment.');
|
||
|
}
|
||
|
|
||
13 years ago
|
$environmentConfigFile = '.mage/config/environment/' . $environmentName . '.yml';
|
||
13 years ago
|
|
||
13 years ago
|
if (file_exists($environmentConfigFile)) {
|
||
13 years ago
|
throw new Exception('The environment already exists.');
|
||
|
}
|
||
|
|
||
11 years ago
|
Console::output('Adding new environment: <dark_gray>' . $environmentName . '</dark_gray>');
|
||
13 years ago
|
|
||
|
$releasesConfig = 'releases:' . PHP_EOL
|
||
|
. ' enabled: true' . PHP_EOL
|
||
|
. ' max: 10' . PHP_EOL
|
||
|
. ' symlink: current' . PHP_EOL
|
||
|
. ' directory: releases' . PHP_EOL;
|
||
|
|
||
|
$baseConfig = '#' . $environmentName . PHP_EOL
|
||
|
. 'deployment:' . PHP_EOL
|
||
|
. ' user: dummy' . PHP_EOL
|
||
|
. ' from: ./' . PHP_EOL
|
||
|
. ' to: /var/www/vhosts/example.com/www' . PHP_EOL
|
||
|
. ' excludes:' . PHP_EOL
|
||
|
. ($withReleases ? $releasesConfig : '')
|
||
|
. 'hosts:' . PHP_EOL
|
||
|
. 'tasks:' . PHP_EOL
|
||
|
. ' pre-deploy:' . PHP_EOL
|
||
|
. ' on-deploy:' . PHP_EOL
|
||
12 years ago
|
. ($withReleases ? (' post-release:' . PHP_EOL) : '')
|
||
13 years ago
|
. ' post-deploy:' . PHP_EOL;
|
||
12 years ago
|
|
||
13 years ago
|
$result = file_put_contents($environmentConfigFile, $baseConfig);
|
||
|
|
||
|
if ($result) {
|
||
11 years ago
|
Console::output('<light_green>Success!!</light_green> Environment config file for <dark_gray>' . $environmentName . '</dark_gray> created successfully at <blue>' . $environmentConfigFile . '</blue>');
|
||
|
Console::output('<dark_gray>So please! Review and adjust its configuration.</dark_gray>', 2, 2);
|
||
13 years ago
|
} else {
|
||
11 years ago
|
Console::output('<light_red>Error!!</light_red> Unable to create config file for environment called <dark_gray>' . $environmentName . '</dark_gray>', 1, 2);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|