mirror of
https://github.com/khuedoan/homelab.git
synced 2024-12-22 21:04:54 +07:00
feat(gitea): auto init GitOps repo and org
This commit is contained in:
parent
6a7709cec9
commit
83a33a2dd5
@ -86,12 +86,13 @@ Distributed under the GPLv3 License. See `LICENSE` for more information.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
- ArgoCD usage in [my coworker's homelab](https://github.com/locmai/humble)
|
||||
- [ArgoCD usage in my coworker's homelab](https://github.com/locmai/humble)
|
||||
- [README template](https://github.com/othneildrew/Best-README-Template)
|
||||
- [Run the same Cloudflare Tunnel across many `cloudflared` processes](https://developers.cloudflare.com/cloudflare-one/tutorials/many-cfd-one-tunnel)
|
||||
- [MAC address environment variable in GRUB config](https://askubuntu.com/questions/1272400/how-do-i-automate-network-installation-of-many-ubuntu-18-04-systems-with-efi-and)
|
||||
- [Official k3s systemd service file](https://github.com/k3s-io/k3s/blob/master/k3s.service)
|
||||
- [Official Cloudflare Tunnel examples](https://github.com/cloudflare/argo-tunnel-examples)
|
||||
- [Initialize GitOps repository on Gitea and integrate with Tekton by RedHat](https://github.com/redhat-scholars/tekton-tutorial/tree/master/triggers)
|
||||
|
||||
## Stargazers over time
|
||||
|
||||
|
64
platform/gitea/files/init_gitops_repo.py
Executable file
64
platform/gitea/files/init_gitops_repo.py
Executable file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
|
||||
|
||||
import requests
|
||||
|
||||
gitea_host = os.getenv('GITEA_HOST', "gitea-http:3000")
|
||||
gitea_user = os.environ['GITEA_USER']
|
||||
gitea_pass = os.environ['GITEA_PASSWORD']
|
||||
seed_repo = "https://github.com/khuedoan/homelab"
|
||||
org = "ops"
|
||||
repo = "homelab"
|
||||
gitea_url = f"http://{gitea_user}:{gitea_pass}@{gitea_host}"
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
data_org = json.dumps({
|
||||
'username': org
|
||||
})
|
||||
|
||||
data_repo = json.dumps({
|
||||
'clone_addr': seed_repo,
|
||||
'uid': 1,
|
||||
'repo_owner': org,
|
||||
'repo_name': repo,
|
||||
'mirror': True
|
||||
})
|
||||
|
||||
resp = requests.post(
|
||||
url=f"{gitea_url}/api/v1/admin/users/{gitea_user}/orgs",
|
||||
headers=headers,
|
||||
data=data_org
|
||||
)
|
||||
|
||||
if resp.status_code == 201:
|
||||
print(f"Created organization {org}")
|
||||
elif resp.status_code == 422:
|
||||
print(f"Organization already exists")
|
||||
else:
|
||||
print(f"Error creating organization {org} ({resp.status_code})")
|
||||
print(resp.content)
|
||||
sys.exit(1)
|
||||
|
||||
resp = requests.post(
|
||||
url=f"{gitea_url}/api/v1/repos/migrate",
|
||||
headers=headers,
|
||||
data=data_repo
|
||||
)
|
||||
|
||||
if resp.status_code == 201:
|
||||
print(f"Created repository {json.loads(str(resp.content, 'utf8'))['html_url']}")
|
||||
elif resp.status_code == 409:
|
||||
print(f"Repository already exists")
|
||||
else:
|
||||
print(f"Error creating git repository ({resp.status_code})")
|
||||
print(resp.content)
|
||||
sys.exit(1)
|
26
platform/gitea/templates/init-job.yaml
Normal file
26
platform/gitea/templates/init-job.yaml
Normal file
@ -0,0 +1,26 @@
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: init-gitops-repo
|
||||
namespace: {{ .Release.Namespace }}
|
||||
annotations:
|
||||
argocd.argoproj.io/sync-wave: "1"
|
||||
spec:
|
||||
backoffLimit: 3
|
||||
template:
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: script
|
||||
image: python
|
||||
env:
|
||||
- name: GITEA_USER
|
||||
value: "{{ .Values.gitea.gitea.admin.username }}"
|
||||
- name: GITEA_PASSWORD
|
||||
value: "{{ .Values.gitea.gitea.admin.password }}"
|
||||
command:
|
||||
- python
|
||||
- -c
|
||||
args:
|
||||
- |
|
||||
{{ .Files.Get "files/init_gitops_repo.py" | indent 14 }}
|
Loading…
Reference in New Issue
Block a user