Revision c2e41963 snf-cyclades-app/synnefo/api/management/commands/server-import.py

b/snf-cyclades-app/synnefo/api/management/commands/server-import.py
38 38

  
39 39
from synnefo.db.models import VirtualMachine, Network, Flavor
40 40
from synnefo.logic.utils import id_from_network_name, id_from_instance_name
41
from synnefo.logic.backend import wait_for_job
41
from synnefo.logic.backend import wait_for_job, connect_to_network
42 42
from synnefo.logic.rapi import GanetiApiError
43
from synnefo.api.util import allocate_public_address
43
from synnefo.logic import servers
44 44
from synnefo import quotas
45 45

  
46 46
import sys
......
143 143
        else:
144 144
            raise CommandError("Unexpected error" + str(e))
145 145

  
146
    if new_public_nic:
147
        remove_instance_nics(instance, backend_client,
148
                             stream=stream)
149
        try:
150
            (network, address) = allocate_public_address(backend)
151
        except Exception as e:
152
            raise CommandError(e)
153
        nic = {'ip': address, 'network': network.backend_id}
154
        add_public_nic(instance_name, nic, backend_client,
155
                       stream=stream)
156
    else:
146
    if not new_public_nic:
157 147
        check_instance_nics(instance)
158 148

  
159 149
    shutdown_instance(instance, backend_client, stream=stream)
......
167 157
                                       flavor=flavor)
168 158

  
169 159
    quotas.issue_and_accept_commission(vm)
160

  
161
    if new_public_nic:
162
        remove_instance_nics(instance, backend_client,
163
                             stream=stream)
164

  
170 165
    # Rename instance
171 166
    rename_instance(instance_name, vm.backend_vm_id, backend_client,
172 167
                    stream)
168

  
169
    if new_public_nic:
170
        ports = servers.create_instance_ports(user_id)
171
        stream.write("Adding new NICs to server")
172
        [servers.associate_port_with_machine(port, vm)
173
         for port in ports]
174
        [connect_to_network(vm, port) for port in ports]
175

  
173 176
    # Startup instance
174 177
    startup_instance(vm.backend_vm_id, backend_client, stream=stream)
175 178

  
......
194 197

  
195 198
def check_instance_nics(instance):
196 199
    instance_name = instance['name']
197
    networks = instance['nic.networks']
200
    networks = instance['nic.networks.names']
201
    print networks
198 202
    try:
199 203
        networks = map(id_from_network_name, networks)
200 204
    except Network.InvalidBackendIdError:
......
215 219
    jobid = backend_client.ModifyInstance(instance_name, nics=op)
216 220
    (status, error) = wait_for_job(backend_client, jobid)
217 221
    if status != 'success':
218
        raise CommandError("Cannot rename instance: %s" % error)
222
        raise CommandError("Cannot remove instance NICs: %s" % error)
219 223

  
220 224

  
221 225
def add_public_nic(instance_name, nic, backend_client, stream=sys.stdout):
......
229 233
def shutdown_instance(instance, backend_client, stream=sys.stdout):
230 234
    instance_name = instance['name']
231 235
    if instance['status'] != 'ADMIN_down':
232
        stream.write("Instance is not down. Shutting down"
233
                     " instance\n")
236
        stream.write("Instance is not down. Shutting down instance...\n")
234 237
        jobid = backend_client.ShutdownInstance(instance_name)
235 238
        (status, error) = wait_for_job(backend_client, jobid)
236 239
        if status != 'success':

Also available in: Unified diff