From 2b4ece5fb7cc2cb8d5529968b8645b981c9959f4 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Sat, 18 Apr 2015 11:31:01 -0400
Subject: [PATCH] Add an ansible playbook for project renames

There are still a few manual tasks, but this takes care of the vast
majority of them.

Change-Id: I919ba42b0d22126719daa7ad308f75ce021720b7
---
 doc/source/gerrit.rst       | 75 ++++++-------------------------------
 playbooks/rename_repos.yaml | 74 ++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 63 deletions(-)
 create mode 100644 playbooks/rename_repos.yaml

diff --git a/doc/source/gerrit.rst b/doc/source/gerrit.rst
index 68df56f329..c8c21db60c 100644
--- a/doc/source/gerrit.rst
+++ b/doc/source/gerrit.rst
@@ -452,66 +452,21 @@ To rename a project:
    not currently in progress.  When it finishes, make sure the entry
    has not been added back to the crontab.
 
-#. Gracefully stop Zuul on zuul.openstack.org::
+#. Prepare a yaml file called repos.yaml that has a single dictionary called
+   `repos` with a list of dictionaries each having an old and new entry.
+   Optionally also add a `gerrit_groups` dict of the same form::
 
-     python /opt/zuul/tools/zuul-changes.py http://zuul.openstack.org gate >gate.sh
-     python /opt/zuul/tools/zuul-changes.py http://zuul.openstack.org check >check.sh
-     sudo invoke-rc.d zuul stop
-     sudo rm -f /var/run/zuul/zuul.pid /var/run/zuul/zuul.lock
+     repos:
+     - old: stackforge/awesome-repo
+       new: openstack/awesome-repo
+     gerrit_groups:
+     - old: old-core-group
+       new: new-core-group
 
-#. Stop Gerrit on review.openstack.org::
+#. Run the ansible rename repos playbook, passing in the path to your yaml
+   file::
 
-     sudo invoke-rc.d gerrit stop
-
-#. Update the database on review.openstack.org::
-
-     sudo -H mysql reviewdb
-
-     update account_project_watches
-     set project_name = "openstack/NEW"
-     where project_name = "openstack/OLD";
-
-     update changes
-     set dest_project_name = "openstack/NEW", created_on = created_on
-     where dest_project_name = "openstack/OLD";
-
-#. Move both the Git repository and the mirror on
-   review.openstack.org::
-
-     sudo mv ~gerrit2/review_site/git/openstack/{OLD,NEW}.git
-     sudo mv /opt/lib/git/openstack/{OLD,NEW}.git
-
-#. Reindex the Lucene search index on review.openstack.org::
-
-     sudo su - gerrit2
-     cp -ax review_site/index index.backup.`date +%s`
-     java -jar review_site/bin/gerrit.war reindex -d /home/gerrit2/review_site --threads 4
-
-#. Move the Git repository on git{01-08}.openstack.org (while the
-   Lucene reindex is running)::
-
-     sudo mv /var/lib/git/openstack/{OLD,NEW}.git
-
-#. Rename the project or transfer ownership in GitHub (while the
-   Lucene reindex is running).
-
-#. Start Gerrit on review.openstack.org::
-
-     sudo invoke-rc.d gerrit start
-
-#. Start Zuul on zuul.openstack.org::
-
-     sudo invoke-rc.d zuul start
-     sudo bash gate.sh
-     sudo bash check.sh
-
-#. Rename any associated groups whose names may have changed::
-
-     ssh -p 29418 review.openstack.org gerrit rename-group OLDNAME NEWNAME
-
-#. If renamed/transfered projects using StoryBoard, update the db:
-
-     update projects set name='stackforge/NEW' where name='openstack/OLD';
+     sudo ansible-playbook -f 10 /opt/system-config/production/playbooks/rename_repos.yaml -e repolist=ABSOLUTE_PATH_TO_VARS_FILE
 
 #. Merge the prepared Puppet configuration change, removing the
    original Jenkins jobs via the Jenkins WebUI later if needed.
@@ -520,12 +475,6 @@ To rename a project:
 
      sudo crontab -u root -e
 
-#. If this is an org move and the project name itself is not
-   changing, gate jobs may fail due to outdated remote URLs. Clear
-   the workspaces on persistent Jenkins slaves to mitigate this::
-
-     sudo ansible-playbook -f 10 /opt/system-config/production/playbooks/clean_workspaces.yaml --extra-vars "project=PROJECTNAME"
-
 #. Submit a change that updates .gitreview with the new location of the
    project.
 
diff --git a/playbooks/rename_repos.yaml b/playbooks/rename_repos.yaml
new file mode 100644
index 0000000000..6400949123
--- /dev/null
+++ b/playbooks/rename_repos.yaml
@@ -0,0 +1,74 @@
+---
+- hosts: zuul.openstack.org
+  gather_facts: False
+  remote_user: root
+  sudo: yes
+  tasks:
+  - shell: kill -USR1 $(cat /var/run/zuul/zuul.pid)
+  - shell: rm -f /var/run/zuul/zuul.pid /var/run/zuul/zuul.lock
+- hosts: review.openstack.org
+  gather_facts: False
+  remote_user: root
+  sudo: yes
+  tasks:
+  - shell: invoke-rc.d gerrit stop
+  - include_vars: "{{ repolist }}"
+  - shell: echo 'update account_project_watches set project_name = "{{ item.new }}" where project_name = "{{ item.old }}";' | mysql reviewdb
+    with_items: repos
+  - shell: echo 'update changes set dest_project_name = "{{ item.new }}", created_on = created_on where dest_project_name = "{{ item.old }}";' | mysql reviewdb
+    with_items: repos
+  - shell: mv {{ item[0] }}{{ item[1].old }} {{ item[0] }}{{ item[1].new }}
+    with_nested:
+    - [ '~gerrit2/review_site/git/', '/opt/lib/git' ]
+    - repos
+- hosts: 'git0*'
+  gather_facts: False
+  tasks:
+  - include_vars: "{{ repolist }}"
+  - shell: mv {{ item[0] }}{{ item[1].old }} {{ item[0] }}{{ item[1].new }}
+    with_nested:
+    - [ '/var/lib/git' ]
+    - repos
+- hosts: storyboard.openstack.org
+  gather_facts: False
+  remote_user: root
+  sudo: yes
+  tasks:
+  - include_vars: "{{ repolist }}"
+  - shell: echo 'update projects set name="{{ item.new }}" where name="{{ item.old }}";' | mysql storyboard
+    with_items: repos
+- hosts: review.openstack.org
+  gather_facts: False
+  remote_user: gerrit2
+  sudo: yes
+  tasks:
+  - shell: cp -ax /home/gerrit2/review_site/index /home/gerrit2/index.backup.$(date +%s)
+  - shell: java -jar /home/gerrit2/review_site/bin/gerrit.war reindex -d /home/gerrit2/review_site
+- hosts: review.openstack.org
+  gather_facts: False
+  remote_user: root
+  sudo: yes
+  tasks:
+  - shell: invoke-rc.d gerrit start
+- hosts: zuul.openstack.org
+  gather_facts: False
+  remote_user: root
+  sudo: yes
+  tasks:
+  - shell: invoke-rc.d zuul start
+- hosts: review.openstack.org
+  gather_facts: False
+  remote_user: gerrit2
+  sudo: yes
+  tasks:
+  - include_vars: "{{ repolist }}"
+  - shell: ssh -p 29418 review.openstack.org gerrit rename-group {{ item.old }} {{ item.new }}
+    with_items: "{{ gerrit_groups|default([]) }}"
+- hosts: '*.slave.openstack.org'
+  remote_user: root
+  sudo: yes
+  gather_facts: False
+  tasks:
+  - include_vars: "{{ repolist }}"
+  - shell: 'rm -rf ~jenkins/workspace/*{{ item.old | basename }}*'
+    with_items: repos