Skip to content

Instantly share code, notes, and snippets.

@misho-kr
Forked from porterjamesj/hello_mesos.py
Last active August 29, 2015 14:09
Show Gist options
  • Save misho-kr/89dba2cce73a51079646 to your computer and use it in GitHub Desktop.
Save misho-kr/89dba2cce73a51079646 to your computer and use it in GitHub Desktop.
import logging
import uuid
import time
import mesos.interface
import mesos.native
from mesos.interface import mesos_pb2
logging.basicConfig(level=logging.INFO)
class HelloWorldScheduler(mesos.interface.Scheduler):
def new_uuided_task(self, offer):
"""Creates a task with whose id is a newly generated uuid on the given
offer."""
task = mesos_pb2.TaskInfo()
id = uuid.uuid4()
task.task_id.value = str(id)
task.slave_id.value = offer.slave_id.value
task.name = "task {}".format(str(id))
cpus = task.resources.add()
cpus.name = "cpus"
cpus.type = mesos_pb2.Value.SCALAR
cpus.scalar.value = 1
mem = task.resources.add()
mem.name = "mem"
mem.type = mesos_pb2.Value.SCALAR
mem.scalar.value = 100
return task
# mesos API methods
def registered(self, driver, framework_id, master_info):
logging.info("Registered with framework id: {}".format(framework_id))
def resourceOffers(self, driver, offers):
logging.info("Recieved resource offers: {}".format(offers))
# whenever we get an offer, we accept it and use it to launch a task that
# just echos hello world to stdout
for offer in offers:
logging.info("Got resource offer: {}".format(offer))
task = self.new_uuided_task(offer)
task.command.value = "echo hello world"
time.sleep(2)
logging.info("Launching task {task} "
"using offer {offer}.".format(task=task.task_id.value,
offer=offer.id))
tasks = [task]
driver.launchTasks(offer.id, tasks)
if __name__ == '__main__':
# make us a framework
framework = mesos_pb2.FrameworkInfo()
framework.user = "" # Have Mesos fill in the current user.
framework.name = "hello-world"
driver = mesos.native.MesosSchedulerDriver(
HelloWorldScheduler(),
framework,
"zk://localhost:2181/mesos" # assumes running on the master
)
driver.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment