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,65 +226,57 @@ 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 = ''
host = session.query( with database.session() as session:
Host).filter_by(id=hostid).first() host = session.query(
if not host: Host).filter_by(id=hostid).first()
logging.error( if not host:
'there is no host for %s in table Host', logging.error(
hostid 'there is no host for %s in table Host',
) hostid
)
if not host.state: if not host.state:
logging.error( logging.error(
'there is no related HostState for %s', 'there is no related HostState for %s',
hostid hostid
) )
if host.state.percentage > host_progress.progress: if host.state.percentage > host_progress.progress:
logging.error( logging.error(
'host %s progress has not been increased' 'host %s progress has not been increased'
' from %s to $s', ' from %s to $s',
hostid, host.state, host_progress hostid, host.state, host_progress
) )
return return
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',
hostid
)
return
if (host.state.percentage == host_progress.progress and if host.state.percentage >= 1.0:
host.state.message == host_progress.message): state = 'SUCCESSFUL'
logging.info( if host.state.severity == 'ERROR':
'host %s update ignored due to same progress' state = 'ERROR'
'in database',
hostid
)
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'
if host.state.severity == 'ERROR':
host.state.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,58 +286,53 @@ class AdapterMatcher(object):
updater updater
): ):
session = database.current_session() clusterhost_state = ''
clusterhost = session.query( with database.session() as session:
ClusterHost).filter_by(id=hostid).first() clusterhost = session.query(
ClusterHost).filter_by(id=hostid).first()
if not clusterhost.state: if not clusterhost.state:
logging.error( logging.error(
'ClusterHost state not found for %s', 'ClusterHost state not found for %s',
hostid) hostid)
if clusterhost.state.percentage > clusterhost_progress.progress: if clusterhost.state.percentage > clusterhost_progress.progress:
logging.error( logging.error(
'clusterhost %s state has not been increased' 'clusterhost %s state has not been increased'
' from %s to %s', ' from %s to %s',
hostid, clusterhost.state, clusterhost_progress hostid, clusterhost.state, clusterhost_progress
) )
return return
if (clusterhost.state.percentage == clusterhost_progress.progress and if (
clusterhost.state.message == clusterhost_progress.message): clusterhost.state.percentage ==
logging.info( clusterhost_progress.progress and
'clusterhost %s update ignored due to same progress' clusterhost.state.message == clusterhost_progress.message
'in database', ):
hostid logging.info(
) 'clusterhost %s update ignored due to same progress'
return 'in database',
hostid
)
return
clusterhost.state.percentage = clusterhost_progress.progress if clusterhost.state.percentage >= 1.0:
clusterhost.state.message = clusterhost_progress.message clusterhost_state = 'SUCCESSFUL'
if clusterhost_progress.severity:
clusterhost.state.severity = clusterhost_progress.severity
if clusterhost.state.percentage >= 1.0: if clusterhost.state.severity == 'ERROR':
clusterhost.state.state = 'SUCCESSFUL' clusterhost_state = 'ERROR'
if clusterhost.state.severity == 'ERROR':
clusterhost.state.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):
@ -397,11 +384,11 @@ class AdapterMatcher(object):
if clusterhost.state.state == 'INSTALLING': if clusterhost.state.state == 'INSTALLING':
cluster_installing_hosts += 1 cluster_installing_hosts += 1
elif (clusterhost.host.state.state not in elif (clusterhost.host.state.state not in
['ERROR', 'INITIALIZED'] and ['ERROR', 'INITIALIZED'] and
clusterhost.state.state != 'ERORR'): clusterhost.state.state != 'ERORR'):
cluster_installing_hosts += 1 cluster_installing_hosts += 1
elif (clusterhost.state.state == 'ERROR' or elif (clusterhost.state.state == 'ERROR' or
clusterhost.host.state.state == 'ERROR'): clusterhost.host.state.state == 'ERROR'):
cluster_failed_hosts += 1 cluster_failed_hosts += 1
if clusterhost.state.message: if clusterhost.state.message:
@ -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 = \
@ -504,19 +490,20 @@ class AdapterMatcher(object):
'progress: state %s progress %s', 'progress: state %s progress %s',
host_name, clusterhost_state, clusterhost_progress) host_name, clusterhost_state, clusterhost_progress)
for hostid in hostids: for hostid in hostids:
if hostid not in host_progresses: if hostid not in host_progresses:
continue continue
if hostid not in clusterhost_progresses: if hostid not in clusterhost_progresses:
continue continue
_, _, host_progress = host_progresses[hostid] _, _, host_progress = host_progresses[hostid]
_, _, clusterhost_progress = clusterhost_progresses[hostid] _, _, clusterhost_progress = clusterhost_progresses[hostid]
self._update_host_progress(hostid, host_progress, updater) self._update_host_progress(hostid, host_progress, updater)
self._update_clusterhost_progress( self._update_clusterhost_progress(
hostid, hostid,
clusterhost_progress, clusterhost_progress,
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,