Skip to content

Commit

Permalink
changed host file handling from list to array
Browse files Browse the repository at this point in the history
added function to remove host name of the starting server before adding a new host name
added function to remove host name when forgetting a server
changed detection of highest ip address
updated README
  • Loading branch information
chrisschmitz committed Feb 11, 2017
1 parent 5efd53d commit dc1610d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 115 deletions.
7 changes: 7 additions & 0 deletions ModuleConfig.cfc
Expand Up @@ -14,4 +14,11 @@ component accessors=true {

return;
}

public void function postServerForget( interceptData ) {

wirebox.getInstance( 'hostupdaterService@commandbox-hostupdater' ).forgetServer( arguments.interceptData.serverInfo.id );

return;
}
}
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -21,7 +21,7 @@ Just provide a host name for your server.
```bash
CommandBox> server start host=myproject.local
```
The module will add the host name (here 'myproject.local') to your hosts file. All entries added by the module will be marked with a comment `# CommandBox <Server-ID> <current timestamp>`.
The module will first remove any host names that you previously assigned *to the same server* and then add the host name (here 'myproject.local') to your hosts file. All entries added by the module will be marked with a comment `# CommandBox <Server-ID> <current timestamp>`.

### Location of the hosts file

Expand All @@ -39,3 +39,9 @@ In order to avoid conflicts with other IP addresses you may assign manually, the

It detects the highest used IP address in that range and increase that by 1. That gives you 255 x 255 = 65.025 IP addresses to use.

### Forgetting a server

If you tell CommandBox to forget a server ```bash
CommandBox> server forget my-fancy-server
```, the module will remove any host name that you may have assigned to that server from the hosts file.
87 changes: 8 additions & 79 deletions box.json
@@ -1,88 +1,17 @@
{
"name":"CommandBox Host Updater",
"version":"1.0.1",
"version":"1.0.2+0",
"author":"Christoph Schmitz",
"location":"chrisschmitz/commandbox-hostupdater#v1.0.0",
"directory":"",
"createPackageDirectory":true,
"packageDirectory":"",
"homepage":"https://github.com/chrisschmitz/commandbox-hostupdater/",
"documentation":"",
"documentation":"https://github.com/chrisschmitz/commandbox-hostupdater/README.md",
"repository":{
"type":"",
"URL":""
"type":"Git",
"URL":"https://github.com/chrisschmitz/commandbox-hostupdater/"
},
"bugs":"",
"bugs":"https://github.com/chrisschmitz/commandbox-hostupdater/issues",
"slug":"commandbox-hostupdater",
"shortDescription":"Adds the host name you assign to your server to your system's hosts file",
"description":"",
"instructions":"",
"changelog":"",
"shortDescription":"Adds the host name you assign to a CommandBox server to the hosts file.",
"description":"This module adds the host name you assign to a CommandBox server to the hosts file. It assumes standard paths to the hosts file on Windows, Linux and OS-X.",
"type":"commandbox-modules",
"keywords":"cfml,coldfusion,lucee,server,",
"private":false,
"engines":[
{
"type":"",
"version":""
}
],
"defaultEngine":"",
"defaultPort":0,
"projectURL":"",
"license":[
{
"type":"",
"URL":""
}
],
"contributors":[

],
"dependencies":{

},
"devDependencies":{

},
"installPaths":{

},
"scripts":{
"postVersion":"package set location='chrisschmitz/commandbox-hostupdater#v`package version`'",
"postPublish":"!git push --follow-tags"
},
"ignore":[
"test",
"tests"
],
"testbox":{
"runner":[
{
"default":""
}
],
"labels":[

],
"reporter":"",
"reporterResults":"",
"bundles":[
""
],
"directory":{
"mapping":"",
"recurse":true
},
"watchers":[

],
"notify":{
"emails":[

],
"growl":"",
"URL":""
}
}
"projectURL":"https://github.com/chrisschmitz/commandbox-hostupdater/"
}
122 changes: 87 additions & 35 deletions models/HostupdaterService.cfc
Expand Up @@ -8,57 +8,109 @@ component accessors="true" singleton {
}

public void function checkIP ( string server_id, string hostname='' ) {
// os-specific new line delimiter
var newline = server.separator.line;


// get os-specific location of hosts file
var hostsFile = variables.fileSystem.isWindows() ? 'C:/Windows/system32/drivers/etc/hosts' :
variables.fileSystem.isMac() ? '/private/etc/hosts' :
variables.fileSystem.isLinux() ? '/etc/hosts' :
'';
var hostsFile = getHostsFileName();

// check hosts file only if we have a file location and if the provided host name is not an ip address
if ( hostsFile.len() && arguments.hostname.reFindNoCase( '[a-z]' ) ) {
var hosts = fileRead( hostsFile );

// only add host to file if it isn't already in there
// use space and tab as possible list delimiters in order to avoid substring-matching
if( !hosts.listFindNoCase(arguments.hostname, ' ') ) {
try {
variables.printBuffer.greenLine( "Adding host '#arguments.hostname#' to your hosts file!" ).toConsole();
var new_ip = getNewIP( hosts );

fileAppend( hostsFile, "#newline##newline##new_ip# #arguments.hostname# ## CommandBox: Server #arguments.server_id# #dateTimeFormat( now(), 'yyyy-mm-dd HH:nn:ss' )# " );
}
catch( any e ) {
variables.printBuffer.boldRedLine( "Can't write to hosts file. Did you remember to start CommandBox with admin privileges?").toConsole();
}
}
var hosts = readHostsFileAsArray( hostsFile );

// remove all lines that already contain either the server id or the host name
// that way we can make sure that the hosts file doesn't grow indefinitely upon
// changing the host name for an existing server
hosts = removeMatchingLines( hosts, [arguments.hostname,arguments.server_id] )
.toList( server.separator.line ); // concatenate the array

variables.printBuffer.greenLine( "Adding host '#arguments.hostname#' to your hosts file!" ).toConsole();
var new_ip = getNewIP( hosts );
hosts=hosts.listAppend( "#server.separator.line##new_ip# #arguments.hostname# ## CommandBox: Server #arguments.server_id# #dateTimeFormat( now(), 'yyyy-mm-dd HH:nn:ss' )#", server.separator.line ) // add the line for the new host entry
saveHostsFile( hostsFile, hosts );


}

return;
}


public void function forgetServer( required string server_id ){
var hostsFile = getHostsFileName();
var hosts = readHostsFileAsArray( hostsFile );

variables.printBuffer.greenLine( "Removing host for server '#arguments.server_id#' from your hosts file!" ).toConsole();

// remove all lines that contain the server id
// and concatenate the array
hosts = removeMatchingLines( hosts, [arguments.server_id] )
.toList( server.separator.line );

saveHostsFile( hostsFile, hosts );

return;
}

private string function getHostsFileName() {
return variables.fileSystem.isWindows() ? 'C:/Windows/system32/drivers/etc/hosts' :
variables.fileSystem.isMac() ? '/private/etc/hosts' :
variables.fileSystem.isLinux() ? '/etc/hosts' :
'';
}

private array function readHostsFileAsArray( required string hostsFile ) {
return fileRead( arguments.hostsFile ).listToArray( server.separator.line );
}

private any function removeMatchingLines( required array hosts, required array expressions ){

for( var elem in arguments.expressions ) {
arguments.hosts = arguments.hosts.filter( function( line ) {
return !line.listFindNoCase( elem, ' ' );
});
}

return arguments.hosts;
}

private string function getNewIP( required string hosts ) {
var ip_array = arguments.hosts.reMatch( '127.127(\.[0-9]+){2}' ).sort( 'text', 'desc');
// get all 127.127.xxx.yyy entries
var ip_array = arguments.hosts.reMatch( '127.127(\.[0-9]+){2}' );

if( ip_array.len() ) {
var highest_ip = ip_array[1];
var third_group = highest_ip.listGetAt( 3, '.' );
var last_group = highest_ip.listLast( '.' );
if( last_group < 255 )
last_group++;
else {
third_group++;
last_group=1;
}

var new_ip = highest_ip.listSetAt( 3, third_group, '.' ).listSetAt( 4, last_group, '.' )
// get highest third group
var group_3 = ip_array.map( function ( address ){
return address.listGetAt( 3, '.' );
}).sort( 'numeric', 'desc' )[1];

// take all addresses that match 127.127. + group_3
// then get highest last group from that set
var group_4 = ip_array.filter( function( address ) {
return address.listGetAt( 3, '.' ) == group_3;
})
.map( function ( address ) {
return address.listLast( '.' );
}).sort( 'numeric', 'desc' )[1];

group_3 = group_4 < 255 ? group_3 : group_3++;
group_4 = group_4 < 255 ? group_4++ : 1;

var new_ip = '127.127.#group_3#.#group_4#';
}
else
var new_ip = '127.127.0.1';

return new_ip;
}

private void function saveHostsFile( string fileName, string fileContent ) {
try {
fileWrite( arguments.fileName, arguments.fileContent );
}
catch ( any e ) {
variables.printBuffer.boldRedLine( "Can't write to hosts file. Did you remember to start CommandBox with admin privileges?" ).toConsole();
}

return;
}

}

0 comments on commit dc1610d

Please sign in to comment.