Built-in Sudo for Ruby Command-Line Tools
by Jim Myhrberg – tags: , , ,

I was looking through my gists today on GitHub, and decided I'd do a couple of posts on some of the pieces of code I've put up there. The first of which is the sudome Ruby method.

Ever written a command-line tool in Ruby that requires root access for one reason or another? The simplest way to achieve this is to have the end user call the command via sudo. It's not the most elegant solution there is, but it works.

A more elegant solution might be what the Fink Project is doing with their fink command. It doesn't need to be run via sudo, as it calls it within itself. Meaning that when you run fink, you'll be prompted to type your password, just as if you had used sudo. Some might argue that this is not good practice, and they are probably right. But it all depends on the details of what you're doing.

A while back I was working on something which the best solution was to make sure the tool always runs as root. To get identical functionality as Fink, I wrote the very simple method shown below:

def sudome
  if ENV["USER"] != "root"
    exec("sudo #{ENV['_']} #{ARGV.join(' ')}")
  end
end

Simply call sudome as early as possible in your code. If needed it will re-run your script with sudo, requiring the user to type his password, at which point your script then has full root access to the system.

» Original Gist on GitHub

Comments

New Site and Blog Powered by Dr. Jekyll
by Jim Myhrberg – tags: , , , , , , ,

I finally found some time to rebuild my site, and add a blog. I'm also working on a portfolio, which I will probably be putting up on heartb.it. I haven't really decided how I'm gonna make the split between my personal site and work portfolio yet though.

For archival reasons I've made the previous versions of my site available for anybody who might be curious.

On an unrelated note, it happens to be my 24th birthday today, I haven't decided yet if that's a good or a bad thing. But at least I found time to push up my new site today, so I guess that's a good start at least :)

Powered by Dr. Jekyll?

My personal site has always been a very simple site. In the past it's just been a single HTML page which I've coded by hand and uploaded via SFTP. It's a simple process, and decently straight forward. This time however, I wanted to incorporate a blog as well. My first choice was WordPress, as I've used it on my previous blog. But it's overkill for what I need, and keept getting hacked all the fucking time even when I was keeping WordPress decently up to date.

So I'm using Jekyll this time around. Jekyll is a small website framework written in Ruby which generates static HTML files. It was created by one of GitHub's founders, and is used on GitHub Pages. Part of what makes it nice is that it's more intended to be a quick and elegant blogging engine, rather than just a static site generator. It let's you write blog posts in pure HTML, Markdown, or Textile. Meaning I'm writing this post in TextMate, which always puts a smile on my face.

I'll soon write a more in-depth article about Jekyll and how I'm using it.

Comments with Disqus

Since I'm using static HTML files, I'm left with only a few — but awesome — solutions to have a commenting feature on the blog. Both Disqus and Intense Debate have great Javascript-based commenting systems which work for static HTML sites. My favorite of the two is Disqus.

Deployment with Rake+Rsync

I've also opted for a much easier way to deploy to the live server once I'm done with changes locally. Namely, Ruby's Make program, Rake.

I've written a couple of custom rake tasks which run Jekyll to build the static HTML files, and to rsync said HTML files to the remote server. So instead of using a SFTP client, or something like Coda to upload and update the remote site, I simply run rake deploy from a terminal.

I get butterflies in my stomach whenever I think about how neat it is.

Source Code Management with Git

After being an avid user of Subversion for about 5 years, I switched permanently to Git last August when I spent 4 hours reading a PDF I had with me on holiday. So I'm obviously using Git for this site, and the source code is available on GitHub in all it's glory.

Design

I really focused on minimalism, to the point I'm not using a single image, but rather only text on a white background. This is a first for me, as I generally like to have nice rounded corners, or drop shadows, or something, but still simple and elegant looking.

Since the design in highly text-focussed, good typography was a must right from the start. I wanted to stray away from the standard web-safe fonts, to create a truly unique and elegant looking site in terms of it's typography. To do this, I needed to embed fonts, and I used the @font-face technique for it.

The two fonts I'm using are Colaborate for body text, and DejaVu Sans Mono for fixed width text and code examples. I got both from Font Squirrel's excellent @font-face fontkit page which has hundreds of free and ready to use kits.

The End

{insert yo mamma joke here}. Have a nice day.

Comments

archive
rss