Skip to content

Commit f1b4b3b

Browse files
committed
build kea Docker image
0 parents  commit f1b4b3b

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

.github/workflows/docker-kea.jsonnet

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
name: 'docker-kea',
3+
on: {
4+
push: {
5+
branches: ['master', 'test'],
6+
paths: ['docker/kea/**'],
7+
},
8+
},
9+
jobs: {
10+
build: {
11+
name: 'build',
12+
'runs-on': 'ubuntu-latest',
13+
permissions: { 'id-token': 'write', contents: 'read' },
14+
steps: [
15+
{ uses: 'actions/checkout@v3' },
16+
{
17+
name: 'setup docker multiarch',
18+
run: |||
19+
mkdir -p ~/.docker
20+
sudo docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes --credential yes
21+
|||,
22+
},
23+
{
24+
uses: 'docker/setup-buildx-action@v2',
25+
with: { install: true },
26+
},
27+
{
28+
uses: 'aws-actions/configure-aws-credentials@v1',
29+
with: {
30+
'aws-region': 'ap-northeast-1',
31+
'role-to-assume': 'arn:aws:iam::005216166247:role/GhaDockerPush',
32+
'role-skip-session-tagging': true,
33+
},
34+
},
35+
{
36+
uses: 'aws-actions/amazon-ecr-login@v1',
37+
id: 'login-ecr',
38+
},
39+
{
40+
uses: 'docker/build-push-action@v3',
41+
with: {
42+
context: 'docker/kea',
43+
platforms: std.join(',', ['linux/arm64']),
44+
tags: std.join(',', [
45+
'${{ steps.login-ecr.outputs.registry }}/kea:${{ github.sha }}',
46+
'${{ steps.login-ecr.outputs.registry }}/kea:latest',
47+
]),
48+
push: true,
49+
},
50+
},
51+
],
52+
},
53+
},
54+
}

.github/workflows/docker-kea.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"jobs": {
3+
"build": {
4+
"name": "build",
5+
"permissions": {
6+
"contents": "read",
7+
"id-token": "write"
8+
},
9+
"runs-on": "ubuntu-latest",
10+
"steps": [
11+
{
12+
"uses": "actions/checkout@v3"
13+
},
14+
{
15+
"name": "setup docker multiarch",
16+
"run": "mkdir -p ~/.docker\nsudo docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes --credential yes\n"
17+
},
18+
{
19+
"uses": "docker/setup-buildx-action@v2",
20+
"with": {
21+
"install": true
22+
}
23+
},
24+
{
25+
"uses": "aws-actions/configure-aws-credentials@v1",
26+
"with": {
27+
"aws-region": "ap-northeast-1",
28+
"role-skip-session-tagging": true,
29+
"role-to-assume": "arn:aws:iam::005216166247:role/GhaDockerPush"
30+
}
31+
},
32+
{
33+
"id": "login-ecr",
34+
"uses": "aws-actions/amazon-ecr-login@v1"
35+
},
36+
{
37+
"uses": "docker/build-push-action@v3",
38+
"with": {
39+
"context": "docker/kea",
40+
"platforms": "linux/arm64",
41+
"push": true,
42+
"tags": "${{ steps.login-ecr.outputs.registry }}/kea:${{ github.sha }},${{ steps.login-ecr.outputs.registry }}/kea:latest"
43+
}
44+
}
45+
]
46+
}
47+
},
48+
"name": "docker-kea",
49+
"on": {
50+
"push": {
51+
"branches": [
52+
"master",
53+
"test"
54+
],
55+
"paths": [
56+
"docker/kea/**"
57+
]
58+
}
59+
}
60+
}

gen-workflows.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env ruby
2+
require 'fileutils'
3+
require 'json'
4+
5+
Dir.chdir(__dir__)
6+
7+
Dir["./.github/workflows/*.jsonnet"].each do |src|
8+
dst = src.sub(/\.jsonnet$/, '.yml')
9+
p [src => dst]
10+
FileUtils.mkdir_p File.dirname(dst)
11+
12+
File.open(dst, 'w') do |io|
13+
system('jsonnet', src, out: io, exception: true)
14+
end
15+
end

kea/Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
FROM public.ecr.aws/ubuntu/ubuntu:22.04 as config
2+
RUN apt-get update && apt-get install -y jsonnet
3+
WORKDIR /app
4+
COPY kea-ctrl-agent.jsonnet /tmp/
5+
RUN jsonnet /tmp/kea-ctrl-agent.jsonnet > /app/kea-ctrl-agent.json
6+
7+
FROM public.ecr.aws/ubuntu/ubuntu:22.04
8+
9+
RUN mkdir -p /run/kea /app
10+
VOLUME /run/kea
11+
12+
RUN apt-get update && apt-get install -y \
13+
ca-certificates \
14+
dumb-init \
15+
ruby3.0 \
16+
iproute2 \
17+
curl \
18+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
19+
20+
ARG KEA_VERSION=2.0.2-1
21+
RUN apt-get update && apt-get install -y --no-install-recommends \
22+
kea-dhcp4-server=${KEA_VERSION} \
23+
kea-admin=${KEA_VERSION} \
24+
kea-ctrl-agent=${KEA_VERSION} \
25+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
26+
27+
RUN curl -Ssf -o /tmp/stork.deb 'https://sorah-pkg.s3.dualstack.ap-northeast-1.amazonaws.com/misc/isc-stork-agent_1.5.0.220824140136_arm64.deb' \
28+
&& ( echo '1e8b67f2afe4404ea3091cbef14e93a23186633a3d571f9cb141d162350d2fe6235d7679e89bed5c2235433f203d706d /tmp/stork.deb' | sha384sum -c --strict ) \
29+
&& dpkg -i /tmp/stork.deb && rm /tmp/stork.deb
30+
31+
COPY run.sh /app/run.sh
32+
COPY choose_dhcp_server_id.rb /app/choose_dhcp_server_id.rb
33+
COPY --from=config /app/kea-ctrl-agent.json /app/kea-ctrl-agent.json
34+
35+
RUN kea-ctrl-agent -t /app/kea-ctrl-agent.json
36+
37+
CMD /app/run.sh

kea/choose_dhcp_server_id.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
require 'ipaddr'
3+
4+
dev = IO.popen(%w(ip -o route get 8.8.8.8), 'r', &:read).match(/dev ([^ ]+)/)[1].chomp
5+
addr = IO.popen([*%w(ip -o address show dev), dev], 'r', &:read).match(/inet ([^ ]+)/)[1].chomp
6+
net = IPAddr.new(addr)
7+
8+
candidates = ENV.fetch('DHCP_SERVER_IDS', '').split(',')
9+
10+
candidates.each do |candidate|
11+
if net.include?(IPAddr.new(candidate))
12+
puts candidate
13+
exit
14+
end
15+
end
16+
17+
puts addr.split(?/)[0]

kea/kea-ctrl-agent.jsonnet

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
'Control-agent': {
3+
'http-host': '127.0.0.1',
4+
'http-port': 10080,
5+
'control-sockets': {
6+
dhcp4: {
7+
comment: 'main server',
8+
'socket-type': 'unix',
9+
'socket-name': '/run/kea/dhcp4.sock',
10+
},
11+
// dhcp6: {
12+
// 'socket-type': 'unix',
13+
// 'socket-name': '/path/to/the/unix/socket-v6',
14+
// 'user-context': { version: 3 },
15+
// },
16+
},
17+
'hooks-libraries': [
18+
// {
19+
// library: '/opt/local/control-agent-commands.so',
20+
// parameters: {
21+
// param1: 'foo',
22+
// },
23+
// },
24+
],
25+
loggers: [
26+
{
27+
name: 'kea-ctrl-agent',
28+
severity: 'WARN',
29+
output_options: [{ output: 'stdout' }],
30+
},
31+
],
32+
},
33+
34+
}

kea/run.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/dumb-init /bin/bash
2+
mkdir /work
3+
cd /work
4+
5+
SERVER_ID=$(/app/choose_dhcp_server_id.rb)
6+
if [ -z "${SERVER_ID}" ]; then
7+
if grep -q __SERVER_ID__ /config/kea-dhcp4.json; then
8+
echo "Failed to choose server id"
9+
exit 1
10+
fi
11+
fi
12+
echo "SERVER_ID=${SERVER_ID}"
13+
14+
(
15+
umask 077
16+
sed \
17+
-e "s|__LEASE_DATABASE_NAME__|${LEASE_DATABASE_NAME}|g" \
18+
-e "s|__LEASE_DATABASE_HOST__|${LEASE_DATABASE_HOST}|g" \
19+
-e "s|__LEASE_DATABASE_USER__|${LEASE_DATABASE_USER}|g" \
20+
-e "s|__LEASE_DATABASE_PASSWORD__|${LEASE_DATABASE_PASSWORD}|g" \
21+
-e "s|__HOSTS_DATABASE_NAME__|${HOSTS_DATABASE_NAME}|g" \
22+
-e "s|__HOSTS_DATABASE_HOST__|${HOSTS_DATABASE_HOST}|g" \
23+
-e "s|__HOSTS_DATABASE_USER__|${HOSTS_DATABASE_USER}|g" \
24+
-e "s|__HOSTS_DATABASE_PASSWORD__|${HOSTS_DATABASE_PASSWORD}|g" \
25+
-e "s|__SERVER_ID__|${SERVER_ID}|g" \
26+
/config/kea-dhcp4.json > /work/kea-dhcp4.json
27+
)
28+
29+
kea-ctrl-agent -c /app/kea-ctrl-agent.json &
30+
stork-agent &
31+
32+
kea-dhcp4 -c /work/kea-dhcp4.json

0 commit comments

Comments
 (0)