Fixing a broken sudoers file without rebooting

Problem

If by accident you make a mistake in your sudoers file then you won’t be able to run sudo command after saving the file.

Solution

Following the answer here, you can simply run the command pkexec visudo, supply your password and provided you already had sudo permissions, fix the sudoers file, make sure that you save it using the Q when prompted, and you should be able to use sudo commands again.

fatal: cannot exec ‘/tmp/…/git-ssh.sh’: Permission denied – Capistrano, Dreamhost, permission denied for git-ssh.sh

Problem

When trying to use the new Capistrano 3.x to set up your rails project in a dreamhost account, you get the following error complaining that the git-ssh.sh script copied to your account by capistrano cannot be executed as the permission is denied:

fatal: cannot exec '/tmp/example.com/git-ssh.sh': Permission denied

Solution

It seems that Dreamhost, and quite possibly other hosting providers are not allowing executables from the /tmp directory, which is where Capistrano places the git-ssh.sh script. So in order to be able to execute the script you can change the directory where the script is copied in the first place and put it in your home directory. You can do that by adding the following to the config/deploy.rb file:

set :tmp_dir, "/home/dh_user_name/tmp"

Creating an alias that migrates both development and test databases in rails project

Problem

In your rails project very often, after creating a new migration, you have to run the migration in the development database and then in the test database.

Solution

A neat way to combine both these steps as one, taken from the book Rails 4 in Action, is to create an alias in your ~/.bashrc configuration file with the following, so that you only have to run migrate after each migration that would apply the migration in both development and test databases:

alias migrate='bin/rake db:migrate && bin/rake db:test:prepare'

Resetting the web-gui dd-wrt password with telnet

Problem

You have forgotten your web-gui credentials to login into your dd-wrt router but you have still telnet access to it.

Solution

Login with telnet to your router and then run the following two commands to reset the web interface:

nvram set http_passwd=
nvram commit

and then go to your Web interface to set a new one.

* Based on this post

rvm “You need to change your terminal emulator preferences to allow login shell.”

Problem

After a new rvm installation in an (k)ubuntu system you get the following error from rvm when trying to switch between rubies:

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.

Solution

As the message suggests you can change the setting in your terminal which is fine if you are using the gnome-terminal by going to the link provided:

Please visit https://rvm.io/integration/gnome-terminal/ for a example.

but if you are using Konsole in KDE you need to go to a different link that explains the change in the settings:

Please visit http://rvm.io/integration/konsole/ for a example.

Integrating BitBucket with PivotalTracker

Problem

You would like to integrate your BitBucket git repository with your Pivotal Tracker issue tracker.

Solution

After creating your bitbucket git repository and pivotal tracker project, do the following.

  • Go to your Pivotal Tracker – Profile and copy your API Token
  • Go to your Bitbucket repository and click on the Administration link (top right corner)
  • From the Repository Details menu on the left select Services
  • From the Service drop down list select Pivotal Tracker
  • Add the Pivotal Tracker API Token that you copied earlier and save

You can now use commits that have a square bracker [#111111] with the pivotal issue ticket and update pivotal when making a commit

Deleting old releases from rails app server using puppet

Problem

You would like to automatically delete old releases from your rails app server using a puppet crontab resource.

Solution

Add the following to your modules/crontab/manifests/init.pp file and modify some of the values to what you need:

 # use // to suppress warning: Warning: Unrecognised escape sequence '\;'
  cron { remove_old_releases:
    command   => "test $(find /var/www/your/path/to/ror/app/ -maxdepth 1 -type d | wc -l) -gt 10 && find /var/www/your/path/to/ror/app/ -maxdepth 1 -type d -mtime +14 -exec rm -rf '{}' \\;",
    user      => ubuntu,
    hour      => 0,
    minute    => 15
  }

You should need to change the following:

/var/www/your/path/to/ror/app/ to your application’s release path
-gt 10 this is the number of minimum old releases you want to keep
-mtime +14 releases older than 14 days are deleted

UPDATE

There is chance that when you do not calculate the frequency of the releases right the above script can potentially delete all of your releases, so you end up with no releases 🙁

A better solution to keep the -n number of releases (based on the solution from here:

find /var/www/your/path/to/ror/app/* -type d -printf '%T@ %p\n' | sort -nr | tail -n+6 | cut -f 2- -d " "  | xargs -i rm -rf {}

and the explanation of each step:

  • find /var/www/your/path/to/ror/app/* -maxdepth 0 -type d -printf ‘%T@ %p\n’ find all the directories (-maxdepth 0 -type d) in the search path (/var/www/your/path/to/ror/app/*) excluding the . and .. directories (/*) and add to them the time information (‘%T %p\n’)
  • sort -nr sort them in numeric and reverse order – newer first, oldest last
  • tail -n+6 keep only the first 5 directories, starting from line 6 (tail -n+6)
  • cut -f 2- -d ” “ Remove the first field (ie the date information) keeping the file from the second field using the space as the delimiter (-d ” “)
  • xargs -i rm -rf {} Pass the rm -rf command to delete the directory for each line produced in the previous steps

So the puppet script should be as follows (including the escape characters \ for the % and the additional \):

 # use // to suppress warning: Warning: Unrecognised escape sequence '\;'
  cron { remove_old_releases:
    command   => "find /var/www/your/path/to/ror/app/* -maxdepth 0 -type d -printf '\\%T@ \\%p\\n' | sort -nr | tail -n+6 | cut -f 2- -d ' ' | xargs -i rm -rf '{}' \\;",
    user      => ubuntu,
    hour      => 0,
    minute    => 15
  }