<commit_before># -*- coding: utf-8 -*-
# @COPYRIGHT_begin
#
# Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland 
#
# Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @COPYRIGHT_end
"""@package src.cm.models.vm
"""
from cm.settings import VNC_PORTS

from cm.models.node import Node
from cm.models.public_ip import PublicIP
from cm.models.template import Template
from cm.models.user import User
from cm.models.farm import Farm
from cm.models.system_image import SystemImage
from cm.models.iso_image import IsoImage
from cm.models.lease import Lease
from cm.models.available_network import AvailableNetwork
from cm.utils.exception import CMException
from cm.utils import log
from cm.utils.monia import RrdHandler

from django.db import models, transaction
from django.conf import settings
from django.template import loader, Context
from django.conf import settings as django_settings

from common.states import vm_states, image_states, farm_states, lease_states, vnc_states
from common.utils import password_gen

from datetime import datetime
import os
import uuid
import libvirt
import subprocess

from netaddr import IPNetwork


class VM(models.Model):
    """
    @model{vm} Virtual machine class

    Virtual Machines creation and management are the main features of the CC1
    system. They make use of the node's CPU (Node) to emulate physical
    hardware. They should be considered as remote workstations with operating
    system (VMImage) running on them.

    VM may be created and further saved to VMImage or destroyed irrevocably.

    VM may have plugged resources of several types, so that it's functionality
    and access to it are extended.
    """
    name = models.CharField(max_length=128)
    node = models.ForeignKey(Node)
    user = models.ForeignKey(User)
    template = models.ForeignKey(Template)
    system_image = models.ForeignKey(SystemImage)
    iso_image = models.ForeignKey(IsoImage, null=True, blank=True)
    libvirt_id = models.IntegerField()
    state = models.IntegerField()
    start_time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True, blank=True)
    ctx_key = models.CharField(max_length=128, null=True, blank=True)
    ctx_api_version = models.CharField(max_length=10, null=True, blank=True)
    vnc_passwd = models.CharField(max_length=45)

    ssh_key = models.TextField(null=True, blank=True)
    ssh_username = models.CharField(max_length=45, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    save_vm = models.IntegerField()
    farm = models.ForeignKey(Farm, related_name='vms', null=True)
    hostname = models.CharField(max_length=256, null=True, blank=True)
    vnc_port = models.IntegerField()
    vnc_enabled = models.IntegerField(default=0)
    reservation_id = models.IntegerField(default=0)
    user_data = models.CharField(max_length=32768, null=True, blank=True)

    class Meta:
        app_label = 'cm'

    # method for printing object instance
    def __unicode__(self):
        return self.name

    @property
    def dict(self):
        """
        @returns{dict} this VM's data
        \n fields:
        @dictkey{id,int}
        @dictkey{user_id,int}
        @dictkey{name,string}
        @dictkey{state,int} @seealso{src.common.states.vm_states}
        @dictkey{leases,list(dict)}
        @dictkey{image_name,string}
        @dictkey{template_name,string}
        @dictkey{platform,string}
        @dictkey{description,string}
        @dictkey{vnc_endpoint,int}
        @dictkey{vnc_enabled,bool}
        @dictkey{vnc_passwd,string}
        @dictkey{iso_images,list(dict)}
        @dictkey{storage_images,list(dict)}
        @dictkey{cpu_load,int}
        """
        d = {}
        d['vm_id'] = self.id
        d['user_id'] = self.user.id
        d['name'] = self.name
        d['state'] = self.state
        d['leases'] = [l.dict for l in self.lease_set.all()]
        d['image_name'] = self.system_image.name
        d['image_id'] = self.system_image.id
        d['template_name'] = self.template.name
        d['platform'] = 0
        d['description'] = self.description or ''
        d['vnc_endpoint'] = '%s:%d' % (settings.VNC_ADDRESS, self.vnc_port)
        d['vnc_enabled'] = self.vnc_enabled
        d['vnc_passwd'] = self.vnc_passwd or ''

        if self.iso_image:
            d['iso_images'] = [{'id': self.iso_image.id, 'name': self.iso_image.name}]
        else:
            d['iso_images'] = []
        d['storage_images'] = [{'storage_image_id': img.id, 'name': img.name} for img in self.storage_images]
        d['cpu_load'] = self.cpu_load

        return d

    @property
    def long_dict(self):
        """
        @returns{dict} this VM's extended data
        \n fields:
        @dictkey{id,int}
        @dictkey{user_id,int}
        @dictkey{name,string}
        @dictkey{state,int} @seealso{src.common.states.command_states}
        @dictkey{leases,list(dict)}
        @dictkey{image_name,string}
        @dictkey{template_name,string}
        @dictkey{platform,string}
        @dictkey{description,string}
        @dictkey{vnc_endpoint,int}
        @dictkey{vnc_enabled,bool}
        @dictkey{vnc_passwd,string}
        @dictkey{start_time,datetime.datetime}
        @dictkey{node,string} node's address
        @dictkey{libvirt_id,int}
        @dictkey{ssh_username,string}
        @dictkey{ssh_key,string}
        @dictkey{iso_images,list(dict)}
        @dictkey{storage_images,list(dict)}
        @dictkey{cpu_load,int}
        @dictkey{reservation_id,int}
        @dictkey{user_data,string}
        """
        d = {}
        d['vm_id'] = self.id
        d['user_id'] = self.user.id
        d['name'] = self.name
        d['state'] = self.state
        d['leases'] = [l.dict for l in self.lease_set.all()]
        d['image_name'] = self.system_image.name
        d['image_id'] = self.system_image.id
        d['template_name'] = self.template.name
        d['platform'] = 0
        d['description'] = self.description or ''
        d['vnc_endpoint'] = '%s:%d' % (settings.VNC_ADDRESS, self.vnc_port)
        d['vnc_enabled'] = self.vnc_enabled
        d['vnc_passwd'] = self.vnc_passwd or ''
        d['start_time'] = self.start_time
        delta = datetime.now() - self.start_time
        d['uptime'] = delta.seconds + 24 * 3600 * delta.days
        d['node'] = self.node.address
        d['libvirt_id'] = self.libvirt_id
        d['ssh_username'] = self.ssh_username or ''
        d['ssh_key'] = self.ssh_key or ''
        d['reservation_id'] = self.reservation_id
        d['user_data'] = self.user_data

        if self.iso_image:
            d['iso_images'] = [{'id': self.iso_image.id, 'name': self.iso_image.name}]
        else:
            d['iso_images'] = []
        d['storage_images'] = [{'storage_image_id': img.id, 'name': img.name, 'disk_controller': img.disk_controller} for img in self.storage_images]
        d['cpu_load'] = self.cpu_load
        return d

    @staticmethod
    def create(user, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, ssh_key=None,
               ssh_username=None, count=1, farm=None, head_template_id=None, node_id=False, lease_id=None, user_data=None):
        from cm.models.storage_image import StorageImage
        from cm.utils.threads.vm import VMThread<commit_msg>vnc_port added to VM dictionary in cm model<commit_after># -*- coding: utf-8 -*-
# @COPYRIGHT_begin
#
# Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland 
#
# Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @COPYRIGHT_end
"""@package src.cm.models.vm
"""
from cm.settings import VNC_PORTS

from cm.models.node import Node
from cm.models.public_ip import PublicIP
from cm.models.template import Template
from cm.models.user import User
from cm.models.farm import Farm
from cm.models.system_image import SystemImage
from cm.models.iso_image import IsoImage
from cm.models.lease import Lease
from cm.models.available_network import AvailableNetwork
from cm.utils.exception import CMException
from cm.utils import log
from cm.utils.monia import RrdHandler

from django.db import models, transaction
from django.conf import settings
from django.template import loader, Context
from django.conf import settings as django_settings

from common.states import vm_states, image_states, farm_states, lease_states, vnc_states
from common.utils import password_gen

from datetime import datetime
import os
import uuid
import libvirt
import subprocess

from netaddr import IPNetwork


class VM(models.Model):
    """
    @model{vm} Virtual machine class

    Virtual Machines creation and management are the main features of the CC1
    system. They make use of the node's CPU (Node) to emulate physical
    hardware. They should be considered as remote workstations with operating
    system (VMImage) running on them.

    VM may be created and further saved to VMImage or destroyed irrevocably.

    VM may have plugged resources of several types, so that it's functionality
    and access to it are extended.
    """
    name = models.CharField(max_length=128)
    node = models.ForeignKey(Node)
    user = models.ForeignKey(User)
    template = models.ForeignKey(Template)
    system_image = models.ForeignKey(SystemImage)
    iso_image = models.ForeignKey(IsoImage, null=True, blank=True)
    libvirt_id = models.IntegerField()
    state = models.IntegerField()
    start_time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True, blank=True)
    ctx_key = models.CharField(max_length=128, null=True, blank=True)
    ctx_api_version = models.CharField(max_length=10, null=True, blank=True)
    vnc_passwd = models.CharField(max_length=45)

    ssh_key = models.TextField(null=True, blank=True)
    ssh_username = models.CharField(max_length=45, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    save_vm = models.IntegerField()
    farm = models.ForeignKey(Farm, related_name='vms', null=True)
    hostname = models.CharField(max_length=256, null=True, blank=True)
    vnc_port = models.IntegerField()
    vnc_enabled = models.IntegerField(default=0)
    reservation_id = models.IntegerField(default=0)
    user_data = models.CharField(max_length=32768, null=True, blank=True)

    class Meta:
        app_label = 'cm'

    # method for printing object instance
    def __unicode__(self):
        return self.name

    @property
    def dict(self):
        """
        @returns{dict} this VM's data
        \n fields:
        @dictkey{id,int}
        @dictkey{user_id,int}
        @dictkey{name,string}
        @dictkey{state,int} @seealso{src.common.states.vm_states}
        @dictkey{leases,list(dict)}
        @dictkey{image_name,string}
        @dictkey{template_name,string}
        @dictkey{platform,string}
        @dictkey{description,string}
        @dictkey{vnc_endpoint,int}
        @dictkey{vnc_enabled,bool}
        @dictkey{vnc_passwd,string}
        @dictkey{iso_images,list(dict)}
        @dictkey{storage_images,list(dict)}
        @dictkey{cpu_load,int}
        """
        d = {}
        d['vm_id'] = self.id
        d['user_id'] = self.user.id
        d['name'] = self.name
        d['state'] = self.state
        d['leases'] = [l.dict for l in self.lease_set.all()]
        d['image_name'] = self.system_image.name
        d['image_id'] = self.system_image.id
        d['template_name'] = self.template.name
        d['platform'] = 0
        d['description'] = self.description or ''
        d['vnc_endpoint'] = '%s:%d' % (settings.VNC_ADDRESS, self.vnc_port)
        d['vnc_enabled'] = self.vnc_enabled
        d['vnc_passwd'] = self.vnc_passwd or ''

        if self.iso_image:
            d['iso_images'] = [{'id': self.iso_image.id, 'name': self.iso_image.name}]
        else:
            d['iso_images'] = []
        d['storage_images'] = [{'storage_image_id': img.id, 'name': img.name} for img in self.storage_images]
        d['cpu_load'] = self.cpu_load

        return d

    @property
    def long_dict(self):
        """
        @returns{dict} this VM's extended data
        \n fields:
        @dictkey{id,int}
        @dictkey{user_id,int}
        @dictkey{name,string}
        @dictkey{state,int} @seealso{src.common.states.command_states}
        @dictkey{leases,list(dict)}
        @dictkey{image_name,string}
        @dictkey{template_name,string}
        @dictkey{platform,string}
        @dictkey{description,string}
        @dictkey{vnc_endpoint,int}
        @dictkey{vnc_enabled,bool}
        @dictkey{vnc_passwd,string}
        @dictkey{start_time,datetime.datetime}
        @dictkey{node,string} node's address
        @dictkey{libvirt_id,int}
        @dictkey{ssh_username,string}
        @dictkey{ssh_key,string}
        @dictkey{iso_images,list(dict)}
        @dictkey{storage_images,list(dict)}
        @dictkey{cpu_load,int}
        @dictkey{reservation_id,int}
        @dictkey{user_data,string}
        """
        d = {}
        d['vm_id'] = self.id
        d['user_id'] = self.user.id
        d['name'] = self.name
        d['state'] = self.state
        d['leases'] = [l.dict for l in self.lease_set.all()]
        d['image_name'] = self.system_image.name
        d['image_id'] = self.system_image.id
        d['template_name'] = self.template.name
        d['platform'] = 0
        d['description'] = self.description or ''
        d['vnc_endpoint'] = '%s:%d' % (settings.VNC_ADDRESS, self.vnc_port)
        d['vnc_port'] = self.vnc_port
        d['vnc_enabled'] = self.vnc_enabled
        d['vnc_passwd'] = self.vnc_passwd or ''
        d['start_time'] = self.start_time
        delta = datetime.now() - self.start_time
        d['uptime'] = delta.seconds + 24 * 3600 * delta.days
        d['node'] = self.node.address
        d['libvirt_id'] = self.libvirt_id
        d['ssh_username'] = self.ssh_username or ''
        d['ssh_key'] = self.ssh_key or ''
        d['reservation_id'] = self.reservation_id
        d['user_data'] = self.user_data

        if self.iso_image:
            d['iso_images'] = [{'id': self.iso_image.id, 'name': self.iso_image.name}]
        else:
            d['iso_images'] = []
        d['storage_images'] = [{'storage_image_id': img.id, 'name': img.name, 'disk_controller': img.disk_controller} for img in self.storage_images]
        d['cpu_load'] = self.cpu_load
        return d

    @staticmethod
    def create(user, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, ssh_key=None,
               ssh_username=None, count=1, farm=None, head_template_id=None, node_id=False, lease_id=None, user_data=None):
        from cm.models.storage_image import StorageImage
        from cm.utils.threads.vm import VMThread
{'fraction_non_alphanumeric': 0.11528218135700698, 'fraction_numerical': 0.0057070386810399495, 'mean_word_length': 3.159018987341772, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3471318', 'n_tokens_mistral': 5049, 'n_tokens_neox': 4934, 'n_words': 1154}