Google Chrome Extension End-To-End build script

Below you will find my simple build script for the end-to-end plugin written by Google for Chrome.

This simple script will follow the same build process as layed out by the developers of the code, but will run it in a automated fashion that will stop if it runs into any problems.

The script starts by checking to make sure the system has the needed programs installed before it will continue. This may be disabled if needed.

Please let me know if you have any issues.

Gitblit: This repository has a working copy and cannot receive pushes

The error message in GitblitThis repository has a working copy and cannot receive pushes” is indicating that the repository in question is not set as a mirror but is instead a full repository.

To resolve this, you need to re-download the repository, or move the .git folder from it and change it to a mirror.

To re-download the repository correctly, run the following command:

git clone <url-of-repo> --mirror

If you wish to change the name you may like this:

git clone <url-of-repo> <local-name-of-repo> --mirror

NGINX Install/Update Script

A complete update script for nginx installs on Ubuntu 12.04 LTS.

Cgit download site

Today I was working on a friends git repository when I noticed that the cgit website is down.  Thankfully I have a backup of the current repository if anyone needs it.

This mirror will auto update if the repository comes back, but will also let you download and install cgit on your own server via the following command.

git clone http://git.mattrude.com/mirror/cgit.git

If you run into any problems with this mirror, please let me know in the comments.

Chrome: Auto Connecting with a Client Certificate

When using client certificates in Chrome, one of the great annoyances is that it prompts you each time you first go to the secured site per session.  This is of course preferred  as a security step to confirm you are connecting with the correct certificate, but you can also configure Chrome to use the certificate you selected for each site.

AutoSelectCertificateForUrls-1

Note the added key’s “Google”, “Chrome”, & “AutoSelectCertificateForUrls”

To accomplish this, in Windows 7, we will be adding a registry keys via regedit.  You will need to first open the Registry Editor by going to Start, then start typing in the lower search box “regedit”.  Once the registry editor is opened, you will need to create a Google key, Chrome Key, and a AutoSelectCertificateForUrls key so it vaguely looks like the following example. You may have many, many, many programs under SOFTWARE and Policies, just right mouse click on Policies and choose New then Key.

Next you need to add the values to the AutoSelectCertificateForUrls key.  Right mouse click on the large white space on the right side of the screen and choose New, then String. Name the new string 1 and add the following to the value:

{"pattern":"https://example.com","filter":{"ISSUER":{"CN":"CertName"}}}

Where https://example.com is the URL of the site you are accessing, and CertName is the CN name from the certificate you are using.

Once you are finished, it should look something like this.

AutoSelectCertificateForUrls-2

WordPress: Allow Auto Updating

When WordPress is properly locked down on your server, you are often unable to automatically update the plugins or themes on your site. The problems comes in when WordPress looks to see if it is able to write to the file system. Since it can’t modify it’s own files, it requests access to the FTP server running on your web-server. My sites NEVER have a FTP server running on them for simple security reasons, so updating via FTP is not an option.

Each of my sites are locked down with the below commands. The first line modifies the file system to allow only the Owner & Group to have modify access to each file or directory. All other users only have read access. The second line changes all files and directories so to be owned by root and the root group. The 3rd line grants owner and group permissions to the user www-data; the Ubuntu default webserver user.

chmod -R ug=rwX,o=rX
chown -R root:root /var/www/example.com
chown -R www-data:www-data /var/www/example.com/wp-content/plugins \
/var/www/example.com/wp-content/themes /var/www/example.com/wp-content/upgrade \
/var/www/example.com/wp-content/uploads

So now that your site is locked down, you can’t update the plugins or themes, well; that’s where this next part comes in.

auto-update-fail

This is the error you will receive if your site is properly locked down, when you try to update a plugin or theme.

If you add the below lines to your wp-config.php file, it will tell WordPress to ignore checking it’s files to see if they are modifiable, and to just try updating the plugin or theme. This will allow you to update a plugin or theme by using the auto update method build directly into WordPress.

Add the following to your wp-config.php file, just above the /* That's all, stop editing! Happy blogging. */ line.

// Allow auto updating
define('FS_METHOD', 'direct');

This change will not allow you to update WordPress Core as the webserver still dose not have access to modify the core files. I suggest you update via SVN or GIT on a nightly bases to ensure you always have the latest version of WordPress running.

Nginx Status Graphs

Below is a simple script to build RRDtool. This script will only update the rrd databases and create graphs, but it will not the actually html site.

This script expect to be put in your systems cron config set for once a minute.

*/01 * * * * perl /var/www/status.example.com/rrd_nginx.pl
#!/usr/bin/perl
use RRDs;
use LWP::UserAgent;
 
# define location of rrdtool databases
my $rrd = '/var/www/status.example.com/rrd';
# define location of images
my $img = '/var/www/status.example.com/images';
# define your nginx stats URL
my $URL = "http://127.0.0.1/nginx_status";
 
my $HOST = "milly";
 
my $ua = LWP::UserAgent->new(timeout => 30);
my $response = $ua->request(HTTP::Request->new('GET', $URL));
 
my $requests = 0;
my $total =  0;
my $reading = 0;
my $writing = 0;
my $waiting = 0;
 
foreach (split(/\n/, $response->content)) {
  $total = $1 if (/^Active connections:\s+(\d+)/);
  if ( /^Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/ ) {
    $reading = $1;
    $writing = $2;
    $waiting = $3;
  }
  $requests = $3 if (/^\s+(\d+)\s+(\d+)\s+(\d+)/);
}
 
# if rrdtool database doesn't exist, create it
if (! -e "$rrd/nginx-$HOST.rrd") {
  RRDs::create "$rrd/nginx-$HOST.rrd",
    "-s 60",
  "DS:requests:COUNTER:90:0:60000",
	"DS:total:ABSOLUTE:90:0:10000",
	"DS:reading:ABSOLUTE:90:0:10000",
	"DS:writing:ABSOLUTE:90:0:10000",
	"DS:waiting:ABSOLUTE:90:0:10000",
	"RRA:AVERAGE:0.5:1:1440",
	"RRA:AVERAGE:0.5:5:288",
	"RRA:AVERAGE:0.5:30:672",
	"RRA:AVERAGE:0.5:120:732",
	"RRA:AVERAGE:0.5:720:1460";
}
 
# insert values into rrd database
RRDs::update "$rrd/nginx-$HOST.rrd",
  "-t", "requests:total:reading:writing:waiting",
  "N:$requests:$total:$reading:$writing:$waiting";
 
# Generate graphs
CreateGraphs("hour");
CreateGraphs("day");
CreateGraphs("week");
CreateGraphs("month");
CreateGraphs("year");
 
#------------------------------------------------------------------------------
sub CreateGraphs($){
  my $period = shift;
  
  RRDs::graph "$img/requests-$HOST-$period.png",
		"-s -1$period",
		"-t HTTP requests on nginx server $HOST.mattrude.com for the last $period",
		"--lazy",
		"-h", "150", "-w", "700",
		"-l 0",
		"-a", "PNG",
		"-v requests/minute",
		"DEF:requests=$rrd/nginx-$HOST.rrd:requests:AVERAGE",
        "CDEF:request=requests,60,*",
		"LINE2:request#336600:Requests",
        "GPRINT:request:LAST:   Current\\: %5.1lf %s",
        "GPRINT:request:MIN:  Min\\: %5.1lf %s",
        "GPRINT:request:AVERAGE: Avg\\: %5.1lf %s",
        "GPRINT:request:MAX:  Max\\: %5.1lf %s\\n",
 
		"HRULE:0#000000";
  if ($ERROR = RRDs::error) { 
    print "$0: unable to generate $period graph: $ERROR\n"; 
  }
 
  RRDs::graph "$img/connections-$HOST-$period.png",
		"-s -1$period",
		"-t HTTP requests on nginx server $HOST.mattrude.com for the last $period",
		"--lazy",
		"-h", "150", "-w", "700",
		"-l 0",
		"-a", "PNG",
		"-v requests/minute",
		"DEF:total=$rrd/nginx-$HOST.rrd:total:AVERAGE",
		"DEF:reading=$rrd/nginx-$HOST.rrd:reading:AVERAGE",
		"DEF:writing=$rrd/nginx-$HOST.rrd:writing:AVERAGE",
		"DEF:waiting=$rrd/nginx-$HOST.rrd:waiting:AVERAGE",
        "CDEF:totals=total,60,*",
        "CDEF:readings=reading,60,*",
        "CDEF:writings=writing,60,*",
        "CDEF:waitings=waiting,60,*",
 
		"LINE2:totals#336600:Total",
		"GPRINT:totals:LAST:   Current\\: %5.1lf %S",
		"GPRINT:totals:MIN:  Min\\: %5.1lf %S",
		"GPRINT:totals:AVERAGE: Avg\\: %5.1lf %S",
		"GPRINT:totals:MAX:  Max\\: %5.1lf %S\\n",
		
		"LINE2:readings#0022FF:Reading",
		"GPRINT:readings:LAST: Current\\: %5.1lf %S",
		"GPRINT:readings:MIN:  Min\\: %5.1lf %S",
		"GPRINT:readings:AVERAGE: Avg\\: %5.1lf %S",
		"GPRINT:readings:MAX:  Max\\: %5.1lf %S\\n",
		
		"LINE2:writings#FF0000:Writing",
		"GPRINT:writings:LAST: Current\\: %5.1lf %S",
		"GPRINT:writings:MIN:  Min\\: %5.1lf %S",
		"GPRINT:writings:AVERAGE: Avg\\: %5.1lf %S",
		"GPRINT:writings:MAX:  Max\\: %5.1lf %S\\n",
		
		"LINE2:waitings#00AAAA:Waiting",
		"GPRINT:waitings:LAST: Current\\: %5.1lf %S",
		"GPRINT:waitings:MIN:  Min\\: %5.1lf %S",
		"GPRINT:waitings:AVERAGE: Avg\\: %5.1lf %S",
		"GPRINT:waitings:MAX:  Max\\: %5.1lf %S\\n",
 
		"HRULE:0#000000";
  if ($ERROR = RRDs::error) { 
    print "$0: unable to generate $period graph: $ERROR\n"; 
  }
}

Or via github: https://gist.github.com/4362399

Disable Console Power Save on Ubuntu

By default Ubuntu console turns off after about 15 minutes of no keyboard commands, regardless of what may be, being displayed at the time. Often it is useful to disable this ‘feature’ on production servers to better monitor the activities being sent to the console, such as errors and logs.

There are two methods of doing this, either per session, or for the whole system.

To disable it for only the current session log in as root run the following command:

setterm -powersave off -blank 0

To disable console blacking for all session on this server and to retain this change after reboot, you will need to start by installing the console-tools packet:

apt-get install console-tools

After you have the console-tools packet installed. To stop the screen blanking both the screen saver (BLANK_TIME) and the power management standby (POWERDOWN_TIME) settings need to be disabled. If these two settings are set to zero (0) in the file /etc/console-tools/config the features will be completely disabled.

Alternatively a local settings file called /etc/console-tools/config.d/disable-blank-console can be created containing the following two lines to achieve the same affect.

POWERDOWN_TIME=0
BLANK_TIME=0

Actually you can name the file anything you want so long as the name consists of only upper/lower case letters, numbers, underscores, and hyphens.

Rackspace Cloud Backup on Ubuntu

Rackspace recently released their Cloud Backup to their normal user base.

This service will store your files on Racksapce’s cloud file service.  You will need to install drivecloud app on your server before running your backups.

To install the software, run:

sh -c 'wget -q "http://agentrepo.drivesrvr.com/debian/agentrepo.key" -O- | apt-key add -'
sh -c 'echo "deb [arch=amd64] http://agentrepo.drivesrvr.com/debian/ serveragent main" > /etc/apt/sources.list.d/driveclient.list'
apt-get update; apt-get install driveclient

You will need your username and API key to complete this step. To locate your API key, click the arrow next to your user name (at the top right of the Control Panel) and select API Keys. On the “API Access” page, click the Show Key button. Then, copy it to your clipboard.

Tip: This step will ask you to confirm that you want to overwrite your config file. Answer yes.

driveclient --configure

Start the Agent:

service driveclient start
update-rc.d driveclient defaults

After you have started the agent, go back to the Rackspace Backup Admin page to finish configuring your new setup.

cgit Installed on a Ubuntu Nginx Server

Installing spawn-fcgi

To start out with, we need to install the Nginx and the other dependencies.

apt-get install nginx fcgiwrap git

Ok, here we go, lets start out by creating a place to play with source, I always build in /var/src/ but your welcome to do this anywhere you wish.

/etc/sysconfig/spawn-fcgi:

FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"

Configuring Nginx

server {
    listen 80;
    server_name code.example.com;
    root /var/www/code.example.com;

    fastcgi_param    DOCUMENT_ROOT    /var/www/code.example.com;

    rewrite ^/secure/ https://code.example.com$request_uri permanent;

    location /cgit.css { expires 180d; }
    location /code-logo.png { expires 180d; }
    location /robots.txt { expires 180d; }

    # Serve static files
    location ~* ^.+.(css|png|ico)$ {
        root /var/www/code.example.com;
        expires 30d;
    }

    location / {
        #rewrite ^/([^?/]+/[^?]*)?(?:?(.*))?$ /cgit?url=$1&$2 last;
        include         "fastcgi_params";
        fastcgi_pass    unix:/var/run/spawn-fcgi.socket;
        fastcgi_index   /;
        fastcgi_param   SCRIPT_FILENAME /var/www/code.example.com/cgit.cgi;
        fastcgi_param   PATH_INFO       $uri;
        fastcgi_param   QUERY_STRING    $args;
    }
}

Using the Git Stash command

The git stash command is used to store changes to a dirty git tree when pulling changes[ref]http://man.github.com/git/git-stash.html[/ref].

For example, if you are working on a repository, but are not ready to commit your changes, you may run.

git stash

This will store the changes you have made since the last revision and allow you to start back fresh at the point of your last commit.

Once you are done, you may run

git stash pip

That will restore your repository back to where you were when you ran git stash.

Git: Add all remote branches

Adding each remote branch to a local git repository sometimes can be a pain. IF there are many, you have to repeat your self over and over. Here is a quick, copy and past drop into you console, way to add all the remote branches to your local repository.

for b in `git remote show origin |grep tracked |awk '{print $1}'`
do
    LOCALBRANCH=`git branch |sed 's/* //g' |sed 's/  //g' |grep $b`
    if [ "$LOCALBRANCH" != "$b" ]; then
        git branch -t $b origin/$b
    fi
done

Once your done, you should still be in your original branch were you started. You will still need to update each branch by it self. You may also use something like git-up to update all the branches at once.

WordPress: Add footer text to feed posts

Here’s a small code snippet for adding text to the bottom of each post in your rss feed. This will not affect the post content shown on your site.

The below should be added to your themes function.php file.

/********************************************************************************
* Add Footer to RSS feed
*/

function mdr_postrss($content) {
    if(is_feed()){
        $site_name = get_bloginfo_rss('name');
        $post_title = get_the_title_rss();
        $home_url = home_url('/');
        $post_url = post_permalink();
        $content = $content.'<a href="'.$post_url.'">'.$post_title.'</a> is a post from: 
<a href="'.$home_url.'">'.$site_name.'</a> which is not allowed to be copied on other sites.';
    }
    return $content;
}
add_filter('the_excerpt_rss', 'mdr_postrss');
add_filter('the_content', 'mdr_postrss');

Or may be found on github.