vagrant-digitalocean provider の簡単な使い方

次のような Vagrantfile を用意します。

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure('2') do |config|
config.vm.provider :digital_ocean do |provider, override|
override.ssh.private_key_path = '~/.ssh/digitalocean-vagrant'
override.vm.box = 'digital_ocean'
override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"
provider.token = ''
provider.image = 'CentOS 6.5 x64'
# San Francisco 1
provider.region = 'sfo1'
provider.size = '512mb'
end
end

前提条件としては

  • Apps & API メニューで Personal Access Tokens を生成すること。Read, Write アクセス付与。
  • Vagrant という名前の秘密鍵が登録されていること。
  • ~/.ssh/digitalocean-vagrant と対になる公開鍵 ~/.ssh/digitalocean-vagrant.pub が存在すること。
  • 選択されているリージョンは San Francisco 1 (slug名になっている)。

ちなみに現在のリージョンとslug名の対応は以下のようになっている。

slug Region Name
nyc1 New York 1
ams1 Amsterdam 1
sfo1 San Francisco 1
nyc2 New York 2
ams2 Amsterdam 2
sgp1 Singapore 1
lon1 London 1
nyc3 New York 3

調べ方はさっきのパーソナルトークンを指定して以下の様な curl コマンドを発行すればいいです。
jq でフィルタリングしてます。

curl -X GET "https://api.digitalocean.com/v2/regions" \
-H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
2>/dev/null | jq '.regions [] | .slug,.name'

EC2 と違って Droplet が存在しているだけで課金対象になるので用が済んだらすぐに

vagrant destroy -f

References

  1. VagrantとSSDなVPS(Digital Ocean)で1時間1円の使い捨て高速サーバ環境を構築する – Glide Note – グライドノート
  2. jq コマンドが強力すぎてヤバい件 | CUBE SUGAR STORAGE
  3. DigitalOcean API

vagrant-digitalocean で vagrant up すると NoMethodError となる件

以下の様なエラーとなります。

% vagrant up --provider=digital_ocean
Bringing machine 'default' up with 'digital_ocean' provider...
==> default: Using existing SSH key: Vagrant
==> default: Creating a new droplet...
/Users/wnoguchi/.vagrant.d/gems/gems/vagrant-digitalocean-0.6.2/lib/vagrant-digitalocean/actions/create.rb:37:in `call': undefined method `first' for nil:NilClass (NoMethodError)
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Users/wnoguchi/.vagrant.d/gems/gems/vagrant-digitalocean-0.6.2/lib/vagrant-digitalocean/actions/setup_key.rb:33:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:95:in `block in finalize_action'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builder.rb:116:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `block in run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/util/busy.rb:19:in `busy'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builtin/call.rb:53:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builtin/config_validate.rb:25:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builder.rb:116:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `block in run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/util/busy.rb:19:in `busy'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:196:in `action_raw'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:173:in `block in action'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/environment.rb:434:in `lock'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:161:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:161:in `action'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/batch_action.rb:82:in `block (2 levels) in run'

DigitalOcean API V2 は Public Beta なので仕様が変わったためのようです。

Public Beta
Due to the large number of changes to API v2 we have decided to release it as a beta to collect user feedback. Please note that >during this period based on user feedback we may make some large changes which may break compatibility.

DigitalOcean API

このあたりは以下の Issue とプルリクで修正方法が確立されているので早く本家にマージされてほしいです。

とりあえずなんとかしたい方は対処療法的には

~/.vagrant.d/gems/gems/vagrant-digitalocean-0.6.2/lib/vagrant-digitalocean/actions/create.rb

の 37 行目を

          @client.wait_for_event(env, result['droplet']['action_ids'].first)
# ↓
@client.wait_for_event(env, result['links']['actions'].first['id'])

とすればいいです。

Vagrant AWS Provider を使ってみた

まずはダミーの box イメージをインポートする。

vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

そして次のような Vagrantfile を書く。
前提条件は以下。

  • IAMで予めEC2にアクセスを制限したユーザのクレデンシャルを払いだしておくこと
  • 使用するキーペアの名前は vagrant
  • 起動するインスタンスタイプは t2.micro (デフォルトだと m3.medium で起動してしまう)
  • Vagrant 用のセキュリティグループを定義してそれを使用
  • 東京リージョンを指定(ap-northeast-1)
  • 起動する AMI は Amazon Linux AMI 2014.03.2 (HVM) (ami-29dc9228)
  • SSH ユーザ名は ec2-user
  • 対応する秘密鍵の場所を指定する。
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "dummy"
config.vm.provider :aws do |aws, override|
aws.access_key_id = ""
aws.secret_access_key = ""
aws.keypair_name = "vagrant"
aws.instance_type = "t2.micro"
aws.security_groups = [ "vagrant" ]
aws.region = "ap-northeast-1"
# Amazon Linux AMI 2014.03.2 (HVM)
aws.ami = "ami-29dc9228"
override.ssh.username = "ec2-user"
override.ssh.private_key_path = "~/.ssh/aws-ec2-vagrant.pem"
end
end

そして

vagrant up --provider=aws

以下の様なエラーが出るが、ここでは気にしない。

==> default: Rsyncing folder: /Users/wnoguchi/Documents/tmp/vagrant-ec2/ => /vagrant
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
mkdir -p '/vagrant'
Stdout from the command:
Stderr from the command:
sudo: sorry, you must have a tty to run sudo

IPアドレス知らなくてもSSHできる。

vagrant ssh

グローバルIPが知りたければインスタンスメタデータcurl で取得する。

[ec2-user@ip-172-31-25-90 ~]$ curl -w "\n" http://169.254.169.254/latest/meta-data/public-hostname
ec2-54-64-60-151.ap-northeast-1.compute.amazonaws.com
[ec2-user@ip-172-31-25-90 ~]$ curl -w "\n" http://169.254.169.254/latest/meta-data/public-ipv4
54.64.60.151

あとは nginx 入れるなりなんなりして実験して、用が済んだら

vagrant halt

か潔く

vagrant destroy -f

References

  1. mitchellh/vagrant-aws
  2. インスタンスメタデータとユーザーデータ – Amazon Elastic Compute Cloud

CentOS 6.5 に MySQL 5.6 をインストールする

けっこう雑です。

sudo rpm -ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
sudo yum -y install mysql-community-server
sudo service mysqld start
sudo chkconfig mysqld on

以下、開発用の設定。

$ sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n]
New password: ← rootパスワードをキーイン
Re-enter new password: ← rootパスワードをキーイン
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n ← リモートからの root ログインを許可しないようにしない
... skipping.
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed!  Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... Success!
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...

あとは Vagrant で立ち上げた時にホストマシンからアクセスできるように権限設定する。

mysql -u root -pvagrant -e "grant all privileges on *.* to 'root'@'127.0.0.1' identified by 'vagrant' with grant option"
mysql -u root -pvagrant -e "grant all privileges on *.* to 'root'@'%' identified by 'vagrant' with grant option"
mysql -u root -pvagrant -e "flush privileges"

おわり。

References

  1. MySQL :: Download MySQL Yum Repository
  2. MySQL :: A Quick Guide to Using the MySQL Yum Repository

Mac OS X の tmux でクリップボードを使えるようにした(pbcopyとか)

いい加減 tmux でクリップボード使えないのはつらくなってきた。
gist コマンドでも -c オプションでクリップボードにコピーできないし。

% gist -c -u d3a3efa1a45eca669574 ~/.tmux.conf
Error: Copying to clipboard failed.
If you're running tmux on a mac, try http://robots.thoughtbot.com/post/19398560514/how-to-copy-and-paste-with-tmux-on-mac-os-x
Attempted to copy: https://gist.github.com/d3a3efa1a45eca669574

pbcopy も使えないしホント困る。
Homebrew でインストールできるらしい。

% brew info reattach-to-user-namespace
reattach-to-user-namespace: stable 2.3, HEAD
https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard
Not installed
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/reattach-to-user-namespace.rb
==> Options
--wrap-launchctl
Include wrapper for launchctl with this fix
--wrap-pbcopy-and-pbpaste
Include wrappers for pbcopy/pbpaste that shim in this fix
--HEAD
install HEAD version

なんかオプションはいらない気がする。
気軽にインストール。

brew install reattach-to-user-namespace

~/.tmux.conf の最後に以下を記述した。
zsh じゃない人は適宜読み替えてね。

# Mac Specific Settings:
#-------------------------------------------------------------------
set-option -g default-command "reattach-to-user-namespace -l zsh"
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
#-------------------------------------------------------------------

pbcopyはもちろんのこと、キーバインディングも vi になったし、tmux の copy モードでクリップボードにコピーできて、脳みそとけちゃいそうです。

.tmux.conf の Gist 更新しとこ。

% gist -c -u d3a3efa1a45eca669574 ~/.tmux.conf

References

  1. tmux Copy & Paste on OS X: A Better Future
  2. ChrisJohnsen/tmux-MacOSX-pasteboard
  3. tmuxとMacのクリップボードを共有する(copy-mode, vim) – Qiita
  4. tmux のコピーモードを使ってみる〜 Mac 編 〜 – ようへいの日々精進 XP