Spend some time in the weekend to implement a very simple browser based ruby interpreter. It is not a new thing, I used it the very first time I know the name ‘Ruby’.
The interesting reason I want to do it myself is that I can not find one on the internet(maybe I didn’t try hard enough). The second reason is that I want to deploy it to cloudfoundry.com and see the way it allows the application to eval the code inside.
The URL is http://onlineirb.cloudfoundry.com . Try it out by yourself and break it! :D
心血来潮,看了看node.js
今天鬼使神差,在等待服务器启动的时间找了一个node.js的youtube视频看了下:
http://www.youtube.com/watch?v=jo_B4LTHi3I
发现内容非常喜感,里面一个例子写了一个chat server. 大家纷纷连上去 “rm -fr”,哈哈!
这个家伙一直说node.js很牛逼,支持idle并不是sleep. 于是我花了10分钟ruby写了一个一样的聊天室,用telnet登上去就能聊,跟那个例子一样。cpu占用一样是0%, sleep咋了?
require 'socket'
server = TCPServer.new(2000)
sockets = []
while(true)
Thread.start(server.accept) do |client|
sockets << client
while(true)
str = client.gets
sockets.each do |sk|
sk.puts str if sk != client
end
end
end
end
Getting started with JRuby on Rails and Mongodb deployed on Micro Cloud Foundry
About last weekend, I kicked off a tiny project for fun named Buckuper which is the pilot project on Cloud Foundry. Now it is time to talk about the first commit.
It’s a Rails project, based on JRuby, because I think I would use some 3rd java libraries. It’s running on MongoDB which very suitable for this document style data storage for a blog backup system.
I use RVM, so the first thing is to install jruby. The current version is jruby-1.6.7.
rvm install jruby
Install Rails of course:
gem install rails
Create the application, skip the active-record because I am going to use mongoid for mongodb:
rails new backuper --skip-active-record
Add the mongoid gem to the Gemfile:
gem 'mongoid', '2.4.8'
Now the gmes part is done. We can use:
bundle install
to install them.
Now it’s time to config the connection to the mongodb, create a file named mongoid.yml under config/ folder, with the following content. You should notice in production environment, all the config information is from Cloud Foundry’s environment variable VCAP_SERVICES.
development:
host: localhost
database: backuper_dev
test:
host: localhost
database: backuper_test
# set these environment variables on your prod server
production:
host: <%= JSON.parse(ENV['VCAP_SERVICES'])['mongodb-1.8'].first['credentials']['host'] rescue 'localhost' %>
port: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['port'] rescue 27017 %>
database: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['db'] rescue 'backuper' %>
username: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['username'] rescue '' %>
password: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['password'] rescue '' %>
Then I need to create the first controller/model/view which named job, I would like to call each scheduled backup is a job. Now it only has a field called name.
rails g scaffold job name:string
It would create the related test files, but for now I don’t use it.
Also I would like to keep them unchanged to make this first app as simple as possible.
Then Let’s pack it and try out! Install warbler first:
gem install warbler
Then execute:
warble config
It will generate a file named warble.rb under config/ folder. Let’s do something with it.
The first thing is to specify which ruby version you want to use at run-time. I would say Ruby 1.9 for sure, so add one config to warble.rb:
config.webxml.jruby.compat.version = "1.9"
OK. That is pretty much all needed to make it run in CF. But I found it is painful that I could not get any log information in CF even there is something seriously wrong there. I prefer slf4j , so let’s add the required libs.
From the explanation of the warble.rb comments, I need to place jar files under lib/java/:
slf4j-api-1.6.4.jar
slf4j-simple-1.6.4.jar
Then config slf4j logger in web xml from the warble.rb:
config.webxml.jruby.rack.logging = "slf4j"
OK. Time to pack it and deploy! run:
warble
It would generate a file named: backuper.war
Open another terminal, move the war file to somewhere else, switch back to ruby. (vmc does not work well in jruby)
vmc push backuper
You will be prompt to choose what service you’d like to add. Choose mongodb of course:
axu$ vmc push backuper
Would you like to deploy from the current directory? [Yn]:
Application Deployed URL [backuper.arkxu.cloudfoundry.me]:
Detected a Java Web Application, is this correct? [Yn]:
Memory Reservation (64M, 128M, 256M, 512M) [512M]:
Creating Application: OK
Would you like to bind any services to 'backuper'? [yN]: y
The following system services are available
1: mongodb
2: mysql
3: postgresql
4: rabbitmq
5: redis
Please select one you wish to provision: 1
Specify the name of the service [mongodb-7f810]:
Creating Service: OK
Binding Service [mongodb-7f810]: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (229K): OK
Push Status: OK
Staging Application: OK
Starting Application: OK
Now open it in Browser!

Now you can deploy the exact same war to cloudfoundry.com !
Tips:
1. While you want to see what is going on on the server, try:
vmc logs backuper
2. Do not push/update the war file from the rails folder directly. As it would make vmc confused whether to deploy it from source as a normal Rails app or as Java app.
Sample:
You can download the sample war file from here:
http://dl.dropbox.com/u/2484514/backuper.war
Also, if you want to see the source code:
http://dl.dropbox.com/u/2484514/arkxu-backuper-b873925f076e.zip
References:
http://blog.cloudfoundry.com/post/13481011043/deploying-jruby-on-rails-applications-on-cloud-foundry
http://docs.cloudfoundry.com/services/mongodb/ruby-mongodb.html
http://stufftohelpyouout.blogspot.com/2009/06/how-to-get-warbler-to-include-custom.html
Backuper, would be my first app on Cloud Foundry?
Going to start on Cloud Foundry, it is not only required by work, but also my personal interest. The first thing come into my mind is: What application should I build on cloudfondry that is fun and useful? After one minute, Backuper come to my mind.
If you read my previous post (ok, it is in Chinese, I am sorry), I had large amount of the blog posts from different service providers. Back to Ten years ago, it was blogcn.com, then MSN Space, I’d been using my own cloud based CMS for a while, now it is tumblr. Each time you do a move, that is really painful. Sometimes there is no handy migration tool, what you get is a backup file (Thank goodness!). I start to feel unsafe to any online backup tool and services. The reason is there is no any online service I’ve been used can last long enough. Blogcn.com cleans the user data which is about 5 years ago, the main reason is the tech platform changes from their own to wordpress. MSN Space, hm…I don’t want to talk about it, it just disappeared. Even my own CMS service, after several years I need to do data migration to tumblr. Tumblr is not the terminator, I can see there would be a move in future for sure. So I need a online tools that you can make a backup schedule on it and can be fetched anytime and downloaded to local for sure.
Now, first thing first, I need a cloud foundry environment. I remember I’d registered cloud foundry at the very beginning since it was released.
Cloud Foundry provides a micro version that you can download to your own PC and runs in a VM. Just following the guide here: http://docs.cloudfoundry.com/infrastructure/micro/installing-mcf.html
I have VMware Fusion on my iMac, the installation is as simple as reading my post.
- Open the micro.vmx file from VMware
- Start up the VM and follow the wizard to set your password and domain. (You can use cloud foundry’s micro CF dns page to get a token to make the installation even simpler: https://my.cloudfoundry.com/micro/dns , the good thing is you can use xxx.cloudfoundry.me to manage/deploy your applications)
- Install VMC and bind to your own micro CF domain. (VMC is a ruby gem, get a ruby environment if you don’t)
OK. I plan to use jruby instead of ruby because my application may rely on some java libs. Will talk about how to build the first app using JRuby, Rails and mongodb. Stay tuned!
终于放弃了用自己的系统搭建博客
终于决定不用自己做的系统了。虽然有完全的自由度,但是没有精力维护。而且一直想着重写MariCMS做到更通用,发现如果完成的话那就是一个Rails On DB 的 PaaS. 所有的东西都是动态的,比如Route可以在线自由添加,比如Operation, Filter都可以。那又是一个浩大的工程。
大概有1年时间没有好好写博客了, 期间有了社交网络,有了微薄,但是博客作为超级个性化自留地的属性还是不能被替代。所以我还是选择重新打开这个地方,而且没有评论,全都是自己的。
这一年多来我也有很大的变化:结婚了,到了陌生的国家继续奋斗了,刚刚还发现,我开始学会放弃了。
花了两天宝贵的时间,把能找的到的以前的博客都搬了过来。他们分别是
MSN Space: http://www.arkxu.com/tagged/MSN-Space
HTMLess(自己系统里的): http://www.arkxu.com/tagged/HTMLess
其实还有一个更早的博客大概是10年前的,Blogcn, 想找回来发现他们已经把老用户数据删除了, 还在想办法中。
我可能是中国最早写博客,而且坚持写博客,几乎博客都还能保留的那个人。比质量,比访问量不行,那就比耐力吧,呵呵!
就拿这篇作为新博客的开张, HoAh!
Q:WHAT IS YOUR FAVORITE INANIMATE OBJECT?
Well, it’s a good question. For now, Apple product.
San Francisco
I like Path!
I was at Amazon for about six and a half years, and now I’ve been at Google for that long. One thing that struck me immediately about the two companies — an impression that has been reinforced almost daily — is that Amazon does everything wrong, and Google does everything right. Sure, it’s a sweeping generalization, but a surprisingly accurate one. It’s pretty crazy. There are probably a hundred or even two hundred different ways you can compare the two companies, and Google is superior in all but three of them, if I recall correctly. I actually did a spreadsheet at one point but Legal wouldn’t let me show it to anyone, even though recruiting loved it.


