first commit

This commit is contained in:
k3nny 2023-03-07 22:06:09 +01:00
commit 5516e10221
4 changed files with 142 additions and 0 deletions

11
config.json Normal file
View File

@ -0,0 +1,11 @@
{
"db": {
"server":"localhost",
"name":"jru",
"login":"jru_user",
"password":"testpasswd"
},
"java_settings":{
"enabled_version": ["8","11","17"]
}
}

10
create_database_jru.sql Normal file
View File

@ -0,0 +1,10 @@
create database jru;
create user jru_user with encrypted password 'nixxamere';
grant all privileges on database jru to jru_user;
CREATE TABLE jru.java_versions (
id SERIAL PRIMARY KEY,
java_version varchar NULL,
latest_release_packaged varchar NULL,
latest_release_packaged_timestamp timestamp with time zone NULL
);

120
main.py Normal file
View File

@ -0,0 +1,120 @@
import requests
import json
import pprint
import psycopg
from psycopg.rows import dict_row
import semver
class jru:
def __init__(self):
self.db_config = self._load_config_from_json("config.json")
self.db_conn = self._db_connect(self.db_config)
def _load_config_from_json(self,config_json_path):
try:
with open(config_json_path, "r") as f:
conf = json.load(f)
except Exception as json_err:
print(json_err)
return conf
def _db_connect(self,db_config):
try:
#pprint.pprint(db_config)
#print(db_config['db']['server'])
conn = psycopg.connect(
host=db_config['db']['server'],
dbname=db_config['db']['name'],
user=db_config['db']['login'],
password=db_config['db']['password'])
print('Connection to database is OK')
except psycopg.OperationalError as connect_err:
print('Error connecting to {}@{}'.format(db_config['db']['name'],db_config['db']['server']))
print(connect_err)
return 1
return conn
def _get_version_from_db(self,db_conn):
cur = db_conn.cursor(row_factory=dict_row)
qres = cur.execute("select * from jru.java_versions").fetchall()
#print(qres)
if qres:
return qres
else:
return []
def _get_version_semver_from_db(self,java_version):
cur = self.db_conn.cursor(row_factory=dict_row)
qres = cur.execute("select latest_release_packaged from jru.java_versions where java_version='{}'".format(java_version)).fetchall()
#print(qres)
if qres:
return qres
else:
return []
def _update_version_in_db(self,java_version,java_semver):
self.db_conn.execute("update jru.java_versions set latest_release_packaged = '{}' where java_version = '{}'".format(java_semver,java_version))
self.db_conn.commit()
def _get_version_from_json(self):
return self.db_config["java_settings"]["enabled_version"]
def _get_latest_release_info(self,java_version,image_type="jre",arch="x64",os="linux"):
# https://api.adoptium.net/v3/assets/latest/8/hotspot?architecture=x64&image_type=jre&os=linux&vendor=eclipse
url = "https://api.adoptium.net/v3/assets/latest/{}/hotspot?architecture={}&image_type={}&os={}&vendor=eclipse".format(java_version,arch,image_type,os)
try:
print(" - requesting adoptium : {}".format(url))
r = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36','Accept': 'application/json'})
#pprint.pprint(r.json())
except Exception as err_json_request:
print(err_json_request)
r = {}
return r.json()
def run(self):
print("Retrieve Java versions from DB : ")
java_db_version = self._get_version_from_db(self.db_conn)
java_db_version_list = [x["java_version"] for x in java_db_version]
# if java_db_version_list:
# for db_vers in java_db_version_list:
# print(' - {}'.format(db_vers))
# else:
# print(" - no version in database yet!")
print("Retrieve Java versions from JSON : ")
java_config_version = self._get_version_from_json()
# Inserting in database first time
for json_vers in java_config_version:
print(' - {}'.format(json_vers))
if json_vers not in java_db_version_list:
print('Java {} is not in database yet!'.format(json_vers))
self.db_conn.execute("insert into jru.java_versions (java_version,latest_release_packaged) values ({},'{}.0.0')".format(json_vers,json_vers))
else:
print('Java {} already found in database'.format(json_vers))
self.db_conn.commit()
#updating java_version from database
java_db_version = self._get_version_from_db(self.db_conn)
java_db_version_list = [x["java_version"] for x in java_db_version]
for db_version in java_db_version_list:
print("\nProcessing Java {}".format(db_version))
api_version_info = self._get_latest_release_info(db_version)
api_version_info_semver = api_version_info[0]["version"]["semver"]
db_version_semver = self._get_version_semver_from_db(db_version)[0]["latest_release_packaged"]
if semver.VersionInfo.parse(api_version_info_semver) > semver.VersionInfo.parse(db_version_semver):
print(" - New version available! - {} > {}".format(api_version_info_semver,db_version_semver))
self._update_version_in_db(db_version,api_version_info_semver)
else:
print(' - No upgrade needed! API {} == DB {}'.format(api_version_info_semver,db_version_semver))
if __name__ == "__main__":
jru_main = jru()
jru_main.run()

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
semver