Gestion version docker

Forum dédié aux questions concernant l'utilisation de DomoticZ dans Docker ou sur un NAS.
pierrotori
Messages : 821
Inscription : 29 févr. 2016, 12:11

Gestion version docker

Message par pierrotori »

Bonjour
j'ai donc un package docker dans un container provenant de la version stable de domoticz.
Je voulais être avertit d'une nouvelle version et donc d'un nouveau dépôt d'une version stable dans le hub docker
le fonctionnement est donc de créer une variable d'environnement dans le shell qui créé le container domoticz en automatique comme cela

Code : Tout sélectionner

	# recuperation sha256 image stable
	tagged=$(echo `docker inspect --format="{{index .RepoDigests 0}}" domoticz/domoticz:stable` | cut -f2 -d'@')
	export tagged
	docker compose up -d
puis ajouter dans le docker-compose.yml une variable d'environnement tagged

Code : Tout sélectionner

  domoticz:
    image: domoticz/domoticz:dpkg
    container_name: domoticz
    environment:
      - PGID=1000
      - PUID=1000
      - WWW_PORT=8080 # Specify default HTTP port
      - SSL_PORT=443 # Specify default SSL port
      - TZ=Europe/Paris
      - EXTRA_CMD_ARG=-sslcert /opt/domoticz/userdata/server_cert.pem -ssldhparam /opt/domoticz/userdata/dhparam.pem
      # - EXTRA_CMD_ARG=debug
      - LOG_PATH=/opt/domoticz/userdata/log/domoticz.log
      # sha256 version image docker stable
      - tagged=${tagged}
      
on peut ensuite constater que dans le container, on retrouve cette variable alimentée
ensuite on créé le script lua qui ira chercher le sha256 de la version sur le hub docker et la comparera avec celle contenue dans la variable environnement.
je stocke dans les data persistent, la version, le build , revison et le sha256

Code : Tout sélectionner

-- Authors  --------------------------------------------------------------------
-- https://easydomoticz.com/forum/viewtopic.php?t=14151
-- persistent data
-- 		state permet de flaguer la notification afin de l'envoyer qu'une fois
-- 		digest contient l'identifiant de l'image docker stable
--		version 	: contient la version publiée
--		revision 	: numero de compilation
--		build		: numero compilation uniquement si version beta
--				stable 	:2024.4
-- 				beta 	:2024.4 (build 15910)
-- https://hub.docker.com/v2/repositories/domoticz/domoticz/tags/stable
-- https://hub.docker.com/v2/repositories/domoticz/domoticz/tags/beta
-- v1.0 pierrotori	20240922  gestion version hub docker
-- -----------------------------------------------------------------------------
local scriptName = 'VERSION'
local scriptVersion = '1.0'

-- Initialisation des variables
local d_internet = "Internet" 	-- Nom du dispositif Interrupteur Internet, nil si non utilisé
local mode ='stable'			-- choix mode STABLE ou BETA
local f_dry_run = 'N'       	-- (Y) pour tester le script sans activer réellement l'action
local f_notification = "Y"  	-- (Y) Active les f_notifications, (N) les désactive

local message = '.'

return {
	active = true,
	on = {
	timer = { 'at 9:00', 'at 13:00', 'at 18:00'},
	httpResponses = { 'getversion' , 'tag_docker'},
	shellCommandResponses = { 'tag_domoticz' },
	customEvents = { 't_alerte_version' }
	},
    logging = {
        -- level = domoticz.LOG_INFO,   -- Seulement un niveau peut être actif; commenter les autres
        -- level = domoticz.LOG_ERROR,
		-- level = domoticz.LOG_DEBUG,
        -- level = domoticz.LOG_MODULE_EXEC_INFO,
        marker = scriptName..' v'..scriptVersion
    },
	data = { 
			state		= { initial = 0, maxItems = 1},
			digest		= { initial = 0, maxItems = 1},
			version		= { initial = 0, maxItems = 1},
			revision	= { initial = 0, maxItems = 1},
			build		= { initial = 0, maxItems = 1},
			},
execute = function(dz,item,trigger) 

-- PROGRAM STARTS
dz.log('============== VERSION ==================', dz.LOG_INFO)
if d_internet and dz.devices(d_internet).state == 'Off' then return end	

if item.isTimer or item.isCustomEvent then
	dz.log('Déclenchement timer ou CustomEvent', dz.LOG_DEBUG)
	-- stockage version domoticz meme si pas utilise pour le control
	url = "http://127.0.0.1:8080/json.htm?type=command&param=getversion"
	dz.openURL( {url = url,	method = 'GET', callback = 'getversion'} )

elseif (item.isHTTPResponse) and item.trigger == 'getversion' then
	if item.ok and item.isJSON then
		dz.log(item.data,dz.LOG_DEBUG)
		if dz.data.revision ~= item.json.Revision then 
			dz.data.revision = item.json.Revision
			dz.data.state = 0
		end
		version_full={}
		version_full = dz.utils.stringSplit(item.json.version," ")
		if dz.data.version ~= version_full[1] then
			dz.data.version = version_full[1]
			dz.data.state = 0
		end
		if version_full[3] ~= nil then
			if dz.data.build ~= version_full[3]:gsub(")","") then
				dz.data.build = version_full[3]:gsub(")","")
				dz.data.state = 0
			end
		end
	end
	-- sha256 de domoticz stable
	dz.executeShellCommand( {
		command = 'echo $tagged',
		callback = 'tag_domoticz',
		timeout = 10                      
		} )
elseif (item.isShellCommandResponse) and item.trigger == 'tag_domoticz' then
	dz.log('Déclenchement shell asynchrone tag_domoticz ', dz.LOG_DEBUG)
	if (item.statusCode==0) then
		dz.log(item.data, dz.LOG_DEBUG)
		if dz.data.digest ~= item.data then dz.data.digest = item.data end
	elseif item.statusCode==1 then
		message = 'Erreur appel version domoticz'
		dz.log(message, dz.LOG_DEBUG)
	else
		message = 'Donnée version non disponible'
		dz.log(message, dz.LOG_DEBUG)
		if f_notification == "Y" then
			dz.helpers.generalNotify(dz, 'EXPLOIT', scriptName, message, 'telegram')
		end		
	end
	-- curl -s https://hub.docker.com/v2/repositories/domoticz/domoticz/tags/stable -H 'Content-Type: application/json' | jq -r .digest
	url_docker = "https://hub.docker.com/v2/repositories/domoticz/domoticz/tags/"..mode
	dz.openURL( 
		{url = url_docker,	
		method = 'GET',
		callback = 'tag_docker'
		} )
elseif (item.isHTTPResponse) and item.trigger == 'tag_docker' then
	dz.log('Déclenchement http asynchrone tag_docker', dz.LOG_DEBUG)
	if item.ok and item.isJSON then
		dz.log(item.data,dz.LOG_DEBUG)
		digest = item.json.digest
		dz.log('Digest :'..digest, dz.LOG_DEBUG)
		if digest ~= dz.data.digest and dz.data.state == 0 then
			if f_notification == "Y" then
				message = 'Nouvelle version de Domoticz'
				dz.helpers.generalNotify(dz, 'EXPLOIT', scriptName, message, 'telegram')
				dz.data.state = 1
			end
		end
	end
end
end -- execute
} -- return

le fichier de data contient

Code : Tout sélectionner

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
	["digest"] = "sha256:ffba80df6855a0cdf853f0c020cfb4404797522a03d91c7512254325403327f9";
	["build"] = 0;
	["state"] = 0;
	["revision"] = 16157;
	["version"] = "2024.7";
}
return obj1
Bonne soirée
Pierrotori
Répondre