diff options
| author | Vishvananda Ishaya <vishvananda@gmail.com> | 2010-07-02 10:39:04 -0500 |
|---|---|---|
| committer | Vishvananda Ishaya <vishvananda@gmail.com> | 2010-07-02 10:39:04 -0500 |
| commit | 04b7b42bde70c53e251f795a1d71cd7cd341b5dc (patch) | |
| tree | 32472679582ce8b5d2962771a73ef12a4c1ec422 | |
| parent | 95e7571a597abdafc638747e2d28c725df96bb17 (diff) | |
| download | nova-04b7b42bde70c53e251f795a1d71cd7cd341b5dc.tar.gz nova-04b7b42bde70c53e251f795a1d71cd7cd341b5dc.tar.xz nova-04b7b42bde70c53e251f795a1d71cd7cd341b5dc.zip | |
Simple network injection
| -rw-r--r-- | nova/compute/disk.py | 23 | ||||
| -rw-r--r-- | nova/compute/network.py | 19 | ||||
| -rw-r--r-- | nova/compute/node.py | 23 |
3 files changed, 50 insertions, 15 deletions
diff --git a/nova/compute/disk.py b/nova/compute/disk.py index bd6a010ee..3f528de61 100644 --- a/nova/compute/disk.py +++ b/nova/compute/disk.py @@ -87,12 +87,14 @@ def partition(infile, outfile, local_bytes=0, local_type='ext2', execute=None): % (infile, outfile, sector_size, primary_first)) @defer.inlineCallbacks -def inject_key(key, image, partition=None, execute=None): - """Injects a ssh key into a disk image. - It adds the specified key to /root/.ssh/authorized_keys +def inject_data(image, key=None, net=None, partition=None, execute=None): + """Injects a ssh key and optionally net data into a disk image. + it will mount the image as a fully partitioned disk and attempt to inject into the specified partition number. + If partition is not specified it mounts the image as a single partition. + """ out, err = yield execute('sudo losetup -f --show %s' % image) if err: @@ -119,8 +121,11 @@ def inject_key(key, image, partition=None, execute=None): raise exception.Error('Failed to mount filesystem: %s' % err) try: - # inject key file - yield _inject_into_fs(key, tmpdir, execute=execute) + if key: + # inject key file + yield _inject_key_into_fs(key, tmpdir, execute=execute) + if net: + yield _inject_net_into_fs(net, tmpdir) finally: # unmount device yield execute('sudo umount %s' % mapped_device) @@ -136,7 +141,7 @@ def inject_key(key, image, partition=None, execute=None): yield execute('sudo losetup -d %s' % device) @defer.inlineCallbacks -def _inject_into_fs(key, fs, execute=None): +def _inject_key_into_fs(key, fs, execute=None): sshdir = os.path.join(os.path.join(fs, 'root'), '.ssh') yield execute('sudo mkdir -p %s' % sshdir) # existing dir doesn't matter yield execute('sudo chown root %s' % sshdir) @@ -144,3 +149,9 @@ def _inject_into_fs(key, fs, execute=None): keyfile = os.path.join(sshdir, 'authorized_keys') yield execute('sudo tee -a %s' % keyfile, '\n' + key.strip() + '\n') +@defer.inlineCallbacks +def _inject_net_into_fs(net, fs, execute=None): + netfile = os.path.join(os.path.join(os.path.join( + fs, 'etc'), 'network'), 'interfaces') + yield execute('sudo tee %s' % netfile, net) + diff --git a/nova/compute/network.py b/nova/compute/network.py index ce156967e..5223e59da 100644 --- a/nova/compute/network.py +++ b/nova/compute/network.py @@ -61,9 +61,22 @@ flags.DEFINE_integer('cloudpipe_start_port', 12000, flags.DEFINE_boolean('simple_network', False, 'Use simple networking instead of vlans') flags.DEFINE_string('simple_network_bridge', 'br100', - 'Bridge for instances') -flags.DEFINE_list('simple_network_ips', ['192.168.1.1'], - 'Available ips for network') + 'Bridge for simple network instances') +flags.DEFINE_list('simple_network_ips', ['192.168.0.2'], + 'Available ips for simple network') +flags.DEFINE_string('simple_network_template', + utils.abspath('compute/interfaces.template'), + 'Template file for simple network') +flags.DEFINE_string('simple_network_netmask', '255.255.255.0', + 'Netmask for simple network') +flags.DEFINE_string('simple_network_network', '192.168.0.0', + 'Network for simple network') +flags.DEFINE_string('simple_network_gateway', '192.168.0.1', + 'Broadcast for simple network') +flags.DEFINE_string('simple_network_broadcast', '192.168.0.255', + 'Broadcast for simple network') +flags.DEFINE_string('simple_network_dns', '8.8.8.8', + 'Dns for simple network') logging.getLogger().setLevel(logging.DEBUG) diff --git a/nova/compute/node.py b/nova/compute/node.py index 1b4714d67..e1503683e 100644 --- a/nova/compute/node.py +++ b/nova/compute/node.py @@ -483,12 +483,23 @@ class Instance(object): if not os.path.exists(basepath('ramdisk')): yield _fetch_file(data['ramdisk_id'], basepath('ramdisk')) - execute = lambda cmd, input=None: self._pool.simpleExecute(cmd=cmd, input=input, error_ok=1) - - if data['key_data']: - logging.info('Injecting key data into image %s', data['image_id']) - yield disk.inject_key( - data['key_data'], basepath('disk-raw'), execute=execute) + execute = lambda cmd, input=None: self._pool.simpleExecute(cmd=cmd, + input=input, + error_ok=1) + + key = data['key_data'] + net = None + if FLAGS.simple_network: + with open(FLAGS.simple_network_template) as f: + net = f.read() % {'address': data['private_dns_name'], + 'network': FLAGS.simple_network_network, + 'netmask': FLAGS.simple_network_netmask, + 'gateway': FLAGS.simple_network_gateway, + 'broadcast': FLAGS.simple_network_broadcast, + 'dns': FLAGS.simple_network_dns} + if key or net: + logging.info('Injecting data into image %s', data['image_id']) + yield disk.inject_data(basepath('disk-raw'), key, net, execute=execute) if os.path.exists(basepath('disk')): yield self._pool.simpleExecute('rm -f %s' % basepath('disk')) |
