Ask Your Question
0

how to attach instance to different networks

asked 2016-04-22 02:25:36 -0500

vuhiep gravatar image

Hi, I have many network are created and when I launch a instance how can I attach that instance to exactly network what I want. please teach me how to do that using libcloud api, thanks. This is my code:

        N = len(names)
        for i in xrange(N):
            #Find exists SSH Key
            key_pair = None
            for keypair in self.conn.list_key_pairs():
                if keypair.name == 'PubKey':
                    key_pair = keypair
                    break
            '''
            if key_pair == None:
                key_pair = self.conn.create_key_pair(name = 'Key-' + names[i])
            '''
            #Optimize : Multi-thread ?
            node = self.conn.create_node(name = utils.VM_NAME % (course_id, names[i], idx), 
                                         image = images[i], 
                                         size = sizes[i], 
                                         ex_keyname = key_pair.name)
            self.nodes.append(node)  
        #Wait until all nodes are considered running
        w_nodes = self.conn.wait_until_running(nodes = self.nodes, wait_period = 10, timeout = 600)
        self.nodes = w_nodes

Blockquote

        ram, disk = info_json_clients[idx]['ram'], info_json_clients[idx]['disk']
        #********** list network name *******#
        netnamelist = [i['netname'] for i in info_json_clients[idx]['networks']]
        #***************** END **************#
        s_flavor = None
        s_image = None
        for flavor in list_flavors:
            if (flavor.ram >= ram and flavor.disk >= disk and flavor.disk >= 10 and (s_flavor == None or s_flavor.disk > flavor.disk or (s_flavor.disk == flavor.disk and s_flavor.ram < flavor.ram))):
                s_flavor = flavor
        sizes = [s_flavor for _ in xrange(num)] 
        for img in list_images:
            if img.name == 'ubuntu_agent':
                s_image = img
                break
        images = [s_image for _ in xrange(num)]
        print s_flavor, s_image
        nodes = cloud.CreateNodes(course_id = course.id, names = students, images = images, sizes = sizes, idx = idx)
        deploy_nodes.append(nodes)

when i run it return error:

server_params['flavorRef'] = kwargs.get('size').id
AttributeError: 'NoneType' object has no attribute 'id'
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-22 05:04:21 -0500

dbaxps gravatar image

See http://dbaxps.blogspot.com/2016/02/py...
It is python code launching server , regarding selection of network :-

nova.networks.list()
network = nova.networks.find(label="demo_network1")
nics = [{'net-id': network.id}]

block_dev_mapping = {'vda': volume.id }

# Starting nova instance

instance = nova.servers.create(name="Ubuntu1510Devs", image='',
                  flavor=flavor,
                  availability_zone="nova:ip-192-169-142-137.ip.secureserver.net",
                  key_name="oskeyadm0302", nics=nics,       
                  block_device_mapping=block_dev_mapping)
edit flag offensive delete link more

Comments

I read document : http://libcloud.readthedocs.org/en/latest/compute/drivers/openstack.html (http://libcloud.readthedocs.org/en/la...) They have a function create_node and have a parameter 'networks'. but when I edit this parameter into mycode. I get error: TypeError: CreateNodes() got an unexpected keyword argument 'networks'

vuhiep gravatar imagevuhiep ( 2016-04-26 22:42:22 -0500 )edit

mycode: node = self.conn.create_node(name = utils.VM_NAME % (course_id, names[i], idx), image = images[i], size = sizes[i], networks = nets[i],

vuhiep gravatar imagevuhiep ( 2016-04-26 22:43:00 -0500 )edit

Thank for your help, it work for me now. This is my code: node = self.conn.create_node(name = utils.VM_NAME % (course_id, names[i], idx), image = images[i], size = sizes[i],

vuhiep gravatar imagevuhiep ( 2016-04-28 04:21:28 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2016-04-22 02:25:36 -0500

Seen: 93 times

Last updated: Apr 22 '16