From fb23e606ccac44dd2bbba12bbd00fcaed6672270 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 11 Jan 2015 21:30:11 -0200 Subject: [PATCH] action/create: Trim automatically generated container names to 64 chars Fixes GH-337 --- CHANGELOG.md | 6 ++++++ lib/vagrant-lxc/action/create.rb | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f70003d..b317c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,12 @@ IMPROVEMENTS: - Show an user friendly message when trying to use the plugin on non-Linux environments. +BUG FIXES: + + - Trim automatically generated container names to 64 chars [[GH-337]] + +[GH-337]: https://github.com/fgrehm/vagrant-lxc/issues/337 + ## [1.0.1](https://github.com/fgrehm/vagrant-lxc/compare/v1.0.0...v1.0.1) (Oct 15, 2014) diff --git a/lib/vagrant-lxc/action/create.rb b/lib/vagrant-lxc/action/create.rb index c9d431a..fcabe2f 100644 --- a/lib/vagrant-lxc/action/create.rb +++ b/lib/vagrant-lxc/action/create.rb @@ -16,11 +16,7 @@ module Vagrant when String # Nothing to do here, move along... else - container_name = "#{env[:root_path].basename}_#{env[:machine].name}" - container_name.gsub!(/[^-a-z0-9_]/i, "") - # milliseconds + random number suffix to allow for simultaneous - # `vagrant up` of the same box in different dirs - container_name << "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}" + container_name = generate_container_name(env) end env[:machine].provider.driver.create( @@ -36,6 +32,19 @@ module Vagrant @app.call env end + + def generate_container_name(env) + container_name = "#{env[:root_path].basename}_#{env[:machine].name}" + container_name.gsub!(/[^-a-z0-9_]/i, "") + + # milliseconds + random number suffix to allow for simultaneous + # `vagrant up` of the same box in different dirs + container_name << "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}" + + # Trim container name to 64 chars, keeping "randomness" + trim_point = container_name.size > 64 ? -64 : -(container_name.size) + container_name[trim_point..-1] + end end end end