Createdomain.js

A node script to create directory structure and apache NGINX configurations

I had previously made a script in Ruby so I translated this in JavaScript Node.
Maybe someday I'm going to build some kind of admin to deploy stuff when creating a domain name.

#!/usr/bin/env node
var sys = require('sys'),  
    exec = require('child_process').exec
    fs = require('fs');
var heredoc = require('heredoc');  
var domainname = process.argv[2] || '';  
var template_q;  
// directories
var nginx_config_dir     = '/var/www/_conf/nginx/',  
    nginx_available_dir  = nginx_config_dir+'available/',
    nginx_enabled_dir    = nginx_config_dir+'enabled/',
    vhosts_dir           = '/var/www/vhosts/';

// Exits if no arguments has been passed
if (typeof process.argv[2] === "undefined") {  
    console.log('use: '+process.argv[1]+' domainname.com');
    process.exit();
}
// displays system output
var puts = function (error, stdout, stderr) { sys.puts(stdout) }  
/*
// String.substitute
// example:
// var myString = "Hello #{var1}, my name is #{var2}"
// var subtituteObject = {var1: 'Derp', var2: 'Cat'};
// var parsedString = subtituteObject.substitute(subtituteObject);
// returns: "Hello Derp, my name is Cat"
*/
String.prototype.substitute = function (object, regexp){  
    return String(this).replace(regexp || (/\\?\#\{([^{}]+)\}/g), function(match, name){
        if (match.charAt(0) == '\\') return match.slice(1);
        return (object[name] != null) ? object[name] : '';
    });
}
RegExp.escape = function (s) {  
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

var substituteObject = {  
    defaultvhosts: vhosts_dir,
    domainname: domainname,
    regexp_domainname: RegExp.escape(domainname)
};

// create directory sturcture for domain
var command_createDirectoryStructure = heredoc(function(){/*  
mkdir -p #{defaultvhosts}#{domainname}/{httpdocs,subdomains}  
echo "#{domainname}" > #{defaultvhosts}#{domainname}/httpdocs/index.html  
*/});
var parsed_createDirectoryStructure = command_createDirectoryStructure.substitute(substituteObject);

// Template for NGINX VHOST
template_q = heredoc(function(){/*  
server {  
    server_name ~^#{regexp_domainname}(\.dev)?$;
    location / {
        root /var/www/vhosts/#{domainname}/httpdocs;
        index index.html index.php;
    }

    location ~ \.php(/|$) {

        fastcgi_pass 0.0.0.0:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;

        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;

    }
}


server {  
    server_name ~^((?<subdomain>.*)\.)#{regexp_domainname}(\.dev)?$;
    root /var/www/vhosts/#{domainname}/subdomains/${subdomain}/httpdocs;

    location / {
        index index.html index.php;
    }

    location ~ \.php(/|$) {

        fastcgi_pass 0.0.0.0:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;

        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;

    }
}
*/});

//template create vhost symlink
var command_enableVhostSymlink = heredoc(function(){/*  
ln -nsf #{nginx_available_dir}/#{domainname}.conf #{nginx_enabled_dir}/#{domainname}.conf  
*/});

console.log(template_q.substitute(substituteObject));  
// exec(myCommands, puts);
comments powered by Disqus