lxc-config/hooks/cleanup-lxc-config

28 lines
595 B
Plaintext
Raw Normal View History

2014-08-18 08:29:43 +00:00
#!/usr/bin/env ruby
require 'json'
require 'fileutils'
abort "Must run as root" unless Process.uid == 0
CONTAINER_DATA = "/etc/lxc/container.json"
data = JSON.load(File.open(CONTAINER_DATA))
containers = `lxc-ls -1`.split(/\n/)
modified = false
data["network"].each do |host, value|
unless containers.include?(host) or value["lxc"] == false
data["network"].delete(host)
modified = true
end
end
if modified
FileUtils.cp(CONTAINER_DATA, CONTAINER_DATA + ".backup")
File.open(CONTAINER_DATA, "w+") do |f|
f.puts JSON.pretty_generate(data)
end
else
puts "Unchanged"
end