from ndscheduler import job


class ShellJob(job.JobBase):
    @classmethod
    def meta_info(cls):
        return {
            'job_class_string':
            '%s.%s' % (cls.__module__, cls.__name__),
            'notes':
            ('This will run an executable program. You can specify as many '
             'arguments as you want. This job will pass these arguments to the '
             'program in order.'),
            'arguments': [{
                'type': 'string',
                'description': 'Executable path'
            }],
            'example_arguments':
            '["/usr/local/my_program", "--file", "/tmp/abc", "--mode", "safe"]'
        }

    def run(self, *args, **kwargs):
        return {'result': check_output(args).decode('utf-8')}


if __name__ == "__main__":
    # You can easily test this job here
    job = ShellJob.create_test_instance()
    job.run('ls', '-l')
Esempio n. 2
0
        return {
            'job_class_string': '%s.%s' % (cls.__module__, cls.__name__),
            'notes': 'This sends a HTTP request to a particular URL',
            'arguments': [
                # url
                {'type': 'string', 'description': 'What URL you want to make a GET call?'},
                # Request Type
                {'type': 'string', 'description': 'What request type do you want? '
                                                  '(currently supported: GET/DELETE)'},

            ],
            'example_arguments': ('["http://localhost:8888/api/v1/jobs", "GET"]'
                                  '["http://localhost:8888/api/v1/jobs/ba12e", "DELETE"]')
        }

    def run(self, url, request_type,  *args, **kwargs):
        print('Calling GET on url: %s' % (url))

        session = requests.Session()
        result = session.request(request_type,
                                 url,
                                 timeout=self.TIMEOUT,
                                 headers=None,
                                 data=None)
        print(result.text)


if __name__ == "__main__":
    job = CurlJob.create_test_instance()
    job.run('http://localhost:888/api/v1/jobs')
Esempio n. 3
0
        # 即将启动的任务所在路径(到dir一层) --> 好像有缺陷!
        # 不用安装也可以了!! 直接引入
        sys.path.append(task_dict['env_path'])
        env_path = task_dict.get('path_dir')
        sys.path.append(env_path)

        # 模块名(文件名以.py结束)
        try:
            o = import_module(task_dict['file_name'].split('.py')[0])
            o.start_task(**task_params)

            logger.info(('Task %s has been finished.' % task_name))

            # 删掉刚刚引入的包,确保每次都是最新的
            sys.path.remove(env_path)
            sys.path.remove(task_dict['env_path'])
            del o

        except Exception as e:
            logger.error('Task %s failed. - %s' % (task_name, e))
            raise e

        return [json.dumps(task_dict)]


if __name__ == "__main__":
    # You can easily test this job here
    job = RunInNdsJob.create_test_instance()
    job.run()
Esempio n. 4
0
            '%s.%s' % (cls.__module__, cls.__name__),
            'notes':
            'This will print a string in your shell. Check it out!',
            'arguments': [
                # argument1
                {
                    'type': 'string',
                    'description': 'First argument'
                },

                # argument2
                {
                    'type': 'string',
                    'description': 'Second argument'
                }
            ],
            'example_arguments':
            '["first argument AAA", "second argument BBB"]'
        }

    def run(self, argument1, argument2, *args, **kwargs):
        print('Hello from AwesomeJob! Argument1: %s, Argument2: %s' %
              (argument1, argument2))
        return [argument1, argument2]


if __name__ == "__main__":
    # You can easily test this job here
    job = AwesomeJob.create_test_instance()
    job.run(123, 456)
Esempio n. 5
0
    @classmethod
    def meta_info(cls):
        return {
            'job_class_string': '%s.%s' % (cls.__module__, cls.__name__),
            'notes': 'This sends a push notification to APNS servers. '
                     'The environment variable APNS_CERT_PATH" should be provided '
                     'for APNS cert file path.',
            'arguments': [
                # APNS device token
                {'token': 'string', 'description': 'Device token'},
                # APNS Title's Alert Text
                {'alert': 'string', 'description': 'What do you want to send?'},
            ],
            'example_arguments': ('["da1232badh2", "Hello from scheduler"]')
        }

    def run(self, token, alert="Hello World",  *args, **kwargs):
        print('Sending %s to %s' % (alert, token))

        cert_file = os.environ['APNS_CERT_PATH'] or 'simple_scheduler/jobs/apns-cert.pem'
        apns = APNs(use_sandbox=False, cert_file=cert_file)
        # Send a notification
        payload = Payload(alert=alert, sound="default", badge=0)
        apns.gateway_server.send_notification(token, payload)


if __name__ == "__main__":
    job = APNSJob.create_test_instance()
    job.run('da1232badh2', 'Hello World')
Esempio n. 6
0
                },
                # Request Type
                {
                    'type':
                    'string',
                    'description':
                    'What request type do you want? '
                    '(currently supported: GET/DELETE)'
                },
            ],
            'example_arguments':
            ('["http://localhost:8888/api/v1/jobs", "GET"]'
             '["http://localhost:8888/api/v1/jobs/ba12e", "DELETE"]')
        }

    def run(self, url, request_type, *args, **kwargs):
        print 'Calling GET on url: %s' % (url)

        session = requests.Session()
        result = session.request(request_type,
                                 url,
                                 timeout=self.TIMEOUT,
                                 headers=None,
                                 data=None)
        print(result.text)


if __name__ == "__main__":
    job = CurlJob.create_test_instance()
    job.run('http://localhost:888/api/v1/jobs')
Esempio n. 7
0
"""A job to run executable programs."""

from subprocess import call

from ndscheduler import job


class ShellJob(job.JobBase):

    @classmethod
    def meta_info(cls):
        return {
            'job_class_string': '%s.%s' % (cls.__module__, cls.__name__),
            'notes': ('This will run an executable program. You can specify as many '
                      'arguments as you want. This job will pass these arguments to the '
                      'program in order.'),
            'arguments': [
                {'type': 'string', 'description': 'Executable path'}
            ],
            'example_arguments': '["/usr/local/my_program", "--file", "/tmp/abc", "--mode", "safe"]'
        }

    def run(self, *args, **kwargs):
        return {'returncode': call(args)}


if __name__ == "__main__":
    # You can easily test this job here
    job = ShellJob.create_test_instance()
    job.run('ls', '-l')
Esempio n. 8
0
    def meta_info(cls):
        return {
            'job_class_string': '%s.%s' % (cls.__module__, cls.__name__),
            'notes': 'This will toggle an RPIO input/ouput!',
            'arguments': [
                {'type': 'string', 'description': 'New state (on / off)'}
            ],
            'example_arguments': '["off"]'
        }

    def run(self, action, *args, **kwargs):
        GPIO.setmode(GPIO.BCM)
        ledPin = 23
        switchPin = 17
        GPIO.setwarnings(False)
        GPIO.setup(switchPin, GPIO.OUT)
        print('--RPIOJob argument: %s' % (action))
        if action == "on":
            GPIO.output(ledPin, GPIO.HIGH)
            GPIO.output(switchPin, GPIO.HIGH)
        if action == "off":
            GPIO.output(ledPin, GPIO.LOW)
            GPIO.output(switchPin, GPIO.LOW)
        return action


if __name__ == "__main__":
    # You can easily test this job here
    job = RPIOJob.create_test_instance()
    job.run("on")
Esempio n. 9
0
"""A sample job that prints string."""

from ndscheduler import job


class AwesomeJob(job.JobBase):

    @classmethod
    def meta_info(cls):
        return {
            'job_class_string': '%s.%s' % (cls.__module__, cls.__name__),
            'notes': 'This will print a string in your shell. Check it out!',
            'arguments': [
                # argument1
                {'type': 'string', 'description': 'First argument'},

                # argument2
                {'type': 'string', 'description': 'Second argument'}
            ],
            'example_arguments': '["first argument AAA", "second argument BBB"]'
        }

    def run(self, argument1, argument2, *args, **kwargs):
        print('Hello from AwesomeJob! Argument1: %s, Argument2: %s' % (argument1, argument2))


if __name__ == "__main__":
    # You can easily test this job here
    job = AwesomeJob.create_test_instance()
    job.run(123, 456)
Esempio n. 10
0
            # https://nextdoor.slack.com/apps/new/A0F7XDUAZ-incoming-webhooks
            url = os.environ['SIMPLE_SCHEDULER_SLACK_URL']
        except KeyError:
            logger.error('Environment variable SIMPLE_SCHEDULER_SLACK_URL is not specified. '
                         'So we cannot send slack message.')
            raise KeyError('You have to set Environment variable SIMPLE_SCHEDULER_SLACK_URL first.')
        else:
            session = requests.Session()
            adapter = requests.adapters.HTTPAdapter(max_retries=self.MAX_RETRIES)
            session.mount('http://', adapter)
            session.mount('https://', adapter)

            message += ' // `sent from %s`' % socket.gethostname()
            payload = {
                'channel': channel,
                'username': name,
                'text': message,
                'link_names': 1,
                "mrkdwn": 1,
                'icon_emoji': icon_emoji
            }
            session.request('POST', url, timeout=self.TIMEOUT,
                            headers={'content-type': 'application/json'},
                            data=json.dumps(payload))


if __name__ == "__main__":
    # You can easily test this job here
    job = SlackJob.create_test_instance()
    job.run('#slack-bot-test', 'ndscheduler', ':satisfied:', 'Standup, team! @channel')
Esempio n. 11
0
            # You can get this url by adding an incoming webhook:
            # https://nextdoor.slack.com/apps/new/A0F7XDUAZ-incoming-webhooks
            url = os.environ['SIMPLE_SCHEDULER_SLACK_URL']
        except KeyError:
            logger.error('Environment variable SIMPLE_SCHEDULER_SLACK_URL is not specified. '
                         'So we cannot send slack message.')
            raise KeyError('You have to set Environment variable SIMPLE_SCHEDULER_SLACK_URL first.')
        else:
            session = requests.Session()
            adapter = requests.adapters.HTTPAdapter(max_retries=self.MAX_RETRIES)
            session.mount('http://', adapter)
            session.mount('https://', adapter)

            message += ' // `sent from %s`' % socket.gethostname()
            payload = {
                'channel': channel,
                'username': name,
                'text': message,
                'link_names': 1,
                "mrkdwn": 1,
                'icon_emoji': icon_emoji
            }
            session.request('POST', url, timeout=self.TIMEOUT,
                            headers={'content-type': 'application/json'},
                            data=json.dumps(payload))

if __name__ == "__main__":
    # You can easily test this job here
    job = SlackJob.create_test_instance()
    job.run('#slack-bot-test', 'ndscheduler', ':satisfied:', 'Standup, team! @channel')
Esempio n. 12
0
        else:
            overrides = {}
        response = client.run_task(taskDefinition=task_def_arn,
                                   overrides=overrides,
                                   cluster=self.cluster)
        _task_ids = [task['taskArn'] for task in response['tasks']]

        # Wait on task completion
        self._track_tasks(_task_ids)


if __name__ == "__main__":
    # You can easily test this job here
    job = ECSJob.create_test_instance()
    job.run(
        'ClusterName', "arn:aws:ecs:<region>:<user_id>:task-"
        "definition/<task_def_name>:<revision_number>")
    job.run(
        'DataETLCluster', None, {
            'family':
            'hello-world',
            'volumes': [],
            'containerDefinitions': [{
                'memory': 1,
                'essential': True,
                'name': 'hello-world',
                'image': 'ubuntu',
                'command': ['/bin/echo', 'hello world']
            }]
        })
Esempio n. 13
0
from ndscheduler import job


class ShellJob(job.JobBase):
    @classmethod
    def meta_info(cls):
        return {
            "job_class_string":
            "%s.%s" % (cls.__module__, cls.__name__),
            "notes":
            ("This will run an executable program. You can specify all params "
             "in a single string."),
            "arguments": {
                "type": "string",
                "description": "Executable path"
            },
            "example_arguments":
            '["/usr/local/my_program --file /tmp/abc --mode safe"]',
        }

    def run(self, shell_cmd, *args, **kwargs):
        print("Running shell job '{}'".format(shell_cmd))
        cmd = shell(shell_cmd)
        return {"stdout": cmd.output(), "errors": cmd.errors()}


if __name__ == "__main__":
    # You can easily test this job here
    job = ShellJob.create_test_instance()
    job.run("ls -l")