8

I'm trying to use vagrant to set up a dev environment that automatically clones two repositories if they haven't already been cloned.

I wrote a simple script to clone the repos, after failing in many, many ways to get puppet to run the git command directly. For some reason I thought this method would be foolproof, but it turns out I'm a better fool than I thought.

exec {"load-repos":
    command =>"/bin/bash /vagrant/manifests/modules/scripts/clone_repos.sh",
    require => Package["git-core"],
  }

Here's the script:

#!/bin/bash
if [ ! -d /vagrant/repo-one-dest ]; then
  git clone git@example.com:/repo-one.git /vagrant/repo-one-dest
fi

if [ ! -d /vagrant/repo-two-dest ]; then
  git clone git@example.com:/repo-two.git /vagrant/repo-two-dest
fi

exit

The private keys are set up properly. When I log into the vm and manually run bash clone_repos.sh, everything works. No matter how many times I reload vagrant and let puppet do its thing, the repos are never loaded via the exec. What am I missing?

jeremiahs
  • 3,985
  • 8
  • 25
  • 30
  • Missing space on the first `[` test. – ormaaj Jun 23 '12 at 22:13
  • Thanks for pointing that out. Unfortunatley, that's an artifact I introduced into the post when I replaced the real directory with 'repo-one-dest'. The script functions correctly when run from the commandline and has the space. I edited the post to remove the syntax error. – jeremiahs Jun 23 '12 at 22:16
  • 1
    Adding a 'logoutput=>true," to your Exec block may result in more output that may help with debugging. You may also want to add a file["/vagrant/manifests/modules/scripts/clone_repos.sh"] to the require section. Puppet may be trying to execute the script before puppet's pushed it to your VM. – pwan Jun 25 '12 at 18:36
  • I'll try that when I get home tonight. Fairly 100% positive that it's not an issue with the file not existing on the path, but hey, I've been wrong before. – jeremiahs Jun 25 '12 at 21:27
  • BTW: If someone reading this has the time and inclination to solve this problem with a public git repo and post the solution, that would be helpful. – jeremiahs Jun 25 '12 at 21:53

1 Answers1

4

This is probably because when you vagrant ssh you're usually logging in as the vagrant user (by default, though this can certainly be modified via configuration). The vagrant user I'm guessing has the keys setup properly.

When Vagrant runs your provisioner (Puppet, in this case), however, it does a sudo, so it runs as the root user.

The way I generally recommend setting up keys to do the deploy is to put the keys somewhere, and then use a GIT_SSH wrapper to properly clone it, or to use SSH agents.

Mitchell
  • 32,819
  • 6
  • 42
  • 35
  • Good insight. Not sure if it works, but it seems plausible. I wound up altering my workflow, and at this point I'm fine with how things are and probably won't go back to my original plan. If I get some free time I may eventually see if that's the issue. – jeremiahs Aug 23 '12 at 23:03