LoginSignup
10
9

More than 5 years have passed since last update.

VagrantでDigitalOceanのドロップレットを作成する

Last updated at Posted at 2014-03-12

参考:

作成するドロップレットのスペック

イメージ: Arch Linux 2013.05 x64
リージョン: Singapore 1
サイズ: 512MB

ファイル

Vagrantfile

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.client_id = "... CLIENT ID ..."
    provider.api_key = "... API KEY ..."

    provider.size = "512MB"
    provider.region = "Singapore 1"
    provider.image = "Arch Linux 2013.05 x64"
  end

  config.vm.provision :fabric do |fabric|
    fabric.fabfile_path = "./fabfile.py"
    fabric.tasks = ["hello"]
  end
end

fabfile.py

from fabric.api import run

def hello():
    run("echo hello")

手順

vagrantでドロップレットを作成し、fabricのプロビジョニング結果を確認
pythonのSimpleHTTPServerモジュールを使ってブラウザからアクセスしてみる
http://192.***.***.***:8000/index.html

$ vagrant plugin install vagrant-digitalocean
$ vagrant plugin install vagrant-fabric
$ vagrant up --provider=digital_ocean --provision
Bringing machine 'default' up with 'digital_ocean' provider...
[default] Using existing SSH key: Vagrant
[default] Creating a new droplet...
[default] Assigned IP address: 192.***.***.***
[default] Rsyncing folder: /path/vagrant/directory/ => /vagrant...
[default] Running provisioner: fabric...
[192.***.***.***] Executing task 'hello'
[192.***.***.***] run: echo hello
[192.***.***.***] out: hello
[192.***.***.***] out:


Done.
Disconnecting from 192.***.***.***... done.
$ vagrant ssh
[root@default ~]# mkdir hello
[root@default ~]# cd hello
[root@default hello]# cat >> index.html << EOF
<h1>Hello, Digital Ocean!</h1>
EOF
[root@default ~]# sed -i -e "s/^Server/#Server/g" /etc/pacman.d/mirrorlist
[root@default ~]# echo "Server = http://mirror.nus.edu.sg/archlinux/$repo/os/$arch" >> /etc/pacman.d/mirrorlist
[root@default ~]# pacman -Syu
[root@default ~]# pacman -S python2
[root@default ~]# python2 -m SimpleHTTPServer

ドロップレットの停止/削除

$ vagrant halt
[default] Powering off the droplet...
$ vagrant destroy
Are you sure you want to destroy the 'default' VM? [y/N] y
[default] Destroying the droplet...
[default] Running cleanup tasks for 'fabric' provisioner...

メモ

  • ~/.ssh/known_hostsに注意. 削除したdropletのIPと新しく作成したdropletのIPが同じだとfabricが失敗する
    • コマンド $ ssh-keygen -R 192.***.***.***
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9