vagrant-sakura 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 377e6c397d621d61e1d26700d65ff16974748ca8
4
+ data.tar.gz: 292136e9093e73295475595801cb10b0bc3faf57
5
+ SHA512:
6
+ metadata.gz: ca7f44c45103ec2310ed62a1562266bb995cb546e025110c2701c6e9d6424f1ffa39224d0dde213cc558df3f036ea088aca1b1276f271c0631be73cc70abe4e3
7
+ data.tar.gz: 457d62c6128fff7883166e234f9c07d07ed250ebe50b136f34f2c8546031da79212b1c13d3f9f81072e79faf385d8c452f27079a8c717f9f3f7d07498b2cecc2
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .vagrant
6
+ .yardoc
7
+ /Vagrantfile
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0 (2013/10/07)
2
+
3
+ - Initial release.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git"
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tomoyuki Sahara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # Vagrant Sakura Provider
2
+
3
+ この gem は [Vagrant](http://www.vagrantup.com) に
4
+ [さくらのクラウド](http://cloud.sakura.ad.jp)上のサーバを操作する
5
+ 機能を追加する provider です。
6
+
7
+ ## 機能 / 制限
8
+
9
+ * さくらのクラウド上にサーバを作成できます。
10
+ * サーバに SSH ログインできます。
11
+ * サーバを停止/再開(オフ/オン)できます。
12
+ * provision は動きません。
13
+
14
+ ## インストール
15
+
16
+ 通常の Vagrant 1.1+ 以降のプラグインの導入方法でインストールできます。
17
+ インストール後は `sakura` プロバイダを指定して `vagrant up` してください。
18
+ 以下に例を示します:
19
+
20
+ ```
21
+ $ vagrant plugin install vagrant-sakura
22
+ ...
23
+ $ vagrant up --provider=sakura
24
+ ...
25
+ ```
26
+
27
+ なお実行前にさくらのクラウド用の Vagrant box を取得しておく必要があります。
28
+
29
+ ## かんたんな使い方
30
+
31
+ プラグインを(先述の通りに)インストールしたら、てっとり早く始めるためには
32
+ ダミーのさくらのクラウド用 box を使い、詳細は `config.vm.provider` で
33
+ 指定します。
34
+ まずはじめに、ダミーの box を好きな名前で追加してください:
35
+
36
+ ```sh
37
+ $ vagrant box add dummy https://github.com/tsahara/vagrant-sakura/raw/master/dummy.box
38
+ ...
39
+ ```
40
+
41
+ 次に、以下のような Vagrantfile を作成し、必要な情報を埋めてください。
42
+
43
+ ```Ruby
44
+ Vagrant.configure("2") do |config|
45
+ config.vm.box = "dummy"
46
+ config.ssh.username = "ubuntu"
47
+
48
+ config.vm.provider :sakura do |sakura|
49
+ sakura.access_token = 'YOUR ACCESS TOKEN'
50
+ sakura.access_token_secret = 'YOUR ACCESS TOKEN SECRET'
51
+ #sakura.sshkey_id = '112500313986'
52
+ #sakura.disk_source_archive = 112500182464
53
+ end
54
+ end
55
+ ```
56
+
57
+ そして ``vagrant up --provider=sakrua`` を実行してください。
58
+
59
+ なお、さくらのクラウド API を利用するための API キー(ACCESS TOKEN)と
60
+ シークレットトークン(ACCESS TOKEN SECRET)は環境変数
61
+ ``SAKURA_ACCESS_TOKEN`` と ``SAKURA_ACCESS_TOKEN_SECRET``で指定することも
62
+ できます。
63
+
64
+
65
+ ## 設定
66
+
67
+ さくらのクラウド provider では以下の設定ができます:
68
+
69
+ - ``access_token`` - さくらのクラウド API にアクセスするための API キー
70
+ - ``access_token_secret`` - API キーのシークレットトークン
71
+ - ``disk_plan`` - サーバで利用するディスクのプラン ID
72
+ - ``disk_source_archive`` - サーバで利用するディスクのベースとするアーカイブ
73
+ - ``server_name`` - サーバ名
74
+ - ``server_plan`` - 作成するサーバのプラン ID
75
+ - ``sshkey_id`` - サーバへのログインに利用する SSH 公開鍵のリソース ID
76
+ - ``zone_id`` - は、まだゾーンが石狩第一しかないので指定できません :)
77
+
78
+ ## ネットワーク
79
+ ``vagrant-sakura`` は ``config.vm.network`` を利用したネットワークの構築を
80
+ まだサポートしていません。
81
+
82
+ ## 開発
83
+
84
+ ``vagrant-sakura`` プラグインをいじる場合は、リポジトリを clone してから
85
+ [Bundler](http://gembundler.com/) を使って依存関係を解決してください。
86
+ ```sh
87
+ $ bundle
88
+ ```
89
+ 依存パッケージが入ったら、。。。
90
+ 開発を始められます。
91
+ ``Vagrantfile`` を clone したディレクトリに置いて
92
+ (.gitignore に書いてあるので git には無視されます)、
93
+ 以下の行を ``Vagrantfile`` に足してやれば、プラグインをインストールしなくても
94
+ 開発中の Vagrant 環境をテストすることができます。
95
+ ```Ruby
96
+ Vagrant.require_plugin "vagrant-sakura"
97
+ ```
98
+
99
+ Vagrant を実行する時は bundler を使ってください:
100
+ ```sh
101
+ $ bundle exec vagrant up --provider=sakura
102
+ ```
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler::GemHelper.install_tasks
data/dummy.box ADDED
Binary file
@@ -0,0 +1,8 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.configure("2") do |config|
5
+ config.vm.provider :sakura do |sakura|
6
+ # default settings here
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ "provider": "sakura"
3
+ }
@@ -0,0 +1,16 @@
1
+ require "pathname"
2
+
3
+ require "vagrant-sakura/plugin"
4
+ require "vagrant-sakura/version"
5
+
6
+ module VagrantPlugins
7
+ module Sakura
8
+ lib_path = Pathname.new(File.expand_path("../vagrant-sakura", __FILE__))
9
+ autoload :Action, lib_path.join("action")
10
+ autoload :Errors, lib_path.join("errors")
11
+
12
+ def self.source_root
13
+ @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,158 @@
1
+ require "pathname"
2
+ require "vagrant/action/builder"
3
+
4
+ module VagrantPlugins
5
+ module Sakura
6
+ module Action
7
+ include Vagrant::Action::Builtin
8
+
9
+ def self.action_destroy
10
+ Vagrant::Action::Builder.new.tap do |b|
11
+ b.use Call, DestroyConfirm do |env, b2|
12
+ if env[:result]
13
+ b2.use ConfigValidate
14
+ b2.use ConnectSakura
15
+ # b2.use Halt
16
+ b2.use DeleteServer
17
+ else
18
+ b2.use MessageWillNotDestroy
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.action_halt
25
+ Vagrant::Action::Builder.new.tap do |b|
26
+ b.use ConfigValidate
27
+ b.use Call, IsCreated do |env, b2|
28
+ if env[:result]
29
+ b2.use ConnectSakura
30
+ b2.use Halt
31
+ else
32
+ b2.use MessageNotCreated
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ # def self.action_provision
39
+ # Vagrant::Action::Builder.new.tap do |b|
40
+ # b.use ConfigValidate
41
+ # b.use Call, IsCreated do |env, b2|
42
+ # if !env[:result]
43
+ # b2.use MessageNotCreated
44
+ # next
45
+ # end
46
+ #
47
+ # b2.use Provision
48
+ # b2.use SyncFolders
49
+ # end
50
+ # end
51
+ # end
52
+
53
+ def self.action_read_ssh_info
54
+ Vagrant::Action::Builder.new.tap do |b|
55
+ b.use ConfigValidate
56
+ b.use ConnectSakura
57
+ b.use ReadSSHInfo
58
+ end
59
+ end
60
+
61
+ def self.action_read_state
62
+ Vagrant::Action::Builder.new.tap do |b|
63
+ b.use ConfigValidate
64
+ b.use ConnectSakura
65
+ b.use ReadState
66
+ end
67
+ end
68
+
69
+ def self.action_reload
70
+ Vagrant::Action::Builder.new.tap do |b|
71
+ b.use ConfigValidate
72
+ b.use ConnectSakura
73
+ b.use Call, ReadState do |env, b2|
74
+ case env[:machine_state_id]
75
+ when :up
76
+ b2.use Reset
77
+ b2.use Provision
78
+ when :down, :cleaning
79
+ b2.use MessageDown
80
+ when :not_created
81
+ b2.use MessageNotCreated
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def self.action_ssh
88
+ Vagrant::Action::Builder.new.tap do |b|
89
+ b.use ConfigValidate
90
+ b.use ConnectSakura
91
+ b.use Call, ReadState do |env, b2|
92
+ case env[:machine_state_id]
93
+ when :up
94
+ b2.use SSHExec
95
+ when :down, :cleaning
96
+ b2.use MessageDown
97
+ when :not_created
98
+ b2.use MessageNotCreated
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ def self.action_ssh_run
105
+ Vagrant::Action::Builder.new.tap do |b|
106
+ b.use ConfigValidate
107
+ b.use ConnectSakura
108
+ b.use Call, ReadState do |env, b2|
109
+ case env[:machine_state_id]
110
+ when :up
111
+ b2.use SSHRun
112
+ when :down, :cleaning
113
+ b2.use MessageDown
114
+ when :not_created
115
+ b2.use MessageNotCreated
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ def self.action_up
122
+ Vagrant::Action::Builder.new.tap do |b|
123
+ b.use HandleBoxUrl
124
+ b.use ConfigValidate
125
+ b.use ConnectSakura
126
+ b.use Call, ReadState do |env, b2|
127
+ case env[:machine_state_id]
128
+ when :up
129
+ b2.use MessageAlreadyCreated
130
+ when :down, :cleaning
131
+ b2.use PowerOn
132
+ b2.use Provision
133
+ when :not_created
134
+ b2.use RunInstance
135
+ b2.use Provision
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ action_root = Pathname.new(File.expand_path("../action", __FILE__))
142
+ autoload :ConnectSakura, action_root.join("connect_sakura")
143
+ autoload :DeleteServer, action_root.join("delete_server")
144
+ autoload :IsCreated, action_root.join("is_created")
145
+ autoload :Halt, action_root.join("halt")
146
+ autoload :MessageAlreadyCreated, action_root.join("message_already_created")
147
+ autoload :MessageDown, action_root.join("message_down")
148
+ autoload :MessageNotCreated, action_root.join("message_not_created")
149
+ autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy")
150
+ autoload :PowerOn, action_root.join("power_on")
151
+ autoload :ReadSSHInfo, action_root.join("read_ssh_info")
152
+ autoload :ReadState, action_root.join("read_state")
153
+ autoload :Reset, action_root.join("reset")
154
+ autoload :RunInstance, action_root.join("run_instance")
155
+ #autoload :SyncFolders, action_root.join("sync_folders")
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,23 @@
1
+ require "log4r"
2
+ require 'vagrant-sakura/driver/api'
3
+
4
+ module VagrantPlugins
5
+ module Sakura
6
+ module Action
7
+ class ConnectSakura
8
+ def initialize(app, env)
9
+ @app = app
10
+ @logger = Log4r::Logger.new("vagrant_sakura::action::connect_sakura")
11
+ end
12
+
13
+ def call(env)
14
+ token = env[:machine].provider_config.access_token
15
+ secret = env[:machine].provider_config.access_token_secret
16
+ env[:sakura_api] = Driver::API.new(token, secret)
17
+
18
+ @app.call(env)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module Sakura
5
+ module Action
6
+ class DeleteServer
7
+ def initialize(app, env)
8
+ @app = app
9
+ @logger = Log4r::Logger.new("vagrant_sakura::action::delete_server")
10
+ end
11
+
12
+ def call(env)
13
+ api = env[:sakura_api]
14
+ serverid = env[:machine].id
15
+
16
+ env[:ui].info(I18n.t("vagrant_sakura.terminating"))
17
+
18
+ response = api.get("/server/#{serverid}")
19
+ unless response["Server"]["Instance"]["Status"] == "down"
20
+ api.delete("/server/#{serverid}/power", { "Force" => true })
21
+ while true
22
+ r = api.get("/server/#{serverid}/power")
23
+ break if r["Instance"]["Status"] == "down"
24
+ sleep 1
25
+ end
26
+ end
27
+
28
+ disks = response["Server"]["Disks"].map { |disk| disk["ID"] }
29
+ data = { "WithDisk" => disks }
30
+ response = api.delete("/server/#{serverid}", data)
31
+ # error check
32
+ env[:machine].id = nil
33
+
34
+ @app.call(env)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module Sakura
5
+ module Action
6
+ class Halt
7
+ def initialize(app, env)
8
+ @app = app
9
+ @logger = Log4r::Logger.new("vagrant_sakura::action::halt")
10
+ end
11
+
12
+ def call(env)
13
+ api = env[:sakura_api]
14
+ serverid = env[:machine].id
15
+
16
+ env[:ui].info(I18n.t("vagrant_sakura.power_off"))
17
+
18
+ response = api.delete("/server/#{serverid}/power")
19
+ sleep 3
20
+ while true
21
+ response = api.get("/server/#{serverid}/power")
22
+ break if response["Instance"]["Status"] == "down"
23
+ sleep 1
24
+ end
25
+
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end