ActiveBlog

ownCloud in the App Store: How to Stackato-ize a PHP cloud app
by Troy Topnik

Troy Topnik, May 18, 2012
ownCloud on Stackato

Don't get me wrong, I love Dropbox. They offer free storage, handy cross-platform sync clients, give out nice t-shirts at conferences, and have delivered a much needed service with a great user experience.

But a little while ago a friend of mine posted a plea to her Facebook feed which went something like this:

OK techie friends - Apparently I can't have Dropbox installed on my work computer [at a hospital in Canada] because of security concerns that the data are stored on US servers. How are the rest of you dealing with this? Is there other similar software out there without this problem? I need a way to access files from multiple computers, and to share some files easily with coworkers. Argh.
Since I've been working on Stackato, and immersed in data governance and ownership issues for the last several months, I duly responded with:
...the hospital is right: potentially confidential patient/research information does not belong on servers within the reach of the USA Patriot Act, where it can be accessed by authorities without notification to the data owner(s). The hospital IT department should set up a self-hosted solution.

When I posted the link, I noticed that one of the solutions listed was a name I had seen before at cloud computing conferences. ownCloud is "a flexible, open source file sync and share solution" - essentially a Dropbox you can run yourself. The software, and the company behind it, aim to address the same data privacy issues at the application level that Stackato does at the platform level. If you have concerns about data security and ownership, or are legally mandated to keep your data private and secure, you should not farm it out to a third party in the cloud.

This looked like a perfect match to me, so I thought I'd try running ownCloud on Stackato.

It was incredibly easy to do. In fact, we had it in the App Store within a few hours, and it makes a perfect "how to" on getting a PHP application running on Stackato and making it available the Stackato App Store.

Running ownCloud on Stackato

Right now, you can install ownCloud on a Stackato VM or on our Sandbox with a couple of clicks, but we'll start from scratch so we can see how it all works together.

Assuming you already have access to a Stackato PaaS or VM, we'll start with getting the ownCloud code. I started with the latest stable release available on the ownCloud Install page.

ownCloud Web Interface

Using Stackato, you can skip a lot of these instructions. In fact, you could just do a 'stackato push ...' from the top directory of the extracted files, answer the prompts, and it would (sort of) work. However, it would end up using an SQLite database and filesystem which is local to the application instance and ephemeral.

We don't want that. We want something that will scale, so let's "Stackato-ize" it.

Setting up the MySQL data service

Stackato can provide a MySQL (or Postgres, or MongoDB, or Redis) database service for any application you push. We need to give the application access to the credentials for that database. You'll find a file called config.sample.php in the config directory which shows the options you can set. After a bit of searching in the ownCloud mailing lists, I discovered you can use an autoconfig.php instead to set these options in a "headless" install.

Borrowing bits of code from the Wordpress and Drupal samples, I came up with something like this:

  <?php
  $services = getenv("VCAP_SERVICES");
  $services_json = json_decode($services,true);
  $mysql_config = $services_json["mysql-5.1"][0]["credentials"];
  
  $AUTOCONFIG = array(
  "installed" => false,
  "adminlogin" => "admin",
  "adminpass" => "changeme",
  "directory" => "/app/app/data",
  "dbtype" => "mysql",
  "dbname" => $mysql_config["name"],
  "dbuser" => $mysql_config["user"],
  "dbpass" => $mysql_config["password"],
  "dbhost" => $mysql_config["hostname"],
  "dbtableprefix" => "oc_"
  );
  ?>

We parse the VCAP_SERVICES environment variable (a JSON object) for the credentials we need to set up and use the database. The database service will be created when we push the app, and VCAP_SERVICES will be available to the application when it runs. For the moment, I've hard-coded the admin username and password.

The "directory" option specifies where all of the user files will be stored. We need to set this up to use Stackato's persistent filesystem so that multiple application instances all store the files in the same place.

Sharing a file system

Let's look at the other important config file for this app: stackato.yml. We could set up the services interactively during 'stackato push ...' but this config file makes the process repeatable. It's also the only place we can set an important "pre-running" hook:

name: owncloud
framework:
  type: php
  runtime: php
mem: 128M
services:
  owncloud-mysql: mysql
  owncloud-fs: filesystem
/
hooks:
  pre-running:
    - ln -s $STACKATO_FILESYSTEM data

Here we specify the default application name, the framework and runtime, the amount of memory (for each instance), and two services.

In the "hooks" section, we specify commands that will be run before the application is launched. In this case, we're linking the mount point of the filesystem service to a directory in the application tree called 'data'. The full path of this link is '/app/app/data' which is what we specified in autoconfig.php earlier.

With these two files, you can push ownCloud to Stackato and scale up the number of application instances as needed. By default, the filesystem service limits each service to only 100MB and you'll want much more to properly run ownCloud with lots of users. A Stackato administrator (in the micro cloud case, you) can change this default in ~/stackato/etc/vcap/filesystem_gateway.yml.

To the App Store!

That might be all you need, but bear with me, there's a way we can set up Stackato to install ownCloud with a couple of clicks from the Management Console.

The Stackato App Store pulls applications from a public Git repository and deploys them to Stackato using options specified in a JSON descriptor for the application (similar to the stackato.yml file). The App Store uses the same API as the client for deploying applications, but as there's very little interaction (usually just one click to install, another to start). This is covered in more detail in Ingy's App Store post from last week, and in the stackato-app-store documentation. The "Third Party Apps" store file is a good example of the store JSON format and shows a number of applications, including ownCloud.

Bonus points

The autoconfig.php I used for the App Store copy is not quite what I've show above. I hate hard-coding usernames and passwords for the initial admin account, but there's often no other way when you're pushing a pre-configured app from the app store. I thought for ownCloud I could at least take the Stackato system user name or group name in the same way that database credentials are harvested from VCAP_SERVICES. Please excuse my rudimentary PHP. There are probably more elegant ways to do this:

<?php
$services = getenv("VCAP_SERVICES");
$appinfo = getenv("VCAP_APPLICATION");
$services_json = json_decode($services,true);
$appinfo_json = json_decode($appinfo,true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];
if (array_key_exists("users", $appinfo_json))
  $admin = $appinfo_json["users"][0];
else
  $admin = $appinfo_json["group"];

$AUTOCONFIG = array(
"installed" => false,
"adminlogin" => $admin,
"adminpass" => "changeme",
...

Though the password is still hard-coded to "changeme", at least the username is unique to the person or group deploying the application. If you're more paranoid, you could re-use the $mysql_config["password"] string for "adminpass".

This might fall into the category of "Stupid Stackato Tricks" but you might find ways to use different environment variables (which can be set in stackato.yml) elsewhere in your application.

Building a Stackato App Store ecosystem

The current ownCloud sample in the App Store is pulled from a Github repo which just contains a copy of the 3.0.2 stable release, but since the store can use a specific branch and commit for the pull, it could instead be pulled from a "stackato' branch of the canonical ownCloud repo itself.

Doing it this way allows web application authors and maintainers to easily expose a current, stable version of their app to the Stackato App Store. If you'd like us to feature your app in Stackato, send us the details!

Subscribe to ActiveState Blogs by Email

Share this post:

Category: stackato
About the Author: RSS

Troy Topnik is ActiveState's technical writer. After joining ActiveState in 2001 as a "Customer Relationship Representative" (AKA Tech Support), Troy went on to lead the PureMessage Enterprise Support team before moving on to a technical writing role in 2004. His talent for describing software for new users stems from his difficulty understanding things that developers find obvious. He has a Bachelor of Music from the University of Victoria.