Ask Your Question
-3

How to count number of files in a directory using puppet script?

asked 2017-03-11 04:39:38 -0500

penny21 gravatar image
  • Both cases
    1. When it counts number of files only in a directory leaving sub directories
    2. Count number of files in a directory and also sub directories it is having
edit retag flag offensive close merge delete

Comments

I would use an exec resource to accomplish this, however this sounds like a bad use case of puppet. I would image whatever you are trying to accomplish, Puppet can solve in a better way.

puser gravatar imagepuser ( 2017-03-13 06:26:30 -0500 )edit
2

The question is not specific enough for you to get a reasonable answer. (a) are you using a puppet master or running masterless? (b) do you want the number of files on the master or the agent? (c) what do you need to do with the information you receive?

JohnsonEarls gravatar imageJohnsonEarls ( 2017-03-13 07:39:35 -0500 )edit

If you are trying to do some sort of cleanup of files in a directory, look to the `tidy` resource https://docs.puppet.com/puppet/latest/types/tidy.html . It would really help to know what you are trying to do, and answer some of @JohnsonEarls questions.

DarylW gravatar imageDarylW ( 2017-03-14 09:08:14 -0500 )edit

@Eric Anderson , JohnsonEarls , DarylW - Thanks for your feedback. Actually this question was asked in an Interview , Even I thought this is a bad use case of puppet but anyhow if interviewer is asking I tried doing same but was unsuccessful. Interviewer wanted a script to be written .pp file whic

penny21 gravatar imagepenny21 ( 2017-03-15 01:10:34 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2017-03-15 06:14:33 -0500

otheus gravatar image

The most portable and generic answer is to do this in Ruby using a custom Type and Ruby-based provider. The POSIX provider would be quite simple and do something like (1) find <dir> -type f -maxdepth 1 | wc -l and (2) find <dir> -type f | wc -l (all subdirs, but not directories themselves), but there are caveats, including hidden files. A Windows provider would do something different. The Ruby-based provider will need to do a lot of checking but would be most portable. Here's an untestesd source snippet:

 # ^^^ Provider scaffolding
 # Type: filecount
 # attributes:
 #   name (string) = path to count
 #   recurse (bool) = true to count files in subdirectories
 #   files-only (bool) = true to exclude directory entries themselves
 #
 # If recurse, then exclude symlinked directories. Avoids recursion, loops, etc
 @dirlist = []
 @count=0
 def count_files_in_dir (dir, recurse, files_only)
    d=Dir.new(dir)
    d.each { |ename|
      e = d.path + "/" + ename # get full path of current entry
      next if files-only && ! File.file?(e)
      ++@count
      dirlist << e  if recurse && File.directory?(e) && ! File.symlink?(e)
   }
 end

 # ^^^ Provider implementation block
 begin
   @dirlist += attributes["name"]
   while ! @dirlist.empty? do
     count_files_in_dir( @dirlist.shift, attributes["recurse"], attributes["files-only"] )
   end
   @count
 end

There's a lot of pseudo code here. And you of course must do the validate() routines on the bool attributes. You might need a "path" attribute to override the "name" in case you have two separate instances of the same path with different attributes, etc.

edit flag offensive delete link more
1

answered 2017-03-15 03:33:01 -0500

karthikeayan gravatar image

If an interviewer asking you these kind of question, then your best answer would be this:

Puppet is a declarative DSL, it brings up the state what it is written in manifest. Most of the time we just write to the system unlike we do it in scripting languages(reading and writing, reading some info from system).

If you are really interested to know how to read values there are few ways:

inline_template:

file { '/home/karthik/count.txt':
  content => inline_template('<%= `ls | wc -l` %>')
}

exec:

exec { 'count number of files':
  command => '/bin/ls | wc -l > /home/karthik/count.txt',
}

You can change the ls command as per your requirement.

edit flag offensive delete link more

Comments

with a note that the inline template is evaluated on the master, so you would only get the 'expected' result if you were running a puppet apply on the client where you care about the file count

DarylW gravatar imageDarylW ( 2017-03-15 11:34:00 -0500 )edit
0

answered 2017-03-15 01:09:06 -0500

penny21 gravatar image

@Eric Anderson , JohnsonEarls , DarylW - Thanks for your feedback.

Actually this question was asked in an Interview , Even I thought this is a bad use case of puppet but anyhow if interviewer is asking I tried doing same but was unsuccessful.

Interviewer wanted a script to be written .pp file which runs and actually count number of files in a folder and then create another file and right the count in new file. Please help me if you know how to write puppet script to count number of files from any existing folder in your system.

edit flag offensive delete link more

Comments

The only solutions I can think of are the Type and Provider answer above (where your custom type would do it, but that's not in a .pp 'script'), having a custom fact that would give the information to the catalog compilation, or if you are running a puppet 'script' on a server via...

DarylW gravatar imageDarylW ( 2017-03-15 11:35:56 -0500 )edit

... puppet apply, then you could use either inline_template or generate to run a command locally to get a value back from the system, which you could then place into a file later. If you really want to, you can wrap anything with an exec and have it do what you want, but then you are not really ...

DarylW gravatar imageDarylW ( 2017-03-15 11:37:03 -0500 )edit

... using puppet to do the work, you are using puppet to orchestrate a script or bash oneliner to do something on the system, and that is arguably not the best use of puppet. There are lots of times where I am asked 'How do I do X with puppet' and I try to step back and see what the larger ...

DarylW gravatar imageDarylW ( 2017-03-15 11:37:58 -0500 )edit

... goal or objective is, and see if we can break that into a more appropriate solution for the larger problem.

DarylW gravatar imageDarylW ( 2017-03-15 11:38:50 -0500 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2017-03-11 04:39:38 -0500

Seen: 214 times

Last updated: Mar 15