vagrant-1cloud 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. metadata +3 -46
  3. data/Gemfile +0 -8
  4. data/LICENSE +0 -373
  5. data/README.md +0 -117
  6. data/Rakefile +0 -21
  7. data/build.sh +0 -32
  8. data/lib/vagrant-1cloud.rb +0 -20
  9. data/lib/vagrant-1cloud/actions.rb +0 -183
  10. data/lib/vagrant-1cloud/actions/check_state.rb +0 -19
  11. data/lib/vagrant-1cloud/actions/create.rb +0 -93
  12. data/lib/vagrant-1cloud/actions/destroy.rb +0 -31
  13. data/lib/vagrant-1cloud/actions/modify_provision_path.rb +0 -38
  14. data/lib/vagrant-1cloud/actions/power_off.rb +0 -35
  15. data/lib/vagrant-1cloud/actions/power_on.rb +0 -45
  16. data/lib/vagrant-1cloud/actions/private_network.rb +0 -108
  17. data/lib/vagrant-1cloud/actions/rebuild.rb +0 -78
  18. data/lib/vagrant-1cloud/actions/reload.rb +0 -42
  19. data/lib/vagrant-1cloud/actions/setup_key.rb +0 -60
  20. data/lib/vagrant-1cloud/actions/setup_sudo.rb +0 -43
  21. data/lib/vagrant-1cloud/actions/setup_user.rb +0 -58
  22. data/lib/vagrant-1cloud/actions/shut_down.rb +0 -35
  23. data/lib/vagrant-1cloud/commands/add_network.rb +0 -59
  24. data/lib/vagrant-1cloud/commands/create_network.rb +0 -135
  25. data/lib/vagrant-1cloud/commands/rebuild.rb +0 -29
  26. data/lib/vagrant-1cloud/config.rb +0 -62
  27. data/lib/vagrant-1cloud/errors.rb +0 -33
  28. data/lib/vagrant-1cloud/helpers/client.rb +0 -181
  29. data/lib/vagrant-1cloud/helpers/result.rb +0 -40
  30. data/lib/vagrant-1cloud/plugin.rb +0 -36
  31. data/lib/vagrant-1cloud/provider.rb +0 -106
  32. data/lib/vagrant-1cloud/version.rb +0 -5
  33. data/locales/en.yml +0 -90
  34. data/pom.xml +0 -59
  35. data/test/Vagrantfile +0 -14
  36. data/test/scripts/provision.sh +0 -3
  37. data/test/test.sh +0 -11
  38. data/test/test_id_rsa +0 -27
  39. data/test/test_id_rsa.pub +0 -1
  40. data/vagrant-1cloud.gemspec +0 -21
@@ -1,58 +0,0 @@
1
- module VagrantPlugins
2
- module OneCloud
3
- module Actions
4
- class SetupUser
5
- def initialize(app, env)
6
- @app = app
7
- @machine = env[:machine]
8
- @logger = Log4r::Logger.new('vagrant::onecloud::setup_user')
9
- end
10
-
11
- def call(env)
12
- # check if a username has been specified
13
- return @app.call(env) unless @machine.config.ssh.username
14
-
15
- # override ssh username to root temporarily
16
- user = @machine.config.ssh.username
17
- @machine.config.ssh.username = 'root'
18
-
19
- env[:ui].info I18n.t('vagrant_1cloud.info.creating_user', {
20
- :user => user
21
- })
22
-
23
- # create user account
24
- @machine.communicate.execute(<<-BASH)
25
- groupadd "#{user}"
26
- useradd -m -d "/home/#{user}" -g "#{user}" -r "#{user}" -s "/bin/bash"
27
- chown #{user}:#{user} -R "/home/#{user}"
28
- BASH
29
-
30
- # grant user sudo access with no password requirement
31
- @machine.communicate.execute(<<-BASH)
32
- echo "#{user} ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
33
- BASH
34
-
35
- # create the .ssh directory in the users home
36
- @machine.communicate.execute("su #{user} -c 'mkdir -p ~/.ssh'")
37
-
38
- # add the specified key to the authorized keys file
39
- path = @machine.config.ssh.private_key_path
40
- path = path[0] if path.is_a?(Array)
41
- path = File.expand_path(path, @machine.env.root_path)
42
- pub_key = OneCloud.public_key(path)
43
- @machine.communicate.execute(<<-BASH)
44
- touch /home/#{user}/.ssh/authorized_keys
45
- echo \"#{pub_key}\" >> /home/#{user}/.ssh/authorized_keys
46
- chown #{user}:#{user} -R /home/#{user}/.ssh
47
- chmod 600 /home/#{user}/.ssh/authorized_keys
48
- BASH
49
-
50
- # reset username
51
- @machine.config.ssh.username = user
52
-
53
- @app.call(env)
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,35 +0,0 @@
1
- require 'vagrant-1cloud/helpers/client'
2
-
3
- module VagrantPlugins
4
- module OneCloud
5
- module Actions
6
- class ShutDown
7
- include Helpers::Client
8
-
9
- def initialize(app, env)
10
- @app = app
11
- @machine = env[:machine]
12
- @client = client
13
- @logger = Log4r::Logger.new('vagrant::onecloud::shut_down')
14
- end
15
-
16
- def call(env)
17
- # submit shutdown droplet request
18
- result = @client.post("/server/#{@machine.id}/action", {
19
- :Type => 'ShutDownGuestOS'
20
- })
21
-
22
- # wait for request to complete
23
- env[:ui].info I18n.t('vagrant_1cloud.info.shutting_down')
24
- @client.wait_for_event(env, @machine.id, result['body']['ID'])
25
-
26
- # refresh droplet state with provider
27
- Provider.droplet(@machine, :refresh => true)
28
-
29
- @app.call(env)
30
- end
31
- end
32
- end
33
- end
34
- end
35
-
@@ -1,59 +0,0 @@
1
- require 'optparse'
2
-
3
- module VagrantPlugins
4
- module OneCloud
5
- module Commands
6
- class AddNetwork < Vagrant.plugin('2', :command)
7
-
8
- # Show description when `vagrant list-commands` is triggered
9
- def self.synopsis
10
- "plugin: vagrant-1cloud: adds VPS to specific private network"
11
- end
12
-
13
- def execute
14
- options = {}
15
-
16
- optparse = OptionParser.new do |opts|
17
- opts.banner = 'Usage: vagrant add-network [vm-name] [options]'
18
-
19
- opts.on('-n', '--net NETNAME', 'Network name') do |net|
20
- options[:Net] = net
21
- end
22
-
23
- options[:IP] = nil
24
- opts.on('-i', '--ip [IP]', 'Private IP address') do |ip|
25
- options[:IP] = ip
26
- end
27
-
28
- opts.on('-h', '--help', 'Display this screen') do
29
- puts opts
30
- exit
31
- end
32
- end
33
-
34
- begin
35
- optparse.parse!
36
- mandatory = [:Net]
37
- missing = mandatory.select{ |param| options[param].nil? }
38
- unless missing.empty?
39
- raise OptionParser::MissingArgument.new(missing.join(', '))
40
- end
41
- rescue OptionParser::InvalidOption, OptionParser::MissingArgument
42
- puts $!.to_s
43
- puts optparse
44
- exit
45
- end
46
-
47
- argv = parse_options(optparse)
48
-
49
- with_target_vms(argv) do |machine|
50
- machine.provider_config.private_net = {options[:Net] => options[:IP]}
51
- machine.action(:addnet)
52
- end
53
-
54
- 0
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,135 +0,0 @@
1
- require 'optparse'
2
- require 'vagrant-1cloud/helpers/result'
3
- require 'faraday'
4
- require 'json'
5
-
6
- module VagrantPlugins
7
- module OneCloud
8
- module Commands
9
- class CreateNetwork < Vagrant.plugin('2', :command)
10
- include Helpers
11
- include Vagrant::Util::Retryable
12
-
13
- # Show description when `vagrant list-commands` is triggered
14
- def self.synopsis
15
- "plugin: vagrant-1cloud: creates new private network"
16
- end
17
-
18
- def execute
19
- options = {}
20
-
21
- optparse = OptionParser.new do |opts|
22
- opts.banner = 'Usage: vagrant create-network [options]'
23
-
24
- opts.on('-n', '--name NAME', 'Network name') do |name|
25
- options[:Name] = name
26
- end
27
-
28
- options[:IsDHCP] = false
29
- opts.on('-d', '--[no-]dhcp', "Use dhcp or not (default #{options[:IsDHCP]})") do |dhcp|
30
- options[:IsDHCP] = dhcp
31
- end
32
-
33
- opts.on('-l', '--location LOCATION', 'Network location') do |location|
34
- options[:DCLocation] = location
35
- end
36
-
37
- opts.on('-t', '--token TOKEN', '1cloud type token') do |token|
38
- options[:token] = token
39
- end
40
-
41
- opts.on('-h', '--help', 'Display this screen') do
42
- puts opts
43
- exit
44
- end
45
- end
46
-
47
- begin
48
- optparse.parse!
49
- mandatory = [:Name, :DCLocation, :token]
50
- missing = mandatory.select{ |param| options[param].nil? }
51
- unless missing.empty?
52
- raise OptionParser::MissingArgument.new(missing.join(', '))
53
- end
54
- rescue OptionParser::InvalidOption, OptionParser::MissingArgument
55
- puts $!.to_s
56
- puts optparse
57
- exit
58
- end
59
-
60
- result = request(options[:token], '/network')
61
- private_network = result['body'].find { |network| network['Name'] == options[:Name] }
62
-
63
- if private_network
64
- @env.ui.info I18n.t('vagrant_1cloud.info.network_exists', network: options[:Name])
65
- else
66
- @env.ui.info I18n.t('vagrant_1cloud.info.network_missing', network: options[:Name])
67
-
68
- result = request(options[:token], '/network', options.except(:token), :post)
69
-
70
- # Waiting for private network to create
71
- @env.ui.info I18n.t('vagrant_1cloud.info.creating_private_network')
72
- wait_for_network(options[:token], result['body']['ID'])
73
- end
74
-
75
- 0
76
- end
77
-
78
- def request(token, path, params = {}, method = :get)
79
- connection = Faraday.new({
80
- :url => 'https://api.1cloud.ru/'
81
- })
82
-
83
- begin
84
- @env.ui.info I18n.t('vagrant_1cloud.info.request', path: path)
85
- @env.ui.info I18n.t('vagrant_1cloud.info.params', params: params)
86
- result = connection.send(method) do |req|
87
- req.url path
88
- req.headers['Authorization'] = "Bearer #{token}"
89
- req.body = params
90
- end
91
- rescue Faraday::Error::ConnectionFailed => e
92
- # TODO this is suspect but because faraday wraps the exception
93
- # in something generic there doesn't appear to be another
94
- # way to distinguish different connection errors :(
95
- if e.message =~ /certificate verify failed/
96
- raise Errors::CertificateError
97
- end
98
- raise e
99
- end
100
-
101
- begin
102
- body = JSON.parse(%Q[{"body":#{result.body}}])
103
- @env.ui.info I18n.t('vagrant_1cloud.info.response', body: body)
104
- rescue JSON::ParserError => e
105
- raise(Errors::JSONError, {
106
- :message => e.message,
107
- :path => path,
108
- :params => params,
109
- :response => result.body
110
- })
111
- end
112
-
113
- unless /^2\d\d$/ =~ result.status.to_s
114
- raise(Errors::APIStatusError, {
115
- :path => path,
116
- :params => params,
117
- :status => result.status,
118
- :response => body.inspect
119
- })
120
- end
121
-
122
- Result.new(body)
123
- end
124
-
125
- def wait_for_network(token, net_id)
126
- retryable(:tries => 400, :sleep => 10) do
127
- # check network status
128
- result = request(token, "/network/#{net_id}")
129
- raise 'Network is not active' if result['body']['State'] != 'Active'
130
- end
131
- end
132
- end
133
- end
134
- end
135
- end
@@ -1,29 +0,0 @@
1
- require 'optparse'
2
-
3
- module VagrantPlugins
4
- module OneCloud
5
- module Commands
6
- class Rebuild < Vagrant.plugin('2', :command)
7
-
8
- # Show description when `vagrant list-commands` is triggered
9
- def self.synopsis
10
- "plugin: vagrant-1cloud: destroys and ups the vm with the same ip address"
11
- end
12
-
13
- def execute
14
- opts = OptionParser.new do |o|
15
- o.banner = 'Usage: vagrant rebuild [vm-name]'
16
- end
17
-
18
- argv = parse_options(opts)
19
-
20
- with_target_vms(argv) do |machine|
21
- machine.action(:rebuild)
22
- end
23
-
24
- 0
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,62 +0,0 @@
1
- module VagrantPlugins
2
- module OneCloud
3
- class Config < Vagrant.plugin('2', :config)
4
- attr_accessor :token
5
- attr_accessor :image
6
- attr_accessor :region
7
- attr_accessor :hdd
8
- attr_accessor :hdd_type
9
- attr_accessor :cpu
10
- attr_accessor :ram
11
- attr_accessor :hi_perf
12
- attr_accessor :ca_path
13
- attr_accessor :private_net
14
- attr_accessor :ssh_key_name
15
-
16
- def initialize
17
- @token = UNSET_VALUE
18
- @image = UNSET_VALUE
19
- @region = UNSET_VALUE
20
- @hdd = UNSET_VALUE
21
- @hdd_type = UNSET_VALUE
22
- @cpu = UNSET_VALUE
23
- @ram = UNSET_VALUE
24
- @hi_perf = UNSET_VALUE
25
- @ca_path = UNSET_VALUE
26
- @private_net = UNSET_VALUE
27
- @ssh_key_name = UNSET_VALUE
28
- end
29
-
30
- def finalize!
31
- @token = ENV['DO_TOKEN'] if @token == UNSET_VALUE
32
- @image = '7' if @image == UNSET_VALUE
33
- @region = 'SdnSpb' if @region == UNSET_VALUE
34
- @hdd = '10' if @hdd == UNSET_VALUE
35
- @hdd_type = 'SAS' if @hdd_type == UNSET_VALUE
36
- @cpu = '1' if @cpu == UNSET_VALUE
37
- @ram = '512' if @ram == UNSET_VALUE
38
- @hi_perf = false if @hi_perf == UNSET_VALUE
39
- @ca_path = nil if @ca_path == UNSET_VALUE
40
- @private_net = nil if @private_net == UNSET_VALUE
41
- @ssh_key_name = 'Vagrant' if @ssh_key_name == UNSET_VALUE
42
- end
43
-
44
- def validate(machine)
45
- errors = []
46
- errors << I18n.t('vagrant_1cloud.config.token') if !@token
47
-
48
- key = machine.config.ssh.private_key_path
49
- key = key[0] if key.is_a?(Array)
50
- if !key
51
- errors << I18n.t('vagrant_1cloud.config.private_key')
52
- elsif !File.file?(File.expand_path("#{key}.pub", machine.env.root_path))
53
- errors << I18n.t('vagrant_1cloud.config.public_key', {
54
- :key => "#{key}.pub"
55
- })
56
- end
57
-
58
- { '1cloud Provider' => errors }
59
- end
60
- end
61
- end
62
- end
@@ -1,33 +0,0 @@
1
- module VagrantPlugins
2
- module OneCloud
3
- module Errors
4
- class OneCloudError < Vagrant::Errors::VagrantError
5
- error_namespace("vagrant_1cloud.errors")
6
- end
7
-
8
- class APIStatusError < OneCloudError
9
- error_key(:api_status)
10
- end
11
-
12
- class JSONError < OneCloudError
13
- error_key(:json)
14
- end
15
-
16
- class ResultMatchError < OneCloudError
17
- error_key(:result_match)
18
- end
19
-
20
- class CertificateError < OneCloudError
21
- error_key(:certificate)
22
- end
23
-
24
- class PublicKeyError < OneCloudError
25
- error_key(:public_key)
26
- end
27
-
28
- class RsyncError < OneCloudError
29
- error_key(:rsync)
30
- end
31
- end
32
- end
33
- end
@@ -1,181 +0,0 @@
1
- require 'vagrant-1cloud/helpers/result'
2
- require 'faraday'
3
- require 'json'
4
-
5
- module VagrantPlugins
6
- module OneCloud
7
- module Helpers
8
- module Client
9
- def client
10
- @client ||= ApiClient.new(@machine)
11
- end
12
- end
13
-
14
- class ApiClient
15
- include Vagrant::Util::Retryable
16
-
17
- def initialize(machine)
18
- @logger = Log4r::Logger.new('vagrant::onecloud::apiclient')
19
- @config = machine.provider_config
20
- @machine = machine
21
- @client = Faraday.new({
22
- :url => 'https://api.1cloud.ru/',
23
- :ssl => {
24
- :ca_file => @config.ca_path
25
- }
26
- })
27
- end
28
-
29
- def delete(path, params = {})
30
- @client.request :url_encoded
31
- request(path, params, :delete)
32
- end
33
-
34
- def post(path, params = {})
35
- @client.headers['Content-Type'] = 'application/json'
36
- request(path, params, :post)
37
- end
38
-
39
- def request(path, params = {}, method = :get)
40
- begin
41
- @logger.info "Request: #{path}"
42
- @logger.info "Parameters: #{params}"
43
- result = @client.send(method) do |req|
44
- req.url path
45
- req.headers['Authorization'] = "Bearer #{@config.token}"
46
- req.body = params.to_json
47
- end
48
- rescue Faraday::Error::ConnectionFailed => e
49
- # TODO this is suspect but because faraday wraps the exception
50
- # in something generic there doesn't appear to be another
51
- # way to distinguish different connection errors :(
52
- if e.message =~ /certificate verify failed/
53
- raise Errors::CertificateError
54
- end
55
- raise e
56
- end
57
-
58
- unless method == :delete
59
- begin
60
- body = JSON.parse(%Q[{"body":#{result.body}}])
61
- @logger.info "Response: #{body}"
62
- rescue JSON::ParserError => e
63
- raise(Errors::JSONError, {
64
- :message => e.message,
65
- :path => path,
66
- :params => params,
67
- :response => result.body
68
- })
69
- end
70
- end
71
-
72
- unless /^2\d\d$/ =~ result.status.to_s
73
- raise(Errors::APIStatusError, {
74
- :path => path,
75
- :params => params,
76
- :status => result.status,
77
- :response => body.inspect
78
- })
79
- end
80
-
81
- Result.new(body)
82
- end
83
-
84
- def wait_for_event(env, m_id, id)
85
- retryable(:tries => 400, :sleep => 10) do
86
- # stop waiting if interrupted
87
- next if env[:interrupted]
88
-
89
- # check action status
90
- result = self.request("/server/#{m_id}/action/#{id}")
91
-
92
- yield result if block_given?
93
- raise 'Action is not completed' if result['body']['State'] != 'Completed'
94
- end
95
- end
96
-
97
- def wait_for_destroy(env, id)
98
- retryable(:tries => 400, :sleep => 10) do
99
- # stop waiting if interrupted
100
- next if env[:interrupted]
101
-
102
- # check action status
103
- begin
104
- result = @client.send(:get) do |req|
105
- req.url "/server/#{id}"
106
- req.headers['Authorization'] = "Bearer #{@config.token}"
107
- end
108
- rescue Faraday::Error::ConnectionFailed => e
109
- # TODO this is suspect but because faraday wraps the exception
110
- # in something generic there doesn't appear to be another
111
- # way to distinguish different connection errors :(
112
- if e.message =~ /certificate verify failed/
113
- raise Errors::CertificateError
114
- end
115
- raise e
116
- end
117
- begin
118
- body = JSON.parse(%Q[{"body":#{result.body}}])
119
- @logger.info "Response: #{body}"
120
- rescue JSON::ParserError => e
121
- raise(Errors::JSONError, {
122
- :message => e.message,
123
- :path => path,
124
- :params => params,
125
- :response => result.body
126
- })
127
- end
128
- result = Result.new(body)
129
- yield result if block_given?
130
- raise 'Destroy is not completed' if result['body']['Message'] != 'Server not found'
131
- end
132
- end
133
-
134
- def wait_for_network(env, net_id)
135
- retryable(:tries => 400, :sleep => 10) do
136
- # stop waiting if interrupted
137
- next if env[:interrupted]
138
-
139
- # check network status
140
- result = self.request("/network/#{net_id}")
141
-
142
- yield result if block_given?
143
- raise 'Network is not active' if result['body']['State'] != 'Active'
144
- end
145
- end
146
-
147
- def wait_for_ssh(env, reboot_num, check_num)
148
- i = 0
149
- while i <= reboot_num do
150
- j = 0
151
- while !@machine.communicate.ready? && j < check_num do
152
- env[:ui].info I18n.t('vagrant_1cloud.info.ssh_off')
153
- sleep 10
154
- j += 1
155
- end
156
-
157
- if j < check_num
158
- env[:ui].info I18n.t('vagrant_1cloud.info.ssh_on')
159
- break
160
- else
161
- if i < reboot_num
162
- # submit reboot droplet request
163
- result = @client.post("/server/#{@machine.id}/action", {
164
- :Type => 'PowerReboot'
165
- })
166
-
167
- # wait for request to complete
168
- env[:ui].info I18n.t('vagrant_1cloud.info.reloading')
169
- @client.wait_for_event(env, @machine.id, result['body']['ID'])
170
-
171
- i += 1
172
- else
173
- raise 'No ssh connection'
174
- end
175
- end
176
- end
177
- end
178
- end
179
- end
180
- end
181
- end