otc-metadata/tools/bootstrap_repositories.py
2022-09-01 09:04:48 +02:00

157 lines
4.3 KiB
Python

#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import logging
import os
import pathlib
import subprocess
import warnings
from git import exc
from git import Repo
from git import SymbolicReference
from jinja2 import PackageLoader
from jinja2 import Environment
from jinja2 import select_autoescape
from cookiecutter.main import cookiecutter
import otc_metadata.services
data = otc_metadata.services.Services()
def create_repo(repo, repo_dir, service):
repo_owner, repo_name = repo['repo'].split('/')
git_repo = None
if repo_dir.exists():
logging.debug(f"Repository {repo['repo']} is already existing")
return
logging.info(f"Creating repository {repo_owner}/{repo_name}")
if repo['type'] == 'gitea':
repo_url = (
f"ssh://git@gitea.eco.tsi-dev.otc-service.com:2222/"
f"{repo['repo']}"
)
git_fqdn = "gitea.eco.tsi-dev.otc-service.com"
elif repo['type'] == 'github':
repo_url = f"git@github.com:/{repo['repo']}"
git_fqdn = "github.com"
try:
git_repo = Repo.clone_from(
repo_url,
repo_dir, branch='main')
except Exception:
logging.debug("Error")
pass
if git_repo:
return
if repo["type"] == "gitea":
subprocess.run(
args=["tea", "repo", "create",
"--name", repo_name,
"--owner", repo_owner,
]
)
elif repo["type"] == "github":
subprocess.run(
args=["gh", "repo", "create",
repo["repo"], "--public",
]
)
cookiecutter(
template="https://github.com/opentelekomcloud/docs-cookiecutter",
output_dir=repo_dir.parent,
no_input=True,
extra_context=dict(
git_fqdn=git_fqdn,
repo_group=repo_owner,
repo_name=repo_name,
project_short_description=(
f"Documentation project for {service['service_title']} "
"service"),
overwrite_if_exists=True
)
)
git_repo = Repo(repo_dir)
git_repo.create_remote('origin', repo_url)
git_repo.remotes.origin.fetch()
git_repo.git.push('--set-upstream', 'origin', 'main')
def process_repositories(args, service):
"""Checkout repositories
"""
logging.debug(f"Processing service {service}")
workdir = pathlib.Path(args.work_dir)
workdir.mkdir(exist_ok=True)
for repo in service["repositories"]:
logging.debug(f"Processing repository {repo}")
repo_dir = pathlib.Path(
workdir, repo["type"], repo["repo"]
)
if repo["environment"] != args.target_environment:
continue
checkout_exists = repo_dir.exists()
logging.debug(f"Repository {repo} exists {checkout_exists}")
repo_dir.parent.mkdir(parents=True, exist_ok=True)
if True: # not checkout_exists:
create_repo(repo, repo_dir, service)
def main():
parser = argparse.ArgumentParser(
description='Bootstrap repositories.')
parser.add_argument(
'--target-environment',
required=True,
help='Environment to be used as a source'
)
parser.add_argument(
'--service-type',
help='Service to update'
)
parser.add_argument(
'--work-dir',
required=True,
help='Working directory to use for repository checkout.'
)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
services = []
if args.service_type:
services = [data.service_dict.get(args.service_type)]
else:
services = data.all_services
for service in services:
process_repositories(args, service)
if __name__ == '__main__':
main()