Skip to content

Commit

Permalink
VAGPROX-11 - Add call to delete file from proxmox local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrem committed Jun 1, 2015
1 parent 6767c0c commit e167782
Show file tree
Hide file tree
Showing 16 changed files with 364 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .idea/.rakeTasks

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.0.0
ruby-2.2.1
78 changes: 43 additions & 35 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.6)
addressable (2.3.8)
awesome_print (1.2.0)
builder (3.2.2)
celluloid (0.15.2)
timers (~> 1.1.0)
celluloid (0.16.0)
timers (~> 4.0.0)
childprocess (0.3.9)
ffi (~> 1.0, >= 1.0.11)
coderay (1.1.0)
crack (0.4.2)
safe_yaml (~> 1.0.0)
cucumber (1.3.16)
cucumber (1.3.19)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
gherkin (~> 2.12)
multi_json (>= 1.7.5, < 2.0)
multi_test (>= 0.1.1)
multi_test (>= 0.1.2)
diff-lcs (1.2.5)
docile (1.1.5)
erubis (2.7.0)
ffi (1.9.3)
ffi (1.9.8)
formatador (0.2.5)
geminabox (0.11.1)
builder
Expand All @@ -56,48 +56,54 @@ GEM
sinatra (>= 1.2.7)
gherkin (2.12.2)
multi_json (~> 1.3)
guard (2.6.1)
guard (2.12.5)
formatador (>= 0.2.4)
listen (~> 2.7)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-rspec (4.2.10)
guard (~> 2.1)
rspec (>= 2.14, < 4.0)
httpclient (2.4.0)
hitimes (1.2.2)
httpclient (2.6.0.1)
i18n (0.6.11)
json (1.8.2)
libnotify (0.8.3)
libnotify (0.8.4)
ffi (>= 1.0.11)
listen (2.7.9)
celluloid (>= 0.15.2)
listen (2.10.0)
celluloid (~> 0.16.0)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
log4r (1.1.10)
lumberjack (1.0.9)
method_source (0.8.2)
mime-types (1.25.1)
minitest (4.7.5)
multi_json (1.10.1)
multi_test (0.1.1)
multi_json (1.11.0)
multi_test (0.1.2)
nenv (0.2.0)
nesty (1.0.2)
net-scp (1.1.2)
net-ssh (>= 2.6.5)
net-ssh (2.7.0)
pry (0.10.0)
notiffany (0.0.6)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.5.2)
rack (1.6.1)
rack-protection (1.5.3)
rack
rake (10.3.2)
rb-fsevent (0.9.4)
rake (10.4.2)
rb-fsevent (0.9.5)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rdoc (4.2.0)
json (~> 1.4)
rest-client (1.6.8)
mime-types (~> 1.16)
rdoc (>= 2.4.2)
Expand All @@ -106,33 +112,35 @@ GEM
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-core (3.0.3)
rspec-core (3.0.4)
rspec-support (~> 3.0.0)
rspec-expectations (3.0.3)
rspec-expectations (3.0.4)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.0.0)
rspec-mocks (3.0.3)
rspec-mocks (3.0.4)
rspec-support (~> 3.0.0)
rspec-support (3.0.3)
safe_yaml (1.0.3)
simplecov (0.9.0)
rspec-support (3.0.4)
safe_yaml (1.0.4)
shellany (0.0.1)
simplecov (0.9.2)
docile (~> 1.1.0)
multi_json
simplecov-html (~> 0.8.0)
simplecov-html (0.8.0)
multi_json (~> 1.0)
simplecov-html (~> 0.9.0)
simplecov-html (0.9.0)
simplecov-rcov (0.2.3)
simplecov (>= 0.4.1)
sinatra (1.4.5)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (>= 1.3, < 3)
slop (3.6.0)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
timecop (0.7.1)
timers (1.1.0)
tzinfo (0.3.42)
thread_safe (0.3.5)
tilt (2.0.1)
timecop (0.7.3)
timers (4.0.1)
hitimes
tzinfo (0.3.44)
webmock (1.18.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand Down
48 changes: 48 additions & 0 deletions features/replace_iso_file.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Feature: Use new iso file
As a system administrator I want to choose an iso file to be uploaded to the proxmox server in order to
use its contents for the virtual machine. It should replace the already existing file of the same name.

Background:
Given a Vagrantfile with these provider settings:
"""
Vagrant.configure('2') do |config|
config.vm.provider :proxmox do |proxmox|
proxmox.endpoint = 'https://proxmox.example.com/api2/json'
proxmox.user_name = 'vagrant'
proxmox.password = 'password'
proxmox.vm_type = :qemu
proxmox.qemu_os = :l26
proxmox.qemu_iso_file = './tmp/justanisofile.iso'
proxmox.replace_qemu_iso_file = true
proxmox.qemu_disk_size = '30G'
end
config.vm.define :machine, primary: true do |machine|
machine.vm.box = 'b681e2bc-617b-4b35-94fa-edc92e1071b8'
machine.vm.network :public_network, ip: '172.16.100.1', macaddress: 'aa:bb:cc:dd:ee:ff'
end
end
"""

Scenario: An iso file is specified in the Vagrantfile and does not exist on the proxmox server
Given An iso file "./tmp/justanisofile.iso" exists locally
When I run "vagrant up --provider=proxmox --no-provision"
Then The iso file "./tmp/justanisofile.iso" is uploaded into the local storage of the vm node
And the new virtual machine using the iso "local:iso/justanisofile.iso" is created

Scenario: An iso file is specified in the Vagrantfile and already exists on the proxmox server
Given An iso file "./tmp/justanisofile.iso" exists locally
And the iso file "justanisofile.iso" exists locally" already exists in the proxmox storage
When I run "vagrant up --provider=proxmox --no-provision"
Then The iso file "local:iso/justanisofile.iso" is deleted from proxmox
And The iso file "./tmp/justanisofile.iso" is uploaded into the local storage of the vm node
And the new virtual machine using the iso "local:iso/justanisofile.iso" is created
Scenario: An iso file is specified in the Vagrantfile but does not exist locally
When I run "vagrant up --provider=proxmox --no-provision"
Then I should see "File for upload not found"
Scenario: An iso file is specified in the Vagrantfile and an error occurs during upload
Given An iso file "./tmp/justanisofile.iso" exists locally
But during upload an error will occur
When I run "vagrant up --provider=proxmox --no-provision"
Then I should see "Error during upload"
45 changes: 45 additions & 0 deletions features/replace_template_file.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Feature: Use new template file
As a system administrator I want to choose a template file to be uploaded to the proxmox server in order to
generate the filesystem of the virtual machine.

Background:
Given a Vagrantfile with these provider settings:
"""
Vagrant.configure('2') do |config|
config.vm.provider :proxmox do |proxmox|
proxmox.endpoint = 'https://proxmox.example.com/api2/json'
proxmox.user_name = 'vagrant'
proxmox.password = 'password'
proxmox.vm_type = :openvz
proxmox.openvz_template_file = './tmp/mytemplate.tar.gz'
proxmox.replace_openvz_template_file = true
end
config.vm.define :machine, primary: true do |machine|
machine.vm.box = 'b681e2bc-617b-4b35-94fa-edc92e1071b8'
machine.vm.network :public_network, ip: '172.16.100.1'
end
end
"""

Scenario: A template is specified in the Vagrantfile and does not exist on the proxmox server
Given A templatefile "./tmp/mytemplate.tar.gz" exists locally
When I run "vagrant up --provider=proxmox --no-provision"
Then The template file "./tmp/mytemplate.tar.gz" is uploaded into the local storage of the vm node
And the new virtual machine using the template "local:vztmpl/mytemplate.tar.gz" is created

Scenario: A template is specified in the Vagrantfile and already exists on the proxmox server
Given A templatefile "./tmp/mytemplate.tar.gz" exists locally
And the template file "mytemplate.tar.gz" already exists in the proxmox storage
When I run "vagrant up --provider=proxmox --no-provision"
Then The template file "local:vztmpl/mytemplate.tar.gz" is deleted from proxmox
And the new virtual machine using the template "local:vztmpl/mytemplate.tar.gz" is created

Scenario: A template is specified in the Vagrantfile but does not exist locally
When I run "vagrant up --provider=proxmox --no-provision"
Then I should see "File for upload not found"

Scenario: A template is specified in the Vagrantfile and an error occurs during upload
Given A templatefile "./tmp/mytemplate.tar.gz" exists locally
But during upload an error will occur
When I run "vagrant up --provider=proxmox --no-provision"
Then I should see "Error during upload"
9 changes: 9 additions & 0 deletions features/step_definitions/general_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@

Then(/^The iso file "([^"]*)" is not uploaded$/) do |arg|
assert_not_requested :post, proxmox_api_url('/nodes/node1/storage/local/upload')
end


Then(/^The iso file "([^"]*)" is deleted from proxmox$/) do |arg|
pending
end

Then(/^The template file "([^"]*)" is deleted from proxmox$/) do |arg|
pending
end
2 changes: 1 addition & 1 deletion lib/vagrant-proxmox/action/message_upload_server_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize app, env
end

def call env
env[:ui].info I18n.t('vagrant_proxmox.errors.upload_server_error')
env[:ui].info I18n.t('vagrant_proxmox.errors.server_upload_error')
next_action env
end

Expand Down
6 changes: 3 additions & 3 deletions lib/vagrant-proxmox/action/upload_iso_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ def call env
env[:result] = :ok
config = env[:machine].provider_config
if config.qemu_iso_file
env[:result] = upload_file env, config.qemu_iso_file
env[:result] = upload_file env, config.qemu_iso_file, config.replace_qemu_iso_file
end
next_action env
end

private
def upload_file env, filename
def upload_file env, filename, replace
if File.exist? filename
begin
connection(env).upload_file(filename, content_type: 'iso', node: env[:proxmox_selected_node], storage: 'local')
connection(env).upload_file(filename, content_type: 'iso', node: env[:proxmox_selected_node], storage: 'local', replace: replace)
:ok
rescue
:server_upload_error
Expand Down
12 changes: 12 additions & 0 deletions lib/vagrant-proxmox/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Config < Vagrant.plugin('2', :config)
# @return [String]
attr_accessor :openvz_template_file

# Should the openvz os template be replaced (deleted before upload)?
#
# @return [Boolean]
attr_accessor :replace_openvz_template_file

# The id range to use for the virtual machines
#
# @return [Range]
Expand Down Expand Up @@ -102,6 +107,11 @@ class Config < Vagrant.plugin('2', :config)
# @return [String]
attr_accessor :qemu_iso_file

# Should the qemu iso file replaced (deleted before upload)?
#
# @return [Boolean]
attr_accessor :replace_qemu_iso_file

# The qemu disk size to use for the virtual machine, e.g. '30G'
#
# @return [String]
Expand Down Expand Up @@ -133,6 +143,7 @@ def initialize
@vm_type = UNSET_VALUE
@openvz_os_template = UNSET_VALUE
@openvz_template_file = UNSET_VALUE
@replace_openvz_template_file = false
@vm_id_range = 900..999
@vm_name_prefix = 'vagrant_'
@vm_memory = 512
Expand All @@ -146,6 +157,7 @@ def initialize
@qemu_sockets = 1
@qemu_iso = UNSET_VALUE
@qemu_iso_file = UNSET_VALUE
@replace_qemu_iso_file = false
@qemu_disk_size = UNSET_VALUE
@qemu_storage = 'raid'
@qemu_nic_model = 'e1000'
Expand Down
18 changes: 13 additions & 5 deletions lib/vagrant-proxmox/proxmox/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,22 @@ def get_free_vm_id
free_vm_ids.empty? ? raise(VagrantPlugins::Proxmox::Errors::NoVmIdAvailable) : free_vm_ids.first
end

def upload_file file, content_type: required('content_type'), node: required('node'), storage: required('storage')
def upload_file file, content_type: required('content_type'), node: required('node'), storage: required('storage'), replace: false
if (is_file_in_storage? filename: file, node: node, storage: storage) && (replace == true)
delete_file(filename: file, node: node, storage: storage)
end
unless is_file_in_storage? filename: file, node: node, storage: storage
res = post "/nodes/#{node}/storage/#{storage}/upload", content: content_type,
filename: File.new(file, 'rb'), node: node, storage: storage
wait_for_completion task_response: res, timeout_message: 'vagrant_proxmox.errors.upload_timeout'
end
end

def delete_file filename: required('filename'), node: required('node'), storage: required('storage')
response = delete "/nodes/#{node}/storage/#{storage}/content/#{File.basename filename}"
wait_for_completion task_response: response, timeout_message: 'vagrant_proxmox.errors.delete_file_timeout'
end

def list_storage_files node: required('node'), storage: required('storage')
res = get "/nodes/#{node}/storage/#{storage}/content"
res[:data].map { |e| e[:volid] }
Expand All @@ -146,9 +154,9 @@ def list_storage_files node: required('node'), storage: required('storage')
def get_vm_info vm_id
response = get '/cluster/resources?type=vm'
response[:data]
.select { |m| m[:id] =~ /^[a-z]*\/#{vm_id}$/ }
.map { |m| {id: vm_id, type: /^(.*)\/(.*)$/.match(m[:id])[1], node: m[:node]} }
.first
.select { |m| m[:id] =~ /^[a-z]*\/#{vm_id}$/ }
.map { |m| {id: vm_id, type: /^(.*)\/(.*)$/.match(m[:id])[1], node: m[:node]} }
.first
end

private
Expand All @@ -175,7 +183,7 @@ def get path
end

private
def delete path
def delete path, params = {}
begin
response = RestClient.delete "#{api_url}#{path}", headers
JSON.parse response.to_s, symbolize_names: true
Expand Down
Loading

0 comments on commit e167782

Please sign in to comment.