1
0
mirror of https://github.com/CMiksche/gitea-auto-update synced 2025-12-10 07:57:23 +01:00

fix: file structure

This commit is contained in:
Christoph Miksche
2019-08-26 22:46:46 +02:00
parent 8b64d5f518
commit 05caef9d41
9 changed files with 20 additions and 16 deletions

View File

33
gitea_auto_update/cli.py Normal file
View File

@@ -0,0 +1,33 @@
'''
Gitea Auto Updater
Copyright 2018, 2019 The Gitea-Auto-Update Authors
All rights reserved.
License: GNU General Public License
'''
import logging
import configparser
import fire
import update
def updater(settings='settings.ini'):
# Config
config = configparser.ConfigParser()
config.read(settings)
# Create a log file
logging.basicConfig(filename=config.get('Gitea', 'logFile'), level=logging.DEBUG)
# Start update
update.Update(config.get('Gitea', 'site'),
config.get('Gitea', 'file'),
config.get('Gitea', 'sourceDir'),
config.get('Gitea', 'apiUrl'),
config.get('Gitea', 'buildFromSource'),
config.get('Gitea', 'tmpDir'),
config.get('Gitea', 'system'))
def main():
fire.Fire(updater)
if __name__ == '__main__':
main()

View File

View File

@@ -0,0 +1,24 @@
'''
Gitea Auto Updater
Copyright 2018, 2019 The Gitea-Auto-Update Authors
All rights reserved.
License: GNU General Public License
'''
import os
class Build:
def __init__(self, gtFile, sourceDir):
self.gtFile = gtFile
self.sourceDir = sourceDir
def fromSource(self, tag):
# Function to build the new version from source
os.chdir(self.sourceDir)
os.system("git checkout master")
os.system("git pull")
os.system("git checkout " + tag)
os.system('TAGS="bindata sqlite sqlite_unlock_notify" make generate build')
os.system("mv gitea " + self.gtFile)

View File

@@ -0,0 +1,77 @@
'''
Gitea Auto Updater
Copyright 2018, 2019 The Gitea-Auto-Update Authors
All rights reserved.
License: GNU General Public License
'''
import requests
import os
import logging
from shutil import which # from whichcraft import which
class Download:
def __init__(self, tmpDir, githubVersion, githubVersionTag, gtSystem, gtFile):
if not self.isTool("xz"):
logging.error('Download: missing dependency: xz')
quit()
self.tmpDir = tmpDir
self.githubVersion = githubVersion
self.githubVersionTag = githubVersionTag
self.gtSystem = gtSystem
self.gtFile = gtFile
self.downloadGiteaFiles()
self.checkAndExtract()
def isTool(name):
# Function to check if tool is available
##Check whether `name` is on PATH and marked as executable.
return which(name) is not None
def download(self, url, fileName):
# Function to download a file
# open in binary mode
with open(fileName, "wb") as file:
# get request
response = requests.get(url)
# write to file
file.write(response.content)
def downloadGiteaFiles(self):
# Set download url
gtDownload = 'https://github.com/go-gitea/gitea/releases/download/' + self.githubVersionTag + '/gitea-' + self.githubVersion + '-' + self.gtSystem + '.xz'
logging.info('Download: Gitea file: %s', gtDownload)
shaDownload = gtDownload + '.sha256'
logging.info('Download: SHA file: %s', shaDownload)
# Download file
logging.info('Download: downloading sha256 hashsum')
self.download(shaDownload, self.tmpDir + 'gitea.xz.sha256')
logging.info('Download: downloading %s', self.githubVersionTag + 'gitea.xz')
self.tmpXz = self.tmpDir +'gitea-' + self.githubVersion + '-' + self.gtSystem + '.xz'
self.download(gtDownload, self.tmpXz)
def shaCheck(self):
return os.system("sha256sum -c gitea.xz.sha256 > /dev/null") == 0
def extractFile(self):
logging.info('Download: sha ok, extracting file to location')
# extracting download file
cmd = "xz -d " + self.tmpXz
os.system(cmd)
# moving temp file to gtfile location
cmd = 'mv ' + self.tmpDir + 'gitea-' + self.githubVersion + '-' + self.gtSystem + ' ' + self.gtFile
os.system(cmd)
def checkAndExtract(self):
os.chdir(self.tmpDir)
if self.shaCheck():
self.extractFile()
else:
logging.error('Download: error: sha256sum failed')
quit()

View File

@@ -0,0 +1,48 @@
'''
Gitea Auto Updater
Copyright 2018, 2019 The Gitea-Auto-Update Authors
All rights reserved.
License: GNU General Public License
'''
from packaging import version
import os
import requests
import logging
class Version:
def __init__(self, gtSite, gtFile):
self.gtSite = gtSite
self.gtFile = gtFile
def checkVersion(self, newVersion, oldVersion):
# Function to check if there is a new version
return version.parse(newVersion) > version.parse(oldVersion)
def parseFileVersion(self, string):
return string.split(" ")[2]
def getVersionFromFile(self):
versionString = os.popen(self.gtFile + " -v").read()
return self.parseFileVersion(versionString)
def getCurrentVersion(self):
# Function to get the current version
try:
# Try to get the version from the file
currentVersion = self.getVersionFromFile()
except:
# Get the version via the web api if the file does fail
currentVersion = requests.get(self.gtSite).json()['version']
if currentVersion.status_code != 200:
currentVersion = self.getVersionFromFile()
finally:
logging.info('Version: current_version = %s', currentVersion)
return currentVersion
def getGithubVersionTag(self, apiUrl):
versionTag = requests.get(apiUrl).json()['tag_name']
logging.info('Version: github_version_tag = %s', versionTag)
return versionTag

View File

@@ -0,0 +1,58 @@
'''
Gitea Auto Updater
Copyright 2018, 2019 The Gitea-Auto-Update Authors
All rights reserved.
License: GNU General Public License
'''
import os
import logging
import lib.version
import lib.download
import lib.build
class Update:
def __init__(self, gtSite, gtFile, sourceDir, apiUrl, buildFromSource, tmpDir, gtSystem):
self.gtSite = gtSite
self.gtFile = gtFile
self.sourceDir = sourceDir
self.apiUrl = apiUrl
self.buildFromSource = buildFromSource
self.tmpDir = tmpDir
self.gtSystem = gtSystem
self.initVersionAndBuild()
self.getVersionAndTag()
def initVersionAndBuild(self):
self.version = lib.version.Version(self.gtSite, self.gtFile)
self.build = lib.build.Build(self.gtFile, self.sourceDir)
def getVersionAndTag(self):
self.currentVersion = self.version.getCurrentVersion() # Version from gitea site
self.githubVersionTag = self.version.getGithubVersionTag(self.apiUrl) # Get version tag from github and remove first char (v)
self.githubVersion = self.githubVersionTag[1:] # Get version from version tag
def doUpdate(self):
if self.buildFromSource: # Should the new version be build from source?
self.build.fromSource(self.githubVersionTag)
else:
self.download = self.download.Download(self.tmpDir,
self.githubVersion,
self.githubVersionTag,
self.gtSystem,
self.gtFile)
def checkAndUpdate(self):
if self.version.checkVersion(self.githubVersion, self.currentVersion): # Check if there is a new version
logging.info('Update: new version available, stopping service')
os.system("systemctl stop gitea.service")
self.doUpdate()
logging.info('Update: starting gitea.service')
os.system("systemctl start gitea.service")
print("update successfully")
else:
print("current version is uptodate")