Skip to content

Instantly share code, notes, and snippets.

@sawanoboly
Created October 5, 2011 05:26
Show Gist options
  • Save sawanoboly/1263713 to your computer and use it in GitHub Desktop.
Save sawanoboly/1263713 to your computer and use it in GitHub Desktop.
Raketask: Create inet_interface for lxc containers.
# Please Modify.
resolvlines:
- nameserver: 8.8.8.8
- nameserver: 8.8.4.4
- search: higanworks.com
server01:
- eth0:
address: "192.168.0.1"
netmask: "255.255.255.0"
- eth1:
address: "10.48.1.1"
netmask: "255.255.255.192"
gateway: "10.48.1.254"
server01:
- eth0:
address: "192.168.0.2"
netmask: "255.255.255.0"
- eth0:0:
address: "192.168.0.3"
netmask: "255.255.255.0"
- eth1:
address: "10.48.1.2"
netmask: "255.255.255.0"
gateway: "10.48.1.254"
## lxc-tool for natty
## Create inet_interface for lxc containers. (ignore the guest status)
## author sawanobori@higanworks.com
## Usage: rake conf_ip RAKE_ENV=environment (default=default)
require 'pp'
require 'erb'
LXC_DIR = "/var/lib/lxc/"
rake_env = ENV['RAKE_ENV'] || "dafault"
# network consts
INET_CONF = "/etc/network/interfaces"
INET_TPL = "./template/interfaces.erb"
## ls LXC_DIR, and remove /^./ Directories(files).
def getCTlist()
tmp_list = Dir::entries(LXC_DIR)
ct_list = []
tmp_list.each do |ct|
if ct !~ /^\./ then
ct_list << ct
end
end
return ct_list
end
desc 'Create inet_interface for container.'
task :conf_ip do
ct_list = getCTlist
require 'yaml'
settings = YAML.load_file("./attribute/#{rake_env}/inetif.yml")
resolvconf = YAML.load_file("./attribute/#{rake_env}/resolv.yml")
ct_list.each do |ct|
# p ct
if settings[ct] then
puts "Setting up #{ct}..."
conf_iface = ERB.new(File.read("./template/interface.erb"), nil, '%').result(binding)
# puts file_iface
tmp_dist = LXC_DIR + ct + "/rootfs" + INET_CONF
# puts tmp_dist
file_iface = File.open(tmp_dist,'w')
file_iface.puts conf_iface
file_iface.close
puts " #{ct}: OverWritten #{tmp_dist}"
## OverWritten hosts
tmp_dist = LXC_DIR + ct + "/rootfs/etc/hosts"
conf_hosts = ERB.new(File.read("./template/hosts.erb"), nil, '%').result(binding)
file_hosts = File.open(tmp_dist,'w')
file_hosts.puts conf_hosts
file_hosts.close
puts " #{ct}: OverWritten #{tmp_dist}"
## OverWritten resolv.conf
tmp_dist = LXC_DIR + ct + "/rootfs/etc/resolv.conf"
if File.symlink?(tmp_dist) then
File.unlink(tmp_dist)
end
conf_resolv = ERB.new(File.read("./template/resolv.conf.erb"), nil, '%').result(binding)
file_resolv = File.open(tmp_dist,'w')
file_resolv.puts conf_resolv
# puts conf_resolv
file_resolv.close
puts " #{ct}: OverWritten #{tmp_dist}"
end
end
end
# modified by mxenzai#lxc-tool with templatefile
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
127.0.0.1 localhost
auto lo
iface lo inet loopback
% settings[ct].each do |ifsets|
% ifsets.each do |ifname, ifconfs|
<%= "auto " + ifname %>
<%= "iface " + ifname + " inet static" %>
% ifconfs.each do |h, j|
<%= " " + h + " " + j %>
% end
<% %>
% end
% end
# modified by giraffi#lxc-tool with templatefile
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
% resolvconf["resolvlines"].each do |line|
% line.each do |item, value|
<%= item + " " + value %>
% end
% end
@sawanoboly
Copy link
Author

Replace filename. "_" -> "/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment