Skip to content

Commit

Permalink
Merge pull request #263 from mrrobot47/feature/site-update-ssl
Browse files Browse the repository at this point in the history
Add site update for non-ssl to ssl updates
  • Loading branch information
rahulsprajapati authored Dec 12, 2018
2 parents 23c7218 + 5428676 commit fd28c62
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/Site_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ public function __invoke( $args, $assoc_args ) {
$type = 'html';

if ( in_array( reset( $args ), [ 'create', 'update' ], true ) || empty( $args ) ) {
$unset = true;
if ( ! empty( $args[0] ) && 'create' === $args[0] ) {
$args = $this->name_checks_and_updates( $args );
\EE\Auth\Utils\init_global_admin_tools_auth( false );
}
if ( ! empty( $args[0] ) && 'update' === $args[0] ) {
$unset = false;
$type = $this->determine_type( $type, $args );
}
\EE\Auth\Utils\init_global_admin_tools_auth( false );
if ( isset( $assoc_args['type'] ) ) {
$type = $assoc_args['type'];
unset( $assoc_args['type'] );
if ( $unset ) {
unset( $assoc_args['type'] );
}
}
} else {
$type = $this->determine_type( $type, $args );
Expand Down
2 changes: 1 addition & 1 deletion src/helper/Site_Letsencrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function check( Array $domains, $wildcard = false, $preferred_challenge =
\EE::warning( 'Challenge Authorization failed. Check logs and check if your domain is pointed correctly to this server.' );

$site_name = isset( $domains[1] ) ? $domains[1] : $domains[0];
$site_name = str_replace( '*.', '', $site_name, 1 );
$site_name = str_replace( '*.', '', $site_name );

\EE::log( "Re-run `ee site ssl $site_name` after fixing the issue." );
throw $e;
Expand Down
71 changes: 71 additions & 0 deletions src/helper/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function EE\Utils\download;
use function EE\Utils\extract_zip;
use function EE\Utils\get_flag_value;
use function EE\Utils\delem_log;
use function EE\Site\Utils\auto_site_name;
use function EE\Site\Utils\get_site_info;
use function EE\Site\Utils\reload_global_nginx_proxy;
Expand Down Expand Up @@ -271,6 +272,76 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = []
\EE::log( "Site $site_url deleted." );
}

/**
* Supports updating and upgrading site.
*
* [<site-name>]
* : Name of the site.
*
* [--ssl=<ssl>]
* : Enable ssl on site
*
* [--wildcard]
* : Enable wildcard SSL on site.
*
* ## EXAMPLES
*
* # Add SSL to non-ssl site
* $ ee site update example.com --ssl=le
*
* # Add SSL to non-ssl site
* $ ee site update example.com --ssl=le --wildcard
*
* # Add self-signed SSL to non-ssl site
* $ ee site update example.com --ssl=self
*
*/
public function update( $args, $assoc_args ) {

delem_log( 'site update start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, true, true, false );
$ssl = get_flag_value( $assoc_args, 'ssl', false );
if ( $ssl ) {
$this->update_ssl( $assoc_args );
}
}

/**
* Funciton to update ssl of a site.
*/
protected function update_ssl( $assoc_args ) {

$ssl = get_flag_value( $assoc_args, 'ssl', false );
$wildcard = get_flag_value( $assoc_args, 'wildcard', false );
$show_error = $this->site_data->site_ssl ? true : false;
$wildcard_error = ( ! $this->site_data->site_ssl_wildcard && $wildcard ) ? true : false;

$error = $wildcard_error ? 'Update from normal ssl to wildcard is not supported yet.' : 'Site ' . $this->site_data->site_url . ' already contains SSL.';

if ( $show_error ) {
EE::error( $error );
}

EE::log( 'Starting ssl update for: ' . $this->site_data->site_url );
try {
$this->site_data->site_ssl = $ssl;
$this->site_data->site_ssl_wildcard = $wildcard ? 1 : 0;

$site = $this->site_data;
$array_data = ( array ) $this->site_data;
$this->site_data = reset( $array_data );
$this->site_data['site_ssl'] = $ssl;
$this->www_ssl_wrapper( [ 'nginx' ] );
$site->site_ssl = $ssl;
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
$site->save();
EE::success( 'Enabled ssl for ' . $this->site_data['site_url'] );
delem_log( 'site ssl update end' );
}

/**
* Enables a website. It will start the docker containers of the website if they are stopped.
*
Expand Down

0 comments on commit fd28c62

Please # to comment.