def setup_cron(): """ Setup cron job for the 1st of the month for the previous month's metrics """ backup_cron() cron_file = 'tmp' file_path = os.path.join(os.path.expanduser('~'), 'espa-site', 'maintenance', 'lsrd_stats.py') cron_str = '00 06 1 * * /usr/local/bin/python {0} -p'.format(file_path) crons = subprocess.check_output(['crontab', '-l']).split('\n') for idx, line in enumerate(crons): if __file__ in line: print('Cron job already exists') return add = ['#-----------------------------', '# LSRD ESPA stats', '#-----------------------------', cron_str] crons.extend(add) with open(cron_file, 'w') as f: f.write('\n'.join(crons) + '\n') msg = subprocess.check_output(['crontab', cron_file]) if 'errors' in msg: print('Error creating cron job') else: subprocess.call(['rm', cron_file])
def update_cron(user, freq=60): """ Updates the crontab to run this script again with the same user and frequency :param user: user to update password for :type user: string :param freq: number days to set the next cron job for :type freq: int """ backup_cron() cron_file = 'cron.tmp' new_date = datetime.date.today() + datetime.timedelta(days=freq) file_path = os.path.join(os.path.expanduser('~'), 'espa-site', 'maintenance', 'change_credentials.py') cron_str = "00 06 {0} {1} * /usr/local/bin/python {2} -u {3} -f {4}".format(new_date.day, new_date.month, file_path, user, freq) crons = subprocess.check_output(['crontab', '-l']).split('\n') check = False for idx, line in enumerate(crons): if __file__ in line: crons[idx] = cron_str check = True if not check: add = ['#-----------------------------', '# Environment Credential Updating', '#-----------------------------', cron_str] crons.extend(add) with open(cron_file, 'w') as f: f.write('\n'.join(crons) + '\n') msg = subprocess.check_output(['crontab', cron_file]) if 'errors' in msg: raise CredentialException('Password Updated, but failed crontab update:\n{0}'.format(msg)) else: subprocess.call(['rm', cron_file])
def setup_cron(env): """ Setup cron job for the 1st of the month for the previous month's metrics :param env: dev/tst/ops """ utils.backup_cron() cron_file = 'tmp' file_path = os.path.join(os.path.expanduser('~'), 'espa-site', 'maintenance', 'lsrd_stats.py') cron_str = ('00 06 1 * * /usr/local/bin/python {0} -p -e {1}'.format( file_path, env)) crons = subprocess.check_output(['crontab', '-l']).split('\n') for idx, line in enumerate(crons): if __file__ in line: print('Cron job already exists') return add = [ '#-----------------------------', '# LSRD ESPA stats', '#-----------------------------', cron_str ] crons.extend(add) with open(cron_file, 'w') as f: f.write('\n'.join(crons) + '\n') msg = subprocess.check_output(['crontab', cron_file]) if 'errors' in msg: print('Error creating cron job') else: subprocess.call(['rm', cron_file])