Skip to content

Commit

Permalink
Merge pull request #392 from kirtangajjar/feature/dynamic-subnet-allo…
Browse files Browse the repository at this point in the history
…cation

Add migration for iproute2
  • Loading branch information
mrrobot47 committed Sep 6, 2021
2 parents 9f0ac29 + 2be0f73 commit a1f161d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
@@ -0,0 +1,46 @@
<?php

namespace EE\Migration;

use EE;
use EE\Migration\Base;
use EE\RevertableStepProcessor;
use EE\Model\Site;

class InstallIproute2 extends Base {

private $sites;
/** @var RevertableStepProcessor $rsp Keeps track of migration state. Reverts on error */
private static $rsp;

/**
* Execute php config updates.
*
* @throws EE\ExitException
*/
public function up() {

if ( $this->skip_this_migration ) {
EE::debug( 'Skipping add-ext-config-8.0 update migration as it is not needed.' );

return;
}

$os = php_uname( 'v' );
$ip_command_present = EE::launch( 'command -v ip' )->return_code === 0;

if ( ( strpos( $os, 'Ubuntu') !== false || strpos( $os, 'Debian' ) !== false )
&& ! $ip_command_present ) {
EE::exec( 'bash -c \'if command -v apt; then apt install -y iproute2; fi\'' );
}
}

/**
* Bring back the existing old php config.
*
* @throws EE\ExitException
*/
public function down() {

}
}
Expand Up @@ -4,6 +4,7 @@
use EE;
use EE\Model\Site;
use PDOException;
use function EE\Service\Utils\ensure_global_network_initialized;
use function EE\Site\Utils\auto_site_name;
use function EE\Site\Utils\get_site_info;

Expand Down Expand Up @@ -40,6 +41,8 @@ public function up() {

$sites = Site::all();

ensure_global_network_initialized();

foreach ( $sites as $site ) {
$site->subnet_ip = EE\Site\Utils\get_available_subnet();
$site->save();
Expand Down
15 changes: 12 additions & 3 deletions src/helper/site-utils.php
Expand Up @@ -821,7 +821,11 @@ function get_available_subnet( int $mask = 24 ) {
array_push( $site_ips, $backend_subnet );
}

$existing_subnets = array_unique( array_merge( $site_ips, $existing_host_subnets ) );
$existing_subnets = array_filter(
array_unique(
array_merge( $site_ips, $existing_host_subnets )
)
);

sort( $existing_subnets, SORT_NATURAL );

Expand All @@ -845,6 +849,11 @@ function get_available_subnet( int $mask = 24 ) {
EE::error( 'It seems you have run out of your private IP adress space.' );
}


function subnet_mask_int2long( int $mask ) {
return ~(( 1 << ( 32 - $mask )) - 1 );
}

/**
* Check if IP is in existing subnets
*
Expand Down Expand Up @@ -874,7 +883,7 @@ function ip_in_subnet(string $IP, string $CIDR) {
list( $subnet, $mask ) = explode ('/', $CIDR );

$ip_subnet = ip2long( $subnet );
$ip_mask = ~(( 1 << ( 32 - $mask )) - 1 );
$ip_mask = subnet_mask_int2long( $mask );
$src_ip = ip2long( $IP );

return (( $src_ip & $ip_mask ) == ( $ip_subnet & $ip_mask ));
Expand All @@ -889,7 +898,7 @@ function ip_in_subnet(string $IP, string $CIDR) {
*/
function get_subnet_range( $ip, $mask ) {
$ipl = ip2long( $ip );
$maskl = ~(( 1 << ( 32 - $mask )) - 1 );
$maskl = subnet_mask_int2long( $mask );

$range_start = $ipl & $maskl;
$range_end = $ipl | ~$maskl;
Expand Down

0 comments on commit a1f161d

Please sign in to comment.