Skip to content

Commit

Permalink
nacos配置支持runOnChange
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce.wu committed Sep 25, 2024
1 parent 161e937 commit 04cc18e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
59 changes: 40 additions & 19 deletions ops/changelog/nacos_change.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os
import logging, os, yaml, hashlib
from ops.utils import config_handler, config_validator
from ops.utils.constants import CHANGE_LOG_EXEXTYPE, SYSTEM_TYPE
from ops.utils.exception import ChangeLogException
Expand Down Expand Up @@ -44,6 +43,11 @@
"type": "string",
"description": "The change set context. Multiple are separated by commas",
},
"runOnChange": {
"type": "boolean",
"default": "false",
"description": "Re-run when the change set changed.",
},
"changes": {
"type": "array",
"items": [
Expand Down Expand Up @@ -281,6 +285,10 @@ def __init_change_log(self):
self.changeSetDict = changeSetDict
self.changeSets = changeSets

def __change_set_checksum(self, changes):
changes_str = yaml.dump(changes, sort_keys=True)
return hashlib.sha256(changes_str.encode()).hexdigest()

def fetch_current(self, client: ConfigOpsNacosClient, nacos_id: str):
"""
获取当前需要执行的changeset
Expand All @@ -303,7 +311,6 @@ def fetch_current(self, client: ConfigOpsNacosClient, nacos_id: str):
or CHANGE_LOG_EXEXTYPE.FAILED.matches(log.exectype)
or CHANGE_LOG_EXEXTYPE.INIT.matches(log.exectype)
):
logger.info(f"Found change set log: {id}")
currentChangeSet = changeSetObj
if log is None:
log = ConfigOpsChangeLog()
Expand Down Expand Up @@ -386,6 +393,10 @@ def fetch_multi(
if not self._is_ctx_included(contexts, changeSetCtx):
continue

# 计算checksum
checksum = self.__change_set_checksum({"changes": changeSetObj["changes"]})

# 查询log
log = (
db.session.query(ConfigOpsChangeLog)
.filter_by(
Expand All @@ -395,23 +406,32 @@ def fetch_multi(
)
.first()
)
# 判断是否已经执行过了
if (
log is None
or CHANGE_LOG_EXEXTYPE.FAILED.matches(log.exectype)
or CHANGE_LOG_EXEXTYPE.INIT.matches(log.exectype)
):
logger.info(f"Found change set log: {id}")
if log is None:
log = ConfigOpsChangeLog()
log.change_set_id = id
log.system_type = SYSTEM_TYPE.NACOS.value
log.system_id = nacos_id

is_execute = True
if log is None:
log = ConfigOpsChangeLog()
log.change_set_id = id
log.system_type = SYSTEM_TYPE.NACOS.value
log.system_id = nacos_id
log.exectype = CHANGE_LOG_EXEXTYPE.INIT.value
log.author = changeSetObj.get("author", "")
log.comment = changeSetObj.get("comment", "")
log.checksum = checksum
db.session.add(log)
elif CHANGE_LOG_EXEXTYPE.FAILED.matches(
log.exectype
) or CHANGE_LOG_EXEXTYPE.INIT.matches(log.exectype):
log.checksum = checksum
else:
runOnChange = changeSetObj.get("runOnChange", False)
if runOnChange and log.checksum != checksum:
log.exectype = CHANGE_LOG_EXEXTYPE.INIT.value
log.author = changeSetObj.get("author", "")
log.comment = changeSetObj.get("comment", "")
db.session.add(log)
db.session.commit()
log.checksum = checksum
else:
is_execute = False

if is_execute:
logger.info(f"Found change set log: {id}")

changeSetIds.append(id)

Expand Down Expand Up @@ -475,6 +495,7 @@ def fetch_multi(
if count > 0 and idx >= count:
break

db.session.commit()
return changeSetIds, list(resultChangeConfigDict.values())

def _get_remote_config(
Expand Down
1 change: 1 addition & 0 deletions ops/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ConfigOpsChangeLog(Base):
system_id = mapped_column(String(32), nullable=False, comment="系统ID")
system_type = mapped_column(String(30), nullable=False, comment="系统类型")
exectype = mapped_column(String(30), nullable=False, comment="执行类型")
checksum = mapped_column(String(128), nullable=False, comment="checksum")
author = mapped_column(String(128))
filename = mapped_column(String(1024))
comment = mapped_column(String(2048))
Expand Down
3 changes: 2 additions & 1 deletion tests/changelog/changelog-1.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ nacosChangeLog:
comment: 注释
ignore: false # 是否忽略此changeSet
context: dev,prod
runOnChange: true
changes:
# namespace+group+dataId的组合在changes中不能重复
- namespace: blue
Expand All @@ -15,7 +16,7 @@ nacosChangeLog:
patchContent: |-
spring:
application:
name: blue-gogo
name: blue-gogogof
# 删除内容
deleteContent: |-
notice:
Expand Down

0 comments on commit 04cc18e

Please sign in to comment.