Thoughts on Software and Technology

Server specific banner using RMagic gem

So, I have a complicated Rails project. I have a local server, a remote dev server, a remote staging server, and a production server. Recently, I had a situation where the dev server was shunting traffic to the production server and I didn’t catch it.

What the hell?! Why aren’t my changes showing up?!

So, I made a banner that said “Dev Server.” That was pretty useful, but then I thought it would be nice to have the banner specific for the server.

So I came up with this helper method:

  def dev_banner
    filename = "#{ENV['QSTREAM_SERVER_TYPE']}_server_banner.png"
    banner_path = "public/images/#{filename}"
    server_type = ENV['QSTREAM_SERVER_TYPE']

    unless File.exists? banner_path
      img = Magick::ImageList.new("public/images/dev_banner_background.png")
      img.background_color = "none"
      gc = Magick::Draw.new

      img.annotate(gc, 0,0,15,130, "| #{server_type} Server |") do
        self.pointsize = 13
        self.rotation = -45
        self.kerning = 1
        self.stroke = "#dfdfdf"
        self.fill = "#dfdfdf"
      end

      picture = img.flatten_images
      picture.background_color = "none"
      picture.write(banner_path)
    end
    filename
  end

This method uses the RMagick gem. It takes the dev_banner_background.png file– a banner with a transparent border (see right)– and annotates it with the string “| <name> Server |” where <name> is a string set in a local environment variable, in this case QSTREAM_SERVER_TYPE.1

I have a dev_banner.css file with the following styling:

#right-corner {
    position: fixed; /* Make sure you can align it exactly */
    cursor: pointer; /* Change the cursor on mouse over */
    top: 0px; /* Change to 100px to put it under a 100px banner */
    left: 0px; /* Change to 100px to put it left of a 100px right-side bar */
    z-index: 99999; /* make sure it is the top element always */
}

and then I have an application.rb file with the following:

%head
...
  - if @server_type.present?
    = stylesheet_link_tag 'dev_banner.css'
...
%body
  - if @server_type.present?
    %a#right-corner{:href=>"#", :target=>"_blank"}
      =image_tag dev_banner

It’s a link because I originally thought of making it able to flip over and show server-specific details on a mouse click.

Because I thought I might change the banner based on other localized information, @server_type is a variable set in the ApplicationController:

  ...
  before_filter { @server_type = ENV["QSTREAM_SERVER_TYPE"]}
  ...

This allows me to set an environment variable on any server I’ve loaded, and have a banner specific to that server auto-generated– with nothing showing on the production server.

Given this, it’s easy to see how you could add information to the banner based on the local system. Just a few simple lines of code. Unless it gets a lot more complex, it seemed unnecessary to make it a gem.

  1. It’s totally possible as well to create multiple banners of different colors, and set those in environment variables (or generate multiple banners on the fly), but I didn’t need that much complexity. []

Comments are closed.

Powered by WordPress | Designed by Elegant Themes