action/create: Trim automatically generated container names to 64 chars

Fixes GH-337
This commit is contained in:
Fabio Rehm 2015-01-11 21:30:11 -02:00
parent ef06ea622e
commit fb23e606cc
2 changed files with 20 additions and 5 deletions

View file

@ -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)

View file

@ -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