2

tl;dr; hg clone ssh://hg@bitbucket.org/team/repo ~/prod/ fails with "destination is not empty" if ~/prod/ is not empty. Can I force cloning?

I am trying to write my first Ansible playbook that should deploy my code from a Bitbucket Mercurial repository to my server. There is a deployment path, ~/prod, which contains all code files as well as the data in ~/prod/media and ~/prod/db.db. To make sure the playbook works even if the ~/prod directory is empty or doesn't exist, this is what I have so far:

- name: create directory
  file: path=/home/user/prod state=directory
- name: clone repo
  hg:
    repo: ssh://hg@bitbucket.org/team/repo
    dest: /home/user/prod
    force: yes

In my understanding, it ensures that the deployment directory exists and then clones the repo there. It works beautifully if the directory doesn't exist or is empty. However, as soon as I've cloned the repo once, this playbook fails with destination is not empty.

I can move media and db.db out first, then delete all other files, then clone, then move the data back. But it looks cumbersome.

I simply want to force cloning. But I cannot find the way to do it. Presumably this is so wrong that Mercurial won't allow me doing this. Why and what's a better way to go?

texnic
  • 3,959
  • 4
  • 42
  • 75
  • 2
    Possible duplicate of [How do clone a Mercurial repository into a directory that already exists?](https://stackoverflow.com/questions/3622374/how-do-clone-a-mercurial-repository-into-a-directory-that-already-exists) – ngoldbaum Aug 04 '17 at 01:21
  • @ngoldbaum, thanks, I've seen that thread, but while providing the options, it didn't answer the question if such cloning is possible. I'll assume it's not. – texnic Aug 04 '17 at 07:22

1 Answers1

6

Though I haven't yet read it anywhere, looks like force-cloning is impossible. The two alternatives then are, as explained in another thread on the same topics:

  • indeed, clone the .hg folder to another directory and then move it to the target directory
  • or, hg init /home/user/prod and then hg pull ssh://hg@bitbucket.org/team/repo /home/user/prod; hg update -C -R /home/user/prod.

With the second one, it is possible to optimise the Ansible task, to perform this action only if the target directory doesn't contain .hg:

- name: recreate repo
  command: 
    hg ssh://hg@bitbucket.org/team/repo -R /home/user/prod 
    creates=/home/user/prod/.hg  # <-- only execute command if .hg does not exist
- name: update files
  hg:
    repo: ssh://hg@bitbucket.org/team/repo
    dest: /home/user/prod
    clone: no
    update: yes                  # optional, for readability
    force: yes
  notify: "restart web services"
texnic
  • 3,959
  • 4
  • 42
  • 75