From 18fce45d9176701d6510ba9f55d974f049282622 Mon Sep 17 00:00:00 2001 From: Tiago Ilieve Date: Sun, 13 Apr 2014 21:18:02 -0300 Subject: [PATCH] Move hostname setting from vagrant into common This closes #19. --- bootstrapvz/base/manifest-schema.json | 8 ++++++-- bootstrapvz/common/tasks/network.py | 16 ++++++++++++++++ bootstrapvz/plugins/vagrant/__init__.py | 2 -- bootstrapvz/plugins/vagrant/manifest-schema.json | 7 +------ bootstrapvz/plugins/vagrant/tasks.py | 16 ---------------- bootstrapvz/providers/ec2/__init__.py | 6 +++++- bootstrapvz/providers/kvm/__init__.py | 6 +++++- bootstrapvz/providers/virtualbox/__init__.py | 6 +++++- manifests/virtualbox-vagrant.manifest.json | 4 ++-- 9 files changed, 40 insertions(+), 31 deletions(-) diff --git a/bootstrapvz/base/manifest-schema.json b/bootstrapvz/base/manifest-schema.json index 1b86fb6..fd0a772 100644 --- a/bootstrapvz/base/manifest-schema.json +++ b/bootstrapvz/base/manifest-schema.json @@ -29,12 +29,16 @@ "sections": { "type": "array", "minItems": 1 - }, + }, "architecture": { "enum": ["i386", "amd64"] }, "bootloader": { "enum": ["pvgrub", "grub", "extlinux"] }, "timezone": { "type": "string" }, "locale": { "type": "string" }, - "charmap": { "type": "string" } + "charmap": { "type": "string" }, + "hostname": { + "type": "string", + "pattern": "^\\S+$" + } }, "required": ["release", "architecture", "bootloader", "timezone", "locale", "charmap"] }, diff --git a/bootstrapvz/common/tasks/network.py b/bootstrapvz/common/tasks/network.py index c175b2d..a85a34d 100644 --- a/bootstrapvz/common/tasks/network.py +++ b/bootstrapvz/common/tasks/network.py @@ -23,6 +23,22 @@ class RemoveHostname(Task): os.remove(os.path.join(info.root, 'etc/hostname')) +class SetHostname(Task): + description = 'Writing hostname into the hostname file' + phase = phases.system_modification + + @classmethod + def run(cls, info): + hostname = info.manifest.system['hostname'].format(**info.manifest_vars) + hostname_file_path = os.path.join(info.root, 'etc/hostname') + with open(hostname_file_path, 'w') as hostname_file: + hostname_file.write(hostname) + + hosts_path = os.path.join(info.root, 'etc/hosts') + from bootstrapvz.common.tools import sed_i + sed_i(hosts_path, '^127.0.0.1\tlocalhost$', '127.0.0.1\tlocalhost\n127.0.1.1\t' + hostname) + + class ConfigureNetworkIF(Task): description = 'Configuring network interfaces' phase = phases.system_modification diff --git a/bootstrapvz/plugins/vagrant/__init__.py b/bootstrapvz/plugins/vagrant/__init__.py index f657f50..f3d53ba 100644 --- a/bootstrapvz/plugins/vagrant/__init__.py +++ b/bootstrapvz/plugins/vagrant/__init__.py @@ -13,13 +13,11 @@ def resolve_tasks(taskset, manifest): from bootstrapvz.common.tasks import network taskset.discard(security.DisableSSHPasswordAuthentication) taskset.discard(loopback.MoveImage) - taskset.discard(network.RemoveHostname) from bootstrapvz.common.tasks import volume taskset.update([tasks.CheckBoxPath, tasks.CreateVagrantBoxDir, tasks.AddPackages, - tasks.SetHostname, tasks.CreateVagrantUser, tasks.PasswordlessSudo, tasks.SetRootPassword, diff --git a/bootstrapvz/plugins/vagrant/manifest-schema.json b/bootstrapvz/plugins/vagrant/manifest-schema.json index 89e98e9..dc4734e 100644 --- a/bootstrapvz/plugins/vagrant/manifest-schema.json +++ b/bootstrapvz/plugins/vagrant/manifest-schema.json @@ -25,12 +25,7 @@ "vagrant": { "type": "object", "properties": { - "hostname": { - "type": "string", - "pattern": "^\\S+$" - } - }, - "required": ["hostname"] + } } } } diff --git a/bootstrapvz/plugins/vagrant/tasks.py b/bootstrapvz/plugins/vagrant/tasks.py index 6ed57d8..2069e5b 100644 --- a/bootstrapvz/plugins/vagrant/tasks.py +++ b/bootstrapvz/plugins/vagrant/tasks.py @@ -48,22 +48,6 @@ class AddPackages(Task): info.packages.add('nfs-client') -class SetHostname(Task): - description = 'Writing hostname into the hostname file' - phase = phases.system_modification - - @classmethod - def run(cls, info): - hostname = info.manifest.plugins['vagrant']['hostname'].format(**info.manifest_vars) - hostname_file_path = os.path.join(info.root, 'etc/hostname') - with open(hostname_file_path, 'w') as hostname_file: - hostname_file.write(hostname) - - hosts_path = os.path.join(info.root, 'etc/hosts') - from bootstrapvz.common.tools import sed_i - sed_i(hosts_path, '^127.0.0.1\tlocalhost$', '127.0.0.1\tlocalhost\n127.0.0.1\t' + hostname) - - class CreateVagrantUser(Task): description = 'Creating the vagrant user' phase = phases.system_modification diff --git a/bootstrapvz/providers/ec2/__init__.py b/bootstrapvz/providers/ec2/__init__.py index 9272d2c..968e905 100644 --- a/bootstrapvz/providers/ec2/__init__.py +++ b/bootstrapvz/providers/ec2/__init__.py @@ -61,6 +61,11 @@ def resolve_tasks(taskset, manifest): if manifest.volume['partitions']['type'] != 'none': taskset.update(task_sets.partitioning_set) + if manifest.system.get('hostname', False): + taskset.add(network.SetHostname) + else: + taskset.add(network.RemoveHostname) + taskset.update([tasks.host.AddExternalCommands, tasks.packages.DefaultPackages, tasks.connection.GetCredentials, @@ -72,7 +77,6 @@ def resolve_tasks(taskset, manifest): boot.DisableGetTTYs, security.EnableShadowConfig, network.RemoveDNSInfo, - network.RemoveHostname, network.ConfigureNetworkIF, tasks.network.EnableDHCPCDDNS, initd.AddExpandRoot, diff --git a/bootstrapvz/providers/kvm/__init__.py b/bootstrapvz/providers/kvm/__init__.py index 313660b..142a8cf 100644 --- a/bootstrapvz/providers/kvm/__init__.py +++ b/bootstrapvz/providers/kvm/__init__.py @@ -37,6 +37,11 @@ def resolve_tasks(tasklist, manifest): if manifest.volume['partitions']['type'] != 'none': tasklist.update(task_sets.partitioning_set) + if manifest.system.get('hostname', False): + tasklist.add(network.SetHostname) + else: + tasklist.add(network.RemoveHostname) + tasklist.update([tasks.packages.DefaultPackages, loopback.Create, @@ -44,7 +49,6 @@ def resolve_tasks(tasklist, manifest): security.EnableShadowConfig, network.RemoveDNSInfo, network.ConfigureNetworkIF, - network.RemoveHostname, initd.AddSSHKeyGeneration, initd.InstallInitScripts, cleanup.ClearMOTD, diff --git a/bootstrapvz/providers/virtualbox/__init__.py b/bootstrapvz/providers/virtualbox/__init__.py index f9fa99b..a133690 100644 --- a/bootstrapvz/providers/virtualbox/__init__.py +++ b/bootstrapvz/providers/virtualbox/__init__.py @@ -37,6 +37,11 @@ def resolve_tasks(taskset, manifest): if manifest.volume['partitions']['type'] != 'none': taskset.update(task_sets.partitioning_set) + if manifest.system.get('hostname', False): + taskset.add(network.SetHostname) + else: + taskset.add(network.RemoveHostname) + taskset.update([tasks.packages.DefaultPackages, loopback.Create, @@ -44,7 +49,6 @@ def resolve_tasks(taskset, manifest): security.EnableShadowConfig, network.RemoveDNSInfo, network.ConfigureNetworkIF, - network.RemoveHostname, initd.AddSSHKeyGeneration, initd.InstallInitScripts, cleanup.ClearMOTD, diff --git a/manifests/virtualbox-vagrant.manifest.json b/manifests/virtualbox-vagrant.manifest.json index 7359103..6641ac2 100644 --- a/manifests/virtualbox-vagrant.manifest.json +++ b/manifests/virtualbox-vagrant.manifest.json @@ -14,7 +14,8 @@ "bootloader": "grub", "timezone": "UTC", "locale": "en_US", - "charmap": "UTF-8" + "charmap": "UTF-8", + "hostname": "localhost" }, "packages": {}, "volume": { @@ -34,7 +35,6 @@ }, "plugins": { "vagrant": { - "hostname": "localhost" } } }