Skip to content

Commit

Permalink
Merge pull request #1 from poliva83/master
Browse files Browse the repository at this point in the history
Version 0.4.0 - 7/12/2015
  • Loading branch information
poliva83 committed Feb 22, 2016
2 parents e114e6d + 5020b24 commit b65d105
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 121 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ClassLength:
Max: 1000

AbcSize:
Max: 40
Max: 62

ParameterLists:
Max: 15
Expand All @@ -39,6 +39,9 @@ ParameterLists:
StringLiterals:
Enabled: false

Style/SpecialGlobalVars:
Enabled: false

Style/SignalException:
Enabled: false

Expand Down
26 changes: 20 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## 0.4.0 (7/12/2015)
- Added http_port to one_image resource so that port other than 8066 (default) can be used to upload images.
- Fixed error message in one_image resource
- Removed :credentials and :secret_file support as driver options. It uses profiles now.
- Fixed ssh timeout. Now the :timeout will be applied to each ssh command and overall timeout to establish
a ssh session is 5 min.
- one_image now takes 'download_url' as an optional attribute. 'image_file' still takes precedence over
'download_url'. If 'download_url' is specified then that location is used for image and it will not try
to start HTTP server locally.
- Changed default port for HTTP server in one_image to be 8066 instead of previously used port 80. This is to
avoid collision with existing web servers running on the host.
- Added :mode attribute to one_image, one_template and one_vnet resources.
- Added bootstrap_options[:mode] so that machines can be created with different permissions

## 0.3.4 (10/29/2015)
- Yanked version 0.3.3 from rubygems due to internal homepage link in gemspec
- Need to release version 0.3.4 to reupload to rubygems.org
Expand All @@ -8,7 +22,7 @@
- Changed driver_url to include profiles. Without profiles machine_file was unable to recreate the driver
and subsequently failed. The new driver_url format is:
opennebula:<endpoint_url>:<profile>
where profile is stored in ~/.one/one_config or ENV['ONE_CONFIG'] or /var/lig/one/.one/one_config file
where profile is stored in ~/.one/one_config or ENV['ONE_CONFIG'] or /var/lib/one/.one/one_config file
- Added check for machine :destroy to verify that the VM is in DONE state. If not successful after 20 seconds
it will fail.
- opennebula 4.14 is not backwards compatible so there is a new gem dependency 'opennebula <4.14'.
Expand All @@ -24,12 +38,12 @@
## 0.3.0 (9/23/2015)
- Added one_user resource
- Added support for machine shutdown. Before 'machine :stop' would call the stop method on the VM,
now this behaviour can be changed to call vm.shutdown by specifying :bootstrap_options[:is_shutdown] = true
now this behavior can be changed to call vm.shutdown by specifying :bootstrap_options[:is_shutdown] = true
- Added optional bootstrap_option flag [:unique_name] for validation of unique machine names in OpenNebula
- Removed :instantiate from one_template resource, because it is a duplicate or 'machine :template'
- Fixed warnings in providers regarding resource_names
- Removed support for :ssh_execute_options => { :prefix => 'sudo '} in favour of :sudo => true
- Added licencse headers
- Added license headers
- Modified permissions for downloaded qcow images to be 777
- Added error message when :bootstrap_options are not defined
- Added missing :cache attribute to one_image resource
Expand All @@ -42,7 +56,7 @@

```json
:ssh_username => 'local',
:ssh_options => {
:ssh_options => {
:keys_only => false,
:forward_agent => true,
:use_agent => true,
Expand All @@ -66,8 +80,8 @@

## 0.2.1 (7/11/2015)

- Added upload/downlod functionality to one_image
- Added upload/download functionality to one_image

## 0.1.0 (4/30/2015)

- Initial submission of chef-provisioning-opennebula gem
- Initial submission of chef-provisioning-opennebula gem
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,6 @@ A sample one_config file would look like this:
}
```

If no profile is specified the driver will attempt to read and use the ```ENV['ONE_AUTH']``` file or ```~/.one/one_auth``` or ```/var/lib/one/.one/one_auth```.
The driver is still backward compatible with previous format and passing ```:secret_file``` or ```:credentials``` in```:driver_options```.

Additional options to OpenNebual client can be passed vi ```:one_options```.

```ruby
with_driver "opennebula:http://1.2.3.4/endpoint:port",
:credentials => "<username>:<text_password>",
:one_options => { :timeout => 3 }
```

or

```ruby
with_driver "opennebula:http://1.2.3.4/endpoint:port",
:secret_file => "<local_path_to_file_with_credentials>",
:one_options => { :timeout => 3 }
```

If additional OpenNebula client options need to be passed they can be specified as a hash ```:one_options => {}```.

In context of OpenNebula ```machine``` resource will take the following additional options:

```ruby
Expand All @@ -79,6 +58,7 @@ machine_options {
:enforce_chef_fqdn => [TrueClass, FalseClass] flag indicating if fqdn names should be used for machine names
:is_shutdown => [TrueClass, FalseClass] call vm.shutodwn instead of vm.stop during :stop action
:shutdown_hard => [TrueClass, FalseClass] flag indicating hard or soft shutdown
:mode => String octed to set permissions to the machine
},
:sudo => true,
:ssh_username => 'local',
Expand Down Expand Up @@ -109,6 +89,7 @@ This resource will allow to create and delete OpenNebula templates.
```ruby
:template => Hash defining OpenNebula template
:template_file => String location of the VM template file
:mode => String octet to set permissions
```

### Actions
Expand Down Expand Up @@ -201,6 +182,8 @@ This resource will manage images within OpenNebula.
:driver => String Image driver eq. 'qcow2'
:machine_id => [String, Integer] id of the machine (VM) for disk attach
:disk_id => [String, Integer] id or name of the disk to attach/snapshot
:mode => String octet to set permissions
:http_port => Integer port number to start local HTTP server at, for :image_file uploads. Default: 8066
```

### Actions
Expand Down Expand Up @@ -272,28 +255,39 @@ one_image "snapshot-img" do
end
```

#### 6. Upload a local qcow2 image file to OpenNebula
#### 6. Upload a local qcow2 image file to OpenNebula, starting HTTP server on port 4567

```ruby
one_image "snapshot-img" do
one_image "upload-img" do
datastore_id 103
image_file "/local/path/to/qcow/image/file"
img_driver "qcow2"
type "OS"
http_port 4567
description "This is my cool qcow image"
action :upload
end
```

#### 7. Download a 'boggi-test-img' and store it in /home/local/my-image.qcow2. Download URL read from ENV[ONE_DOWNLOAD] variable. It will be stored locally in Chef::Config[:file_cache_path]/boggi-test-img.qcow2.
#### 7. Upload a qcow2 image file residing on a different host to OpenNebula

```ruby
one_image "upload-img" do
datastore_id 103
download_url "http://my.image.host/path/to/qcow/image/file"
action :upload
end
```

#### 8. Download a 'boggi-test-img' and store it in /home/local/my-image.qcow2. Download URL is read from ENV[ONE_DOWNLOAD] variable. It will be stored locally in Chef::Config[:file_cache_path]/boggi-test-img.qcow2.

```ruby
one_image "boggi-test-img" do
action :download
end
```

#### 8. Download image ID 12345 and store it in /tmp/image.qcow2.
#### 9. Download image ID 12345 and store it in /tmp/image.qcow2.

```ruby
one_image "boggi-test-img" do
Expand All @@ -319,6 +313,7 @@ This resource will allow to create and delete OpenNebula vnets.
:mac_ip => String ip or mac address
:template_file => String local file containing the template of a new vnet
:cluster_id => Integer cluster in which to create a vnet
:mode => String octet to set permissions
```

### Actions
Expand Down
100 changes: 59 additions & 41 deletions lib/chef/provider/one_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def action_handler

def exists?
new_driver = driver
@image = new_driver.one.get_resource('img', :name => new_resource.name)
@image = new_driver.one.get_resource('img', :name => @new_resource.name)
!@image.nil?
end

Expand All @@ -56,14 +56,15 @@ def exists?

action_handler.perform_action "allocated image '#{new_resource.name}'" do
@image = new_driver.one.allocate_img(
new_resource.name,
new_resource.size,
new_resource.datastore_id,
new_resource.type || 'OS',
new_resource.fs_type || 'ext2',
new_resource.img_driver || 'qcow2',
new_resource.prefix || 'vd',
new_resource.persistent || false)
:name => new_resource.name,
:size => new_resource.size,
:datastore_id => new_resource.datastore_id,
:type => new_resource.type || 'OS',
:fs_type => new_resource.fs_type || 'ext2',
:driver => new_resource.img_driver || 'qcow2',
:prefix => new_resource.prefix || 'vd',
:persistent => new_resource.persistent || false)
new_driver.one.chmod_resource(@image, new_resource.mode)
Chef::Log.info("Image '#{new_resource.name}' allocate in initial state #{@image.state_str}")
@new_resource.updated_by_last_action(true)
end
Expand Down Expand Up @@ -143,6 +144,7 @@ def exists?
fail "Failed to create snapshot '#{new_resource.name}': #{@image.message}" if OpenNebula.is_error?(@image)

@image = new_driver.one.wait_for_img(new_resource.name, @image)
new_driver.one.chmod_resource(image, new_resource.mode)
if new_resource.persistent
action_handler.report_progress "make image '#{new_resource.name}' persistent"
@image.persistent
Expand All @@ -153,47 +155,63 @@ def exists?

action :upload do
fail "'datastore_id' is required" unless new_resource.datastore_id
fail "'image_file' is required" unless new_resource.image_file
fail "image_file #{new_resource.image_file} does not exist" unless ::File.exist? new_resource.image_file
fail "'image_file' or 'download_url' attribute is required" unless new_resource.image_file || new_resource.download_url

file_url = "http://#{node['ipaddress']}/#{::File.basename(@new_resource.image_file)}"
description = @new_resource.description || "#{@new_resource.name} image"
image_driver = @new_resource.img_driver || 'qcow2'
file_url = nil
if new_resource.image_file
fail "image_file #{new_resource.image_file} does not exist" unless ::File.exist? new_resource.image_file
file_url = "http://#{node['ipaddress']}:#{@new_resource.http_port}/#{::File.basename(@new_resource.image_file)}"
else
file_url = new_resource.download_url
end
image_config = {
:name => @new_resource.name,
:datastore_id => @new_resource.datastore_id.to_s,
:path => file_url,
:driver => @new_resource.img_driver || 'qcow2',
:description => @new_resource.description || "#{@new_resource.name} image",
:type => @new_resource.type,
:mode => @new_resource.mode,
:prefix => @new_resource.prefix,
:persistent => @new_resource.persistent,
:public => @new_resource.public,
:target => @new_resource.target,
:disk_type => @new_resource.disk_type,
:source => @new_resource.source,
:size => @new_resource.size,
:fs_type => @new_resource.fs_type
}

if exists?
if @image.name == @new_resource.name &&
@image['PATH'] == file_url &&
@image['TEMPLATE/DRIVER'] == image_driver &&
@image['TEMPLATE/DESCRIPTION'] == description &&
@image['DATASTORE_ID'] == @new_resource.datastore_id.to_s
if @image.name == image_config[:name] &&
@image['PATH'] == image_config[:path] &&
@image['TEMPLATE/DRIVER'] == image_config[:driver] &&
@image['TEMPLATE/DESCRIPTION'] == image_config[:description] &&
@image['DATASTORE_ID'] == image_config[:datastore_id]
action_handler.report_progress("image '#{@new_resource.name}' (ID: #{@image.id}) already exists - nothing to do")
else
fail "image '#{new_resource.name}' already exists, but it is not the same image"
end
else
action_handler.perform_action "upload image '#{@new_resource.image_file}'" do
begin
pid = Process.spawn("sudo python -m SimpleHTTPServer 80", :chdir => ::File.dirname(@new_resource.image_file), STDOUT => "/dev/null", STDERR => "/dev/null", :pgroup => true)
fail "Failed to start 'SimpleHTTPServer'" if pid.nil?
new_driver.one.upload_img(
@new_resource.name,
@new_resource.datastore_id,
file_url,
image_driver,
description,
@new_resource.type,
@new_resource.prefix,
@new_resource.persistent,
@new_resource.public,
@new_resource.target,
@new_resource.disk_type,
@new_resource.source,
@new_resource.size,
@new_resource.fs_type)

action_handler.perform_action "upload image '#{@new_resource.name}'" do
if @new_resource.image_file
begin
success = false
pid = nil
trap("CLD") do
cpid = Process.wait
fail "Could not start HTTP server on port #{@new_resource.http_port}" if cpid == pid && !success
end
pid = Process.spawn("python -m SimpleHTTPServer #{@new_resource.http_port}", :chdir => ::File.dirname(@new_resource.image_file), STDOUT => "/dev/null", STDERR => "/dev/null", :pgroup => true)
new_driver.one.upload_img(image_config)
success = true
@new_resource.updated_by_last_action(true)
ensure
system("sudo kill -9 -#{pid}")
end
else
new_driver.one.upload_img(image_config)
@new_resource.updated_by_last_action(true)
ensure
system("sudo kill -9 -#{pid}")
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/chef/provider/one_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def exists?
template_str = new_driver.one.create_template(new_resource.template) if new_resource.template
template_str << "\nNAME=\"#{new_resource.name}\""
@template = new_driver.one.allocate_template(template_str)
new_driver.one.chmod_resource(@template, new_resource.mode)
@new_resource.updated_by_last_action(true)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/chef/provider/one_vnet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def exists?(filter)
vnet = new_driver.one.allocate_vnet(template_str, @new_resource.cluster_id)
Chef::Log.debug(template_str)
fail "failed to create vnet '#{@new_resource.name}': #{vnet.message}" if OpenNebula.is_error?(vnet)
new_driver.one.chmod_resource(vnet, new_resource.mode)
@new_resource.updated_by_last_action(true)
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/chef/provisioning/opennebula_driver/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ def load_profiles
begin
content_hash = JSON.parse(File.read(file), :symbolize_names => true)
content_hash.each { |k, v| json[k.to_s] = v }
rescue Exception => e
Chef::Log.warn("Failed to read and parse config file #{file}: #{e.message}")
rescue StandardError => e_file
Chef::Log.warn("Failed to read config file #{file}: #{e_file.message}")
rescue JSON::ParserError => e_json
Chef::Log.warn("Failed to parse config file #{file}: #{e_json.message}")
rescue
Chef::Log.warn("Failed to read or parse config file #{file}: #{$!}")
end
@credentials.merge!(json)
end
Expand Down
Loading

0 comments on commit b65d105

Please sign in to comment.