Place update_state methods out of db session

Place update_state methods out of db session;
Match package installer name by regex.
patch2: fix style

Change-Id: I26d3f21e2d3c022ebe2b4635583c33074434ae46
This commit is contained in:
xichengc 2014-08-04 15:45:17 -07:00
parent 8710a15261
commit e87ed4342d
2 changed files with 95 additions and 108 deletions

View File

@ -111,7 +111,7 @@ class PackageMatcher(object):
self.__class__.__name__,
min_progress, max_progress))
self.name_ = package_installer_name
self.name_ = re.compile(package_installer_name)
self.target_system_ = target_system
self.matcher_ = item_matcher
self.matcher_.update_progress_range(min_progress, max_progress)
@ -124,7 +124,7 @@ class PackageMatcher(object):
def match(self, package_installer_name, target_system):
"""Check if the package matcher is acceptable."""
return all([
self.name_ == package_installer_name,
self.name_.match(package_installer_name),
self.target_system_ == target_system])
def update_progress(self, fullname, progress):
@ -226,7 +226,8 @@ class AdapterMatcher(object):
def _update_host_progress(cls, hostid, host_progress, updater):
"""Updates host progress to db."""
session = database.current_session()
state = ''
with database.session() as session:
host = session.query(
Host).filter_by(id=hostid).first()
if not host:
@ -248,9 +249,10 @@ class AdapterMatcher(object):
hostid, host.state, host_progress
)
return
if (host.state.percentage == host_progress.progress and
host.state.message == host_progress.message):
if (
host.state.percentage == host_progress.progress and
host.state.message == host_progress.message
):
logging.info(
'host %s update ignored due to same progress'
'in database',
@ -258,33 +260,23 @@ class AdapterMatcher(object):
)
return
host.state.percentage = host_progress.progress
host.state.message = host_progress.message
if host_progress.severity:
host.state.severity = host_progress.severity
if host.state.percentage >= 1.0:
host.state.state = 'SUCCESSFUL'
state = 'SUCCESSFUL'
if host.state.severity == 'ERROR':
host.state.state = 'ERROR'
if host.state.state != 'INSTALLING':
host.mutable = True
state = 'ERROR'
host_api.update_host_state(
session,
updater,
hostid,
state=host.state.state,
percentage=host.state.percentage,
message=host.state.message,
state=state,
percentage=host_progress.progress,
message=host_progress.message,
id=hostid
)
logging.debug(
'update host %s state %s',
hostid, host.state)
hostid, state)
@classmethod
def _update_clusterhost_progress(
@ -294,7 +286,8 @@ class AdapterMatcher(object):
updater
):
session = database.current_session()
clusterhost_state = ''
with database.session() as session:
clusterhost = session.query(
ClusterHost).filter_by(id=hostid).first()
@ -311,8 +304,11 @@ class AdapterMatcher(object):
)
return
if (clusterhost.state.percentage == clusterhost_progress.progress and
clusterhost.state.message == clusterhost_progress.message):
if (
clusterhost.state.percentage ==
clusterhost_progress.progress and
clusterhost.state.message == clusterhost_progress.message
):
logging.info(
'clusterhost %s update ignored due to same progress'
'in database',
@ -320,32 +316,23 @@ class AdapterMatcher(object):
)
return
clusterhost.state.percentage = clusterhost_progress.progress
clusterhost.state.message = clusterhost_progress.message
if clusterhost_progress.severity:
clusterhost.state.severity = clusterhost_progress.severity
if clusterhost.state.percentage >= 1.0:
clusterhost.state.state = 'SUCCESSFUL'
clusterhost_state = 'SUCCESSFUL'
if clusterhost.state.severity == 'ERROR':
clusterhost.state.state = 'ERROR'
if clusterhost.state.state != 'INSTALLING':
clusterhost.mutable = True
clusterhost_state = 'ERROR'
cluster_api.update_clusterhost_state(
session,
updater,
hostid,
state=clusterhost.state.state,
percentage=clusterhost.state.percentage,
message=clusterhost.state.message
state=clusterhost_state,
percentage=clusterhost_progress.progress,
message=clusterhost_progress.message
)
logging.debug(
'update clusterhost %s state %s',
hostid, clusterhost.state)
hostid, clusterhost_state)
@classmethod
def _update_cluster_progress(cls, clusterid):
@ -446,9 +433,8 @@ class AdapterMatcher(object):
host_progresses = {}
clusterhost_progresses = {}
updater = user_api.get_user_object(
'admin@abc.com',
expire_timestamp=datetime.datetime.now() +
datetime.timedelta(seconds=10000))
'admin@abc.com'
)
with database.session():
for hostid in hostids:
host_name, host_state, host_progress = \
@ -519,4 +505,5 @@ class AdapterMatcher(object):
updater
)
with database.session():
self._update_cluster_progress(clusterid)

View File

@ -403,7 +403,7 @@ OS_ADAPTER_CONFIGURATIONS = [
PACKAGE_ADAPTER_CONFIGURATIONS = [
PackageMatcher(
package_installer_name='chef',
package_installer_name='chef.*',
target_system='openstack',
item_matcher=PACKAGE_INSTALLER_CONFIGURATIONS['openstack'],
min_progress=0.0,