Skip to content

Commit

Permalink
update [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
mabels committed Oct 7, 2016
1 parent 137bde5 commit e208c8f
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 80 deletions.
14 changes: 14 additions & 0 deletions examples/clavator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

module Clavator
def self.run(region)
region.hosts.add("clavator", "flavour" => "nixian", "dialect" => "ubuntu") do |host|
region.interfaces.add_device(host, "lo", "mtu" => "9000", :description=>"#{host.name} lo",
"address" => region.network.addresses.add_ip(Construqt::Addresses::LOOOPBACK))
host.configip = host.id ||= Construqt::HostId.create do |my|
my.interfaces << region.interfaces.add_device(host, "eth0", "mtu" => 1500,
'address' => region.network.addresses.add_ip("192.168.16.1/24",
"dhcp" => Construqt::Dhcp.new.start("192.168.16.100").end("192.168.16.200").domain("clavator")))
end
end
end
end
3 changes: 3 additions & 0 deletions examples/construqt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def setup_region(name, network)
require_relative "./postfix.rb"
Postfix.run(region)

require_relative "./clavator.rb"
Clavator.run(region)

Construqt.produce(region)

require_relative 'always-connected'
Expand Down
3 changes: 3 additions & 0 deletions examples/postfix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Smtp
def initialize(name)
@name = name
end
def self.add_component(cps)
cps.register(POSTFIX).add('postfix')
end

module Renderer
module Nixian
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def create_host(name, cfg)
cfg['result'] = nil
host = Construqt::Flavour::Nixian::Dialect::Ubuntu::Host.new(cfg)
host.result = CoreOs::Result.new(host)
#binding.pry
host
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
docker network rm <%= iface.name %>
docker network create --driver=bridge \
--opt=com.docker.network.bridge.enable_ip_masquerade=false \
--opt=com.docker.network.bridge.name=br169 \
<% iface.address.ips.each do |adr| -%>
--gateway=<%= adr.to_s %> --subnet=<%= adr.network.to_string -%> \
<% end -%>
<%= iface.name %>
Original file line number Diff line number Diff line change
@@ -1,106 +1,173 @@
require 'yaml'
require "base64"
module Construqt
module Flavour
module Nixian
module Dialect
module CoreOs
class CloudInit
def initialize(host)
@host = host
@yaml = {
'ssh_authorized_keys' => [],
'write_files' => [],
'coreos' => {
'units' => []
}
}
host.region.users.get_authorized_keys(host.delegate).each do |pk|
add_ssh_pubkey(pk)
end
end

def add_ssh_pubkey(pub)
@yaml['ssh_authorized_keys'] << pub
end

def add_file(obj)
@yaml['write_files'] << obj
end

def add_units(sysrv)
@yaml['coreos']['units'] << {
'name' => sysrv.get_name,
'content' => sysrv.as_systemd_file
}
if sysrv.get_command
@yaml['coreos']['units']['command'] = sysrv.get_command
end
end

def write
Util.write_str(@host.region, "#cloud-config\n\n" + YAML.dump(@yaml), @host.name, 'coreos-cloud-config')
end
end
class Result
def initialize(host)
@ures = Construqt::Flavour::Nixian::Dialect::Ubuntu::Result.new(host)
end

def host
@ures.host
@ures.host
end

def add(block, digest, *path)
@ures.add(block, digest, *path)
@ures.add(block, digest, *path)
end

def add_component(component)
@ures.add_component(component)
@ures.add_component(component)
end

def etc_network_iptables
@ures.etc_network_iptables
@ures.etc_network_iptables
end

def etc_network_interfaces
@ures.etc_network_interfaces
@ures.etc_network_interfaces
end

def write_cloud_config(deployer_sh)
# binding.pry
out = {}
akeys = host.region.users.get_authorized_keys(host.delegate)
out['ssh_authorized_keys'] = akeys
out['coreos'] = { }
out['write_files'] = [
{
"path"=> "/home/core/deployer.sh",
"permissions"=> "0600",
"owner"=> "root",
"content"=> IO.read(deployer_sh)
}
]
Util.write_str(host.region, "#cloud-config\n\n"+YAML.dump(out), host.name, 'coreos-cloud-config')
# def prefix_cloud_config(fd)
# akeys = host.region.users.get_authorized_keys(host.delegate)
# out['ssh_authorized_keys'] = akeys
# out['coreos'] = { }
# out['write_files'] = [
# {
# "path"=> "/home/core/deployer.sh",
# "permissions"=> "0600",
# "owner"=> "root",
# "content"=> IO.read(deployer_sh)
# }
# ]
# Util.write_str(host.region, "#cloud-config\n\n"+YAML.dump(out), host.name, 'coreos-cloud-config')
# end
def write_file(ccc, host, fname, block)
if host.files
return [] if host.files.find do |file|
file.path == fname && file.is_a?(Construqt::Resources::SkipFile)
end
end
text = block.flatten.select { |i| !(i.nil? || i.strip.empty?) }.join("\n")
unless text.empty?
Util.write_str(host.region, text, host.name, fname)
end
ccc.add_file({
"path"=> fname,
"permissions"=> block.right.right,
"owner"=> block.right.owner,
"encoding" => "base64",
"content"=> Base64.encode64(text)
})
end

def commit
add(Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::EtcNetworkIptables, @ures.etc_network_iptables.commitv4, Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::FW4), 'etc', 'network', 'iptables.cfg')
add(Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::EtcNetworkIptables, @ures.etc_network_iptables.commitv6, Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::FW6), 'etc', 'network', 'ip6tables.cfg')
add(Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::EtcNetworkInterfaces, @ures.etc_network_interfaces.commit, Construqt::Resources::Rights.root_0644, 'etc', 'network', 'interfaces')
#@ures.etc_network_vrrp.commit(self)

#Lxc.write_deployers(@host)
out = [
'#!/bin/bash',
'ARGS=$@',
'SCRIPT=$0',
'SCRIPTNAME=`basename $0`',
'SCRIPTPATH=`dirname $0`',
'CONSTRUQT_GIT=/home/core/construqt.git'
]

out << @ures.sh_is_opt_set
out << @ures.sh_function_git_add
#out << sh_install_packages

#out << Construqt::Util.render(binding, 'result_package_list.sh.erb')

#out << offline_package

#out << 'for i in $(seq 8)'
#out << 'do'
#out << " systemctl mask container-getty@\$i.service > /dev/null"
#out << 'done'
#
#out << 'if [ $(is_opt_set skip_mother) != found ]'
#out << 'then'
#out << Construqt::Util.render(binding, 'result_host_check.sh.erb')
#
# out << "[ $(is_opt_set skip_packages) != found ] && install_packages #{Lxc.package_list(@host).join(' ')}"
#
out << Construqt::Util.render(binding, 'result_git_init.sh.erb')
# binding.pry
add(Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::EtcNetworkIptables, @ures.etc_network_iptables.commitv4, Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::FW4), 'etc', 'network', 'iptables.cfg')
add(Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::EtcNetworkIptables, @ures.etc_network_iptables.commitv6, Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::FW6), 'etc', 'network', 'ip6tables.cfg')
add(Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::EtcNetworkInterfaces, @ures.etc_network_interfaces.commit, Construqt::Resources::Rights.root_0644, 'etc', 'network', 'interfaces')

# out += @uressetup_ntp(host)
#out += Lxc.commands(@host)

@ures.results.each do |fname, block|
if !block.clazz.respond_to?(:belongs_to_mother?) ||
block.clazz.belongs_to_mother?
out += @ures.write_file(host, fname, block)
end
end
ccc = CloudInit.new(host)
host.interfaces.values.each do |iface|
next unless iface.clazz == 'bridge'
c_docker = Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::SystemdService.new(self, "construqt-#{iface.name}-docker.service")
c_docker.description("Construqt Docker Network up script")
c_docker.after("docker.service")
c_docker.type("oneshot")
c_docker.exec_start("/etc/network/#{iface.name}-docker-up.sh")
c_docker.wanted_by("basic.target")
ccc.add_units(c_docker)
docker_up = Construqt::Util.render(binding, "docker_up.erb")
# binding.pry
add(self.class, docker_up, Construqt::Resources::Rights.root_0755, 'etc', 'network', "#{iface.name}-docker-up.sh")
# writer = host.result.etc_network_interfaces.get(iface, iface.name)
# writer.lines.up("ip link set mtu #{mtu || iface.delegate.mtu} dev #{ifname} up")
# writer.lines.down("ip link set dev #{ifname} down")
# docker network rm br169
# docker network create --driver=bridge --gateway=169.254.200.1 --subnet=169.254.200.0/24 --gateway=fd00::1 --subnet=fd00::/64 br169
#
# docker network create --driver=bridge --gateway=169.254.200.1 --subnet=169.254.200.0/24 br169
# docker run --net=br169 --ip=169.254.200.200 busybox ifconfi
# # vips-eu-0 ~ # docker ps -q | xargs docker inspect --format '{{.State.Pid}}'
# 3722
# #vips-eu-0 ~ # ip link set netns 3722 dev lion-int
#
# out << 'fi'
# @results.each do |fname, block|
# if block.clazz.respond_to?(:belongs_to_mother?) && !block.clazz.belongs_to_mother?
# out += write_file(host, fname, block)
# end
# end

# out += Lxc.deploy(@host)
# out += [Construqt::Util.render(binding, 'result_git_commit.sh.erb')]
Util.write_str(host.region, out.join("\n"), host.name, 'deployer.sh')
write_cloud_config(Util.get_filename(host.region, host.name, 'deployer.sh'))
end
# vips-eu-0 ~ # nsenter -t 3722 -n ip a a 169.254.210.200/24 dev lion-int
# vips-eu-0 ~ # nsenter -t 3722 -n ip link set lion-int up
# vips-eu-0 ~ # nsenter -t 3722 -n ip r a 0.0.0.0/0 via 169.254.210.1
# --opt=com.docker.network.bridge.enable_ip_masquerade=false \
# --opt=com.docker.network.bridge.name=br169 \

end

c_up = Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::SystemdService.new(self, "construqt-network-up.service")
c_up.description("Construqt Network up script")
c_up.before("network.target")
c_up.type("oneshot")
c_up.exec_start("/etc/network/network_up.sh")
c_up.wanted_by("basic.target")
ccc.add_units(c_up)
c_down = Construqt::Flavour::Nixian::Dialect::Ubuntu::Result::SystemdService.new(self, "construqt-network-down.service")
c_down.description("Construqt Network up script")
c_down.before("shutdown.target")
c_down.type("oneshot")
c_down.exec_start("/etc/network/network_down.sh")
c_down.wanted_by("shutdown.target")
ccc.add_units(c_down)
@ures.results.each do |fname, block|
if !block.clazz.respond_to?(:belongs_to_mother?) ||
block.clazz.belongs_to_mother?
write_file(ccc, host, fname, block)
end
end

# @ures.each do |fname, block|
# if block.clazz.respond_to?(:belongs_to_mother?) && !block.clazz.belongs_to_mother?
# write_file(ccc, host, fname, block)
# end
# end
ccc.write
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative 'result/etc_network_interfaces'
require_relative 'result/etc_network_iptables'
require_relative 'result/etc_conntrackd_conntrackd'
require_relative 'result/systemd_service'
require_relative 'ipsec/ipsec_secret'
require_relative 'ipsec/ipsec_cert_store'

Expand Down Expand Up @@ -215,7 +216,7 @@ def offline_package
if @host.packager
path = [ENV['HOME'] || './', '.construqt', 'package-cache']
FileUtils.mkdir_p path
cacheJd=DateTime.now.jd
cacheJd=ENV['JD']||DateTime.now.jd
package_params = {
'dist' => 'ubuntu',
'arch' => @host.arch || 'amd64',
Expand All @@ -228,10 +229,10 @@ def offline_package
unless File.exist?(cacheFname)
Construqt.logger.debug "Load Woko for: #{File.basename(cacheFname)}"

uri = URI('https://woko.construqt.net:7878/')
uri = URI('https://woko.construqt.net/')
req = Net::HTTP::Post.new(uri, initheader = { 'Content-Type' => 'application/json' })
req.body = package_params.to_json
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(req)
end
packages = JSON.parse(res.body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class SystemdService
include Util::Chainable
chainable_attr_value :description, "unknown"
chainable_attr_value :name
chainable_attr_value :command, nil
chainable_attr_value :type, "oneshot"
chainable_attr_value :exec_start, ""
attr_reader :befores, :conflicts
attr_reader :afters, :befores, :conflicts
def initialize(result, name)
# binding.pry
@name = name
@result = result
@entries = {}
@befores = []
@afters = []
@conflicts = []
@wanted_bys = []
#@default_dependencies = ['no']
Expand All @@ -34,6 +36,11 @@ def also(name)
self
end

def after(name)
@afters << name
self
end

def before(name)
@befores << name
self
Expand All @@ -44,8 +51,14 @@ def conflict(name)
self
end

def as_systemd_file
Construqt::Util.render(binding, "systemd.erb")
end



def commit
@result.add(SystemdService, Construqt::Util.render(binding, "systemd.erb"),
@result.add(SystemdService, as_systemd_file,
Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::FW4),
'etc', 'systemd', 'system', @name)

Expand Down

0 comments on commit e208c8f

Please sign in to comment.