Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Vagrantの後継ツールOttoで自動判別、自動構築を試す

f:id:tbpg:20150929224057p:plain

Vagrantの後継ツールOttoで自動判別、自動構築を試します。

インストール

  • Ottoのバイナリを ダウンロードする
  • ファイルを解凍してパスが通った場所に配置します。
  • 疎通確認をします
$ otto --version
Otto v0.1.1

サンプル

現在時刻をランダムなWebフォントで表示するアプリケーションを作成します。
フォルダ構成は以下。

$tree
.
├ config.ru
├ Gemfile
├ Gemfile.lock
├ Procfile
└ timer.rb

アプリケーション

  • Gemfile
source 'https://rubygems.org'

gem 'sinatra', '~> 1.4.6'
gem 'thin'
  • timer.rb
require 'sinatra'

module Template
  HTML =<<-EOS
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>Timer</title>
  <link href='https://fonts.googleapis.com/css?family=$font-family$' rel='stylesheet' type='text/css'>
</head>
<body>
  <div style="font-size:64px;font-family: '$font-family$', sans-serif;">$font-family$</div>
  <div style="font-size:64px;font-family: '$font-family$', sans-serif;">$time$</div>
</body>
</html>
  EOS

  WEB_FONTS = %w(Roboto Lato Oswald Slabo Lora Montserrat Raleway Merriweather Arimo Bitter)
end

get '/' do
  html = Template::HTML.gsub('$time$', Time.now.strftime("%Y/%m/%d %H:%M:%S"))
  html.gsub('$font-family$', Template::WEB_FONTS.sample)
end
  • Procfile
web: bundle exec rackup config.ru -p 4567 -s thin -o 0.0.0.0
  • config.ru
require './timer'
run Sinatra::Application

otto compile

アプリケーションの自動判別機能によって、Ruby向けにコンパイルされます。

$ otto compile
==> Loading Appfile...
==> No Appfile found! Detecting project information...
    No Appfile was found. If thereVB is no Appfile, Otto will do its best
    to detect the type of application this is and set reasonable defaults.
    This is a good way to get started with Otto, but over time we recommend
    writing a real Appfile since this will allow more complex customizations,
    the ability to reference dependencies, versioning, and more.
==> Fetching all Appfile dependencies...
==> Compiling...
    Application:    timer (ruby)
    Project:        timer
    Infrastructure: aws (simple)

    Compiling infra...
    Compiling foundation: consul

生成ファイルを確認

$ tree -a
.
├── .DS_Store
├── .otto
│   ├── appfile
│   │   ├── Appfile.compiled
│   │   └── version
│   ├── compiled
│   │   ├── app
│   │   │   ├── build
│   │   │   │   ├── build-ruby.sh
│   │   │   │   └── template.json
│   │   │   ├── deploy
│   │   │   │   └── main.tf
│   │   │   ├── dev
│   │   │   │   └── Vagrantfile
│   │   │   └── foundation-consul
│   │   │       ├── app-build
│   │   │       │   ├── main.sh
│   │   │       │   └── upstart.conf
│   │   │       ├── app-deploy
│   │   │       │   └── main.sh
│   │   │       ├── app-dev
│   │   │       │   ├── main.sh
│   │   │       │   └── upstart.conf
│   │   │       ├── app-dev-dep
│   │   │       │   └── main.sh
│   │   │       └── deploy
│   │   │           ├── main.tf
│   │   │           ├── module-aws
│   │   │           │   ├── join.sh
│   │   │           │   ├── main.tf
│   │   │           │   ├── outputs.tf
│   │   │           │   └── variables.tf
│   │   │           ├── module-aws-simple
│   │   │           │   ├── main.tf
│   │   │           │   ├── outputs.tf
│   │   │           │   ├── setup.sh
│   │   │           │   └── variables.tf
│   │   │           └── variables.tf
│   │   ├── foundation-consul
│   │   │   ├── app-build
│   │   │   │   ├── main.sh
│   │   │   │   └── upstart.conf
│   │   │   ├── app-deploy
│   │   │   │   └── main.sh
│   │   │   ├── app-dev
│   │   │   │   ├── main.sh
│   │   │   │   └── upstart.conf
│   │   │   ├── app-dev-dep
│   │   │   │   └── main.sh
│   │   │   └── deploy
│   │   │       ├── main.tf
│   │   │       ├── module-aws
│   │   │       │   ├── join.sh
│   │   │       │   ├── main.tf
│   │   │       │   ├── outputs.tf
│   │   │       │   └── variables.tf
│   │   │       ├── module-aws-simple
│   │   │       │   ├── main.tf
│   │   │       │   ├── outputs.tf
│   │   │       │   ├── setup.sh
│   │   │       │   └── variables.tf
│   │   │       └── variables.tf
│   │   └── infra-timer
│   │       ├── main.tf
│   │       └── outputs.tf
│   └── data
│       └── dev_ip
├── .ottoid
├── Gemfile
├── Gemfile.lock
├── Procfile
├── config.ru
└── timer.rb

otto dev

ローカル開発環境を起動します

$ otto dev
# 中略
==> Caching SSH credentials from Vagrant...
==> Development environment successfully created!
    IP address: 172.16.1.27
    
    A development environment has been created for writing a generic
    Ruby-based app.
    
    Ruby 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 your project, run 'otto dev ssh' to enter
    the development environment. You'll be placed directly into the working
    directory where you can run 'bundle' and 'ruby' as you normally would.
    
    You can access any running web application using the IP above.

otto dev ssh

VMSSH接続をし、sinatraのWebアプリケーションを起動します。

$ otto dev ssh
==> Executing SSH. This may take a few seconds...
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:/vagrant$ ls
config.ru  Gemfile  Gemfile.lock  Procfile  timer.rb
vagrant@precise64:/vagrant$ bundle
vagrant@precise64:/vagrant$ ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux-gnu]

vagrant@precise64:/vagrant$ rackup --host 0.0.0.0
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on 0.0.0.0:9292, CTRL+C to stop

ブラウザで確認をします。

otto dev時に表示された IP + 9292 ( http://172.16.1.27:9292 ) でプラウザから確認をします。

動作確認1回目

f:id:tbpg:20150930004128p:plain

動作確認2回目

f:id:tbpg:20150930004132p:plain

※今回はローカル開発環境の試行まで