Drupal & shell bookmarklet for updating modules 4 (updated – bookmarklet w/ base code)

*Note: A new post is available here: Drupal & shell bookmarklet for updating modules 5 (updated – mulitple dbs, backup only option)

This is a follow up to the posts:

Here they are.
This first one is bare-bones; you get prompted for every var:
Install Mods Prompt

This one needs to be customized to contain your directory and server data but then you don’t have to answer questions every time:
Install mods mysite
You can use http://subsimple.com/bookmarklets/jsbuilder.htm to edit the bookmarklet and set the defaults before saving it.

Details
I’ve refined the module install bookmarklets again.
I considered abandoning these in favor of the drush component but drush requires php 5.2 and we’re still using 5.1 at this point.
This works pretty well for me. Click it on a Drupal project page, pick the module and get the ssh to install it.
Click it on the admin/reports/updates and get the code to install all the recommended updates

Example:
Click the prompt bookmarklet it on the Views module page. Follow the prompts and you get this:

##ssh myServer
cd /var/www/html/
sudo tar -c --checkpoint site_directory/ | bzip2 > /home/yourName/drupal_backup/site_directory_1-21-2010.tar.bz2;  ##Backupfiles. Use: cd /var/www/html/; bzip2 -dc /home/yourName/drupal_backup/site_directory_1-21-2010.tar.bz2 | tar -x
#enter sudo password
mysqldump -u drupal6-db-user -p drupal6-db > /home/yourName/drupal_backup/site_directory_1-21-2010.sql; ##BackupDB Use to restore: mysql --user=drupal6-db-user -p drupal6-db < /home/yourName/drupal_backup/site_directory_1-21-2010.sql
#enter db password
cd /var/www/html/site_directory/sites/all/modules/
wget http://ftp.drupal.org/files/projects/views-6.x-2.8.tar.gz
gzip -dc views-6.x-2.8.tar.gz | tar -x
rm views-6.x-2.8.tar.gz
ls -al

Breakdown of the example

table tr td {vertical-align:top;}
table tr:hover td {background-color:#9C3; color:white;}
table tr:hover td a {color:white; white-space:nowrap;}
table tr td table tr:hover td {background-color:white; color:#9C3;}
table td td table tr:hover td a {color:blue; white-space:nowrap;
table tr td a {white-space:nowrap;}

Command Explanation
##ssh myServer Connect to your server via ssh. Commented out to avoid ssh-ing while ssh-ing
cd /var/www/html/ Change to the directory below the declared home dir
sudo tar -c –checkpoint site_directory/ | bzip2 > /home/yourName/drupal_backup/site_directory_1-21-2010.tar.bz2;  ##Backupfiles. Use: cd /var/www/html/; bzip2 -dc /home/yourName/drupal_backup/site_directory_1-21-2010.tar.bz2 | tar -x
Command Explanation
This line creates a back up of the site directory.
sudo I need privileges to do this. You may not
tar -c create an arive of the dir
–checkpoint get updates while the tar is being created
site_directory/ the folder to be tar’d
| bzip2 > Compress the archive using bzip2 into the following directory
# comment out the remainder of this line
#Backup files. Use this portion is included for convenience, so you can do a ‘history | grep backup’ and find your backup with the code needed to resurrect it.
cd /var/www/html/; change to the dir needed to restore.
bzip2 -dc -d=Decompress, -c=standard output
/home/yourName/…tar.bz2 the file that was saved previously
|tar -x untar (unzip) the file
#enter sudo password Comment to let you know which password you’re about to be prompted for
mysqldump -u drupal6-db-user -p drupal6-db > /home/yourName/drupal_backup/site_directory_1-21-2010.sql; ##BackupDB Use to restore: mysql –user=drupal6-db-user -p drupal6-db < /home/yourName/drupal_backup/site_directory_1-21-2010.sql
Command Explaination
mysqldump created a back up of a database 
-u drupal6… the database user name 
-p prompt for password
drupal6-db the db name
> /home/yourName….sql; Save the output to this location
# Comment out the remainder of this line 
#BackupDB. Use to restore this portion is included for convenience, so you can do a ‘history | grep backup’ and find your backup with the code needed to resurrect it.
mysql connect to mysql
–user=drupal6-db-user db user name 
-p’ prompt for password
drupal6-db database name
< /home/yourName…sql The sql file you are restoring 
#enter db password Comment to let you know which password you’re about to be prompted for
cd /var/www/html/site_directory/sites/all/modules/ Change to the modules dir.
wget http://ftp.drupal.org/files/projects/views-6.x-2.8.tar.gz Download module via FTP
gzip -dc views-6.x-2.8.tar.gz | tar -x un-gzip and un tar it
rm views-6.x-2.8.tar.gz remove it from the directory
ls -al list the directory coentents with details

What’s New:
Well the vars you might want to customize are just about all you need in the bookmarklet now.
In fact you don’t need those either.
Without them you will be prompted for each variable.
The big change is that the codebase was moved to a central location so it’s easier to keep a bookmarklet for each site you manage with out updating several sets of code. I need to be able to download updates and install modules for 3 different sites right now.
I also combined the jquery collection script and simple javascript single link options. The bookmarklet automatically detects if you’re on admin/reports/updates and in that case, verifies that jQuery is available, then giving you the option to capture all the recommended updates. The default behavior is still to capture one update at a time.
A few small changes:

  • I added some code to ignore ‘project’ links on the module pages. So the first link you choose from is the latest download link.
  • I started including the restore code in a comment that follows the back up code for the directory and mysql
  • The current tar commands work but I’m debating about the best way to ensure that relative directories are being used. So I’m cd ‘ing to the directory and then taring it locally now instead of including the full base path.
    Part of the reason for that is to accommodate restoring a backup into a different folder (on the dev server for example)

Potential changes:

  • More refinements/updates to the tar command
  • A backup/restore only option.
    I currently use something like this to generate the backup/restore code prior to making big changes and would like to have it available to go between the dev and prod servers.
  • jQuery updates?… The jQuery portion should work fine but I need to wait for some recommended updates to some of my modules to be sure

Here’s the bare minimum needed for each bookmarklet:

javascript:
void(z=document.body.appendChild(document.createElement('script')));
void(z.language='javascript');
void(z.type='text/javascript');
void(z.src='http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js');

or minified:

javascript:void(z=document.body.appendChild(document.createElement('script')));void(z.language='javascript');void(z.type='text/javascript');void(z.src='http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js');

Here’s the link:
Install Mods Prompt

To customize a bookmarklet for a specific site:

javascript:
var%20siteDirName='site_directory_name';
var%20siteBasePath='/var/www/html/';
var%20backupBasePath='/home/yourName/drupal_backup/';
var%20dbUser='my_drupal';
var%20dbName='my_drupal_db';
var%20serverName='servername';
/*var%20connect=true;*/
var%20shellLocation=siteBasePath+siteDirName+'/sites/all/modules/';
void(z=document.body.appendChild(document.createElement('script')));
void(z.language='javascript');
void(z.type='text/javascript');
void(z.src='http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js');

Or minified:

javascript:var%20siteDirName='site_directory_name';var%20siteBasePath='/var/www/html/';var%20backupBasePath='/home/yourName/drupal_backup/';var%20dbUser='my_drupal';var%20dbName='my_drupal_db';var%20serverName='servername';/*var%20connect=true;*/var%20shellLocation=siteBasePath+siteDirName+'/sites/all/modules/';void(z=document.body.appendChild(document.createElement('script')));void(z.language='javascript');void(z.type='text/javascript');void(z.src='http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js');

Here’s the link:
Install mods mysite

You can use http://subsimple.com/bookmarklets/jsbuilder.htm to edit the bookmarklet and set the defaults before saving it.

I’ll continue to make updates to the code as long as I use it but you may want to host the code yourself to avoid unexpected customizations.
Here’s the code as it stands today from http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js:

javascript:
/* #########################################################################
install_mods_wget_jquery.js

Usage:

javascript:
void(z=document.body.appendChild(document.createElement('script')));
void(z.language='javascript');
void(z.type='text/javascript');
void(z.src='http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js');

OR include the base vars:

javascript:
var%20siteDirName='site_directory_name';
var%20siteBasePath='/var/www/html/';
var%20backupBasePath='/home/yourName/drupal_backup/';
var%20dbUser='my_drupal';
var%20dbName='my_drupal_db';
var%20serverName='servername';
var%20connect=true;
var%20shellLocation=siteBasePath+siteDirName+'/sites/all/modules/';
void(z=document.body.appendChild(document.createElement('script')));
void(z.language='javascript');
void(z.type='text/javascript');
void(z.src='http://uoregon.edu/~vid/v_js/install_mods_wget_jquery.js');

######################################################################### */
if (window.siteDirName === undefined) siteDirName=prompt('Site directory name','site_directory');
if (window.siteBasePath === undefined) siteBasePath=prompt('Site base path','/var/www/html/');
if (window.backupBasePath === undefined) backupBasePath=prompt('Backup base path','/home/yourName/drupal_backup/');
if (window.dbUser === undefined) dbUser=prompt('DB UserName','drupal6-db-user');
if (window.dbName === undefined) dbName=prompt('DB Name','drupal6-db');
if (window.serverName === undefined) serverName=prompt('Server name','myServer');
if (window.connect === undefined) connect=confirm('Would you like to include the connection data?');
if (window.shellLocation === undefined) shellLocation=prompt('What is the location of your install directory?',siteBasePath+siteDirName+'/sites/all/modules/');
if (window.use_jQuery === undefined) {
		if(window.location.href.search(/admin/reports/updates/i)!==-1){
			use_jQuery=true;
		}else{
			use_jQuery=false;
		}
}
if(use_jQuery){
	if (window.$ !== undefined) {
			use_jQuery=confirm('Would you like to capture all the recommended updates with jQuery?');
	}
}
var l='',q='',r='',date=new Date(), shellText='';
var saveDate=(date.getMonth()+1)+'-'+date.getDate()+'-'+date.getFullYear();
if(use_jQuery){
	with($('table.version-recommended td li.update-download a').each(
		function(){
			q=this.href.toString().split('/').pop();
				if(q!==""&&this.href!=""){
					shellText+="<br />wget "+unescape(this.href);shellText+="<br />gzip -dc "+escape(q)+" | tar -x";shellText+="<br />rm "+escape(q);
				}
			}
		)
	);
	if(shellText===''){alert("There were no files to download");}
} else {
	grab=window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text;
	if(grab!=''){
		lnk=grab.getRangeAt(0).commonAncestorContainer.parentNode;
		switch(lnk.tagName.toLowerCase()){
			case('a'):l=lnk;
			break;
			case('td'):l=lnk.childNodes[0].childNodes[0].childNodes[0].href;
			break;
		}
	}
	if(l==''||!l){
		lnks=document.links;
		for(var i=0;i<lnks.length;i++){
			if(r==&#039;&#039;||r===null){
				if(lnks[i].innerHTML.toLowerCase().search(&#039;download&#039;)!=-1 && lnks[i].href!=="http://drupal.org/project"){
					r=prompt(&#039;Is this the Download Link you want?&#039;,escape(lnks[i].href).split(&#039;/&#039;).pop()+&#039; (&#039;+unescape(lnks[i].href)+&#039;)&#039;);
					if(r!==null){
						l=unescape(lnks[i].href);
					}
				}
			}
		}
	}
	if(l!=&#039;&#039;){
		q=l.toString().split(&#039;/&#039;).pop();
	}
	else{
		q=prompt(&#039;No link was highlighted. What is the name of the file you would like to unzip/install? (fileName.tar.gz)&#039;,&#039;&#039;);
	}


  if(q!==&#039;&#039;){
    if(shellLocation!==null){
      shellText+=&#039;<br />cd '+shellLocation;
    }
    if(l!=''){
      shellText+='<br />wget '+unescape(l);
    }
    shellText+='<br />gzip -dc '+escape(q)+' | tar -x';
    shellText+='<br />rm '+escape(q);


}
if(q==''||q===null){
  alert('Nothing to processes. Quiting.');
}else{
     if(connect){
			/*prepend shellText*/
      shellText='<br />mysqldump -u '+dbUser+' -p '+dbName+' > '+backupBasePath+siteDirName+saveDate+'.sql; ##BackupDB Use to restore: mysql --user='+dbUser+' -p '+dbName+' < &#039;+backupBasePath+siteDirName+&#039;_&#039;+saveDate+&#039;.sql&#039;+shellText;
 			shellText=&#039;<br />sudo tar -c --checkpoint '+siteDirName+'/ | bzip2 > '+backupBasePath+siteDirName+saveDate+'.tar.bz2;  ##Backupfiles. Use cd '+siteBasePath+'; bzip2 -dc '+backupBasePath+siteDirName+saveDate+'.tar.bz2 | tar -x'+shellText;
      shellText='<br />cd '+siteBasePath+shellText;
      shellText='##ssh '+serverName+shellText;
      /*append shellText*/
			shellText+='<br />ls -al';
    }
  }
  (function(){
    if(q!==''){
      if(window.clipboardData){
        window.clipboardData.setData('Text',shellText);
      }
      else{
        wnd=open('','Shell_Command','width=900,height=200,top=0,left=0,scrollbars,resizable');
        with(wnd.document){
          writeln('<pre>');
          writeln(shellText);
          writeln('</pre>');
        }
        wnd.document.close();
      }
    }
  }
  )()
}
Comments
Leave a Comment