2015-02-19 18:43:45 +00:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-07-22 04:58:28 +00:00
import sys
2015-07-28 14:05:48 +00:00
import os . path
2015-04-27 20:19:17 +00:00
from aiohttp . web import HTTPConflict
2016-05-14 00:00:07 +00:00
from gns3server . web . route import Route
from gns3server . compute . project_manager import ProjectManager
from gns3server . schemas . nio import NIO_SCHEMA
from gns3server . schemas . node import NODE_LIST_IMAGES_SCHEMA
from gns3server . compute . qemu import Qemu
from gns3server . config import Config
from gns3server . schemas . qemu import (
QEMU_CREATE_SCHEMA ,
QEMU_UPDATE_SCHEMA ,
QEMU_OBJECT_SCHEMA ,
QEMU_BINARY_LIST_SCHEMA ,
QEMU_BINARY_FILTER_SCHEMA ,
QEMU_CAPABILITY_LIST_SCHEMA ,
QEMU_IMAGE_CREATE_SCHEMA
)
2015-02-19 18:43:45 +00:00
class QEMUHandler :
"""
API entry points for QEMU .
"""
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
201 : " Instance created " ,
400 : " Invalid request " ,
409 : " Conflict "
} ,
2015-04-12 21:09:37 +00:00
description = " Create a new Qemu VM instance " ,
2015-02-19 18:43:45 +00:00
input = QEMU_CREATE_SCHEMA ,
output = QEMU_OBJECT_SCHEMA )
def create ( request , response ) :
qemu = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = yield from qemu . create_node ( request . json . pop ( " name " ) ,
request . match_info [ " project_id " ] ,
request . json . pop ( " node_id " , None ) ,
linked_clone = request . json . get ( " linked_clone " , True ) ,
qemu_path = request . json . pop ( " qemu_path " , None ) ,
console = request . json . pop ( " console " , None ) ,
console_type = request . json . pop ( " console_type " , " telnet " ) ,
platform = request . json . pop ( " platform " , None ) )
2015-03-16 18:45:21 +00:00
2015-04-12 21:09:37 +00:00
for name , value in request . json . items ( ) :
if hasattr ( vm , name ) and getattr ( vm , name ) != value :
setattr ( vm , name , value )
2015-02-19 19:22:30 +00:00
2015-02-19 18:43:45 +00:00
response . set_status ( 201 )
response . json ( vm )
@Route.get (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
200 : " Success " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Get a Qemu VM instance " ,
2015-02-19 18:43:45 +00:00
output = QEMU_OBJECT_SCHEMA )
def show ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-02-19 18:43:45 +00:00
response . json ( vm )
@Route.put (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
200 : " Instance updated " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist " ,
409 : " Conflict "
} ,
2015-04-12 21:09:37 +00:00
description = " Update a Qemu VM instance " ,
2015-02-19 18:43:45 +00:00
input = QEMU_UPDATE_SCHEMA ,
output = QEMU_OBJECT_SCHEMA )
def update ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-04-12 21:09:37 +00:00
for name , value in request . json . items ( ) :
if hasattr ( vm , name ) and getattr ( vm , name ) != value :
setattr ( vm , name , value )
2015-02-19 18:43:45 +00:00
response . json ( vm )
@Route.delete (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
204 : " Instance deleted " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Delete a Qemu VM instance " )
2015-02-19 18:43:45 +00:00
def delete ( request , response ) :
2016-05-11 17:35:36 +00:00
yield from Qemu . instance ( ) . delete_node ( request . match_info [ " node_id " ] )
2015-02-19 18:43:45 +00:00
response . set_status ( 204 )
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /start " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
2016-02-02 17:25:17 +00:00
200 : " Instance started " ,
2015-02-19 18:43:45 +00:00
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2016-02-02 17:25:17 +00:00
description = " Start a Qemu VM instance " ,
output = QEMU_OBJECT_SCHEMA )
2015-02-19 18:43:45 +00:00
def start ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2016-05-14 00:00:07 +00:00
if sys . platform . startswith ( " linux " ) and qemu_manager . config . get_section_config ( " Qemu " ) . getboolean ( " enable_kvm " , True ) and " -no-kvm " not in vm . options :
2015-07-22 04:58:28 +00:00
pm = ProjectManager . instance ( )
if pm . check_hardware_virtualization ( vm ) is False :
2015-07-27 01:21:30 +00:00
raise HTTPConflict ( text = " Cannot start VM with KVM enabled because hardware virtualization (VT-x/AMD-V) is already used by another software like VMware or VirtualBox " )
2015-02-19 18:43:45 +00:00
yield from vm . start ( )
2016-02-02 17:25:17 +00:00
response . json ( vm )
2015-02-19 18:43:45 +00:00
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /stop " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
204 : " Instance stopped " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Stop a Qemu VM instance " )
2015-02-19 18:43:45 +00:00
def stop ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-02-19 18:43:45 +00:00
yield from vm . stop ( )
response . set_status ( 204 )
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /reload " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID " ,
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
204 : " Instance reloaded " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Reload a Qemu VM instance " )
2015-02-19 18:43:45 +00:00
def reload ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-02-19 18:43:45 +00:00
yield from vm . reload ( )
response . set_status ( 204 )
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /suspend " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID " ,
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
204 : " Instance suspended " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Suspend a Qemu VM instance " )
2015-02-19 18:43:45 +00:00
def suspend ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-02-19 18:43:45 +00:00
yield from vm . suspend ( )
response . set_status ( 204 )
2015-02-20 23:15:56 +00:00
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /resume " ,
2015-02-20 23:15:56 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID " ,
2015-02-20 23:15:56 +00:00
} ,
status_codes = {
204 : " Instance resumed " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Resume a Qemu VM instance " )
2015-02-20 23:15:56 +00:00
def resume ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-02-20 23:15:56 +00:00
yield from vm . resume ( )
response . set_status ( 204 )
2015-02-19 18:43:45 +00:00
@Route.post (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /adapters/ { adapter_number: \ d+}/ports/ { port_number: \ d+}/nio " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID " ,
2015-02-19 18:43:45 +00:00
" adapter_number " : " Network adapter where the nio is located " ,
2015-03-07 03:08:00 +00:00
" port_number " : " Port on the adapter (always 0) "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
201 : " NIO created " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Add a NIO to a Qemu VM instance " ,
2015-04-27 20:19:17 +00:00
input = NIO_SCHEMA ,
output = NIO_SCHEMA )
2015-02-19 18:43:45 +00:00
def create_nio ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-04-27 20:19:17 +00:00
nio_type = request . json [ " type " ]
2015-05-06 20:59:01 +00:00
if nio_type not in ( " nio_udp " , " nio_tap " , " nio_nat " ) :
2015-04-27 20:19:17 +00:00
raise HTTPConflict ( text = " NIO of type {} is not supported " . format ( nio_type ) )
2015-02-19 18:43:45 +00:00
nio = qemu_manager . create_nio ( vm . qemu_path , request . json )
2015-03-07 03:08:00 +00:00
yield from vm . adapter_add_nio_binding ( int ( request . match_info [ " adapter_number " ] ) , nio )
2015-02-19 18:43:45 +00:00
response . set_status ( 201 )
response . json ( nio )
@Route.delete (
2016-05-11 17:35:36 +00:00
r " /projects/ {project_id} /qemu/nodes/ {node_id} /adapters/ { adapter_number: \ d+}/ports/ { port_number: \ d+}/nio " ,
2015-02-19 18:43:45 +00:00
parameters = {
2016-05-14 00:00:07 +00:00
" project_id " : " Project UUID " ,
" node_id " : " Node UUID " ,
2015-02-19 18:43:45 +00:00
" adapter_number " : " Network adapter where the nio is located " ,
2015-03-07 03:08:00 +00:00
" port_number " : " Port on the adapter (always 0) "
2015-02-19 18:43:45 +00:00
} ,
status_codes = {
204 : " NIO deleted " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
2015-04-12 21:09:37 +00:00
description = " Remove a NIO from a Qemu VM instance " )
2015-02-19 18:43:45 +00:00
def delete_nio ( request , response ) :
qemu_manager = Qemu . instance ( )
2016-05-11 17:35:36 +00:00
vm = qemu_manager . get_node ( request . match_info [ " node_id " ] , project_id = request . match_info [ " project_id " ] )
2015-03-07 03:08:00 +00:00
yield from vm . adapter_remove_nio_binding ( int ( request . match_info [ " adapter_number " ] ) )
2015-02-19 18:43:45 +00:00
response . set_status ( 204 )
2015-02-20 13:39:13 +00:00
@Route.get (
2015-02-20 15:54:23 +00:00
r " /qemu/binaries " ,
2015-02-20 13:39:13 +00:00
status_codes = {
200 : " Success " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
description = " Get a list of available Qemu binaries " ,
2016-01-01 00:40:12 +00:00
input = QEMU_BINARY_FILTER_SCHEMA ,
2015-02-20 13:39:13 +00:00
output = QEMU_BINARY_LIST_SCHEMA )
def list_binaries ( request , response ) :
2016-01-04 15:30:06 +00:00
binaries = yield from Qemu . binary_list ( request . json . get ( " archs " , None ) )
2015-02-20 13:39:13 +00:00
response . json ( binaries )
2015-04-13 12:33:13 +00:00
2015-05-10 17:46:51 +00:00
@Route.get (
r " /qemu/img-binaries " ,
status_codes = {
200 : " Success " ,
400 : " Invalid request " ,
404 : " Instance doesn ' t exist "
} ,
description = " Get a list of available Qemu-img binaries " ,
output = QEMU_BINARY_LIST_SCHEMA )
def list_img_binaries ( request , response ) :
binaries = yield from Qemu . img_binary_list ( )
response . json ( binaries )
2016-01-01 00:40:12 +00:00
@Route.get (
r " /qemu/capabilities " ,
status_codes = {
200 : " Success "
} ,
description = " Get a list of Qemu capabilities on this server " ,
output = QEMU_CAPABILITY_LIST_SCHEMA
)
def get_capabilities ( request , response ) :
2016-01-04 15:30:06 +00:00
capabilities = { " kvm " : [ ] }
kvms = yield from Qemu . get_kvm_archs ( )
2016-01-01 00:40:12 +00:00
if kvms :
capabilities [ " kvm " ] = kvms
response . json ( capabilities )
2015-07-27 17:18:36 +00:00
@Route.post (
r " /qemu/img " ,
status_codes = {
201 : " Image created " ,
} ,
2015-07-28 15:50:57 +00:00
description = " Create a Qemu image " ,
input = QEMU_IMAGE_CREATE_SCHEMA
2015-07-27 17:18:36 +00:00
)
def create_img ( request , response ) :
qemu_img = request . json . pop ( " qemu_img " )
path = request . json . pop ( " path " )
2015-07-28 14:05:48 +00:00
if os . path . isabs ( path ) :
config = Config . instance ( )
if config . get_section_config ( " Server " ) . getboolean ( " local " , False ) is False :
response . set_status ( 403 )
return
2015-07-27 17:18:36 +00:00
yield from Qemu . instance ( ) . create_disk ( qemu_img , path , request . json )
response . set_status ( 201 )
2015-04-13 12:33:13 +00:00
@Route.get (
2016-05-14 00:00:07 +00:00
r " /qemu/images " ,
2015-04-13 12:33:13 +00:00
status_codes = {
2016-05-14 00:00:07 +00:00
200 : " List of Qemu images " ,
2015-04-13 12:33:13 +00:00
} ,
description = " Retrieve the list of Qemu images " ,
2016-05-11 17:35:36 +00:00
output = NODE_LIST_IMAGES_SCHEMA )
2016-05-14 00:00:07 +00:00
def list_qemu_images ( request , response ) :
2015-04-13 12:33:13 +00:00
qemu_manager = Qemu . instance ( )
2016-05-14 00:00:07 +00:00
images = yield from qemu_manager . list_images ( )
2015-04-13 12:33:13 +00:00
response . set_status ( 200 )
2016-05-14 00:00:07 +00:00
response . json ( images )
2015-04-24 08:15:23 +00:00
@Route.post (
2016-05-14 00:00:07 +00:00
r " /qemu/images/ { filename:.+} " ,
parameters = {
" filename " : " Image filename "
} ,
2015-04-24 08:15:23 +00:00
status_codes = {
204 : " Image uploaded " ,
} ,
raw = True ,
2016-05-14 00:00:07 +00:00
description = " Upload Qemu image " )
2016-05-11 17:35:36 +00:00
def upload_image ( request , response ) :
2015-04-24 08:15:23 +00:00
qemu_manager = Qemu . instance ( )
2016-05-14 00:00:07 +00:00
yield from qemu_manager . write_image ( request . match_info [ " filename " ] , request . content )
2015-04-24 08:15:23 +00:00
response . set_status ( 204 )