0

I want to checkout several SVN projects and I need to be able to choose the directory names for each branch or trunk.

Now I want to check out the SVN project called svn_name in the folder service_dir and inside that folder I want to create a trunk folder and a branches folder with all branches.

---SVN folder

--------branches

--------------- branch1_name

--------------- branch2_name

------- trunk_name

I have the following structure of my hash

$hash = { 
         'svn_name' => { 
             service_dir  => 'directory_name',
                   branch => [ { branch => '0.1', branch_dir => 'branch1_name'}, 
                               { branch => '0.2', branch_dir => 'branch2_name'} ],
                    trunk => {  service_dir => 'trunk_name'},
        }
}

Now I use create_resources with the hash and my defined type to create the necessary folders and checkout the working copies.

The problem is that I'm unable to iterate through my branch array, I don't know how to access the values inside the hash.

define test (
  $service_dir,
  $branch,
  $trunk
){
  file { "/xxx/${service_dir}/branches/${branch[branch_dir]}":
    ensure => present
  }
}

How do I access the variables? Or is there a much easier way to accomplish this? I don't want to use vcsrepo or similar modules because I'm still new to Puppet and need to practice.

Edit1: I probably don't need the hash for the trunk, right? trunk => 'trunk_name' should be enough

2 Answers 2

1

To help you achieving your goal faster I'd highly recommend using the vcsrepo type provided by PuppetLabs on github or puppet forge. This wraps most common VCS tools, including subversion. Just to prevent you from reinventing the wheel.

About your puppet syntax question, I've adapted the example from the manual slightly to fit your problem:

$foo = "key"

$myhash = [ { key       => "some value 0",
              other_key => "some other value 0" },
            { key       => "some value 1",
              other_key => "some other value 1" } ]
notice( $myhash[0][$foo] )
notice( $myhash[1][$foo] )

Testing it:

% puppet apply foo.pp
Notice: Scope(Class[main]): some value 0
Notice: Scope(Class[main]): some value 1
Notice: Finished catalog run in 0.08 seconds
1

You can use the each function (I think it was implemented in puppet 4.6) to iterate through hashes and arrays to define resources.

$hash = {
  'svn_name' => {
      service_dir  => 'directory_name',
      branch => [
          { branch => '0.1', branch_dir => 'branch1_name'},
          { branch => '0.2', branch_dir => 'branch2_name'} ],
      trunk => {  service_dir => 'trunk_name'},
  }
}

$hash['svn_name']['branch'].each | $branch | {
  notify { "TestNotify ${branch[branch]}\\${branch[branch_dir]}" : }
}

I only showed doing one 'each' function, but you do an each function inside of an each function, but it wouldn't make sense with your scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.