Factor out fileserver updates and clean_old_jobs

This commit is contained in:
Thomas S Hatch 2014-02-05 10:30:43 -07:00
parent 888c63c39e
commit 1cd54d1226
2 changed files with 50 additions and 30 deletions

View File

@ -9,6 +9,8 @@ import os
import re
import logging
import getpass
import shutil
import datetime
try:
import pwd
except ImportError:
@ -36,6 +38,7 @@ import salt.utils.verify
import salt.utils.minions
import salt.utils.gzip_util
from salt.utils.event import tagify
from salt.exceptions import SaltMasterError
log = logging.getLogger(__name__)
@ -43,6 +46,32 @@ log = logging.getLogger(__name__)
# only accept valid minion ids
def clean_old_jobs(opts):
'''
Clean out the old jobs from the job cache
'''
if opts['keep_jobs'] != 0:
jid_root = os.path.join(opts['cachedir'], 'jobs')
cur = '{0:%Y%m%d%H}'.format(datetime.datetime.now())
if os.path.exists(jid_root):
for top in os.listdir(jid_root):
t_path = os.path.join(jid_root, top)
for final in os.listdir(t_path):
f_path = os.path.join(t_path, final)
jid_file = os.path.join(f_path, 'jid')
if not os.path.isfile(jid_file):
continue
with salt.utils.fopen(jid_file, 'r') as fn_:
jid = fn_.read()
if len(jid) < 18:
# Invalid jid, scrub the dir
shutil.rmtree(f_path)
elif int(cur) - int(jid[:10]) > \
opts['keep_jobs']:
shutil.rmtree(f_path)
def access_keys(opts):
'''
A key needs to be placed in the filesystem with permissions 0400 so
@ -92,6 +121,24 @@ def access_keys(opts):
keys[user] = key
return keys
def fileserver_update(fileserver):
'''
Update the fileserver backends, requires that a built fileserver object
be passed in
'''
try:
if not fileserver.servers:
log.error('No fileservers loaded, the master will not be'
'able to serve files to minions')
raise SaltMasterError('No fileserver backends available')
fileserver.update()
except Exception as exc:
log.error(
'Exception {0} occurred in file server update'.format(exc)
)
class RemoteFuncs(object):
'''
Funcitons made available to minions, this class includes the raw routines

View File

@ -144,7 +144,6 @@ class Master(SMaster):
controller for the Salt master. This is where any data that needs to
be cleanly maintained from the master is maintained.
'''
jid_root = os.path.join(self.opts['cachedir'], 'jobs')
search = salt.search.Search(self.opts)
last = int(time.time())
rotate = int(time.time())
@ -164,25 +163,8 @@ class Master(SMaster):
while True:
now = int(time.time())
loop_interval = int(self.opts['loop_interval'])
if self.opts['keep_jobs'] != 0 and (now - last) >= loop_interval:
cur = '{0:%Y%m%d%H}'.format(datetime.datetime.now())
if os.path.exists(jid_root):
for top in os.listdir(jid_root):
t_path = os.path.join(jid_root, top)
for final in os.listdir(t_path):
f_path = os.path.join(t_path, final)
jid_file = os.path.join(f_path, 'jid')
if not os.path.isfile(jid_file):
continue
with salt.utils.fopen(jid_file, 'r') as fn_:
jid = fn_.read()
if len(jid) < 18:
# Invalid jid, scrub the dir
shutil.rmtree(f_path)
elif int(cur) - int(jid[:10]) > \
self.opts['keep_jobs']:
shutil.rmtree(f_path)
if (now - last) >= loop_interval:
salt.daemons.masterapi.clean_old_jobs(self.opts)
if self.opts.get('publish_session'):
if now - rotate >= self.opts['publish_session']:
@ -191,16 +173,7 @@ class Master(SMaster):
if self.opts.get('search'):
if now - last >= self.opts['search_index_interval']:
search.index()
try:
if not fileserver.servers:
log.error('No fileservers loaded, the master will not be'
'able to serve files to minions')
raise SaltMasterError('No fileserver backends available')
fileserver.update()
except Exception as exc:
log.error(
'Exception {0} occurred in file server update'.format(exc)
)
salt.daemons.masterapi.fileserver_update(fileserver)
# check how close to FD limits you are
salt.utils.verify.check_max_open_files(self.opts)