Ask Your Question
0

Deleting files older than two months using puppet

asked 2017-03-31 00:31:37 -0500

queszama gravatar image

updated 2017-03-31 00:47:30 -0500

I need to delete all files in a directory which are older than two months. For this I am using tidy resource. The file naming convention inside the directory are as follows:

  -rw-r--r-- 1 root     admin 61446360 Dec  5 17:59 myapp-179.war
  -rw-r--r-- 1 root     admin 51443750 Dec  5 18:18 hisapp-6.1.war
  -rw-r--r-- 1 root     admin 51444181 Dec  6 09:56 myapp-6.2.war
  -rw-r--r-- 1 root     admin 61446361 Dec  6 13:02 hisapp-180.war

For this to work , I have defined the below manifests .

tidy { "/etc/puppetlabs/code/environments/qa/modules/helloworld/files"}:
     path=> "/etc/puppetlabs/code/environments/qa/modules/helloworld/files",
     age => "9w"    
     recurse => true,
     matches => [ 'myapp-*.war','hisapp-*.war' ],
  }

Will the above manifests will work to delete all files older than 9 weeks. Since this is a production system I want to make sure whether the rule is fine before I apply it on system. Puppet version is as follows:

      $ puppet --version
       4.4.1
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-03-31 06:21:14 -0500

puser gravatar image

An easy way to figure this our is to do a --noop operation on this system alongside your puppet execution.

puppet agent -t --environment production --noop

obviously if the environment is different than production then change to whatever environment branch you are testing off of. This will run your manifests without actually running the code. It will output all the changes but it will not take any actions. Very useful for determining if the code will fire or not. Also it may be beneficial to stand up a sandbox server for testing or if that is not available, do some testing around the /tmp directory instead.

Also as people on this any other sites may be helpful for debugging, advice, solutions, etc. I wouldn't base a decision of whether or not you need to can safely deploy your code based on our answers.

edit flag offensive delete link more

Comments

I second the sandbox suggestion. You also could copy the existing files to a different directory on the server, and add a `tidy` resource pointing at the copy of the files to see if it behaves as expected.

DarylW gravatar imageDarylW ( 2017-03-31 07:39:43 -0500 )edit
1

answered 2017-04-11 05:39:48 -0500

queszama gravatar image

updated 2017-04-11 07:41:18 -0500

DarylW gravatar image

I tried using tidy resource manifests on a test box to remove files based on certain retention and it worked fine. My manifests is as follows . It will deletes files more than 9 weeks old which matches 'myapp-.war' and 'hisapp-.war'

node "Host-003" {
     tidy {"/home/admin/temp/":
     path=> "/home/admin/temp/",
     age => "9w",
     recurse => true,
     matches => [ 'myapp-*.war','hisapp-*.war' ],
  }
}

Output is as follows after executing

       # puppet agent --verbose --no-daemonize --onetime
       Info: Using configured environment 'production'
       Info: Retrieving pluginfacts
       Info: Retrieving plugin
       Info: Caching catalog for aus-lnaswgp-003.wiley.com
       Notice: /Stage[main]/Main/Node[host-003]/Tidy[/home/admin/temp/]: Tidying 16 files
       Info: Applying configuration version '1491903867'
       Notice: /Stage[main]/Main/Node[host-003]/File[/home/admin/temp/myapp-9.war]/ensure: removed
       Notice: /Stage[main]/Main/Node[host-003]/File[/home/admin/temp/myapp-8.war]/ensure: removed
       Notice: /Stage[main]/Main/Node[host-003]/File[/home/admin/temp/hisapp-7.war]/ensure: removed
       Notice: /Stage[main]/Main/Node[host-003]/File[/home/admin/temp/hisapp-6.war]/ensure: removed
       Notice: Applied catalog in 0.05 seconds
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-03-31 00:31:37 -0500

Seen: 53 times

Last updated: Apr 11