def generate_orchestrate_file(self):
        hosts = self.hosts.all()
        stack_target = 'G@stack_id:{0}'.format(self.pk)

        def _matcher(sls_set):
            return ' and '.join(
                [stack_target] + ['G@roles:{0}'.format(i) for i in sls_set]
            )

        groups = {}
        for host in hosts:
            for component in host.formula_components.all():
                groups.setdefault(component.order, set()).add(component.sls_path)

        orchestrate = {}
        for order in sorted(groups.keys()):
            for role in groups[order]:
                orchestrate[role] = {
                    'salt.state': [
                        {'tgt': _matcher([role])},
                        {'tgt_type': 'compound'},
                        {'sls': role},
                    ]
                }
                depend = order - 1
                while depend >= 0:
                    if depend in groups.keys():
                        orchestrate[role]['salt.state'].append(
                            {'require': [{'salt': req} for req in groups[depend]]}
                        )
                        break
                    depend -= 1

        yaml_data = yaml.safe_dump(orchestrate, default_flow_style=False)

        if not self.orchestrate_file:
            self.orchestrate_file.save('orchestrate.sls', ContentFile(yaml_data))
        else:
            with open(self.orchestrate_file.path, 'w') as f:
                f.write(yaml_data)
dataset/ETHPy150Open stackdio/stackdio/stackdio/api/stacks/models.py/Stack.generate_orchestrate_file/original