Giter Site home page Giter Site logo

drmonkeysee / ecs-scheduler Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 2.0 289 KB

A task scheduling application for Amazon ECS

License: MIT License

Makefile 0.38% Python 99.23% Dockerfile 0.31% Shell 0.08%
aws aws-ecs scheduler apscheduler scheduling rest-api flask flask-restful docker python

ecs-scheduler's Introduction

ECS Scheduler

NOTE (August 2021)

ECS Scheduler was created in 2015 for a specific need we had when running much of our infrastructure on Amazon ECS and is no longer under active development. Since then, Amazon has advanced the ECS product to the point where it natively handles most or all of what this service does. In addition Lambda, Serverless, and other AWS products provide more powerful alternatives for running medium-to-heavy tasks. I would advise you to review your options among AWS's product offerings before using this service.

Summary

A scheduler for executing Amazon ECS docker tasks, controlled via a JSON REST API.

Amazon ECS makes it possible to manage and run docker containers on EC2 instances. An ECS task, consisting of one or more docker containers, can be run indefinitely as a service or can be launched manually as a standalone task.

However there is a third execution model in between one-off and persistent tasks: tasks that execute on a scheduled interval or when a certain environmental condition is met. These container tasks may need to perform a few seconds or a few days of work, but they do not need to run indefinitely. Amazon ECS does not support this natively so ECS Scheduler was created to fill that gap. ECS Scheduler allows you to manage the execution schedules of such ephemeral containers turning ECS and docker into a high-octane version of AWS Lambda!

Getting Started

ECS Scheduler is designed to be run as a standalone application rather than an installable Python package. Docker is the preferred means to host it using the provided Dockerfile. It is written in Flask and uses the APScheduler package for job scheduling.

boto3 is the package used to communicate to AWS services. You will need AWS credentials to access, at a minimum, ECS. In addition boto3 clients require some basic configuration, such as the default AWS region. How to specify AWS credentials and configuration can be found in the boto3 developer guide.

Specifically, all boto3 clients created in ecs-scheduler rely on passing credentials and configuration indirectly (i.e. all credential locations listed in the developer guide from point 3 on down). For local development it's easiest to use environment variables or home directory files; for production consult with your DevOps team, though generally the most robust approach is to use IAM roles.

System Requirements

Development

Run make check to execute the unit tests.

Run make or make debug to launch ECS Scheduler in debug mode.

Either make target will set up a virtual environment automatically if it does not already exist.

Docker and Deployment

If you want to run ECS Scheduler in docker use make docker to build the image. The docker image is the recommended way to deploy and run ECS Scheduler outside of a local development environment. The docker container hosts ECS Scheduler within uWSGI behind an nginx server, making it more robust than a standalone Flask application.

The following example runs an instance of the ECS Scheduler container using an on-image SQLite database as the persistent store and passes your AWS credentials to the container via an environment file named docker-env:

> docker run --name ecs-scheduler -p 8080:80 -e ECSS_ECS_CLUSTER=test-cluster -e ECSS_SQLITE_FILE=/var/opt/ecss/ecs-scheduler.db --env-file ~/.aws/docker-env -d ecs-scheduler

For reference docker-env would look something like:

AWS_DEFAULT_REGION=<my_region>
AWS_ACCESS_KEY_ID=<my_key>
AWS_SECRET_ACCESS_KEY=<my_secret>

make docker-clean will delete all stopped ECS Scheduler containers and remove the image.

Credits

This application is adapted from an internal Openmail project. Thanks to the following people for their contributions during its development:

And very special thanks to Michael Schaffer for the original idea back in 2015.

ecs-scheduler's People

Contributors

dependabot[bot] avatar drmonkeysee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ecs-scheduler's Issues

boto3 need region set

FYI:
get this error

*** Stats server enabled on /tmp/ecss-stats.sock fd: 11 ***
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/uwsgidecorators.py", line 73, in postfork_chain_hook
    f()
  File "/usr/lib/python3.9/site-packages/uwsgidecorators.py", line 94, in __call__
    return self.f()
  File "/opt/ecs-scheduler/./ecs_scheduler/app.py", line 48, in _setup_application
    _launch_scheduld(ops_queue, jobs_dc)
  File "/opt/ecs-scheduler/./ecs_scheduler/app.py", line 55, in _launch_scheduld
    scheduler = scheduld.create(ops_queue, jobs_dc)
  File "/opt/ecs-scheduler/./ecs_scheduler/scheduld/__init__.py", line 14, in create
    job_exec = JobExecutor()
  File "/opt/ecs-scheduler/./ecs_scheduler/scheduld/execution.py", line 36, in __init__
    self._ecs = boto3.client('ecs')
  File "/usr/lib/python3.9/site-packages/boto3/__init__.py", line 93, in client
    return _get_default_session().client(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/boto3/session.py", line 258, in client
    return self._session.create_client(
  File "/usr/lib/python3.9/site-packages/botocore/session.py", line 847, in create_client
    client = client_creator.create_client(
  File "/usr/lib/python3.9/site-packages/botocore/client.py", line 86, in create_client
    client_args = self._get_client_args(
  File "/usr/lib/python3.9/site-packages/botocore/client.py", line 355, in _get_client_args
    return args_creator.get_client_args(
  File "/usr/lib/python3.9/site-packages/botocore/args.py", line 71, in get_client_args
    final_args = self.compute_client_args(
  File "/usr/lib/python3.9/site-packages/botocore/args.py", line 148, in compute_client_args
    endpoint_config = self._compute_endpoint_config(
  File "/usr/lib/python3.9/site-packages/botocore/args.py", line 220, in _compute_endpoint_config
    return self._resolve_endpoint(**resolve_endpoint_kwargs)
  File "/usr/lib/python3.9/site-packages/botocore/args.py", line 302, in _resolve_endpoint
    return endpoint_bridge.resolve(
  File "/usr/lib/python3.9/site-packages/botocore/client.py", line 430, in resolve
    resolved = self.endpoint_resolver.construct_endpoint(
  File "/usr/lib/python3.9/site-packages/botocore/regions.py", line 133, in construct_endpoint
    result = self._endpoint_for_partition(
  File "/usr/lib/python3.9/site-packages/botocore/regions.py", line 148, in _endpoint_for_partition
    raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.
```

resolved by:
https://stackoverflow.com/questions/40377662/boto3-client-noregionerror-you-must-specify-a-region-error-only-sometimes

i'm good on my side - just letting you knows

AWS IAM permission required to launch tasks into ECS

Hi, me again!

So I have got the dockerised ecs-scheduler running as a service in ECS and am trying to launch a task definition on a schedule.

I can run the task definition manually no problem, but am having a few issues with the permissions. I can work through the errors, but was wondering whether you had stored a list of required permissions? It would expedite things for me :)

My current permissions are:

      policy_tf_expr = {
        "Version" : "2012-10-17",
        "Statement" : [
          {
            "Sid" : "VisualEditor0",
            "Effect" : "Allow",
            "Action" : [
              "ecs:SubmitTaskStateChange",
              "ecs:PutAttributes",
              "ecs:ListAttributes",
              "ecs:ExecuteCommand",
              "ecs:UpdateContainerInstancesState",
              "ecs:StartTask",
              "ecs:DescribeTaskSets",
              "ecs:DeleteTaskSet",
              "ecs:DescribeClusters",
              "ecs:SubmitAttachmentStateChanges",
              "ecs:Poll",
              "ecs:ListTagsForResource",
              "ecs:RunTask",
              "ecs:ListTasks",
              "ecs:SubmitContainerStateChange",
              "ecs:ListContainerInstances",
              "ecs:DescribeContainerInstances",
              "ecs:DeregisterContainerInstance",
              "ecs:TagResource",
              "ecs:UntagResource",
              "ecs:UpdateTaskSet"
            ],
            "Resource" : [
              "arn:aws:ecs:eu-west-1:*:task-set/devops-release-kraken/*/*",
              "arn:aws:ecs:eu-west-1:*:cluster/devops-release-kraken",
              "arn:aws:ecs:eu-west-1:*:container-instance/*",
              "arn:aws:ecs:eu-west-1:*:task-definition/devops-release-kraken-td:*"
            ]
          },
          {
            "Sid" : "VisualEditor1",
            "Effect" : "Allow",
            "Action" : [
              "ecs:DeregisterTaskDefinition",
              "ecs:ListServices",
              "ecs:DiscoverPollEndpoint",
              "ecs:ListTaskDefinitionFamilies",
              "ecs:RegisterTaskDefinition",
              "ecs:ListTaskDefinitions",
              "ecs:DescribeTaskDefinition",
              "ecs:ListClusters"
            ],
            "Resource" : "*"
          },
          {
            "Sid" : "VisualEditor2",
            "Effect" : "Allow",
            "Action" : [
              "iam:CreateServiceLinkedRole",
              "iam:UpdateRoleDescription"
            ]
            "Resource" : "arn:aws:iam::*:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS*",
            "Condition" : {
              "StringLike" : {
                "iam:AWSServiceName" : "ecs.amazonaws.com"
              }
            }
          },
          {
            "Sid" : "VisualEditor3",
            "Effect" : "Allow",
            "Action" : [
              "iam:PassRole"
            ]
            "Resource" : "arn:aws:iam::*:role/*",
          }
        ]
      }

Many thanks!

Best

lack of pinned package versions in requirements.txt results in all tests failing

lack of pinned package versions in requirements.txt results in all tests failing

example:

======================================================================
ERROR: test.webapi.test_subpackage (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test.webapi.test_subpackage
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/test/webapi/test_subpackage.py", line 5, in <module>
    import ecs_scheduler.webapi.home
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/ecs_scheduler/webapi/__init__.py", line 8, in <module>
    from .home import Home
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/ecs_scheduler/webapi/home.py", line 5, in <module>
    from .jobs import Jobs
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/ecs_scheduler/webapi/jobs.py", line 9, in <module>
    from ..datacontext import JobAlreadyExists, JobNotFound, InvalidJobData
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/ecs_scheduler/datacontext.py", line 16, in <module>
    from .serialization import JobSchema, JobCreateSchema
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/ecs_scheduler/serialization.py", line 67, in <module>
    class JobSchema(marshmallow.Schema):
  File "/home/ubuntu/000_github_repos/ecs-scheduler-1.0.1/ecs_scheduler/serialization.py", line 80, in JobSchema
    scheduleStart = marshmallow.fields.LocalDateTime()
AttributeError: module 'marshmallow.fields' has no attribute 'LocalDateTime'


----------------------------------------------------------------------
Ran 126 tests in 0.300s

FAILED (errors=32)
make: *** [Makefile:28: check] Error 1

this code looks pretty awesome. :) fixing this would probably reduce friction in user uptake

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.