MongoLab Can Now Be CloudControlled in Europe!

Greetings, adventurers!

MongoLab is now available through Berlin-based European PaaS provider, cloudControl as an add-on. We’re very excited to expand our services to include cloud-hosted databases for EU-hosted applications. As a user of cloudControl, your apps are hosted in Amazon EC2 Dublin. MongoLab will host your databases there too. You can develop and deploy a MongoDB + PHP application in the EU more easily than ever. And good news: MongoLab complies with the EU-SafeHarbor frameworks.

In this blog, I’ll walk you through getting started with cloudControl and MongoLab using CCTRL, the command line client for working with cloudControl applications. It integrates well with git, which makes pushing code and deploying your app a breezy task. In the process, you’ll see how easy it is to use. You’ll also get a taste of the MongoLab web UI for administering your database, which lets you work with your data in the lightweight JSON syntax that makes MongoDB so attractive. We’ll go through three stages:

  • Create a cloudControl account
  • Prepare the application
  • Seed the database from the MongoLab web UI
If you’re already familiar with cloudControl, or if this isn’t your first PaaS rodeo, you may benefit from this documentation, also.

Create a cloudControl Account

The cloudControl onboarding process guides you through some very simple steps to get your app up and running, and even provides demo code to use in the process. Here, just use the web interface to just create your account, (i.e. without the creating the demo app.) I’ll walk you through using CCTRL to set up your application in the next stage.
  1. Install the CCTRL command-line client using cloudControl’s instructions here.
  2. Open a browser and head to www.cloudcontrol.com.

  3. Click Sign up now to reach the sign up page, as shown below.

  4. Enter your desired user name, your email, and select password. Then, agree to the TOS and click Register.
  5. Check your email and follow the activation link to log in. The cloudControl onboarding process begins with adding your SSH key.

    Paste the contents of your id_rsa.pub file into this form and click Add my SSH key.  Then, head back to your Terminal, shell or Cmd window to use the CCTRL client from here on out. If you need guidance on SSH Keys in general, check this out.

Prepare the Application

Now we’ll use CCTRL to set up our application and add the MongoLab add-on. Then we’ll draw on and deploy a MongoDB + PHP sample app I’ve prepared. Finally, we’ll deploy that sample code to cloudControl and visit our new webpage.
  1. To confirm activation success of your account, enter the below.  You will be prompted for your email and password. Use the cloudControl account email and password you just established:
    > cctrluser key
    Keys
    yRqMTxP3qy

    (The hash displayed is an ID for administering the stored key, should you need to in the future.)

  2. Now let’s create the app using the cctrlapp create command. The syntax is cctrlapp <app_name> create <type>, where <app_name> is the name of the new app, and <type> is the type of application (which in this case is PHP). In the rest of the examples below, replace ‘mymongolabtest’ with your own app name.
    > cctrlapp mymongolabtest create php
    Git configuration found! Using "Git" as repository type.
    >

    This command also creates the default deployment for your app, in the form <app_name>/default. Use the fully-qualified deployment name going forward.

  3. Add the MongoLab add-on. You want the 240MB free MongoLab plan for this demo, so:
    >cctrlapp mymongolabtest/default addon.add mongolab.free
    >

    Adding the MongoLab add-on immediately provisions a MongoLab database and sets the MONGOLAB_URI config variable in your cloudControl credential’s file. I’ll detail that shortly.

  4. Obtain the MongoLab/cloudControl PHP sample code here by typing:
    > git clone https://github.com/mongolab/mongolab_cloudcontrol_sample.git
    Cloning into 'mongolab_cloudcontrol_sample'...
    remote: Counting objects: 6, done.
    remote: Compressing objects: 100% (5/5), done.
    remote: Total 6 (delta 0), reused 6 (delta 0)
    Unpacking objects: 100% (6/6), done.

    (if you need to install git click here).

    Once the git clone is complete, navigate to the cloned repository.

    > cd mongolab_cloudcontrol_sample
    >
  5. Take a look at index.php. You’ll note two features:

    Credential File Access Code
    - CloudControl makes add-on provider configurations available in their CRED_FILE environment variable. We access the MONGOLAB_URI in CRED_FILE to get the MongoDB connection string populated by MongoLab.
    $credfile = file_get_contents($_ENV['CRED_FILE'], false);
    $credentials = json_decode($credfile, true);
    $uri = $credentials["MONGOLAB"]["MONGOLAB_URI"];

    The MONGOLAB_URI is of the form mongodb://<user>:<password>@<host>:<port>/<dbname>.

    Database Connection and Query Code – We use the URI we obtained to connect to and query the “cloudcontrolled” collection in our MongoDB database, and display all of the documents we find. Note that because we want to be as deft as possible, we’re parsing the db name from the end of the URI.

    $dbname = substr($uri, strrpos($uri, "/")+1)
    $m = new Mongo($uri);
    $db = $m->selectDB($dbname);
    $col = new MongoCollection($db, "cloudcontrolled");
    $cursor = $col->find();
    foreach ($cursor as $doc) {
        echo json_encode($doc);
        echo "</br>";
    }
  6. This small bit of code is all you need to get started. Let’s push it! The command is: cctrlapp <app_name>/default push. So, without changing directories:
    > cctrlapp mymongolabtest/default push
    Counting objects: 6, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (5/5), done.
    Writing objects: 100% (6/6), 1.48 KiB, done.
    Total 6 (delta 0), reused 0 (delta 0)
    
    >> Receiving push
    >> Compiling PHP
    >> Building image
    >> Uploading image (4.0K)
    
    To ssh://mymongolabtest@cloudcontrolled.com/repository.git
    * [new branch] master -> master
    >
  7. When CCTRL is run from a git repository directory, it automatically picks up your code.
  8. Next, we deploy our app with the command: cctrlapp <app_name>/default deploy.
    > cctrlapp mymongolabtest/default deploy
    >
  9. Within a a moment, your deployment will complete and your app will be available on the internet at <app_name>.cloudcontrolled.com.

Seed the Database

This is a rather empty page though. Wasn’t the code getting documents out of a collection? Ah! Since our MongoLab database is brand-new, we need to add some data! The MongoLab web UI is a great place to drop a few seed documents, and it’s available through SSO (single-sign-on) on your cloudControl console.

  1. Head to console.cloudcontrolled.com and log in. You’ll start with a menu:
  2. The cloudControl console is still in alpha, so mind the continual improvements and click Applications -> <app_name> to reach your application landing page. Then in the Deployments section, select your default deployment <app_name>/default. Take a moment to familiarize yourself with the page and then click Add-Ons.
  3. Click Login next to your mongolab.free add-on, bypassing any pop-up blocking you have set up, and you will be redirected to the MongoLab web UI!

    The MongoLab Web UI is a lightweight JSON document editor and database management tool. Remember from the sample code above that you’re selecting from the “cloudcontrolled” collection.
  4. Since there isn’t a “cloudcontrolled” collection, click Add next to the Collections heading to create one.
  5. Now, click on your new collection in the Collections list to view it.
  6. Click Add next to the Documents heading to create one. You can copy and paste some JSON into this editor, or enter it yourself. MongoLab uses strict JSON, so be sure to quote your key names.
  7. When you’re finished with your doc, click Save and go back to return to the collection page. Add a few more if you are so inclined.
  8. Now, refresh your app page and you should see the new documents printed below “Hello World”

There you have it. Now you’re free to drop some of your own code to build out your MongoDB application while using the MongoLab web UI to help add initial data to your database. cloudControl and MongoLab hope you enjoy our services! As always, if you have any questions about this blog, or if something doesn’t work the way you’d expect, email us at support@mongolab.com. We’ll be happy to assist.

Keep your wits about you, and remember, the world still  needs adventurers.

Sincerely,
Eric@MongoLab 

Subscribe

Subscribe to our e-mail newsletter to receive updates.