def set_patch_cron(self): script_file_path = os.path.realpath(sys.argv[0]) script_dir = os.path.dirname(script_file_path) script_file = os.path.basename(script_file_path) old_line_end = ' '.join([script_file, '-patch']) if self.disabled: new_line = '\n' else: hr = str(self.start_time.hour) minute = str(self.start_time.minute) minute_cleanup = str(self.start_time.minute + 1) dow = ','.join([str(day) for day in self.day_of_week]) new_line = ' '.join([ '\n' + minute, hr, '* *', dow, 'root cd', script_dir, '&& python check.py', self.interval_of_weeks, '&& python', script_file, '-patch >/dev/null 2>&1\n' ]) new_line += ' '.join([ minute_cleanup, hr, '* *', dow, 'root rm -f', self.stop_flag_path, '\n' ]) waagent.ReplaceFileContentsAtomic( self.crontab, "\n".join( filter( lambda a: a and (old_line_end not in a) and (self.stop_flag_path not in a), waagent.GetFileContents(self.crontab).split('\n'))) + new_line)
def init_suse_hostsfile(host_name, ipaddrs): hostsfile = '/etc/hosts' if not os.path.isfile(hostsfile): return try: newhpcd_entries = '' for ipaddr in ipaddrs: newhpcd_entries += '{0:24}{1:30}#HPCD\n'.format(ipaddr, host_name) curhpcd_entries = '' newcontent = '' hpcentryexists = False with open(hostsfile, 'r') as F: for line in F.readlines(): if re.match(r"^[0-9\.]+\s+[^\s#]+\s+#HPCD\s*$", line): curhpcd_entries += line hpcentryexists = True elif re.match(r"^[0-9\.]+\s+[^\s#]+\s+#HPC\s*$", line): hpcentryexists = True else: newcontent += line if newhpcd_entries != curhpcd_entries: if hpcentryexists: waagent.Log("Clean the HPC related host entries from hosts file") waagent.Log("Add the following HPCD host entries:\n{0}".format(newhpcd_entries)) if newcontent and newcontent[-1] != '\n': newcontent += '\n' newcontent += newhpcd_entries waagent.ReplaceFileContentsAtomic(hostsfile,newcontent) os.chmod(hostsfile, 0o644) except : raise
def set_download_cron(self): script_file_path = os.path.realpath(sys.argv[0]) script_dir = os.path.dirname(script_file_path) script_file = os.path.basename(script_file_path) old_line_end = ' '.join([script_file, '-download']) if self.disabled: new_line = '\n' else: if self.download_time > self.start_time: dow = ','.join( [str((day - 1) % 7) for day in self.day_of_week]) else: dow = ','.join([str(day % 7) for day in self.day_of_week]) hr = str(self.download_time.hour) minute = str(self.download_time.minute) new_line = ' '.join([ '\n' + minute, hr, '* *', dow, 'root cd', script_dir, '&& python check.py', self.interval_of_weeks, '&& python', script_file, '-download > /dev/null 2>&1\n' ]) waagent.ReplaceFileContentsAtomic( self.crontab, '\n'.join( filter(lambda a: a and (old_line_end not in a), waagent.GetFileContents(self.crontab).split('\n'))) + new_line)
def cleanup_host_entries(): hostsfile = '/etc/hosts' if not os.path.isfile(hostsfile): return try: hpcentryexists = False newcontent='' with open(hostsfile, 'r') as F: for line in F.readlines(): if re.match(r"^[0-9\.]+\s+[^\s#]+\s+#HPCD?\s*$", line): hpcentryexists = True else: newcontent += line if hpcentryexists: waagent.Log("Clean all HPC related host entries from hosts file") waagent.ReplaceFileContentsAtomic(hostsfile,newcontent) os.chmod(hostsfile, 0o644) except : raise
def set_patch_cron(self): script_file_path = os.path.realpath(sys.argv[0]) script_dir = os.path.dirname(script_file_path) script_file = os.path.basename(script_file_path) old_line_end = ' '.join([script_file, '-patch']) if self.disabled: new_line = '\n' else: start_time_dt = datetime.datetime(100, 1, 1, self.start_time.hour, self.start_time.minute) start_hr = str(self.start_time.hour) start_minute = str(self.start_time.minute) start_dow = ','.join([str(day % 7) for day in self.day_of_week]) cleanup_time_dt = start_time_dt + datetime.timedelta(minutes=1) cleanup_hr = str(cleanup_time_dt.hour) cleanup_minute = str(cleanup_time_dt.minute) if start_time_dt.day < cleanup_time_dt.day: cleanup_dow = ','.join( [str((day + 1) % 7) for day in self.day_of_week]) else: cleanup_dow = ','.join( [str(day % 7) for day in self.day_of_week]) new_line = ' '.join([ '\n' + start_minute, start_hr, '* *', start_dow, 'root cd', script_dir, '&& python check.py', self.interval_of_weeks, '&& python', script_file, '-patch >/dev/null 2>&1\n' ]) new_line += ' '.join([ cleanup_minute, cleanup_hr, '* *', cleanup_dow, 'root rm -f', self.stop_flag_path, '\n' ]) waagent.ReplaceFileContentsAtomic( self.crontab, "\n".join( filter( lambda a: a and (old_line_end not in a) and (self.stop_flag_path not in a), waagent.GetFileContents(self.crontab).split('\n'))) + new_line)
def _allow_password_auth(): configPath = '/etc/ssh/sshd_config' config = waagent.GetFileContents(configPath).split("\n") _set_sshd_config(config, "PasswordAuthentication", "yes") _set_sshd_config(config, "ChallengeResponseAuthentication", "yes") waagent.ReplaceFileContentsAtomic(configPath, "\n".join(config))