2017-10-26 8 views
1

OpenStacks Glanceでcirrosイメージを作成する人形クラスを作成しようとしています。Execリソースによって作成されたファイルをクリーンアップする方法はありますか?

私はこの人形クラスを持っています。イメージファイルをダウンロードしてrawに変換します。 その後、生画像フォーマットファイルを使用してグラン画像を作成します。 ダウンロードした画像ファイルと生の画像ファイルをローカルディスク から削除したいと考えています。ここで

は、私が試したマニフェストです:私はそれを実行すると

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', 
    } 
} 

私はこのエラーを取得する:インクルードは、exec年代は、実行させる

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' 

必要がなかったのはなぜ?

アップデート:私は私のマニフェストはこのように見えるように修正Mattの提案に基づいて:refreshonlyにごexecリソースを設定

exec { $download_command: 
    creates  => $target_file, 
    unless => "/usr/bin/openstack image list --format=value | cut -d' ' -f2 | grep \"^${image_name}$\"", 
    notify => Exec[$convert_command], 
} 

exec { $convert_command: 
    creates  => $raw_target_file, 
    refreshonly => true, 
} 

glance_image { $image_name: 
    ensure   => present, 
    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, 
} 

exec { "/bin/rm -f ${target_file}": 
    subscribe => Exec[$convert_command], 
    refreshonly => true, 
} 

file { $raw_target_file: 
    ensure => 'absent', 
    require => Glance_image[$image_name], 
} 

答えて

1

は、彼らがトリガーするリフレッシュ信号を必要として適用されることを意味しています。これはsubscribeまたはnotifyで行うことができます。

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

か:あなたの二execが最初に依存するので、あなたはこれを行うことができます

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

最初のものは、それが何との関係を確立していないので、トリッキーです。ファイルのダウンロードを偶数にする場合は、代わりにfileリソースを使用することをお勧めします。

file { $target_file: 
    source => $image_url, 
} 

これはあなたのリソースの両方が冪等させることが、あなたは彼らがしたいときにのみ適用されたときに、このようにあなたの目標を達成するでしょう。

イメージファイルの削除を変更してexecにする必要があります。このような何かが働くだろう:

exec { "/bin/rm -f ${target_file}": 
    subscribe => Exec[$convert_command] 
    refreshonly => true, 
} 

あなたのRAW画像ファイルの削除もその作成と使用後に適用する必要があります

file { $raw_target_file: 
    ensure => 'absent', 
    require => Glance_image[$image_name], 
} 
関連する問題