Ask Your Question
0

How to clean up files created by Exec resources?

asked 2017-10-26 11:23:47 -0600

Red Cricket gravatar image

I am trying to write a puppet class that will create a cirros image with OpenStacks Glance.

I have this puppet class. It downloads the image file and converts it to raw. It then creates the glance image using the raw image format file. I also want to remove the downloaded image file and the raw image file from local disk.

Here is the manifest I tried:

class create_glance_cirros_image (
    $cirrosver           = '0.3.5',
    $cirros_download_url = 'http://download.cirros-cloud.net',
    $curl                = '/usr/bin/curl',
    $download_dir        = '/root',
    $qemu_img            = '/usr/bin/qemu-img',
    $qemu_img_args       = 'convert -f qcow2 -O raw',
    $image_name          = 'cirros',
    $is_public           = 'no',
    $container_format    = 'bare',
    $disk_format         = 'raw',
    $min_ram             = '1024',
    $min_disk            = '1',
    $properties          = { 'img_key' => img_value },
    $ensure              = 'present',
) {
    $cirros_image = "cirros-${cirrosver}-x86_64-disk.img"
    $raw_cirros_image = "cirros-${cirrosver}-x86_64-disk.raw"
    $image_url = "${cirros_download_url}/${cirrosver}/${cirros_image}"
    $target_file = "${download_dir}/${cirros_image}"
    $raw_target_file = "${download_dir}/${raw_cirros_image}"
    $curl_args = "--output ${target_file}"
    $download_command = "${curl} ${curl_args} ${image_url}"
    $convert_command = "${qemu_img} ${qemu_img_args} ${target_file} ${raw_target_file}"

    exec { $download_command:
       creates     => $target_file,
       refreshonly => true,
    }
    exec { $convert_command:
       creates     => $raw_target_file,
       refreshonly => true,
       require    => Exec[$download_command],
    }

    glance_image { $image_name:
       ensure           => $ensure,
       name             => $image_name,
       is_public        => $is_public,
       container_format => $container_format,
       disk_format      => $disk_format,
       source           => $raw_target_file,
       min_ram          => $min_ram,
       min_disk         => $min_disk,
       properties       => $properties,
       require         => Exec[$convert_command],
    }

    file { $target_file:
       ensure => 'absent',
    }
    file { $raw_target_file:
       ensure => 'absent',
    }
}

When I run it I get this error:

Error: Execution of '/usr/bin/openstack image create --format shell cirros --private --container-format=bare --disk-format=raw --min-disk=1 --min-ram=1024 --property img_key=img_value --file=/root/cirros-0.3.5-x86_64-disk.raw' returned 1: [Errno 2] No such file or directory: '/root/cirros-0.3.5-x86_64-disk.raw'
Error: /Stage[main]/Create_glance_cirros_image/Glance_image[cirros]/ensure: change from absent to present failed: Execution of '/usr/bin/openstack image create --format shell cirros --private --container-format=bare --disk-format=raw --min-disk=1 --min-ram=1024 --property img_key=img_value --file=/root/cirros-0.3.5-x86_64-disk.raw' returned 1: [Errno 2] No such file or directory: '/root/cirros-0.3.5-x86_64-disk.raw'

Why didn't the require cause the exec's to execute?

edit retag flag offensive close merge delete

Comments

Some of your code is contradictory: On the hand Exec[$download_command] creates $target_file, but File[$taget_file] is supposed to be absent. On the one hand Exec[$convert_command] creates $raw_target_file, but File[$raw_target_file] is supposed to be absent. The latter resource declaration wins.

Kai Burghardt gravatar imageKai Burghardt ( 2017-10-26 15:05:41 -0600 )edit

I have no idea, what refreshes your Exec[] resources, but `require` only orders the resources in a chronological manner. It does not “pull” dependencies, as you might've expected. — Do you actually notify your class anywhere?

Kai Burghardt gravatar imageKai Burghardt ( 2017-10-26 15:10:59 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2017-10-27 10:40:32 -0600

Red Cricket gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-10-26 11:23:47 -0600

Seen: 30 times

Last updated: Oct 27