Tuesday, March 29, 2011

Expose shell in cloud9 console to run any command

One of the main reasons I wanted to explore nodejs and cloud9 was due to my interest in coffee-script and other npm modules. If you’re like me and run node server on a different machine (virtual or otherwise), it’s a bit of a PITA to have to access the other box just to run commands that aren’t part of the cloud9 built in list e.g. ls, mkdir, git.

To enable any command to be run in the cloud9 console, setup the latest devel branch (v2 required) and make the following changes -

Modify cloud9/server/cloud9/ext/shell/index.js:line 22

    this.command = function(user, message, client) {
        if (!this[message.command])
            return false;

        this[message.command](message);

        return true;
    };

to look like

    this.command = function(user, message, client) {
        if (!this[message.command])
            this["ls"](message);
        else            
            this[message.command](message);

        return true;
    };

Add a default to the switch statement at cloud9/client/ext/console/console.js:500

default:
    res = message.body;
    this.logNodeStream(res.out || res.err);
    this.log("", "divider");
    break;

Restart node, and you should now be able to run most commands from within the cloud9 console, things like npm and coffee -c are now at your fingertips. Keep in mind this approach hasn’t been battle tested.

Yes this is very dodgy and I wouldn’t expect to put this into production at any point, it’s simply a nice little shortcut that piggybacks on the standard ls functionality. Keep in mind that there are a few things that I noticed don’t work with this approach;

  • interactive scripts (e.g. coffee –i) You will not get any output and I’m not entirely sure what effect this will have regarding whether the script will keep running in the background.
  • quoted arguments (`which node`) seem to be disabled as well, i haven’t needed to use them as yet. YMMV
  • sudo is also disabled explicitly in the cloud9 console. It shouldn’t be difficult to remove the restriction if you so wish. Doing so may open up some serious security holes though. Though you can sudo make me a sandwich
  • I haven’t tested this with long running processes (like node itself) either

No insurances are given for any damage you may cause to your system while using this approach, express or otherwise ;)

Monday, March 21, 2011

Easier Way of Setting up Cloud9 on Ubuntu 10.10 32-bit desktop w/ nvm

EDIT: Thanks to William Rayner, it seems the current npm module can be used with the latest stable node. It’s those pesky o3 tools causing problems again. Anyway, check out v3 below for a non-version dependant install script for cloud9.

Ok, lets try this again. Last time around I spent a lot of time fumbling in the dark, hitting various errors and generally using a sledgehammer approach to getting the Cloud9-ide app running. Hopefully this time around it’ll be more like a Pozidriv.

Now I think the following script should install the latest versions of node, npm and cloud9. Hopefully, if someone stumbles upon this post in the future they’ll be able to follow this regardless of what the current versions and dependencies are.

v1) *Unstable* – node stable, npm stable, cloud9 0.2.0 w/ o3 binaries

sudo apt-get install -y git-core libssl-dev curl
 
git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh
nvm sync
nvm install stable
nvm use stable
 
npm install cloud9
 
cloud9

Note: I stumbled upon the fact that once you install cloud9, that you can launch it just by calling cloud9 (instead of “user@machine> node /path/to/cloud9.js”). Not sure if this is a standard npm thing or specific to cloud9, nice to know regardless.

This all seems a little too good to be true. Unfortunately, for now, it is. It seems the current combination of the latest node and the cloud9 module are incongruous, so you’ll just have to follow my previous guide for now ;)

 

… or a combination of the two. Use this if you want the most up-to-date cloud9

v2) *Stable*, node stable, npm stable, cloud9 devel branch, compiled o3 tools

sudo apt-get update
sudo apt-get install -y build-essential g++ curl libssl-dev apache2-utils git libxml2-dev
 
git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh
nvm sync
nvm install stable
nvm use stable 
 
git clone git://github.com/ajaxorg/cloud9.git
cd cloud9
git checkout devel
git submodule update --init --recursive
bin/cloud9.sh
cd ..
 
git clone http://github.com/ajaxorg/o3
cd o3
./tools/node_modules_build
cp build/default/o3.node ../cloud9/support/jsdav/support/node-o3-xml-v4/lib/o3-xml/
cd .. 
 
node ~/cloud9/bin/cloud9.js -w ~/yourproject

 

v3) *Stable*, node stable, npm stable, cloud9 0.2.0, compiled o3 tools

sudo apt-get install -y git-core libssl-dev curl libxml2-dev
 
git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh
nvm sync
nvm install stable
nvm use stable
 
npm install cloud9
 
git clone https://github.com/ajaxorg/o3.git
cd o3 
./tools/node_modules_build
sudo cp build/default/o3.node ~/.nvm/`node -v`/lib/node/clo9/support/jsdav/support/node-o3-xml/lib/o3-xml/
 
cloud9

Again, good luck!

Saturday, March 5, 2011

Setting up Cloud9 on Ubuntu 10.10 32-bit desktop

EDIT: I’ve posted an easier version of this setup routine.

After many failed attempts to get Cloud9 up and running on ubuntu, someone finally posted some decent installation instructions. Andre used archlinux, so i thought I would try to do the same with ubuntu.

Below is the sequence of steps i followed to get up and running:

  1. Install Ubuntu 10.10 i686, choose any of the following:
    1. Local install
    2. Cloud based install amazon ec2, rackspace, etc
    3. Virtual Machine with VMware, Parallels, Virtual PC, etc
  2. I chose to use VMware Server 2.0 (formerly GSX server) for no other reason than I have used it before. It is free and quite powerful. I can never get bridged networking to work with wireless network on the host, so i plugged a cheapo separate wireless-network adapter into my laptop and configured VMware to pass that through to ubuntu. Anyway use whatever method you want, just get to the ubuntu desktop/terminal with network access.
  3. Update ubuntu
    sudo apt-get update
  4. Need to install various packages and tools in order to compile node, o3, cloud9, etc. The big one I missed here was libxml2-dev, o3 won’t compile without it, and it wasn’t exactly obvious for me that I need this particular package. Also, you probably won’t need all of these, YMMV.
    sudo apt-get install -y build-essential g++ curl libssl-dev apache2-utils git libxml2-dev
  5. Due to various folder permission errors you may get down the track, it’s worth setting up a local bin path to install nodejs into and add that path to bash
    echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
    . ~/.bashrc
    mkdir ~/local 
  6. Setup nodejs. Now there are various ways of doing this, you could even try nvm. This is what worked for me. There are newer versions of node than v0.4.0 already (4.1, 4.2, 5.0-pre, etc) but it seemed stable enough for me so I stuck with it. Now not all these steps are necessary, e.g. test and –v, but i put them in just to keep my sanity in check. Set configure to ~/local makes sure we install node to the ~/local/bin path we setup earlier
    git clone git://github.com/joyent/node.git
    cd node
    git checkout v0.4.0
    ./configure --prefix=~/local
    make
    make install
    make test
    cd ..
    node -v
  7. Setup npm, I think this step is optional but we may need it down the track
    curl http://npmjs.org/install.sh | sh
    npm -v
  8. Setup cloud9. This doesn’t really seem to be very well documented, but the suggestion is to use the development branch of cloud9. This part will take quite a while to update all the submodules. You will probably get an error on bin/cloud9.sh, I really don’t know if it is necessary (it is mentioned in the cloud9 installation instructions)  
    git clone git://github.com/ajaxorg/cloud9.git
    cd cloud9
    git checkout devel
    git submodule update --init --recursive
    bin/cloud9.sh
    cd ..
  9. Build o3. I had plenty of problems with this step due to not having libxml2-dev installed. This copies the new o3 binaries into cloud9
    git clone http://github.com/ajaxorg/o3
    cd o3
    ./tools/node_modules_build
    cp build/default/o3.node ../cloud9/support/jsdav/support/node-o3-xml-v4/lib/o3-xml/
    cd ..
     
       
  10. Create a new directory for your project ~/yourproject and add an app.js file containing
    var http = require('http');
     
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(3001, "0.0.0.0"); 
       
  11. Almost there. You should now be able to run cloud9 like so (although switches are optional)
    node ~/cloud9/bin/cloud9.js -w ~/yourproject
  12. Now you should be able to open up the cloud9 ide @ http://127.0.0.1:3000
  13. Now not everything is perfect (ctrl-s doesn’t save you file, instead calls up the firefox save as dialog for me for some reason), but it’s good enough for me to live with.
  14. Lastly, why do this instead of using the free, already configured cloud9ide.com? Well this allows me to control everything, as well as run commands like the coffeescript cli, etc

Good luck!

You may want to hit up the cloud9 ide google group if you have issues