A place to breathe

Sunday, August 31, 2014

A new kind of animal: VM, Vagrant and Rails

Now, I manage to run a Vagrant provided by Discourse team. Next, I want to serve static page on Rails. 

All I want to do is to serve a static page. And now let's see what kind of mess I am getting into!
 
Once I plunged into Rails development, I realize that it is hard to develop on my local machine. I don't want to mess up my Mac environment with silly Rails gems dependencies, I am willing to invest a bit more time to understand all kind of Ruby tools. For all its worth, you better be good, Ruby on Rails!!!
  
My idea is to install a Virtual box using Vagrant so that I can ssh into a virtual box. Plus, I will run a Ruby environment within the Vagrant and install all the "germs" (yeah, pun intended) into that vagrant (read: it's not a misspelled of Viagra).
Now that's clean.

First, get the Vagrant image. Create a project folder, say "myapp" and make the source code development "into" a virtual box: 

mkdir myapp
cd myapp 
vagrant init precise32 http://files.vagrantup.com/precise32.box 

This will download a Vagrant config file called "Vagrantfile". Now, I want to host my Rails on port 3000 (following convention), so that, I edit the file here:

config.vm.network :forwarded_port, guest: 3000, host: 3000

Now, we're ready to configure the machines. Issue the following command:

vagrant up

Now, what it does is to do some magic of creating a default VM environment for your "myapp" folder so that you can build an isolated environment within that folder. To access that isolated environment, you need to remote login using ssh :

vagrant ssh
cd /vagrant

You now see your "Vagrantfile". Basically your "myapp" folder has been mounted into "/vagrant" folder in the VM. Cool so far? Run everything to get Rails started:

sudo apt-get update
sudo apt-get install curl 
\curl -sSL https://get.rvm.io | bash -s stable --rails 

Try check your rails:

vagrant@precise32:~$ which rails
/home/vagrant/.rvm/gems/ruby-2.1.3/bin/rails

I feel so safe that all these commands live within the Ubuntu VM on my Mac. At least if things screws up, I can just forget about this VM and starts over.

vagrant@precise32:~$ rvm -v
rvm 1.25.29 (stable) by Wayne E. Seguin , Michal Papis [https://rvm.io/]


A note of caution here: make sure you are running rails version from your home's RVM!.

which rvm 
/home/vagrant/.rvm/bin/rvm 

Now, install Rails. Because you need Rails to create projects.

gem install rails
rails new /vagrant 

make sure you have 'therubyracer' and 'execjs' inside your Gemfile.

bundle install 
rails server 


Go to "http://localhost:3000" . See something? Rejoice !!









Saturday, August 23, 2014

Helpful Unix command to check the biggest files in the server

Often, we suddenly realize that we are running out of space in the server. Here's a command that you can use to check the top files that is consuming your disk space:

du . | sort -nr | head -n10

Tuesday, August 12, 2014

Discourse on Docker

The Discourse team is solving the Rails complicated installation using Docker install. But docker itself is not easy to work with ! (at least for now).

I've spend around 3 hours fiddling with Docker to get the right installation ready. I spent another 2 hours demistify SMTP relay in Discourse. Fortunately i manage to sort out the email sending and my install works like breeze.

Part of the reason why is that docker daemon doesn't seem to be stable. And if you pull non-standard docker library, it may not work.

But honestly, i would rather work with docker than messing up my vps install. Kudos to Discourse team!

Now I'm having a blast. 

Monday, August 11, 2014

Fixing ReCom's forum

Recently, I've decided to fix ReCom's forum (www.recom.org) once and for all. I've enough of VB and I don't want to be adding custom codes and all variety of silly markups just to be broken on the next upgrade.

The last (hasty) upgrade that we did, we broke so many functions that the site is really unusable. I am very disappointed and I don't want to spend anymore time on VB. I blame it on myself for trusting VB.

OK enough ranting.

After a few days of searching, I've decided to migrate ReCom's VB forum. I figured that the work is so much that I quit my full time to work on it full time (of course, I have other things to work on as part time to support myself for a few months). In addition, I am turning it into startup for obvious reason that if i don't do it, the Forum will find its death thanks to the VB 4.x upgrade.

We're going to migrate to Discourse.

After a few days reading and trying out, I manage to install Discourse development on the VPS. But the really bad problem that I have is we have so much dependencies on Rails and I really don't like to way I need to install so many packages on the VPS.

Fortunately, the Discourse team is recommending to use Docker. I decided to then just follow their setups and so far so good.

When installing, I realize that I didn't configure "smtp.example.com" to be ReCom's email.
That would be my next move: to configure email



Monday, July 7, 2014

Swift Syntax overview - more switch

Swift has powerful switch statement construct. Switch statement can compare multiple values, range, as well as tuples. The case statement can also do logic comparison. The following code demonstrates its capability

let myCar = "Ford Focus"
let yourCar = "Ford Fiesta"



switch car {

  case “BMW” :

      let myComment = “Need more cash”


   case let x where x.hasSuffix(“Ford”)   // put the content into   temp variable "x" and compare 


      let myComment = “your car now is \(x) ? “    // interpret the var x inside the bracket with slashes


  default:

     myComment = “Any car will do for me as long as it is cheap”

}
 


Swift Syntax overview - Control

Control Structure 

With the exception of the bracket (for whatever reason) when  we try to iterate in for loop or at the if-else statement, the control in Swift can be considered normal: 

let gameScorePerLevel =  [ 20, 25, 30 ]

var totalBonus = 0 

for score in gameScorePerLevel {
    if score > 30 { 
        totalBonus += 5 
    } else if score > 40 {
        totalBonus += 6 
     } 
 } 


Fallthrough in switch statements

By default, Swift doesn't have the "break" statement because at the end of the "case" statement, it will break. But because C-style programming is avoiding "break" to let the cases fallthrough, so the opposite is introduced in Swift with the 'fallthrough" statement:

let currentLevel = 2
let levelScore = 50 
var currentLife : Int = 1

var levelDescription = "Your score for \(currentLevel) is \(levelScore) " 

switch levelScore { 
      case 10,20 : 
         // update description and break out of the switch stmt
          levelDescription +=  "but you did not get any additional life"
      case 30,40,50 : 
         currentLife += 1    
         levelDescription +=  "and you get extra life"
         fallthrough     // fall to the default stmt
      default: 
         currentLife += 1    //extra bonus life
         levelDescription += "and another bonus life"
}




Labeled Statement


Cool things added to swift is you can label your control statement. For example, if you have a simple arcade shootout game that ask you to fire a target ball with limited attempt, you can just simply do the following loop, and

let attempt = 5 
var totalBall = 10

ballGameLoop : while totalBall > 0 { 

   if attempt == 0 { break ballGameLoop }  // we can go out of the label loop here. Game Over.
 
   switch  ballHit {  

       case targetBall : 
              break ballGameLoop     // we win so go out of labelled loop to finish the game
 
    default : 
           --attempt    
           -- totalBall
       }
         
}





Swift Syntax overview - Array and Dictionary

We declare arrays in Swift as follows:

var myJobs = String()[]    //empty array
var myBooks = ["bus ride", "computer", "cars", "road to success"]    //initialize Array with values

Accessing it using array:

var bestSeller = myBooks[1]

Dictionary:

var bestBoss = Dictionary < String,Float > ()     //empty dictionary
var bestEmployee = [
       "Kyle" : "computer engineer",
       "Jason" : "web developer", 
       "Azeez" : "designer",


About Me

I'm currently a software engineer. My specific interest is games and networking. I'm running software company called Nusantara Software.