2014-08-18 08:29:43 +00:00
|
|
|
#!/usr/bin/env ruby
|
2015-01-28 21:22:30 +00:00
|
|
|
require "json"
|
2014-08-18 08:29:43 +00:00
|
|
|
require "pathname"
|
|
|
|
require 'ostruct'
|
|
|
|
require 'optparse'
|
|
|
|
require 'json'
|
|
|
|
require 'netaddr'
|
2015-01-28 21:22:30 +00:00
|
|
|
require_relative 'lib/container'
|
2014-08-18 08:29:43 +00:00
|
|
|
|
|
|
|
def try_env(key)
|
2015-01-28 21:22:30 +00:00
|
|
|
ENV[key] or abort("environment variable '#{key}' not set")
|
2014-08-18 08:29:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
options = OpenStruct.new
|
|
|
|
options.container_name = try_env("LXC_NAME")
|
|
|
|
options.container_config = try_env("LXC_CONFIG_FILE")
|
|
|
|
options.rootfs = try_env("LXC_ROOTFS_PATH")
|
|
|
|
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: create-lxc-config [options]"
|
|
|
|
|
|
|
|
opts.on("-4", "--ipv4", "private Ipv4 subnet") do |v|
|
|
|
|
options.ipv4 = v
|
|
|
|
end
|
|
|
|
opts.on("-6", "--ipv6", "public Ipv6 subnet") do |v|
|
|
|
|
options.ipv6 = v
|
|
|
|
end
|
2015-01-28 21:22:30 +00:00
|
|
|
opts.on("--dn42-ipv4", String, "dn42 ipv4") do |v|
|
|
|
|
options.dn42_ipv4 = v
|
|
|
|
end
|
|
|
|
opts.on("--dn42-ipv6", String, "dn42 ipv6") do |v|
|
|
|
|
options.dn42_ipv6 = v
|
|
|
|
end
|
2014-08-18 08:29:43 +00:00
|
|
|
opts.on("--group GROUP", String, "set ansible group (default NONE)") do |group|
|
|
|
|
options.group = group
|
|
|
|
end
|
|
|
|
opts.on("--vars FILE", String, "set json file for ansible variables") do |vars|
|
|
|
|
begin
|
|
|
|
options.vars = JSON.load(File.open(vars))
|
|
|
|
unless options.vars.is_a? Hash
|
|
|
|
abort "vars: Should be a json object"
|
|
|
|
end
|
|
|
|
rescue SystemCallError => e
|
|
|
|
abort "Failed to open '#{vars}': #{e.message}"
|
|
|
|
rescue JSON::ParserError => e
|
|
|
|
abort "Failed to parse ansible variables: #{e.message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.parse!
|
2015-01-17 20:18:15 +00:00
|
|
|
CONFIG_PATH = Pathname.new("/etc/lxc/")
|
2014-08-18 08:29:43 +00:00
|
|
|
container_data = CONFIG_PATH.join("container.json")
|
|
|
|
|
|
|
|
data = if File.exists?(container_data)
|
|
|
|
JSON.load(File.open(container_data))
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
data["network"] ||= {}
|
|
|
|
data["network"][options.container_name] = {}
|
2015-01-28 21:22:30 +00:00
|
|
|
network = data["network"][options.container_name]
|
|
|
|
network["ipv4"] = NetAddr::CIDR.create(ipv4_address).to_s(Short: true)
|
|
|
|
network["ipv6"] = NetAddr::CIDR.create(ipv6_address).to_s(Short: true)
|
|
|
|
network["group"] = options.group
|
|
|
|
network["vars"] = options.vars
|
2014-08-18 08:29:43 +00:00
|
|
|
|
|
|
|
open(container_data, File::CREAT|File::TRUNC|File::RDWR) do |f|
|
|
|
|
f.write(JSON.pretty_generate(data))
|
|
|
|
end
|
|
|
|
|
2015-01-28 21:22:30 +00:00
|
|
|
container = Container.new(data,
|
|
|
|
name: options.container_name,
|
|
|
|
ipv4: ipv4_address,
|
|
|
|
ipv6: ipv6_address,
|
|
|
|
rootfs: options.rootfs,
|
|
|
|
dn42_ipv4: options.dn42_ipv4,
|
|
|
|
dn42_ipv6: options.dn42_ipv4)
|
|
|
|
container.write_config(options.container_config)
|