Added task for install gitea in a server

This commit is contained in:
absurdo 2023-10-23 14:21:51 +02:00
parent d73ac7e66a
commit 8098b2bf83
6 changed files with 342 additions and 3 deletions

View file

@ -0,0 +1,87 @@
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
###
# Don't forget to add the database service dependencies
###
#
#Wants=mysql.service
#After=mysql.service
#
#Wants=mariadb.service
#After=mariadb.service
#
#Wants=postgresql.service
#After=postgresql.service
#
#Wants=memcached.service
#After=memcached.service
#
#Wants=redis.service
#After=redis.service
#
###
# If using socket activation for main http/s
###
#
#After=gitea.main.socket
#Requires=gitea.main.socket
#
###
# (You can also provide gitea an http fallback and/or ssh socket too)
#
# An example of /etc/systemd/system/gitea.main.socket
###
##
## [Unit]
## Description=Gitea Web Socket
## PartOf=gitea.service
##
## [Socket]
## Service=gitea.service
## ListenStream=<some_port>
## NoDelay=true
##
## [Install]
## WantedBy=sockets.target
##
###
[Service]
# Uncomment the next line if you have repos with lots of files and get a HTTP 500 error because of that
# LimitNOFILE=524288:524288
RestartSec=2s
Type=simple
User=gitea
Group=gitea
WorkingDirectory=/var/lib/gitea/
# If using Unix socket: tells systemd to create the /run/gitea folder, which will contain the gitea.sock file
# (manually creating /run/gitea doesn't work, because it would not persist across reboots)
#RuntimeDirectory=gitea
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=gitea HOME=/home/gitea GITEA_WORK_DIR=/var/lib/gitea
WatchdogSec=30s
# If you install Git to directory prefix other than default PATH (which happens
# for example if you install other versions of Git side-to-side with
# distribution version), uncomment below line and add that prefix to PATH
# Don't forget to place git-lfs binary on the PATH below if you want to enable
# Git LFS support
#Environment=PATH=/path/to/git/bin:/bin:/sbin:/usr/bin:/usr/sbin
# If you want to bind Gitea to a port below 1024, uncomment
# the two values below, or use socket activation to pass Gitea its ports as above
###
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_BIND_SERVICE
###
# In some cases, when using CapabilityBoundingSet and AmbientCapabilities option, you may want to
# set the following value to false to allow capabilities to be applied on gitea process. The following
# value if set to true sandboxes gitea service and prevent any processes from running with privileges
# in the host user namespace.
###
#PrivateUsers=false
###
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,190 @@
#!/opt/pythonenv/bin/python3 -u
import sys, os
import subprocess
import argparse
import platform
import shutil
import pathlib
import distro
import pwd
import getpass
import re
import platform
#import pymysql.cursors
#pymysql.install_as_MySQLdb
pyv=platform.python_version_tuple()
if pyv[0]!='3':
print('Need python 3 for execute this script')
sys.exit(1)
"""
parser = argparse.ArgumentParser(description='Script for create a Gitea site.')
parser.add_argument('--domain', help='The domain where is the site', required=True)
parser.add_argument('--home_user', help='The name of the new user', required=True)
parser.add_argument('--user', help='The name of the domain user', required=True)
parser.add_argument('--path', help='The path of the gitea install')
args = parser.parse_args()
"""
linux_distro=distro.id()
"""
home_user=args.home_user
if home_user.find('/', -1)==-1:
print("Error: you need backslash in home_user option")
exit(1)
user=args.user
"""
apache_cmd='apache2'
apachectl='apache2ctl'
if linux_distro!='debian' and linux_distro!='ubuntu':
apache_cmd='httpd'
apachectl='apachectl'
if linux_distro=='rocky' or linux_distro=='fedora':
apache_group='apache'
if linux_distro=='arch':
apache_group='http'
system_user=getpass.getuser()
"""
path='/'
if args.path:
if args.path.find('/', -1)==-1:
print("Error: you need backslash in path option")
exit(1)
path=args.path[:-1]
if path=='':
path='/'
"""
print("Downloading gitea binary...")
arch_cpu=platform.uname().machine
if arch_cpu!='aarch64' and arch_cpu!='x86_64':
print("Error, {} is not supported\n".format(arch_cpu))
sys.exit(1)
# aarch64 x86_64 only
#sudo wget -O /tmp/gitea https://dl.gitea.io/gitea/1.20/gitea-1.20-linux-amd64 && sudo mv /tmp/gitea /usr/local/bin
#curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | grep 'browser_download_url' | cut -d\" -f4 | grep 'linux-amd64$'
archs={}
archs['aarch64']='arm64'
archs['x86_64']='amd64'
if os.path.isfile('/usr/local/bin/gitea'):
print("Error, an installation of gitea exists, please, delete old installation\n")
sys.exit(1)
#get_url_last_version="wget -O /tmp/gitea `curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | grep 'browser_download_url' | cut -d '\"' -f4 | grep 'linux-{}$'` && sudo mv /tmp/gitea /usr/local/bin && sudo useradd -m -s /bin/bash gitea && chmod +x /usr/local/bin/gitea".format(archs[arch_cpu])
# curl -o file.pdf https://your-domain-name/long-file-name.pdf
# wget -O /tmp/gitea https://dl.gitea.com/gitea/$GITEA_VERSION/gitea-$GITEA_VERSION-linux-{} &&\
get_url_last_version="curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | grep 'browser_download_url' | cut -d '\"' -f4 | grep 'linux-{}$' | grep -Eo '[0-9]\.[0-9]+\.[0-9]+' | head -1".format(archs[arch_cpu])
print("Downloading and installing Gitea...\n")
#if subprocess.call(get_url_last_version, shell=True) > 0:
proc=subprocess.Popen(get_url_last_version, shell=True, stdout=subprocess.PIPE)
if proc.returncode:
print("Error, cannot download the last version for Gitea\n")
sys.exit(1)
gitea_version=proc.stdout.read().decode('UTF-8').strip()
download_gitea="wget -O /tmp/gitea https://dl.gitea.com/gitea/{}/gitea-{}-linux-{} &&\
sudo mv /tmp/gitea /usr/local/bin && \
sudo useradd -m -s /bin/bash gitea && chmod +x /usr/local/bin/gitea".format(gitea_version, gitea_version, archs[arch_cpu])
print("Downloading Gitea...\n")
if subprocess.call(download_gitea, shell=True) > 0:
print("Error, cannot the last version for Gitea\n")
sys.exit(1)
print("Creating directory system for Gitea..\n")
dir_gitea="sudo mkdir -p /var/lib/gitea/{custom,data,log} && sudo chown -R gitea:gitea /var/lib/gitea/ && sudo chmod -R 750 /var/lib/gitea/ && sudo mkdir /etc/gitea && sudo chown root:gitea /etc/gitea && sudo chmod 770 /etc/gitea"
if subprocess.call(dir_gitea, shell=True) > 0:
print("Error, cannot create the directories for Gitea\n")
sys.exit(1)
print("Creating the Systemd service for GITEA...")
# && sudo systemctl enable --now gitea
"""
if subprocess.call('sudo wget https://raw.githubusercontent.com/go-gitea/gitea/master/contrib/systemd/gitea.service -P /etc/systemd/system/'), shell=True) > 0:
print("Error, cannot create the service for Gitea\n")
sys.exit(1)
"""
if subprocess.call('sudo cp modules/pastafari2/scripts/servers/cvs/files/gitea.service /etc/systemd/system/ && sudo systemctl start gitea && sudo systemctl enable gitea', shell=True) > 0:
print("Error, cannot create and run the service for Gitea\n")
sys.exit(1)
"""
base_name_file='{}-{}.conf'.format(args.domain, name)
name_file='/home/{}/{}-{}.conf'.format(system_user, args.domain, name)
with open(name_file, 'w') as f:
f.write("<Location %s>\n" % path)
#ProxyPass unix:/home/root/flask_rest/flaskrest.sock|http://127.0.0.1/
f.write("ProxyPass http://127.0.0.1:3000/\n")
f.write("ProxyPassReverse http://127.0.0.1:3000/\n")
f.write("</Location>\n")
if path=='/':
f.write("<Location %s.well-known/acme-challenge/>\n" % path)
f.write("ProxyPass !\n")
f.write("</Location>\n")
print('Updating apache configuration for wordpress outside of htdocs...')
if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra && sudo chown root:root /etc/{}/vhosts.d/extra/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0:
print('Error')
sys.exit(1)
print("Preparing apache for use proxy http...")
if linux_distro=='debian' or linux_distro=='ubuntu':
if subprocess.call('sudo a2enmod proxy_http', shell=True) > 0:
print("Error: cannot enable proxy_http in apache in debian/ubuntu distro")
sys.exit(1)
service=args.domain+'-'+name
if subprocess.call("sudo systemctl enable %s && sudo systemctl start %s && sudo systemctl restart %s" % (service, service, apache_cmd), shell=True) > 0:
print("Error: cannot update and restart apache")
sys.exit(1)
"""
print("Gitea installed successfully! You can access via :3000 http port")