OpenShift OnlineでMojolicious::Liteを動かしてみたい!

OpenShift Online
http://www.openshift.com/

OpenShift Onlineは、RedHatが提供するPaaSで、Java,PHP,Python,Ruby,Perl,Node.jsなどのプログラミング言語MySQL,PostgreSQL,MongoDBのデータベースを利用できます。

https://www.openshift.com/products/pricing
Gearと言う名称がインスタンスの単位で、Free Planを選択すると3 Small Gearsまで無料で使えます。
もちろんFree Planはノーサポートですが、無料で使えるのですしマジリスペクトですよね!

ちなみに、1 Small Gearの中身はMemory 512MB、Storage 1GBになっています。

Gearの構築

今回はPerlで環境構築してみたいと思いまーす。

会員登録して、Create Applicationをクリック。
上記に記載したプログラミング環境などのミドルウェアはCartridgeと言う名前で提供されています。
Perl 5.10を選択します。

あとはPublic URLを入力すれば環境が完成です。簡単!
URLはこんな感じになります。
http://ApplicationName–YourID.rhcloud.com

ローカル環境の構築

gem install rhc

管理ツールをgemからインストールします。

rhc setup

インストールが完了したら、続いてセットアップです。

OpenShift Client Tools (RHC) Setup Wizard

This wizard will help you upload your SSH keys, set your application namespace, and check that other programs like Git are properly installed.

Login to openshift.redhat.com:
Password: ********

OpenShift can create and store a token on disk which allows to you to access the server without using your password. The key is stored in your home directory and should
be kept secret. You can delete the key at any time by running 'rhc logout'.
Generate a token now? (yes|no) yes
Generating an authorization token for this client ... lasts about 22 hours

Saving configuration to /Users/otaku/.openshift/express.conf ... done

Your public SSH key must be uploaded to the OpenShift server to access code. Upload now? (yes|no) yes

Since you do not have any keys associated with your OpenShift account, your new key will be uploaded as the 'default' key.

Uploading key 'default' ... done

Checking for git ... found git

Checking common problems .. done

Checking your namespace ... sample

Checking for applications ... found 1

first http://momoclo-sample.rhcloud.com/

You are using 1 of 3 total gears
The following gear sizes are available to you: small

Your client tools are now configured.

ウィザードの指示する通りにログインして、トークンを生成して、公開鍵をアップします。
gitや公開鍵などは先に自分で用意しておいてくださいませ。

これでローカルの環境構築は完了です。

rhc

Getting started:
  setup              Connects to OpenShift and sets up your keys and domain
  create-app         Create an application
  apps               List all your applications
  cartridges         List available cartridges
  add-cartridge      Add a cartridge to your application
  server             Display information about the status of the OpenShift service.
  logout             End the current session

Working with apps:
  tail               Tail the logs of an application
  port-forward       Forward remote ports to the workstation
  threaddump         Trigger a thread dump for JBoss and Ruby apps
  snapshot           Save the current state of your application locally
  git-clone          Clone and configure an application's repository locally

Management commands:
  account            Display details about your OpenShift account
  alias              Add or remove a custom domain name for an app
  app                Commands for creating and managing applications
  authorization      Show the authorization tokens for your account
  cartridge          Manage your application cartridges
  domain             Add or rename the container for your apps
  sshkey             Add and remove keys for Git and SSH

rhcの管理ツール、とても便利ですね。

git clone ssh://foobar@momoclo-sample.rhcloud.com/~/git/momoclo.git/
cd momoclo/

gitでひな形を取得します。

perl/ For not-externally exposed perl code
libs/ Additional libraries
misc/ For not-externally exposed perl code
.openshift/ Location for OpenShift specific files
action_hooks/ See the Action Hooks documentation [1]
markers/ See the Markers section [2]

ディレクトリ構造はこんな感じです。

perl/index.pl

このファイルをいじればいいみたいです。

#!/usr/bin/env perl
use strict;
use warnings;
use Mojolicious::Lite;

# Route with placeholder
get '/' => sub {
  my $self = shift;
  $self->render(text => "Yes! Yes! We are the ももいろクローバー れに かなこ しおり あやか ももか");
};

# Start the Mojolicious command system
app->start;

Mojolicious::Liteのサンプルまんまを動かしてみます。
ローカル環境で表示できることを確認したら、OpenShift Onlineにデプロイしてみましょう。

git add .
git commit -m 'Initial Commit'
git push

Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 570 bytes, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Waiting for stop to finish
remote: --> Working on Mojolicious::Lite
remote: Fetching http://mirror1.ops.rhcloud.com/mirror/perl/CPAN/authors/id/S/SR/SRI/Mojolicious-4.16.tar.gz ... OK
remote: Configuring Mojolicious-4.16 ... OK
remote: Building Mojolicious-4.16 ... OK
remote: Successfully installed Mojolicious-4.16
remote: 1 distribution installed
remote: skipping R/RJ/RJBS/perl-5.18.0.tar.gz
remote: skipping R/RJ/RJBS/perl-5.18.0.tar.gz
master -> master

これで動くはずだったんだけど、Internal Server Error〜(;´Д`)

rhc tail momoclo

管理ツールからログを確認してみると、[error] Can't locate Time/HiRes.pm in @INCと表示されていました。
モジュールが入ってないよってことなんだろうけど、どこからインストールするのが正しいのかよくわからなかったけど、

use Time::HiRes;

単純にソースにモジュールの記述追加して、デプロイしたらモジュールをインストールしてくれるみたいです。

サーバー設定する手間考えたらすごいお手軽だし、ちょっとしたデモプログラムをネット経由で人に見せるためとかに最適ですね!
Herokuでも頑張って設定すればPerl動くみたいですけど、デフォルトで対応してくれているOpenShift Online、なかなかいいのではないでしょうか。まぁ、Herokuのように色んなアドオン使えるようにならないとしんどいですが。