ikemonn's blog

技術ネタをちょこちょこと

【Node.js】ottoでExpressの環境を作る

きっかけ

ottoでSymfony2を構築してみちゃったを読んで、Expressで試してみた。

ottoのDL

適当なディレクトリにダウンロードしてきて解凍すればOKです。 ~/binをmkdirして置きました。

$ cd ~/bin
$ wget -qO- -O tmp.zip https://dl.bintray.com/mitchellh/otto/otto_0.1.1_darwin_amd64.zip && unzip tmp.zip && rm -rf tmp.zip

Expressでひな形をつくる

$ mkdir otto_sample
$ cd otto_sample
$ express otto -e -s

Appfileの準備をする

アプリケーションの開発とデプロイに必要な設定をAppfileに記述します。

$ cd otto
$ vim Appfile
application {
  name = "otto_sample"
  type = “node"
}

Ottoにはアプリケーションの環境と設定を自動で判別してくれる機能があり、それらのデータをもとにOttoの設定ファイルを作成するらしいです。 そのためのコメンドがotto compileです。

$ otto compile
==> Loading Appfile...
==> Fetching all Appfile dependencies...
==> Compiling...
    Application:    otto_sample (node)
    Project:        otto_sample
    Infrastructure: aws (simple)

    Compiling infra...
    Compiling foundation: consul
==> Compiling main application...
==> Compilation success!
    This means that Otto is now ready to start a development environment,
    deploy this application, build the supporting infastructure, and
    more. See the help for more information.

    Supporting files to enable Otto to manage your application from
    development to deployment have been placed in the output directory.
    These files can be manually inspected to determine what Otto will do.

開発環境の構築

$ otto dev
==> Creating local development environment with Vagrant if it doesn't exist...
    Raw Vagrant output will begin streaming in below. Otto does
    not create this output. It is mirrored directly from Vagrant
    while the development environment is being created.

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'hashicorp/precise64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'hashicorp/precise64'
    default: URL: https://atlas.hashicorp.com/hashicorp/precise64
==> default: Adding box 'hashicorp/precise64' (v1.1.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/hashicorp/boxes/precise64/versions/1.1.0/providers/virtualbox.box


==> Caching SSH credentials from Vagrant...
==> Development environment successfully created!
    IP address: 172.16.1.32

    A development environment has been created for writing a generic Node-based
    application. For this development environment, Node is pre-installed. To
    work on your project, edit files locally on your own machine. The file changes
    will be synced to the development environment.

    When you're ready to build or test your project, run 'otto dev ssh'
    to enter the development environment. You'll be placed directly into the
    working directory where you can run "npm install", "npm run", etc.

    You can access the environment from this machine using the IP address above.
    For example, if your app is running on port 5000, then access it using the
    IP above plus that port.
otto dev  12.77s user 9.26s system 6% cpu 5:20.22 total

Vagrant上でExpressを動かす

$ otto dev ssh 
# vagrant内
$ cd /vagrant/
$ npm install
$ npm start

ブラウザからアクセスしてみる

# Mac上で
$ otto dev address 

otto.png

Vagrantの停止

先ほど立ち上げたvagrantを停止するには

$ otto dev vagrant halt

Dev環境の削除

$ otto dev destroy

雑感

  • Vagrantでnodeの環境を作るより圧倒的に楽だった
  • Nodeはv4.1.0が入ってたので、バージョンを指定する方法を探す

参考

Otto by HashiCorp ottoでSymfony2を構築してみちゃった

広告を非表示にする