Ask Your Question
0

how to strip the filename from the complete path [closed]

asked 2016-07-29 10:17:33 -0500

updated 2016-07-29 10:18:04 -0500

There are 3 files with the complete path as below:

       https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc-10.5.jar
       https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc4-10.5.jar
       https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc_license_cu-10.5.jar

I would like to create file resources in agent nodes with the above files in an array, the sample definition is as follows:

  $files_with_full_path = hiera('database_model/datasource1/urls_database_driver_jars',[])
  file { : ????
         ensure => 'present',
         mode   => '0644',
         owner  => 'root',
         group  => 'root',
         source => $files_with_full_path,
         path   => join([$::tomcat::catalina_home,'/lib/',$????])
   }

My question is, what do i fill in the question marks above, how do I strip the name of the file from the entire path? Is there an stdlib function that does that? Please provide an example.

Many thanks!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Redsmile
close date 2016-08-08 13:37:08.975842

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-07-29 13:50:03 -0500

DarylW gravatar image

updated 2016-08-01 23:05:24 -0500

Are you using puppet3 or puppet4? I guess either way is similar, one inside of a .each block, the other inside of a 'define', but what you can do is match the array with a regex, and use a matchgroup to pull out the file.

If you know that it's always going to be a .jar file, you can use something like the below inside of your block/define.

if $full_path =~ /(.*\/)(.*\.jar)/ {
  $base_path = $1
  $jar_file = $2

  file { $jar_file :
         ensure => 'present',
         mode   => '0644',
         owner  => 'root',
         group  => 'root',
         source => $full_path,
         path   => join([$::tomcat::catalina_home,'/lib/',$jar_file])
   }
}

--- EDIT ---

Below is an example using a define. Notice that I built a notify resource, but a combination of using either puppet-staging or puppet-archive to pull down the remote files, and then using a file resource to source them into the right location should be sufficient.

#example.pp
$jar_paths_csv =
'https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc-10.5.jar,https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc4-10.5.jar,https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc_license_cu-10.5.jar'

$jar_paths = split($jar_paths_csv, ',')

define make_jar(
  $remote_jar_path = $name,
) {
  if $remote_jar_path =~ /(.*\/)(.*\.jar)/ {
    $base_path = $1
    $jar_file = $2

    notify { $jar_file:
      message => join(['path','to','lib',$jar_file],'/'),

    }
  }
}

make_jar { $jar_paths: }

and here's the output

@ubuntu:~$ sudo puppet apply ~/example.pp
Notice: Compiled catalog for ubuntu in environment production in 0.09 seconds
Notice: path/to/lib/db2jcc-10.5.jar
Notice: /Stage[main]/Main/Make_jar[https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc-10.5.jar]/Notify[db2jcc-10.5.jar]/message: defined 'message' as 'path/to/lib/db2jcc-10.5.jar'
Notice: path/to/lib/db2jcc4-10.5.jar
Notice: /Stage[main]/Main/Make_jar[https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc4-10.5.jar]/Notify[db2jcc4-10.5.jar]/message: defined 'message' as 'path/to/lib/db2jcc4-10.5.jar'
Notice: path/to/lib/db2jcc_license_cu-10.5.jar
Notice: /Stage[main]/Main/Make_jar[https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc_license_cu-10.5.jar]/Notify[db2jcc_license_cu-10.5.jar]/message: defined 'message' as 'path/to/lib/db2jcc_license_cu-10.5.jar'
Notice: Applied catalog in 0.03 seconds

And using the each method with puppet4 syntax

$jar_paths_csv =
'https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc-10.5.jar,https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc4-10.5.jar,https://myrepo.domain.com/aaa/prod-cache/com.ibm/db2/10.5/jars/db2jcc_license_cu-10.5.jar'

$jar_paths = split($jar_paths_csv, ',')

$jar_paths.each |$remote_jar_path| {
  if $remote_jar_path =~ /(.*\/)(.*\.jar)/ {
    $base_path = $1
    $jar_file = $2

    notify { $jar_file:
      message => join(['path','to','lib',$jar_file],'/'),

    }
  }
}
edit flag offensive delete link more

Comments

Thank you very much. The full path is not the path to a single file, it is comma separated path for 3 or more files. So there will be 3 jar_file names, will this RegEx pick all three of them? I am using puppet 4.5.2. Please provide example of .each as well. Thanks in advance.

Redsmile gravatar imageRedsmile ( 2016-07-29 15:27:39 -0500 )edit

If you want to split the comma separated string, you can use the split method, and that will produce an array of each of the strings, which then can be fed to the above example.

DarylW gravatar imageDarylW ( 2016-08-01 22:28:26 -0500 )edit

Thank you very much, I tried the .each approach you suggested and it worked. My mistake earlier was that i was just trying to do the split or the .each - I indeed had to do both for it to work. My results from consul was a string so split made it work. Appreciate all your help!

Redsmile gravatar imageRedsmile ( 2016-08-02 09:54:07 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-29 10:17:33 -0500

Seen: 160 times

Last updated: Aug 01 '16