Esempio n. 1
0
def install_ftprd(tasks):
    """
    Register tasks with cron.
    :param tasks: a list where every element is an object with 2 keys: 'command' and 'schedule'.
    :return: the number of cron jobs created.
    """
    # print('Valori di kronos:')
    # print(KRONOS_PYTHON)
    # print(KRONOS_MANAGE)
    # print(KRONOS_PYTHONPATH)

    # Se non ho capito male a me questo non serve perché non mi interessa cercare i management command custom che sono
    # decorati con kronos.
    # load()

    my_platform = platform.system().upper()
    if my_platform.find('LINUX') > -1:
        tab = crontab.CronTab(user=True)
    elif my_platform.find('DARWIN') > -1:
        tab = crontab.CronTab(user=True)
    elif my_platform.find('WINDOWS') > -1:
        # reference: https: // pypi.python.org / pypi / python - crontab
        # NB: actually we never call the next line in a windows environment
        tab = crontab.CronTab(tabfile="c:\\temp\\filename.tab")
    else:
        return 0

    for task in tasks:
        tab.new(task['command'], KRONOS_BREADCRUMB).setall(task['schedule'])
        tab.write()
    return len(tasks)
Esempio n. 2
0
    def __init__(self, cwd, pypath, file2run):

        self._crontab = crontab.CronTab(user=True)
        self.pypath = pypath
        self.cwd = cwd
        self.file2run = file2run
        self.job = None
Esempio n. 3
0
    def do_check_CronJobs(self):
        # check cron/date (changes of self.refs are tracked (and reload) in
        # on_pubmsg)
        page = self.refs[self.templ]
        ctab = self.refs[self.cron].get()
        # extract 'rev' and 'timestmp' from 'crontab' page text ...

        # hacky/ugly/cheap
        for line in ctab.splitlines():
            (rev, timestmp) = [item.strip() for item in line[1:].split(',')]

            # [min] [hour] [day of month] [month] [day of week]
            # (date supported only, thus [min] and [hour] dropped)
            if not crontab:
                pywikibot.error(
                    '"crontab" library is needed to run the script properly.')
                return None
            entry = crontab.CronTab(timestmp)
            # find the delay from current minute
            # (does not return 0.0 - but next)
            now = datetime.datetime.now().replace(second=0, microsecond=0)
            delay = entry.next(now - datetime.timedelta(microseconds=1))

            if (delay <= bot_config['CRONMaxDelay']):
                pywikibot.output('CRONTAB: %s / %s / %s' %
                                 (page, rev, timestmp))
                self.do_check(page.title(), int(rev))
Esempio n. 4
0
def get_cron_tasks(cron_task_id=None, user='******'):
    """Function to return all the user created cron."""
    cron_list = []
    try:
        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)

        if cron_task_id is None:
            query = 'select * from cron_tasks'
        else:
            query = 'select * from cron_tasks where cron_task_id=%s' % cron_task_id

        cron_db_entries, err = db.get_multiple_rows(db_path, query)
        if err:
            raise Exception(err)

        if cron_db_entries:
            cron = crontab.CronTab(user)
            for cron_db_entry in cron_db_entries:
                cron_dict = {}
                cron_dict['description'] = cron_db_entry['description']
                cron_dict['command'] = cron_db_entry['command']
                cron_dict['cron_task_id'] = cron_db_entry['cron_task_id']
                jobs = cron.find_comment(str(cron_db_entry['cron_task_id']))
                if jobs:
                    for job in jobs:
                        cron_dict['schedule_description'] = job.description(
                            use_24hour_time_format=True)
                        cron_dict['job'] = job
                        break
                cron_list.append(cron_dict)
    except Exception, e:
        return None, 'Error listing all cron entries : %s' % str(e)
Esempio n. 5
0
 def test_06_touser(self):
     """Write to use API"""
     cron = crontab.CronTab(tab=USER)
     cron.write_to_user('bob')
     filename = os.path.join(TEST_DIR, 'data', 'bob.tab')
     self.filenames.append(filename)
     self.assertTrue(os.path.exists(filename))
Esempio n. 6
0
  def Parse(self, stat, file_object, knowledge_base):
    """Parse the crontab file."""
    _ = knowledge_base
    entries = []

    crondata = file_object.read()
    jobs = crontab.CronTab(tab=crondata)

    for job in jobs:
      entries.append(
          rdf_cronjobs.CronTabEntry(
              minute=utils.SmartStr(job.minute),
              hour=utils.SmartStr(job.hour),
              dayofmonth=utils.SmartStr(job.dom),
              month=utils.SmartStr(job.month),
              dayofweek=utils.SmartStr(job.dow),
              command=utils.SmartStr(job.command),
              comment=utils.SmartStr(job.comment)))

    try:
      source_urn = file_object.urn
    except AttributeError:
      source_urn = None

    yield rdf_cronjobs.CronTabFile(aff4path=source_urn, jobs=entries)
Esempio n. 7
0
def buildcron(myuser, mybasename, cmds, times):
    crony = crontab.CronTab(user=str(myuser))
    job = crony.new(command=cmds, comment=mybasename)
    job.setall(times)
    job.enable()
    print(job)
    crony.write_to_user(myuser)
Esempio n. 8
0
def remove_crontab_entry():
    """
        remove_crontab_entry:

        removes the background_daemon entry in the crontab

        input:
            Nothing

        output:
            
            the resulting crontab

        side-effects
            
            the bg_daemon entry in the crontab will be removed
    """
    tab = crontab.CronTab(user=True)

    jobs = [x for x in tab.find_command("background_daemon.py")]

    if len(jobs) == 0:
        return tab

    for job in jobs:
        if job.command == (DEFAULT_COMMAND):
            tab.remove(job)

    tab.write_to_user(user=True)
    return tab
Esempio n. 9
0
def update_cron_schedule(cron_task_id=None,
                         user='******',
                         min='0',
                         hour='0',
                         day='*',
                         dow='*',
                         month='*'):
    """Update schedule of a specific crontab entry(not the cron_tasks table entry)

    """
    try:
        if cron_task_id is None:
            raise Exception('cron_task_id required')
        found = False

        cron = crontab.CronTab(user)
        # can not use 'jobs = cron.find_comment(str(cron_task_id))' since it
        # returns True always
        if cron:
            for entry in cron:
                if entry.comment == str(cron_task_id):
                    found = True
                    entry.setall(min, hour, day, dow, month)
                    if entry.is_valid():
                        entry.enable()
                        cron.write()
                    else:
                        raise Exception('Cron entry not valid.')
        if not found:
            raise Exception('Found no matching entry with comment string %s' %
                            str(cron_task_id))

    except Exception, e:
        return None, 'Error updating cron entry: %s' % str(e)
Esempio n. 10
0
def set_cert_auto_renew(mode='on', flog=None):

    try:
        cron = crontab.CronTab(user='******')
        cron.remove_all(comment=LETSENCRYPY_TAG)
        cron.write()
    except Exception as e:
        flog.error(inspect.stack()[0][3] +
                   ": crontab kon niet worden geopend, gestopt. Fout=" +
                   str(e.args[0]))
        return False

    if mode == 'on':
        try:
            job = cron.new(
                command=
                '/p1mon/scripts/P1NginxConfig.py --renewcerts >/dev/null 2>&1',
                comment=LETSENCRYPY_TAG)
            # be nice to LetsEncrypt and not DDOS with P1 monitor.
            hour = random.randint(0, 6)
            min = random.randint(0, 59)
            job.setall(str(min), str(hour), '*', '*', '*')
            cron.write()
            flog.info(inspect.stack()[0][3] +
                      ": certficaat vernieuwing wordt elke dag om " +
                      str('{:02d}'.format(hour)) + ":" +
                      str('{:02d}'.format(min)) + " uitgevoerd.")
        except Exception as e:
            flog.error(inspect.stack()[0][3] + ": crontab update fout=" +
                       str(e.args[0]))
            return False

    flog.debug(inspect.stack()[0][3] + ": succes")
    return True
Esempio n. 11
0
def create_updater():
    print('\nDDNS Updater New Config')

    # Found In The Dynamic DNS Drop Down
    username = input("Enter Username: "******"Enter Password: "******"Enter Hostname: ")

    offline = filter_input("Would you like to set status to offline? (y/n): ")

    with open('config.json', 'w') as config:
        json.dump(
            {
                "username": username,
                "password": password,
                "hostname": hostname,
                "offline": offline
            },
            config,
            indent=4)

    subprocess.run(['python', f'{os.getcwd()}/update.py'])

    # Create cronjob to automatically update DDNS
    with crontab.CronTab(user=True) as cron:
        job = cron.new(command=f'python {os.getcwd()}/update.py',
                       comment=f'DDNS Updater: {hostname}')
        job.hour.every(1)
        print(job.every().hour())
Esempio n. 12
0
def create_cron_task(command, description, min="1", hour='*', day='*', dow='*', month='*', user='******', task_type_id=0):
    cron_task_id = None
    try:
        if not command or not description:
            raise Exception('Invalid parameters')

        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)

        cmd = [
            'insert into cron_tasks(command,description, task_type_id) values (?,?,?)', (command, description, task_type_id)]
        cron_task_id, err = db.execute_iud(db_path, [cmd], get_rowid=True)
        if err:
            raise Exception(err)

        log_dir, err = config.get_cron_log_dir_path()
        if err:
            raise Exception(err)
        if not os.path.isdir(log_dir):
            os.mkdir(log_dir)
        log_file = '%s/%d.log' % (log_dir, cron_task_id)
        command = '%s >> %s 2>&1' % (command, log_file)

        cron = crontab.CronTab(user)
        job = cron.new(command=command, comment='%d' % cron_task_id)
        job.setall(min, hour, day, dow, month)
        if job.is_valid():
            job.enable()
            cron.write()
        else:
            raise Exception('Cron entry not valid.')

    except Exception, e:
        return None, 'Error creating cron entry : %s' % str(e)
    def test_06_escaped_chars(self):
        """Do escaped chars parse correctly when read in"""
        cron = crontab.CronTab(tab="""
* * * * * cmd arg_with_\#_character # comment
""")
        self.assertEqual(cron[0].command, 'cmd arg_with_\\#_character')
        self.assertEqual(cron[0].comment, 'comment')
Esempio n. 14
0
 def setUp(self):
     self.crontab = crontab.CronTab(tab=INITAL_TAB)
     self.job = list(self.crontab.find_command('execute'))[0]
     try:
         import cron_descriptor
     except ImportError:
         self.skipTest("Cron-descriptor module not installed")
Esempio n. 15
0
    def ParseFile(self, knowledge_base, pathspec, filedesc):
        del knowledge_base  # Unused.
        del pathspec  # Unused.

        entries = []

        crondata = filedesc.read().decode("utf-8")
        jobs = crontab.CronTab(tab=crondata)

        for job in jobs:
            entries.append(
                rdf_cronjobs.CronTabEntry(minute=str(job.minute),
                                          hour=str(job.hour),
                                          dayofmonth=str(job.dom),
                                          month=str(job.month),
                                          dayofweek=str(job.dow),
                                          command=str(job.command),
                                          comment=str(job.comment)))

        try:
            source_urn = filedesc.urn
        except AttributeError:
            source_urn = None

        yield rdf_cronjobs.CronTabFile(aff4path=source_urn, jobs=entries)
Esempio n. 16
0
    def poll_outgoing_item(self, item):
        """
        Check if it is time to trigger event for given source

        :param item: source instance to check
        
        """
        if item.source == "CrontabSource":
            now = datetime.datetime.utcnow()
            future = crontab.CronTab(item.key).next(default_utc=True, delta=False)
            future = datetime.datetime.utcfromtimestamp(future)

            prev_val = item.get
            prev_st = item.status_code

            if prev_st == StatusCode.NONE:
                item.status_code = StatusCode.INITIAL
                item.get = future
                item.source_time = now
                if self.send_inital_value:
                    log.debug("Initial %s %s", item.key, future)
                    self.send_outgoing(item)
            else:
                if now >= prev_val:
                    log.debug("next %s at %s now: %s", item.key, future, now)
                    item.status_code = StatusCode.GOOD
                    item.get = future
                    item.source_time = now
                    self.send_outgoing(item)
Esempio n. 17
0
def main(dry_run=True):

    cron = crontab.CronTab(user=settings.CRON_USER)

    analytics = ensure_item(
        cron, 'bash {}'.format(app_prefix('scripts/analytics.sh')))
    analytics.hour.on(2)
    analytics.minute.on(0)  # Daily 2:00 a.m.

    digest = ensure_item(
        cron, 'bash {}'.format(app_prefix('scripts/send_digest.sh')))
    digest.hour.on(2)
    digest.minute.on(0)  # Daily 2:00 a.m.

    box = ensure_item(
        cron, 'bash {}'.format(app_prefix('scripts/refresh_box_tokens.sh')))
    box.hour.on(2)
    box.minute.on(0)  # Daily 2:00 a.m.

    retractions = ensure_item(
        cron, 'bash {}'.format(app_prefix('scripts/retract_registrations.sh')))
    retractions.hour.on(0)
    retractions.minute.on(0)  # Daily 12 a.m.

    embargoes = ensure_item(
        cron, 'bash {}'.format(app_prefix('scripts/embargo_registrations.sh')))
    embargoes.hour.on(0)
    embargoes.minute.on(0)  # Daily 12 a.m.

    registration_approvals = ensure_item(
        cron, 'bash {}'.format(app_prefix('scripts/approve_registrations.sh')))
    registration_approvals.hour.on(0)
    registration_approvals.minute.on(0)  # Daily 12 a.m.

    files_audit = ensure_item(
        cron,
        'bash {}'.format(app_prefix('scripts/osfstorage/files_audit.sh')))
    files_audit.dow.on(0)
    files_audit.hour.on(2)
    files_audit.minute.on(0)  # Sunday 2:00 a.m.

    glacier_inventory = ensure_item(
        cron, 'bash {}'.format(
            app_prefix('scripts/osfstorage/glacier_inventory.sh')))
    glacier_inventory.dow.on(0)
    glacier_inventory.hour.on(0)
    glacier_inventory.minute.on(0)  # Sunday 12:00 a.m.

    glacier_audit = ensure_item(
        cron,
        'bash {}'.format(app_prefix('scripts/osfstorage/glacier_audit.sh')))
    glacier_audit.dow.on(0)
    glacier_audit.hour.on(6)
    glacier_audit.minute.on(0)  # Sunday 6:00 a.m.

    logger.info('Updating crontab file:')
    logger.info(cron.render())

    if not dry_run:
        cron.write_to_user(settings.CRON_USER)
Esempio n. 18
0
def cron_update():
    # Use global
    global cfg_crontab
    # Access crontab
    print('tsk_ctrl: cron job update', file=sys.stdout)
    try:
        # request access
        jobs = crontab.CronTab(user='******')
        # Delete cron jobs
        jobs.remove_all(comment=cfg_crontab['id'])
        # Saveing list of jobs
        jobs.write()
        # Update cron jobs
        if not bool(cfg_crontab['doQuery']) and not bool(
                cfg_crontab['doClean']):
            return
        # Timecheck
        if int(cfg_crontab['time']) < 0:
            return
        # Create Cron Job
        # Get path
        dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
        # Create job
        job = jobs.new(command=dirname + '/tsk_logi.py ./tsk_cron.py',
                       comment=cfg_crontab['id'])
        # Every day at ...
        time = int(cfg_crontab['time']) % (24 * 60)
        job.minute.on(time % 60)
        job.hour.on(time // 60)
        jobs.write()
        print('tsk_ctrl: job [' + job.__str__() + '] created.',
              file=sys.stdout)
        return
    except:
        return
def getModulesWithStatus():
    """Retrieves a list of all items in the modules folder"""
    ignore_modules = ["sdmodulebase.py", "example.py"]
    modules = []
    for file in os.listdir(os.path.join(BASEDIR, "modules")):
        if file.endswith(".py") and file not in ignore_modules:
            # Get status | True = on, False = off
            name = os.path.splitext(file)[0]
            freq = "off"
            try:
                job = [
                    x for x in crontab.CronTab(user=True) if x.comment == name
                ][0]
                if job.frequency() == 8784:
                    freq = "hourly"
                elif job.frequency() == 366:
                    freq = "daily"
                elif job.frequency() == 52:
                    freq = "weekly"
                elif job.frequency() == 12:
                    freq = "monthly"
            except:
                pass

            modules.append([name, freq])

    return modules
Esempio n. 20
0
    def ParseFile(
        self,
        knowledge_base: rdf_client.KnowledgeBase,
        pathspec: rdf_paths.PathSpec,
        filedesc: IO[bytes],
    ) -> Iterator[rdf_cronjobs.CronTabFile]:
        del knowledge_base  # Unused.

        entries = []

        crondata = filedesc.read().decode("utf-8")
        jobs = crontab.CronTab(tab=crondata)

        for job in jobs:
            entries.append(
                rdf_cronjobs.CronTabEntry(minute=str(job.minute),
                                          hour=str(job.hour),
                                          dayofmonth=str(job.dom),
                                          month=str(job.month),
                                          dayofweek=str(job.dow),
                                          command=str(job.command),
                                          comment=str(job.comment)))

        yield rdf_cronjobs.CronTabFile(
            # We're interested in the nominal file path, not the full Pathspec.
            path=pathspec.last.path,
            jobs=entries)
Esempio n. 21
0
def delete_cron(cron_task_id, user='******'):
    """Delete a cron by the cron_task_id."""
    try:
        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)

        query_tasks = 'select * from tasks where cron_task_id="%d"' % int(
            cron_task_id)
        tasks, err = db.get_multiple_rows(db_path, query_tasks)
        if err:
            raise Exception(err)

        cmd_list = []
        cmd_list.append(
            ['delete from cron_tasks where cron_task_id=%d' % int(cron_task_id)])
        cmd_list.append(
            ['update tasks set status="cancelled" where cron_task_id=%d and (status is not "completed" and status is not "failed")' % int(cron_task_id)])
        if tasks:
            for task in tasks:
                cmd_list.append(
                    ['update subtasks set status="cancelled" where task_id=%d  and (status is not "completed" and status is not "failed")' % task['task_id']])

        ret, err = db.execute_iud(db_path, cmd_list)
        if err:
            raise Exception(err)

        cron = crontab.CronTab(user)
        cron.remove_all(comment=str(cron_task_id))
        cron.write()
    except Exception, e:
        return False, "Error deleting cron entry : %s" % str(e)
def delete_cron(cron_task_id, user='******'):
    """Delete a cron by the cron_task_id."""
    try:
        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)

        tasks, err = get_tasks_by_cron_task_id(cron_task_id)
        if err:
            raise Exception(err)
        cmd_list = []
        cmd_list.append(
            ['delete from cron_tasks where cron_task_id=%d' % cron_task_id])
        cmd_list.append([
            'update tasks set status="cancelled" where cron_task_id=%s and status is not "completed"'
            % cron_task_id
        ])
        if tasks:
            for task in tasks:
                cmd_list.append([
                    'update subtasks set status="cancelled" where task_id=%d  and status is not "completed"'
                    % task['task_id']
                ])
        # print cmd_list

        ret, err = db.execute_iud(db_path, cmd_list)
        if err:
            raise Exception(err)

        cron = crontab.CronTab(user)
        cron.remove_all(comment=str(cron_task_id))
        cron.write()
    except Exception, e:
        return False, "Error deleting cron entry : %s" % str(e)
Esempio n. 23
0
def get_next_scheduled_time(cron_string, use_local_timezone=False):
    """Calculate the next scheduled time by creating a crontab object
    with a cron string"""
    now = datetime.now()
    cron = crontab.CronTab(cron_string)
    next_time = cron.next(now=now, return_datetime=True)
    tz = dateutil.tz.tzlocal() if use_local_timezone else dateutil.tz.UTC
    return next_time.astimezone(tz)
Esempio n. 24
0
 def create_cronjob(self, interval_minutes: int):
     cron = crontab.CronTab(user='******')
     command_string = ''
     for command in self.commands:
         command_string += f'{" ".join(command)};'
     job = cron.new(command=command_string, comment=self.name)
     job.minute.every(interval_minutes)
     cron.write()
Esempio n. 25
0
    def get(self):
        """ Shows active cron entries for given appid
    """
        app_id = self.request.get("appid")
        mail_to = []
        cron_jobs = []
        warnings = []
        cron_info = self.helper.get_application_cron_info(app_id)

        yaml_file = cron_info.get("cron_yaml_file", [])
        etc_crond_file = cron_info.get("etc_crond_file", "")
        try:
            crond_file = crontab.CronTab(tab=etc_crond_file, user=False)
        except IOError as ioe:
            logging.error(ioe)
        else:
            crond_records = defaultdict(list)
            yaml_records = {}
            for yaml_entry in yaml_file["cron"]:
                url = yaml_entry["url"]
                yaml_records[url] = yaml_entry
                for crond_entry in crond_file:
                    if url in crond_entry.command:
                        crond_records[url].append(crond_entry)
                        logging.info(crond_entry)

            if len(yaml_records) != len(crond_records):
                warnings.append(
                    "One of the cron jobs from cron.yaml is missing in crond file for appid {0}."
                    "Look at controller logs for more information at /var/log/appscale/"
                    .format(app_id))
                logging.warning(warnings)

            for url, entries in crond_records.iteritems():
                yaml_record = yaml_records.get(url, {})
                query_params = {"url": url, "appid": app_id}
                url_command = "/cron/run?" + urllib.urlencode(query_params)
                cron_jobs.append({
                    "url":
                    url,
                    "frequency":
                    yaml_record.get("schedule", ""),
                    "frequency_cron_format":
                    "\n".join(str(entry.slices) for entry in entries),
                    "description":
                    yaml_record.get("description", ""),
                    "url_command":
                    url_command
                })
            logging.info(cron_jobs)

            self.render_app_page(page='cron',
                                 values={
                                     'mail_to': mail_to,
                                     'cron_jobs': cron_jobs,
                                     'warnings': warnings,
                                     'page_content': self.TEMPLATE
                                 })
 def start(self):
     pass
     cron = crontab.CronTab(user=self.username)
     cmd_ = "%s %s" % (self.pythonInterpreter,
                       self.logisticRegressionTrainer)
     print(cmd_)
     job = cron.new(command=cmd_)
     job.hour.every(12)
     cron.write()
Esempio n. 27
0
def uninstall():
    """
    Uninstall tasks from cron.
    """
    tab = crontab.CronTab(user=True)
    count = len(list(tab.find_comment(KRONOS_BREADCRUMB)))
    tab.remove_all(comment=KRONOS_BREADCRUMB)
    tab.write()
    return count
Esempio n. 28
0
def setup_mail_crontab():
    cron = crontab.CronTab(user=True)

    jobs = list(cron.find_command(mail_cmd))
    if len(jobs) is 0:
        print 'Setting up mail job'
        mail_job = cron.new(mail_cmd, mail_cmt)
        mail_job.minute.every(3)
        cron.write()
    def test_03_specials(self):
        """Ignore Special Symbols"""
        tab = crontab.CronTab(
            tabfile=os.path.join(TEST_DIR, 'data', 'specials.tab'))
        self.assertEqual(
            tab.render(), """0 * * * * hourly
0 0 * * * daily
0 0 * * 0 weekly
""")
Esempio n. 30
0
 def test_06_touser(self):
     """Write to use API"""
     cron = crontab.CronTab(tab=USER)
     self.assertEqual(repr(cron), "<Unattached CronTab>")
     cron.write_to_user('bob')
     filename = os.path.join(TEST_DIR, 'data', 'spool', 'bob')
     self.filenames.append(filename)
     self.assertTrue(os.path.exists(filename))
     self.assertEqual(repr(cron), "<User CronTab 'bob'>")