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

View File

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