def update_or_create_job()

in tools/deploy/app.py [0:0]


    def update_or_create_job(self, callback):
        """
        Update the current job for a Peloton app. Create a new job if the job
        does not exist yet.
        """

        # TODO: find the leader/follower role of each app instance

        if self.current_job_config is None:
            # Create the new Job in Aurora and check the response code
            print(
                "Creating new job for %s with %s instances"
                % (self.name, self.num_instances)
            )

            resp = self.client.createJob(self.desired_job_config)
            if resp.responseCode != ResponseCode.OK:
                raise Exception(combine_messages(resp))

            # Wait for all instances are running
            retval = self.wait_for_running(Role.ALL)

            if retval:
                callback(self)

            return retval

        # Get current leader and follower instances
        cur_instances = self.get_instances()

        # First updade all existing instances, followers first then leader
        for role in [Role.FOLLOWER, Role.LEADER]:
            instances = cur_instances.get(role, [])

            if role == Role.LEADER and len(instances) > 1:
                raise Exception(
                    "Found %d leaders for %s" % (len(instances), self.name)
                )

            if len(instances) == 0:
                print("No %s %s instances to update" % (self.name, role.name))
                continue

            print(
                "Start updating %d %s %s instances"
                % (len(instances), self.name, role.name)
            )

            retval = self.update_instances(instances, self.desired_job_config)

            print(
                "Finish updating %d %s %s instances -- %s"
                % (
                    len(instances),
                    self.name,
                    role.name,
                    "SUCCEED" if retval else "FAILED",
                )
            )

            if not retval or not callback(self):
                # Rollback the update by the caller
                return False

        # Then add any missing instances if needed
        cur_total = len(cur_instances.get(role.ALL, []))
        new_total = self.num_instances > cur_total

        if new_total > 0:
            print("Start adding %d new %s instances" % (new_total, self.name))
            retval = self.update_instances([], self.desired_job_config)
            print(
                "Finish adding %d new %s instances -- %s"
                % (new_total, self.name, "SUCCEED" if retval else "FAILED")
            )

            if not retval or not callback(self):
                # Rollback the update by the caller
                return False

        return True