Esempio n. 1
0
 def kvm_stop(self, options):
     status, response = self._request('POST', '/api/1/%s/stop' % self.name)
     if status != 200:
         raise DriverException(response)
     response = json.loads(response)
     if response['state'] != 'STOPPED':
         raise DriverException(
             'VM is not in the STOPPED state after stopping')
Esempio n. 2
0
 def kvm_start(self, options):
     status, response = self._request('POST', '/api/1/%s/start' % self.name)
     if status != 200:
         raise DriverException(response)
     response = json.loads(response)
     if response['state'] != 'RUNNING':
         raise DriverException(
             'VM is not in the RUNNING state after starting')
Esempio n. 3
0
 def set_outlet_state(self, outlet, state, session=None):
     oid = self._get_port_oid(outlet)
     oid = oid.replace('1.3.6.1.4.1.1718.3.2.3.1.2',
                       '1.3.6.1.4.1.1718.3.2.3.1.11')
     r = self._snmp_set(oid, state)
     if r.PDU.varbindlist[0].value.val != state:
         raise DriverException('Unable to set SNMP state')
Esempio n. 4
0
    def kvm_create(self, options):
        status, response = self._request('POST', '/api/1/%s' % self.name, {
            'memory': options.memory,
            'disk': options.disk,
        })
        if status != 200:
            raise DriverException(response)

        response = json.loads(response)

        config = response['config']

        try:
            clusto.begin_transaction()
            self.set_attr(key='system',
                          subkey='memory',
                          value=config['memory'])
            self.set_attr(key='system', subkey='disk', value=config['disk'])
            self.set_attr(key='system', subkey='cpucount', value=1)
            self.set_attr(key='kvm',
                          subkey='console-port',
                          value=config['console'])
            self.set_attr(key='kvm',
                          subkey='vnc-port',
                          value=5900 + config['vnc'])
            self.set_port_attr('nic-eth', 1, 'mac', config['mac'])
            self.set_port_attr('nic-eth', 1, 'model', config['nic'])
            clusto.SESSION.clusto_description = 'Populate KVM information for %s' % self.name
            clusto.commit()
        except:
            sys.stderr.write(format_exc() + '\n')
            clusto.rollback_transaction()
Esempio n. 5
0
 def get_hypervisor(self):
     from clusto.drivers import VMManager
     host = VMManager.resources(self)
     if not host:
         raise DriverException(
             'Cannot start a VM without first allocating a hypervisor')
     return host[0].value
Esempio n. 6
0
    def reboot(self, porttype, portnum):
        if porttype != 'pwr-nema-5':
            raise DriverException('Cannot reboot ports of type: %s' %
                                  str(porttype))

        portnum = portnum.lstrip('.').lower()

        state = self.get_outlet_state(portnum)

        nextstate = None
        if state == 'off':
            nextstate = 1
        if state in ('idleOn', 'on', 'wakeOn'):
            nextstate = 3

        if not nextstate:
            raise DriverException('Outlet in unexpected state: %s' % state)

        self.set_outlet_state(portnum, nextstate)
Esempio n. 7
0
    def kvm_update(self, options):
        attr = dict([(x.subkey, x.value) for x in self.attrs(key='system')])

        status, response = self._request(
            'PUT', '/api/1/%s' % self.name, {
                'memory': attr['memory'],
                'disk': attr['disk'],
                'mac': self.get_port_attr('nic-eth', 1, 'mac'),
                'nic': self.get_port_attr('nic-eth', 1, 'model'),
            })
        if status != 201:
            raise DriverException(response)
Esempio n. 8
0
    def set_attr(self, key, value, number=False, subkey=None):
        """replaces all attributes with the given key"""
        self._check_attr_name(key)

        attrs = self.attrs(key=key, number=number, subkey=subkey)

        if len(attrs) > 1:
            raise DriverException("cannot set an attribute when args match more than one value")

        else:
            if len(attrs) == 1:
                if attrs[0].value == value:
                    return attrs[0]
                self.del_attrs(key=key, number=number, subkey=subkey)

            attr = self.add_attr(key, value, number=number, subkey=subkey)

        return attr
Esempio n. 9
0
    def attr_value(self, *args, **kwargs):
        """Return a single value for the given arguments or the default if none exist

        extra parameters:
          default - the default value to return if none exist
        """

        if 'default' in kwargs:
            default = kwargs.pop('default')
        else:
            default = None

        vals = self.attr_values(*args, **kwargs)

        if vals:
            if kwargs.get('merge_container_attrs', False):
                return vals[0]
            if len(vals) != 1:
                raise DriverException("args match more than one value")
            return vals[0]
        else:
            return default
Esempio n. 10
0
 def set_power_on(self, porttype, portnum):
     if porttype != 'pwr-nema-5':
         raise DriverException('Cannot turn off ports of type: %s' %
                               str(porttype))
     portnum = portnum.lstrip('.').lower()
     state = self.set_outlet_state(portnum, 1)
Esempio n. 11
0
 def kvm_status(self, options):
     status, response = self._request('GET', '/api/1/%s' % self.name)
     if status != 200:
         raise DriverException(response)
     response = json.loads(response)
     return response['state']
Esempio n. 12
0
 def kvm_delete(self, options):
     status, response = self._request('DELETE', '/api/1/%s' % self.name)
     if status != 200:
         raise DriverException(response)