Add support for initial project creation.

If replicate_local is set, this will ensure that /var/lib/git is created,
and that projects listed in the projects.config have repos there.

Additionally, it creates a new config file, projects.config which is a
yaml file listing all of the projects and various operational semantics about
them, such as whether or not they should have pull requests closed and whether
or not they track any remotes. This replaces remotes.config and github.config.

Moving forward, there is no reason to not have this script be able to
do github api calls to create the github repo if it's not there, set the
github project description, gerrit api calls to create the project in gerrit,
and initial project permissions templates.

Change-Id: I1ad803b0aa5f7386206d0c3f4cd858017242fe64
diff --git a/fetch_remotes.py b/fetch_remotes.py
index 776188e..4dda133 100755
--- a/fetch_remotes.py
+++ b/fetch_remotes.py
@@ -13,18 +13,19 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-# Ensure that the specified remote exists in the repo and pull revisions
-# from upstream
+# Fetch remotes reads a project config file called projects.yaml
+# It should look like:
 
-# [project "UPSTREAM_PROJECT"]
-# remote = https://gerrit.googlesource.com/gerrit
+# - project: PROJECT_NAME
+#   options:
+#   - remote: https://gerrit.googlesource.com/gerrit
 
-import ConfigParser
+
 import logging
 import os
-import re
 import subprocess
 import shlex
+import yaml
 
 def run_command(cmd, status=False, env={}):
     cmd_list = shlex.split(str(cmd))
@@ -46,29 +47,22 @@
 
 REPO_ROOT = os.environ.get('REPO_ROOT',
                            '/home/gerrit2/review_site/git')
-REMOTES_CONFIG = os.environ.get('REMOTES_CONFIG',
-                               '/home/gerrit2/remotes.config')
+PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
+                               '/home/gerrit2/projects.yaml')
 
-PROJECT_RE = re.compile(r'^project\s+"(.*)"$')
+config = yaml.load(open(PROJECTS_YAML))
 
-config = ConfigParser.ConfigParser()
-config.read(REMOTES_CONFIG)
+for section in config:
+    project = section['project']
 
-
-for section in config.sections():
-    # Each section looks like [project "openstack/project"]
-    m = PROJECT_RE.match(section)
-    if not m:
+    if 'remote' not in section:
         continue
-    project = m.group(1)
+
     project_git = "%s.git" % project
     os.chdir(os.path.join(REPO_ROOT, project_git))
 
-    if not (config.has_option(section, "remote")):
-        continue
-
     # Make sure that the specified remote exists
-    remote_url = config.get(section, "remote")
+    remote_url = section['remote']
     # We could check if it exists first, but we're ignoring output anyway
     # So just try to make it, and it'll either make a new one or do nothing
     run_command("git remote add -f upstream %s" % remote_url)