Revision b578d9e7 snf-cyclades-app/synnefo/logic/backend.py

b/snf-cyclades-app/synnefo/logic/backend.py
118 118
    Update the state of the VM in the DB accordingly.
119 119
    """
120 120

  
121
    ganeti_nics = process_ganeti_nics(nics)
122
    if not nics_changed(vm.nics.order_by('index'), ganeti_nics):
123
        log.debug("NICs for VM %s have not changed", vm)
124

  
121 125
    release_instance_nics(vm)
122 126

  
123
    new_nics = enumerate(nics)
124
    for i, new_nic in new_nics:
127
    for nic in ganeti_nics:
128
        ipv4 = nic.get('ipv4', '')
129
        if ipv4:
130
            net = nic['network']
131
            net.reserve_address(ipv4)
132

  
133
        nic['dirty'] = False
134
        vm.nics.create(**nic)
135
        # Dummy save the network, because UI uses changed-since for VMs
136
        # and Networks in order to show the VM NICs
137
        net.save()
138

  
139
    vm.backendtime = etime
140
    vm.save()
141

  
142

  
143
def process_ganeti_nics(ganeti_nics):
144
    """Process NIC dict from ganeti hooks."""
145
    new_nics = []
146
    for i, new_nic in enumerate(ganeti_nics):
125 147
        network = new_nic.get('network', '')
126 148
        n = str(network)
127 149
        pk = utils.id_from_network_name(n)
......
138 160
        if not firewall_profile and net.public:
139 161
            firewall_profile = settings.DEFAULT_FIREWALL_PROFILE
140 162

  
141
        if ipv4:
142
            net.reserve_address(ipv4)
143

  
144
        vm.nics.create(
145
            network=net,
146
            index=i,
147
            mac=mac,
148
            ipv4=ipv4,
149
            ipv6=ipv6,
150
            firewall_profile=firewall_profile,
151
            dirty=False)
152
        # Dummy save the network, because UI uses changed-since for VMs
153
        # and Networks in order to show the VM NICs
154
        net.save()
155

  
156
    vm.backendtime = etime
157
    vm.save()
163
        nic = {
164
               'index': i,
165
               'network': net,
166
               'mac': mac,
167
               'ipv4': ipv4,
168
               'ipv6': ipv6,
169
               'firewall_profile': firewall_profile}
170

  
171
        new_nics.append(nic)
172
    return new_nics
173

  
174

  
175
def nics_changed(old_nics, new_nics):
176
    """Return True if NICs have changed in any way."""
177
    if len(old_nics) != len(new_nics):
178
        return True
179
    for old_nic, new_nic in zip(old_nics, new_nics):
180
        if not (old_nic.ipv4 == new_nic['ipv4'] and\
181
                old_nic.ipv6 == new_nic['ipv6'] and\
182
                old_nic.mac == new_nic['mac'] and\
183
                old_nic.firewall_profile == new_nic['firewall_profile'] and\
184
                old_nic.index == new_nic['index'] and\
185
                old_nic.network == new_nic['network']):
186
            return True
187
    return False
158 188

  
159 189

  
160 190
def release_instance_nics(vm):

Also available in: Unified diff