cloudpack の 自称 Sensu芸人 の かっぱこと 川原 洋平@inokara)です。

はじめに

Play という Web フレームワークがあることを教えてもらってググると Heroku だったら簡単に動かせるらしいので、まずはどげなもんかを試してみました。


登場人物

Play Framework

  • Play framework
  • Play Framework
  • Scala と Java 言語で書かれたオープンソースの Web アプリケーションフレームワーク
  • MVC アーキテクチャを採用
  • 元々は Java 書かれていたが Scala 言語をサポート、その後はコア部分を Scale で書き直された
  • Ruby on Rails と Django から大きな影響を受けている
  • Webサーバーは Netty

Heroku

  • Heroku
  • 泣く子も黙る PaaS の決定版
  • OS は Debian 又は Ubuntu
  • Ruby や Node.js や Scala 等をサポート
  • データベースは PostgreSQL や MySQL や Couchbase 等をサポート

Heroku の準備

アカウント

ここから。

Heroku Toolbelt

heroku をコマンドラインから利用出来るツールです。
ここから DL して展開してインストールしましょう。


Play Framework

インストール

MacOS X Marvericks に brew で一発。

brew install play

とっても簡単ね。

play コマンドを help してみる

bash-3.2$ play help
       _
 _ __ | | __ _ _  _
| '_ \| |/ ' | || |
|  /|_|\____|\ /
||            |__/

play 2.2.3 built with Scala 2.10.3 (running Java 1.7.0_60), http://www.playframework.com
Welcome to Play 2.2.3!

These commands are available:

license Display licensing informations. new [directory] Create a new Play application in the specified directory. You can also browse the complete documentation at http://www.playframework.com.

ホントに簡単ね。

初めてのアプリケーション

play new ${APP_NAME} でアプリケーションのひな形を生成します。

bash-3.2$ play new test-app
       _
 _ __ | | __ _ _  _
| '_ \| |/ ' | || |
|  /|_|\____|\ /
||            |__/

play 2.2.3 built with Scala 2.10.3 (running Java 1.7.0_60), http://www.playframework.com

The new application will be created in /Users/kappa/git/myrepo/play/test-app

What is the application name? [test-app]
>

Which template do you want to use for this new application?

1             - Create a simple Scala application
  2             - Create a simple Java application

> 2
OK, application test-app is created.

Have fun!

2 を選択して Java アプリケーションのひな形を作成します。

play run でアプリケーションを起動

play run でアプリケーションを起動します。

bash-3.2$ play run
[info] Loading project definition from /path/to/test-app/project
[info] Set current project to test-app (in build file:/path/to/test-app/)
[info] Updating {file:/path/to/test-app/}test-app...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

簡単ですね…。ちなみにアプリケーションはポート 9000 番で Listen しています。

Heroku で Play Framework を動かす: アプリケーション起動


初めての Heroku で Play

次はこのアプリケーションのひな形をあきこのままで(そのままで)Heroku にデプロイしてみましょう。

heroku コマンドを help する

bash-3.2$ heroku help
Usage: heroku COMMAND [--app APP] [command-specific-options]

Primary help topics, type "heroku help TOPIC" for more details:

addons    #  manage addon resources
  apps      #  manage apps (create, destroy)
  auth      #  authentication (login, logout)
  config    #  manage app config vars
  domains   #  manage custom domains
  logs      #  display logs for an app
  ps        #  manage dynos (dynos, workers)
  releases  #  manage app releases
  run       #  run one-off commands (console, rake)
  sharing   #  manage collaborators on an app

Additional topics:

certs        #  manage ssl endpoints for an app
  drains       #  display syslog drains for an app
  features     #  manage optional features
  fork         #  clone an existing app
  git          #  manage git for apps
  help         #  list commands and display help
  keys         #  manage authentication keys
  labs         #  manage optional features
  maintenance  #  manage maintenance mode for an app
  members      #  manage membership in organization accounts
  orgs         #  manage organization accounts
  pg           #  manage heroku-postgresql databases
  pgbackups    #  manage backups of heroku postgresql databases
  plugins      #  manage plugins to the heroku gem
  regions      #  list available regions
  stack        #  manage the stack for an app
  status       #  check status of heroku platform
  twofactor    #
  update       #  update the heroku client
  version      #  display version

アプリケーションを Heroku に登録する

以下、ざっとした流れです。

アプリケーションディレクトリに移動
bash-3.2$ ls
myapp           test-app
bash-3.2$ cd test-app/
bash-3.2$

該当のアプリケーションディレクトリに移動しました。

git init

Heroku のアプリケーションは git を介してデプロイされます。

bash-3.2$ git init
Initialized empty Git repository in /Users/kappa/git/myrepo/play/test-app/.git/
bash-3.2$
アプリケーションを登録

heroku create ${アプリケーション名} を実行して Heroku にアプリケーションを登録します。

bash-3.2$ heroku create xxxxx-test-app
Creating xxxxx-test-app... done, stack is cedar
https://xxxxx-test-app.herokuapp.com/ | git@heroku.com:xxxxx-test-app.git
Git remote heroku added

アプリケーション名は他のアプリケーションとは重複が内容に設定します。

続いて git add して git commit でローカルのリポジトリにアプリケーションを登録します。

bash-3.2$ git add -A .
bash-3.2$ git commit -m "test commit"
[master (root-commit) bcf5064] test commit
 Committer: kappa <kappa@kappa-at-clp.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config --global user.name &quot;Your Name&quot;
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
15 files changed, 229 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 app/controllers/Application.java create mode 100644 app/views/index.scala.html create mode 100644 app/views/main.scala.html create mode 100644 build.sbt create mode 100644 conf/application.conf create mode 100644 conf/routes create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 public/images/favicon.png create mode 100644 public/javascripts/jquery-1.9.0.min.js create mode 100644 public/stylesheets/main.css create mode 100644 test/ApplicationTest.java create mode 100644 test/IntegrationTest.java bash-3.2$
アプリケーションをでぷろーい

git push heroku master を実行して heroku にアプリケーションをでぷろーいします。

bash-3.2$ git push heroku master
Initializing repository, done.
Counting objects: 27, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (27/27), 37.01 KiB | 0 bytes/s, done.
Total 27 (delta 0), reused 0 (delta 0)

-----> Play 2.x - Java app detected
-----> Installing OpenJDK 1.6...done
-----> Downloading SBT...done
-----> Priming Ivy cache (Scala-2.10, Play-2.2)... done
-----> Running: sbt update
       [info] Loading global plugins from /tmp/scala_buildpack_build_dir/.sbt_home/plugins
       [info] Updating {file:/tmp/scala_buildpack_build_dir/.sbt_home/plugins/}global-plugins...
       [info] Resolving org.scala-lang#scala-library;2.10.2 ...
       [info] Resolving org.scala-sbt#sbt;0.13.0 ...
       [info] Resolving org.scala-sbt#main;0.13.0 ...
       [info] Resolving org.scala-sbt#actions;0.13.0 ...
       [info] Resolving org.scala-sbt#classpath;0.13.0 ...
       [info] Resolving org.scala-sbt#launcher-interface;0.13.0 ...
       [info] Resolving org.scala-sbt#interface;0.13.0 ...
       [info] Resolving org.scala-sbt#io;0.13.0 ...
       [info] Resolving org.scala-sbt#control;0.13.0 ...
       [info] Resolving org.scala-lang#scala-compiler;2.10.2 ...
       [info] Resolving org.scala-lang#scala-reflect;2.10.2 ...
       [info] Resolving org.scala-sbt#completion;0.13.0 ...
       [info] Resolving org.scala-sbt#collections;0.13.0 ...
(中略)
       [info] Resolving org.pegdown#pegdown;1.4.0 ...
       [info] Resolving org.parboiled#parboiled-java;1.1.5 ...
       [info] Resolving org.parboiled#parboiled-core;1.1.5 ...
       [info] Resolving org.ow2.asm#asm;4.1 ...
       [info] Resolving org.ow2.asm#asm-tree;4.1 ...
       [info] Resolving org.ow2.asm#asm-analysis;4.1 ...
       [info] Resolving org.ow2.asm#asm-util;4.1 ...
       [info] Resolving commons-io#commons-io;2.4 ...
       [info] Resolving org.scala-lang#scala-compiler;2.10.3 ...
       [info] Resolving org.scala-lang#jline;2.10.3 ...
       [info] Resolving org.fusesource.jansi#jansi;1.4 ...
       [info] downloading http://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-java-jdbc_2.10/2.2.3/play-java-jdbc_2.10-2.2.3.jar ...
       [info]   [SUCCESSFUL ] com.typesafe.play#play-java-jdbc_2.10;2.2.3!play-java-jdbc_2.10.jar (50ms)
       [info] downloading http://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-java-ebean_2.10/2.2.3/play-java-ebean_2.10-2.2.3.jar ...
       [info]   [SUCCESSFUL ] com.typesafe.play#play-java-ebean_2.10;2.2.3!play-java-ebean_2.10.jar (46ms)
       [info] downloading http://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-cache_2.10/2.2.3/play-cache_2.10-2.2.3.jar ...
       [info]   [SUCCESSFUL ] com.typesafe.play#play-cache_2.10;2.2.3!play-cache_2.10.jar (49ms)
(さらに中略)
       [info] downloading http://repo1.maven.org/maven2/org/avaje/ebeanorm/avaje-ebeanorm/3.2.2/avaje-ebeanorm-3.2.2.jar ...
       [info]   [SUCCESSFUL ] org.avaje.ebeanorm#avaje-ebeanorm;3.2.2!avaje-ebeanorm.jar (234ms)
       [info] downloading http://repo1.maven.org/maven2/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar ...
       [info]   [SUCCESSFUL ] org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Final!hibernate-jpa-2.0-api.jar (149ms)
       [info] downloading http://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-test_2.10/2.2.3/play-test_2.10-2.2.3.jar ...
       [info]   [SUCCESSFUL ] com.typesafe.play#play-test_2.10;2.2.3!play-test_2.10.jar (53ms)
       [info] downloading http://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-docs_2.10/2.2.3/play-docs_2.10-2.2.3.jar ...
       [info]   [SUCCESSFUL ] com.typesafe.play#play-docs_2.10;2.2.3!play-docs_2.10.jar (379ms)
       [info] Done updating.
       [success] Total time: 17 s, completed Oct 30, 2014 2:27:54 PM
-----> Running: sbt compile stage
       [info] Loading global plugins from /tmp/scala_buildpack_build_dir/.sbt_home/plugins
       [info] Loading project definition from /tmp/scala_buildpack_build_dir/project
       [info] Set current project to test-app (in build file:/tmp/scala_buildpack_build_dir/)
       [info] Compiling 4 Scala sources and 2 Java sources to /tmp/scala_buildpack_build_dir/target/scala-2.10/classes...
       [info] 'compiler-interface' not yet compiled for Scala 2.10.3. Compiling...
       [info]   Compilation completed in 15.768 s
       [success] Total time: 25 s, completed Oct 30, 2014 2:28:28 PM
       [info] Wrote /tmp/scala_buildpack_build_dir/target/scala-2.10/test-app_2.10-1.0-SNAPSHOT.pom
       [info] Packaging /tmp/scala_buildpack_build_dir/target/scala-2.10/test-app_2.10-1.0-SNAPSHOT.jar ...
       [info] Done packaging.
       [success] Total time: 1 s, completed Oct 30, 2014 2:28:28 PM
-----> Dropping ivy cache from the slug
-----> Dropping compilation artifacts from the slug
-----> Discovering process types
       Procfile declares types           -> (none)
       Default types for Play 2.x - Java -> web

-----> Compressing... done, 193.3MB
-----> Launching... done, v6
       https://xxxxx-test-app.herokuapp.com/ deployed to Heroku

To git@heroku.com:inokappa-test-app.git
 * [new branch]      master -> master

必要なモジュール等が順次導入されていきます。そして、最後に…

https://xxxxx-test-app.herokuapp.com/ deployed to Heroku

と表示されればでぷろーい終了です。

早速 https://xxxxx-test-app.herokuapp.com/ にアクセスしてみましょう。

Heroku で Play Framework を動かす: 動作確認

ペラ一枚のページですが、git に push しただけででぷろーい(公開)が完了しました。


最後に…

ホントに触ってみただけですが、ここまで 15 分、Play が Heroku で Eroku てビックリしました…。

元記事はこちらです。
たった 15 分の Play で Heroku が Eroku て泣けた(簡単に始めることが出来ました)