示例#1
0
def get_operating_systems(request):
    return HttpResponse(operating_systems())
示例#2
0
文件: models.py 项目: grnet/ganetimgr
    def submit(self):
        if self.status not in [STATUS_APPROVED, STATUS_FAILED]:
            raise ApplicationError("Invalid application status %d" %
                                   self.status)
        import sys
        if sys.argv[1:2] == ['test']:
            return None

        def map_ssh_user(user, group=None, path=None):
            if group is None:
                if user == "root":
                    group = ""   # snf-image will expand to root or wheel
                else:
                    group = user
            if path is None:
                if user == "root":
                    path = "/root/.ssh/authorized_keys"
                else:
                    path = "/home/%s/.ssh/authorized_keys" % user
            return user, group, path

        tags = []
        tags.append("%s:user:%s" %
                    (GANETI_TAG_PREFIX, self.applicant.username))

        tags.append("%s:application:%d" % (GANETI_TAG_PREFIX, self.id))

        if self.hosts_mail_server:
            tags.append("%s:service:mail" % GANETI_TAG_PREFIX)

        if self.organization:
            tags.append("%s:org:%s" % (GANETI_TAG_PREFIX,
                                       self.organization.tag))
        if 'vgs' in self.instance_params.keys():
            if self.instance_params['vgs'] != 'default':
                tags.append("%s:vg:%s" % (
                    GANETI_TAG_PREFIX,
                    self.instance_params['vgs'])
                )
        uses_gnt_network = self.cluster.use_gnt_network
        nic_dict = dict(link=self.instance_params['network'],
                        mode=self.instance_params['mode'])

        if self.instance_params['mode'] == 'routed' and uses_gnt_network:
            nic_dict = dict(network=self.instance_params['network'])

        if self.instance_params['mode'] == "routed":
            nic_dict.update(ip="pool")

        from ganeti.utils import operating_systems
        fetch_op_systems = operating_systems()
        op_systems = json.loads(fetch_op_systems).get('operating_systems')
        os = dict(op_systems).get(self.operating_system)
        provider = os.get('provider')
        osparams = os.get("osparams", {})
        if "ssh_key_param" in os:
            fqdn = "https://" + Site.objects.get_current().domain
            osparams[os["ssh_key_param"]] = self.get_ssh_keys_url(fqdn)
        # For snf-image: copy keys to ssh_key_users using img_personality
        if "ssh_key_users" in os and os["ssh_key_users"]:
            ssh_keys = self.applicant.sshpublickey_set.all()
            if ssh_keys:
                ssh_lines = [key.key_line() for key in ssh_keys]
                ssh_base64 = base64.b64encode("".join(ssh_lines))
                if "img_personality" not in osparams:
                    osparams["img_personality"] = []
                for user in os["ssh_key_users"].split():
                    # user[:group[:/path/to/authorized_keys]]
                    owner, group, path = map_ssh_user(*user.split(":"))
                    osparams["img_personality"].append(
                        {
                            "path": path,
                            "contents": ssh_base64,
                            "owner": owner,
                            "group": group,
                            "mode": 0600,
                        }
                    )
        for (key, val) in osparams.iteritems():
            # Encode nested JSON. See
            # <https://code.google.com/p/ganeti/issues/detail?id=835>
            if not isinstance(val, basestring):
                osparams[key] = json.dumps(val)
        disk_template = self.instance_params['disk_template']
        nodes = None
        # Handle ExtStorage Providers
        # provider name is included in the disks dictionary (along with any
        # custom params) and disk_template is just ext
        # Other disk_templates should remain untouched
        if disk_template.endswith('[ext]'):
                ext_provider = disk_template.replace('[ext]', '')
                disks = [
                    self.cluster.get_extstorage_disk_params(ext_provider) or {}
                ]
                disks[0]['size'] = self.disk_size * 1024
                disks[0]['provider'] = ext_provider
                disk_template = 'ext'
        else:
                disks = [{"size": self.disk_size * 1024}]
        if self.instance_params['node_group'] != 'default':
            if self.instance_params['disk_template'] == 'drbd':
                nodes = self.cluster.get_available_nodes(
                    self.instance_params['node_group'],
                    2
                )
            else:
                nodes = self.cluster.get_available_nodes(
                    self.instance_params['node_group'],
                    1
                )
            # We should select the two first non offline nodes
        if self.instance_params['disk_template'] in ['drbd', 'plain']:
            if self.instance_params['vgs'] != 'default':
                disks[0]['vg'] = self.instance_params['vgs']
        job = self.cluster.create_instance(
            name=self.hostname,
            os=provider,
            vcpus=self.vcpus,
            memory=self.memory,
            disks=disks,
            nics=[nic_dict],
            tags=tags,
            osparams=osparams,
            nodes=nodes,
            disk_template=disk_template,
        )
        self.status = STATUS_SUBMITTED
        self.job_id = job
        self.backend_message = None
        self.save()
        application_submitted.send(sender=self)

        b = beanstalkc.Connection()
        if BEANSTALK_TUBE:
            b.use(BEANSTALK_TUBE)
        b.put(json.dumps({
            "type": "CREATE",
            "application_id": self.id
        }))