From 8902e5d93f9efcda0de8defcd5ddc525e1c25b98 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Mon, 20 Jan 2014 21:21:09 +0100 Subject: [PATCH] Set the hostname on vagrant boxes --- plugins/vagrant/__init__.py | 3 +++ plugins/vagrant/manifest-schema.json | 15 +++++++++++++++ plugins/vagrant/tasks.py | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/plugins/vagrant/__init__.py b/plugins/vagrant/__init__.py index d7cf824..b0af5d3 100644 --- a/plugins/vagrant/__init__.py +++ b/plugins/vagrant/__init__.py @@ -10,13 +10,16 @@ def validate_manifest(data, validator, error): def resolve_tasks(taskset, manifest): from common.tasks import security from common.tasks import loopback + from common.tasks import network taskset.discard(security.DisableSSHPasswordAuthentication) taskset.discard(loopback.MoveImage) + taskset.discard(network.RemoveHostname) from common.tasks import volume taskset.update([tasks.CheckBoxPath, tasks.CreateVagrantBoxDir, tasks.AddPackages, + tasks.SetHostname, tasks.CreateVagrantUser, tasks.PasswordlessSudo, tasks.SetRootPassword, diff --git a/plugins/vagrant/manifest-schema.json b/plugins/vagrant/manifest-schema.json index c6720d7..89e98e9 100644 --- a/plugins/vagrant/manifest-schema.json +++ b/plugins/vagrant/manifest-schema.json @@ -18,6 +18,21 @@ } }, "required": ["backing"] + }, + "plugins": { + "type": "object", + "properties": { + "vagrant": { + "type": "object", + "properties": { + "hostname": { + "type": "string", + "pattern": "^\\S+$" + } + }, + "required": ["hostname"] + } + } } } } diff --git a/plugins/vagrant/tasks.py b/plugins/vagrant/tasks.py index c8076bc..4118e08 100644 --- a/plugins/vagrant/tasks.py +++ b/plugins/vagrant/tasks.py @@ -48,6 +48,22 @@ 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 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