Nam Nguyen
Nam Nguyen

Reputation: 5750

mktemp command in Chef

does anyone know if Chef has a resource that is similar mktemp command on linux?

Basically, I'm looking for a way to download a file remotely and save it under /tmp but I want it save to a uniq file name

remote_file "/tmp/ec2-ami-tools.zip" do
  source "http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools-1.5.6.zip"
  action :create
end

instead of using /tmp/ec2-ami-tools.zip as the destination file name, I want to have a safe uniq file name. Beside using datetime random, is there an official resource doing that in Chef? I'm wondering if Chef has resource that can generate UUID

Thanks

Upvotes: 1

Views: 259

Answers (1)

Tensibai
Tensibai

Reputation: 15784

To exactly answer your question (creating a tempfile):

require 'tempfile'

mydest = Tempfile.new('ec2-ami-tools.zip')

remote_file mydest do 
  source "http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools-1.5.6.zip"
  action :create
  notifies :run,"execute[unzip #{mydest}", :immediately
end
execute "unzip #{mydest}" do
  action :nothing
end

I highly discourage doing that as at each run the file would be downloaded and unzipped (the Tempfile will change) without any idempotency of the recipe.


The ark cookbook can help you on this case.

It has a LighWeight Resource Provider aimed exactly at this goal, downloading a tarball, extracting it and optionnaly doing something after that.

Exemple from the ark README:

This example copies ivy.tar.gz to /var/cache/chef/ivy-2.2.0.tar.gz, unpacks its contents to /usr/local/ivy-2.2.0/ -- stripping the leading directory, and symlinks /usr/local/ivy to /usr/local/ivy-2.2.0

 # install Apache Ivy dependency resolution tool
 ark "ivy" do
   url 'http://someurl.example.com/ivy.tar.gz'
   version '2.2.0'
   checksum '89ba5fde0c596db388c3bbd265b63007a9cc3df3a8e6d79a46780c1a39408cb5'
   action :install
 end

Adapted to your exemple I would do something like this:

ark 'ec2-ami-tools' do
  url 'http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools-1.5.6.zip'
  version '1.5.6'
  action :install
end

The checksum will avoid a call to the webserver to see if the file has changed as the on disk zip checksum will be compared with the resource checksum and if they match it will stop at this point.

The ckecksum is a sha256sum of the file.

Upvotes: 1

Related Questions