コード例 #1
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
 def server_action(self, params: inputs.ServerActionInput, **kwargs):
     """
     操作虚拟主机
     :return:
         outputs.ServerActionOutput()
     """
     service_instance = self._get_openstack_connect()
     try:
         if params.action == inputs.ServerAction.START:
             service_instance.compute.start_server(params.server_id)
         elif params.action == inputs.ServerAction.SHUTDOWN:
             service_instance.compute.stop_server(params.server_id)
         elif params.action == inputs.ServerAction.DELETE:
             service_instance.compute.delete_server(params.server_id)
         elif params.action == inputs.ServerAction.DELETE_FORCE:
             service_instance.compute.delete_server(params.server_id,
                                                    force=True)
         elif params.action == inputs.ServerAction.POWER_OFF:
             service_instance.compute.stop_server(params.server_id)
         elif params.action == inputs.ServerAction.REBOOT:
             service_instance.compute.reboot_server(params.server_id,
                                                    'HARD')
         else:
             return outputs.ServerActionOutput(
                 ok=False, error=exceptions.Error('server action failed'))
         return outputs.ServerActionOutput()
     except Exception as e:
         return outputs.ServerActionOutput(
             ok=False, error=exceptions.Error('server action failed'))
コード例 #2
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
    def server_status(self, params: inputs.ServerStatusInput, **kwargs):
        """
        :return:
            outputs.ServerStatusOutput()
        """
        service_instance = self._get_openstack_connect()
        status_map = {
            'ACTIVE': 1,
            'UNKNOWN': 0,
            'PAUSED': 3,
            'SHUTOFF': 4,
            'SUSPENDED': 7
        }
        try:
            server = service_instance.compute.get_server(params.server_id)
            if server is None:
                status_code = outputs.ServerStatus.MISS
                status_mean = outputs.ServerStatus.get_mean(status_code)
                return outputs.ServerStatusOutput(status=status_code,
                                                  status_mean=status_mean)

            status = server.status
            status_code = status_map[status]
            if status_code not in outputs.ServerStatus():
                status_code = outputs.ServerStatus.NOSTATE
            status_mean = outputs.ServerStatus.get_mean(status_code)
            return outputs.ServerStatusOutput(status=status_code,
                                              status_mean=status_mean)
        except Exception as e:
            return outputs.ServerStatusOutput(
                ok=False,
                error=exceptions.Error(f'get server status failed, {str(e)}'),
                status=outputs.ServerStatus.NOSTATE,
                status_mean='')
コード例 #3
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
 def server_delete(self, params: inputs.ServerDeleteInput, **kwargs):
     """
     删除虚拟服务器
     :return:
         outputs.ServerDeleteOutput()
     """
     service_instance = self._get_openstack_connect()
     try:
         service_instance.compute.delete_server(params.server_id,
                                                force=True)
         return outputs.ServerDeleteOutput()
     except Exception as e:
         return outputs.ServerDeleteOutput(
             ok=False,
             error=exceptions.Error(
                 message=f'Failed to delete server, {str(e)}'))
コード例 #4
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
    def network_detail(self, params: inputs.NetworkDetailInput, **kwargs):
        """
        查询子网网络信息

        :return:
            outputs.NetworkDetailOutput()
        """
        try:
            service_instance = self._get_openstack_connect()
            network = service_instance.network.get_network(params.network_id)
            subnet = service_instance.network.get_subnet(network.subnet_ids[0])
            new_net = outputs.NetworkDetail(id=params.network_id,
                                            name=network.name,
                                            public=False,
                                            segment=subnet.cidr)
            return outputs.NetworkDetailOutput(network=new_net)
        except Exception as e:
            return outputs.NetworkDetailOutput(ok=False,
                                               error=exceptions.Error(str(e)),
                                               network=None)
コード例 #5
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
 def server_vnc(self, params: inputs.ServerVNCInput, **kwargs):
     """
     :return:
         outputs.ServerVNCOutput()
     """
     try:
         service_instance = self._get_openstack_connect()
         server = service_instance.compute.get_server(params.server_id)
         console = service_instance.compute.create_server_remote_console(
             server, protocol='vnc', type='novnc')
         vnc_url = console.url
         port_pos = vnc_url.find(':', 7)
         if port_pos != -1:
             vnc_url = self.endpoint_url + vnc_url[port_pos:len(vnc_url)]
         return outputs.ServerVNCOutput(vnc=outputs.ServerVNCOutputVNC(
             url=vnc_url))
     except Exception as e:
         return outputs.ServerVNCOutput(
             ok=False,
             error=exceptions.Error(f'get vnc failed, {str(e)}'),
             vnc=None)
コード例 #6
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
 def list_networks(self, params: inputs.ListNetworkInput, **kwargs):
     """
     列举子网
     :return:
     """
     service_instance = self._get_openstack_connect()
     try:
         result = []
         for net in service_instance.network.networks():
             public = False
             subnet = service_instance.network.get_subnet(net.subnet_ids[0])
             new_net = outputs.ListNetworkOutputNetwork(id=net.id,
                                                        name=net.name,
                                                        public=public,
                                                        segment=subnet.cidr)
             result.append(new_net)
         return outputs.ListNetworkOutput(networks=result)
     except Exception as e:
         return outputs.ListNetworkOutput(
             ok=False,
             error=exceptions.Error('list networks failed'),
             networks=[])
コード例 #7
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
    def server_create(self, params: inputs.ServerCreateInput, **kwargs):
        """
        创建虚拟服务器
        :return:
            outputs.ServerCreateOutput()
        """
        service_instance = self._get_openstack_connect()
        try:
            flavor = self.get_or_create_flavor(params.ram, params.vcpu)
            server_re = service_instance.compute.create_server(
                name='gosc-instance-' + str(uuid.uuid1()),
                image_id=params.image_id,
                flavor_id=flavor.id,
                networks=[{
                    "uuid": params.network_id
                }])

            server = outputs.ServerCreateOutputServer(uuid=server_re.id)
            return outputs.ServerCreateOutput(server=server)
        except Exception as e:
            return outputs.ServerCreateOutput(ok=False,
                                              error=exceptions.Error(str(e)),
                                              server=None)
コード例 #8
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
    def server_detail(self, params: inputs.ServerDetailInput, **kwargs):
        """
        :return:
            outputs.ServerDetailOutput()
        """
        try:
            service_instance = self._get_openstack_connect()
            server = service_instance.compute.get_server(params.server_id)
            try:
                adresses = server.addresses
                server_ip = {
                    'ipv4': adresses[list(adresses.keys())[0]][0]['addr'],
                    'public_ipv4': None
                }
            except Exception as e:
                server_ip = {'ipv4': '', 'public_ipv4': None}

            ip = outputs.ServerIP(**server_ip)
            image_temp = service_instance.image.get_image(server.image.id)

            image = outputs.ServerImage(name=image_temp.name,
                                        system=image_temp.properties['os'])

            flavor = server.flavor
            server = outputs.ServerDetailOutputServer(
                uuid=server.id,
                ram=flavor['ram'],
                vcpu=flavor['vcpus'],
                ip=ip,
                image=image,
                creation_time=iso_to_datetime(server.created_at))
            return outputs.ServerDetailOutput(server=server)
        except exceptions.Error as e:
            return outputs.ServerDetailOutput(
                ok=False,
                error=exceptions.Error('server detail failed'),
                server=None)
コード例 #9
0
ファイル: adapter.py プロジェクト: GOSC-CNIC/gosc
 def list_images(self, params: inputs.ListImageInput, **kwargs):
     """
     列举镜像
     :return:
         output.ListImageOutput()
     """
     service_instance = self._get_openstack_connect()
     try:
         result = []
         for image in service_instance.image.images():
             img_obj = outputs.ListImageOutputImage(
                 id=image.id,
                 name=image.name,
                 system=image.properties['os'],
                 desc=image.properties['description'],
                 system_type=image.os_type,
                 creation_time=image.created_at)
             result.append(img_obj)
         return outputs.ListImageOutput(images=result)
     except Exception as e:
         return outputs.ListImageOutput(
             ok=False,
             error=exceptions.Error('list image failed'),
             images=[])