Harrington Web

Crude Ruby Script to ask for a Password to use for Private git Repo

Friday, December 19, 2014

The Intro

Good ole Docker. It is a powerful tool to setup your environments whether in production, dev or test. Plowing through and building a new container I discovered I had the need or want to pass an argument to the Dockerfile and let it log me into a private repo.

The Environment Variable. 

Looking through the Dockerfile documentation on
the docker.io website I saw this wonderful looking thing called an environment variable. When I first looked at it I thought that meant that we could pass them in from the host cli running the docker command. I was dead wrong. The environment variable does create a variable but its only for the container's environment. So what does that mean for us. Well if you think about the reason for Docker is for an unattended setup of an environment. Now if I were going to do this completely automated then I would probably create a ssh key and copy it over using Dockerfile. This is not what I wanted. I really wanted a seperation where I didn't store my password or ssh key within the environment. Below is my crude way of getting around this. 

The Gemfile 

source 'https://rubygems.org'
gem 'highline'
The Gemfile in this instance is very simple. You really could skip using highline all together, however I thought it would be cool to mask the password as you type.

The Ruby

require 'highline/import'

pass = ask("Enter your Password : "){ |q| q.echo = "*" }
file = File.open("dock_temp", "r")
text = file.read()
file.close
out = File.open("Dockerfile", "w")
out.puts text.gsub("$PASS", pass)
out.close

`docker build -t yourtag .`
`rm Dockerfile`

The Ruby script will ask you for a password. Once input it will open the "dock_temp" file within the directory. Then we do a simple replacement of the $PASS variable to be the actual password that was input by the user. Once that is complete we write it to a file called Dockerfile.
The Docker file is the file used when you run "docker build". The Ruby script then makes a system call and it will build your environment. Lastly we want to get rid of the password just laying around so we delete the Dockerfile

The Temp Dockerfile

FROM ubuntu:14.10
RUN apt-get update
RUN apt-get install -y git

RUN git clone https://username:[email protected]/username/your_repo.git
WORKDIR /your_repo
The Dockerfile template is very simple. It grabs a copy of ubuntu, updates the repo, installs git. At this point all you have to do is run the git clone and you have you code
Voila!!!

Using Fack to find files by partial name.

Tuesday, November 11, 2014

Install

$ gem install fack

To search for “green”

$ fack green
You can find the source to fack on GitHub

Speedometer

Thursday, September 11, 2014



Ukulele Chords

Tuesday, August 26, 2014


  • Select your Chord and Match your fingers to the diagram.



Sending and email using Telnet

Monday, August 18, 2014
Open up Telnet
telnet smtp.yourdomain.com 25

If you need SSL do this instead
openssl s_client -connect smtp.yourdomain.com:465

Tell the server where you are coming from.
ehlo localhost

Login to the server. You will need your username and password converted to Base 64.



auth login
334 VXNlcm5hbWU6
username
334 UGFzc3dvcmQ6
password

Now compose your email.
mail from: <[email protected]>
rcpt to: <[email protected]>
data
Type in your email 
.

God is bigger than the Boogie Man - Guitar Chords

Wednesday, July 23, 2014

This song is called "God is Bigger than the Boogie Man" which came from Veggietales and is quite the hit for the preschoolers at church. There are some more words that are said when played in the movie however for the kids at church this chorus works wonders.

GGod is bigger than Dthe boogie man

AHe is bigger than Godzilla

DOr the monsters on TV

GOh God is bigger than Dthe boogie man

AHe is watching out for Gyou and Dme.

Jesus Loves Me - Chords

G
Jesus loves me this I know
C             G
For the Bible tells me so
G
Little ones to him belong
C            G
For they are weak but 
D     G
He is strong.

G         C
Yes Jesus loves me
G         D
Yes Jesus loves me
G         C
Yes Jesus loves me
G             D        G
For the Bible tells me so

Robot moving across a NxM matrix.

Thursday, June 5, 2014

Lets say I have a matrix that is a 2 dimensional array of NxM elements and the Robot needs to find all the ways from the top left corner all the way to the bottom right corner. Now the Robot it self has some rules to follow on the way. The Robot can move up, down, left, right in order to get there. However the Robot may not move over the same square more than once.

def robot(matrix,n ,m)
  return 0 if n<0 || m<0 || n>matrix.length-1 || m>matrix.length-1
  return 0 if matrix[n][m] == 1
  return 1 if n==m && n==0
  count=0
  tmp=matrix
  tmp[n][m]=1
  count+=robot(tmp, n+1, m) #right
  count+=robot(tmp, n-1, m) #left
  count+=robot(tmp, n, m+1) #up
  count+=robot(tmp, n, m-1) #down
  tmp[n][m]=0
  return count
end

Now lets say that you have the same size matrix but the Robot can only move down and left.

def robot(matrix,n ,m)
  return 0 if n<0 || m<0 || n>matrix.length-1 || m>matrix.length-1
  return 0 if matrix[n][m] == 1
  return 1 if n==m && n==0
  count=0
  tmp=matrix
  tmp[n][m]=1
  count+=robot(tmp, n-1, m) #left
  count+=robot(tmp, n, m-1) #down
  tmp[n][m]=0
  return count
end

JRuby Image Comparison - Part 2

Sunday, April 6, 2014

In continuation to find ways of comparing if images are equal I decided to try comparing them as byte arrays.

First we need to declare some library's. We will be using JRuby for these examples. If you want to use Java this will look simular.

require 'java'

java_import 'java.awt.image.BufferedImage'
java_import 'javax.imageio.ImageIO'
java_import 'java.io.ByteArrayOutputStream'

Now we need to make a function that will allow us to open an image as a bufferedImage, and a function to convert image to a byte array.

def get_image(filename)
  file = java::io::File.new(filename)
  return ImageIO::read(file)
end

def byte_array(image)
  os = ByteArrayOutputStream.new
  ImageIO::write(image, "png", os)
  return os.toByteArray()
end

Lastly we need a way to compare the 2 arrays to see if they are equal

def compare(img1, img2)
  java::util::Arrays.equals(b1, b2)
end

Want to download the whole script checkout Github

JRuby Image Comparison

Friday, April 4, 2014

Working on a project to validate some images and I thought this would be a good opportunity to show a simple way to compare 2 images to see if they are equal.

First we need to declare some library's. We will be using JRuby for these examples. If you want to use Java this will look simular.

require 'java'

java_import 'java.awt.event.InputEvent'
java_import 'java.awt.image.BufferedImage'
java_import 'javax.imageio.ImageIO'

Now we need to make a function that will allow us to open an image as a bufferedImage

def get_image(filename)
  file = java::io::File.new(filename)
  return ImageIO::read(file)
end

Lets start figuring out if the images are the same. A good place to start is to define a function that with tell us whether the width and height are the same. If they are equal then the images could be the same.

def compare_size(img1, img2)
  if (img1.getWidth == img2.getWidth && img1.getHeight == img2.getHeight)
    return true
  else
    return false
  end
end

Now we are cooking with grease. The compare_size function above will get the width and height of both images and compare them. If they are equal the function will return a true value. Now that we know that both the images are possibly the same size we need to validate if the images are identical. With buffered images this is not as simple as the example below.

if (img1 == img2)
  puts "Equal"
else
  puts "Not Equal"
end

Nice try!! This don't work. For a simple mundane way of checking images we need to comapre them pixel by pixel with the RGB value.

def compare_pixels(img1, img2)
  (0..img1.getWidth-1).each do |x|
    (0..img1.getHeight-1).each do |y|
      if(img1.getRGB(x, y) != img2.getRGB(x, y))
          return false
      end
    end
  end
  return true
end

Now you have a function to fully check if 2 images are the same pixel by pixel. Please note that if the images have different color profiles or use alpha color this function could still fail. We'll save that lesson for another time.

Lets string these in order to finish the simple program up.

img1 = get_image('example1.png')
img2 = get_image('example2.png')

if (compare_size(img1, img2))
  if (compare_pixels(img1, img2))
    puts "Images are Equal"
  else
    puts "Images are Different"
  end
else
  puts "Images are Not Same Size"
end

Want to download the whole script checkout Github

Setup Sinatra Application on Hostmonster.

Monday, January 27, 2014

Step 1 - Environment Setup

Make sure that you have ruby installed on environment, and have SSH access. To your account.

Step 2 - Create Ruby on Rails Environment

  1. Login to your Hostmonster C-Panel.
  2. Click Ruby on Rails icon.
  3. Fill out form with your information

Step 3 - Setup Directory Link

SSH into your account and goto where you would like the base of your app to be in. For example if you have website www.example.com and you want it hosted at www.example.com/myApp

ln -s ~/rails_apps/myApp ~/public_html/myApp

Step 4 - Clean up automatic Rails files.

rm -Rf ~/rails_apps/myApp/*

Once you have cleaned up the directory copy your sinatra files to ~/rails_apps/myApp

If you have a Gemfile this would also be a good time to run a "bundle install" while you are in the directory. If you don't get your gems loaded you will experience problems.

Step 5 - Setup your .htaccess file

Open up your .htaccess file for the myApp directory and make it look like this. Make sure that you replace the sections that say "yourAccount" and "myApp" with the appropriate names for your domain and application.

<IfModule mod_passenger.c>
  Options -MultiViews
  Allow from all
  PassengerResolveSymlinksInDocumentRoot on
  #Set this to whatever environment you'll be running in
  RailsEnv production
  RackBaseURI /
  PassengerAppRoot /home1/yourAccount/rails_apps/myApp
  SetEnv GEM_HOME /home1/yourAccount/ruby/gems
</IfModule>

Step 6 - Restart your app

touch ~/rails_apps/myApp/tmp/restart.txt
Remember this little trick to restart the app. This allows you to restart the Sinatra application without having to restart apache.

Set Git to stop tracking file

Thursday, January 23, 2014

I have found the need to ignore a file with out using the .gitignore. In my case its because of a chrome driver that we using for Cucumber and Watir. The office has a standard setup with a certain repository, however on my Mac chrome likes to update. This causes an issue with versioning of the driver. So I wanted to put a new chromedriver in my repository folder but not ignore it from the repository. To do this little trick I can update the index and tell it to assume the file is unchanged.

git update-index --assume-unchanged <file>

If you have the need to reverse the tracking of the file.

git update-index --no-assume-unchanged <file>

Git Autocomplete

Monday, January 13, 2014

Step 1

Run this command to get the .git-completion.bash file downloaded to your home directory.

curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

Step 2

Add the following code to your .bash_profile which is located in the root of your home directory.

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

Now when you open up your shell you will be able to use the TAB key for your Git commands.

What Isaiah might say.

Monday, January 6, 2014
During my daily bible study I came upon Isaiah 1:1-20 and thought wow this sounds really familiar. I thought to myself... I wonder what it would sound like if Isaiah the prophet lived in the United States and saw all the things that are happening today. What would he say. This is my take. Maybe this could inspire you to take a crack at it on your own.
Ladies and Gentlemen of the United States of America please take this to your heart. The sons and daughters of current and future generations are revolting against the Lord God Most High. We have lost the common knowledge of whom we serve, and placed and idol of money, and property in His glorious place. We are the richest sinful nation in the world, and are weighed down by the luxurious appeal of corruption and iniquity of others. We have abandoned the Lord, have no regret for the sin in our hearts and have completely turned away from him. When will this rebellion end? When will we see that the pain and suffering of bad health, depression, social decay and lack of compassion is wrong. From one end of the universe to the other there is not one good thing that can come from the rebellion against God. Our land is becoming a wasted land being devoured by strangers that our founding fathers were against. Thankfully God has left good people in our land or it would be completely destroyed like Sodom and Gomorrah of old. It is time for us all to learn to do good, seek justice, and remove the ruthless malice in our hearts. Let us defend the helpless and lift up our fellow believers and non believers. Let us show compassion, and that we can make mistakes, yet admit that we made the mistakes and repent for them as sins. Let us reason together with the lord and say “Though your sins are as scarlet, they will be white as snow; Though they are red like crimson, they will be like wool”. If we consent and obey we may have the best of the land. However if we refuse we will loose it all.