def generate_global_orchestrate_file(self):
        accounts = set([host.cloud_image.account for host in self.hosts.all()])

        orchestrate = {}

        for account in accounts:
            # Target the stack_id and cloud account
            target = 'G@stack_id:{0} and G@cloud_account:{1}'.format(
                self.id,
                account.slug)

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

            for order in sorted(groups.keys()):
                for role in groups[order]:
                    state_title = '{0}_{1}'.format(account.slug, role)
                    orchestrate[state_title] = {
                        'salt.state': [
                            {'tgt': target},
                            {'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.global_orchestrate_file:
            self.global_orchestrate_file.save('global_orchestrate.sls', ContentFile(yaml_data))
        else:
            with open(self.global_orchestrate_file.path, 'w') as f:
                f.write(yaml_data)
dataset/ETHPy150Open stackdio/stackdio/stackdio/api/stacks/models.py/Stack.generate_global_orchestrate_file/original