ben robison
when only more words will do
Howto: Put WSDL on Rails
Why do I love Rails so much? Because there is so much to love! Who ever dreamed that creating web services could be so simple? Where other frameworks can consume them very simply, Rails defines simplicity when it comes to creating them. Consuming them is easy as well, but it’s on the creation side that Rails really stands out from the competition. So how do you do it?
First off, let me say that my inexperience in web services means that I’m far from a subject matter expert. I can however follow simple instructions, and I found a beautiful set over at the SOA Ranch. This is a 3 part series, though more are supposedly coming. I hope these articles continue to be published, but I have my doubts as the most recent articles are nearly 6 months old now. The first one I found was part 3 part 2.5 in the series, and you can follow the links back to part 1 & part 2.
So let’s get down to business. I’ll make the assumption that you’ve got a working Rails environment. For Mac users, the latest version of Locomotive has everything you need for creating web services. For everyone else you’ll have to make your Rails environment the old fashioned way, although you can look forward to Rubuntu soon. We’re going to create a very simple web service that accepts a string, changes it a bit and returns a string.
First create yourself a new application
rails WebServiceProvider
Then you’ll want to open your application in your favorite Rails editor. Then we need to generate your web service.
./script/generate web_service Manipulation manipulate
This will create an API file for us to define our web service’s interface. It also creates a controller for us to use, and then a test file for us to use later (we won’t get to scripted testing in this article, although we will test it manually to prove that it works).
Now open up your manipulation_api.rb file in app/apis. You’ll see the following line of code that is the beginnings of our string manipulating web service.
api_method :manipulate
We’ll need to define parameters and data types for our method like so:
api_method :manipulate, :expects => [:string], :returns => [:string]
There are many data types that can be used here, but we’re keeping things easy for now. Now we’ll go look at the ManipulationController at app/controllers/manipulation_controller.rb. Because we followed the standard Rails naming conventions the mapping of the web service to the controller is already done, but if we hadn’t, this mapping can be done explicitly by adding the following to our controller.
web_service_api ManipulationApi
We can also give our web service a different name with the wsdl_service_name directive
wsdl_service_name 'Manipulation'
Notice also that in the controller is where we define all our logic for what happens with the parameters being passed in by our web service consumer. the manipulate method has already been created for us, so we simply need to tell our method to accept a string, perform our logic, and return another string.
Again we’ll keep things simple.
def manipulate(terms)
return "Yeah! " + terms + "!"
end
Now for all intents and purposes, our web service has been coded. We’ll add one more line to our controller that will generate a web front-end for the service so we can go see how it looks on the browser end. Just add the following line to your controller.
web_service_scaffold :invoke
Now let’s turn on the server and see how things look. Back at the terminal type
./script/server
Open your browser and point it to http://localhost:3000/Manipulation/invoke. This shows us the web view of our API. Follow the manipulate link to see the auto-generated web form that will show us what our service looks like. Type your message into Param0 box and click the invoke button to see all your beautiful web service XML. Another important point for those wishing to consume your web service is that Rails is automatically generating your wsdl for you. You can see how this works by checking the routes.rb file in app/config but just know that visiting http://localhost:3000/Manipulation/service.wsdl you can see the WSDL file that others will need to use your web service. You can use something besides service.wsdl by editing your routes.rb file appropriately.
Now this is obviously a simple example, but it illustrates the basic steps and important points. You probably wouldn’t have much use for a web service that adds the word “Yeah” and a few exclamation points to some user-input, but if we were doing this in the context of a real web application, you can start to imagine the possibilities available in your controller if you’ve got a dynamic database-driven application to work with. You can add other methods to your API, define the param and return types there, then define the logic in the controller in the appropriate method. Once you get the basics down, you’re on your way and the sky is the limit. Good luck developing your first piece of the Web 2.0.
Howto: Port forward to Your Virtual Machine
So sometimes I do things just for the fun factor. As mentioned in a previous post, I like having Windows server, but I prefer Linux for web-hosting. I finally found the solution to do both at the same time on one machine. In the real world people have been doing this for years, but it’s a first for me.
After doing all my installing, I then installed VMWare’s now free VMWare Server. I downloaded the Ubuntu Server distro and decided on the 6.10 release dubbed Edgy Eft. It doesn’t have the LTS, but is perfect for my needs because the repositories include everything I need for my Ruby on Rails setup (including Lighttpd).
Then I just had to figure out how to get traffic from my host to my virtual host. I wanted to use Bridged Ethernet and did that originally, but even though my VM grabbed an IP address on my Lan, when trying to access it from another computer, I was told that there was:
No route to host
If you’re smarter than I am, you can probably figure out a way around this, but I was just trying to get it to work. I turned to NAT. Go to VM > Settings > Ethernet in your VMWare Server Console, select NAT, and click OK.
Now boot your VM log in, and run an
ifconfig
to see what your IP Address is. Again in your server console go to Host > Virtual Network Settings. Select the NAT tab. Make sure to move this window so you can clearly read your VM’s IP address. VMNet8 is the default virtual adapter for NAT, so if this is your only VM go ahead and click the Edit button (if you have others, make sure to select the proper adapter for your VM from the dropdown menu, then click edit). Now click the Port Forwarding button and then the Add button. Fill in the host port, the VM ip address, and the VM port. For me this meant forwarding port 80 from the host to port 80 on the VM. Don’t forget a nice description. Click OK 4 times. Now just forward traffic through your router (as needed) and you’re good to go.
My Ubuntu VM is now serving Apache2+MySql+PHP5 to the world. Soon it will be serving Ruby on Rails.
For a graphical tutorial see:
Howto: Secure VNC through SSH Tunneling
So the my web server sits in the baby’s room at my house. It sits in the corner, and the only thing plugged into it is power and network. This is fine for just about everything that I do, but every once in awhile, I have a problem that requires a user interface. VNC to the rescue. Ubuntu comes with Vino, a little VNC Server, pre-installed. You can go to System > Preferences > Remote Desktop to set a password, turn off local user verification, and turn on desktop sharing (as opposed to just viewing).
Then you find your favorite VNC viewer, and type in the network address of your server. This works fine as long as you’re on the local network, but what happens when you’re not on your local network. You could always forward the VNC port through your firewall (port 5900 by default), but VNC is not a secure protocol. Any password typed in would be transmitted in plain-text, and anyone in the world could intercept, and then control your computer, no hack attack needed.
This is where SSH comes to the rescue. SecureSHell (or SSH) creates an encrypted tunnel between two end-points over the network, and gives you a shell (command prompt for you windows folks) to the remote computer. It’s been around for years, it’s secure, and it continues to prove it’s worth as people come up with more and more uses for it. Tunneling is an example of this.
You can set SSH to accept traffic from a certain port on your computer, send it through an encrypted tunnel, and then end up at a certain port once it gets to the other side of the tunnel - SSH Tunneling.
So when I want to get a graphical interface to my server at home in my office, I can simply open my own shell (Windows users can use Putty) and type
ssh -L 5900:localhost:5900 username@remote.server.address
where the first “5900” represents the local port number and the second represents the remote port number.
You are then prompted for a password like any other SSH connection, and then logged in. Then you simply open your favorite VNC Viewer (I use VNCViewer on my Mac, Chicken of the VNC had serious speed issues) and connect to localhost. Your traffic which would normally be destined for port 5900 is forwarded through the tunnel and instead goes to port 5900 at the other end of the tunnel.
You’ve now got secure VNC.
Howto: Start Subversion at Boot on Ubuntu
I don’t know if I’ve extolled the virtues of Ubuntu on this blog yet, but they are many. They are, however, not the topic of this post. Every once in a while, I like to try different operating systems on my server, and at the moment, I’m just coming back to an Ubuntu server after a brief fling with Windows Server 2003.
On the list of things to do after install was to get Ubuntu to start the svnserve daemon at boot. I’ve taken the time to look this up enough times that I figured I’d just add it here. This procedure holds for anything you’d like to do at boot, I’m simply running my svn daemon.
Step 1 - Create your script.
Simply create a new file (I called mine svnserve) and type the command you’d like to run
cd /etc/init.d/ # (thanks Alfonso)
sudo touch svnserve
sudo vi svnserve
svnserve -d -r /usr/local/svn/repository_name
Step 2 - Save the script in the /etc/init.d/ folder
Step 3 - Make the script executable
sudo chmod +x svnserve
Step 4 - Add the script to the boot sequence
sudo update-rc.d svnserve defaults
That’s it. When you’re done you should see some output similar to
Adding system startup for /etc/init.d/svnserve ...
/etc/rc0.d/K20svnserve -> ../init.d/svnserve
/etc/rc1.d/K20svnserve -> ../init.d/svnserve
/etc/rc6.d/K20svnserve -> ../init.d/svnserve
/etc/rc2.d/S20svnserve -> ../init.d/svnserve
/etc/rc3.d/S20svnserve -> ../init.d/svnserve
/etc/rc4.d/S20svnserve -> ../init.d/svnserve
/etc/rc5.d/S20svnserve -> ../init.d/svnserve
Howto: Remote Root Access to MySql
Very quickly, another thing that I typically like to do on my server boxes is allow root access to my Mysql database from remote computers. I don’t forward the port through my router and I use a very secure password (doesn’t everyone?). I don’t want to create a security risk, I just want to connect to the database from other computers around my network - particularly from my laptop.
Again (like most of my instructions) these instructions are for Ubuntu - currently Edgy Eft.
sudo apt-get install mysql-server
Ubuntu installs Mysql at /etc/mysql/ by default. Now we need to set a root password.
mysql -u root
mysql> SET PASSWORD FOR 'ROOT'@'LOCALHOST"
> = PASSWORD('new_password');
Now while we’re still here, we’ll create a new HOST for root and allow root to login from anywhere.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'
> IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> exit
We’re almost done now. We just have to tell Mysql to allow remote logins.
sudo vi /etc/mysql/my.cnf
Now find the line that says
bind-address = 127.0.0.1
and comment it out. That’s all there is to it! Now get your favorite MySql client and start developing.
Subscribe to RSS