Module: Kitchen::Docker::Helpers::DockerfileHelper

Includes:
Configurable
Included in:
Container::Linux
Defined in:
lib/kitchen/docker/helpers/dockerfile_helper.rb

Overview

Per-distribution Dockerfile fragments that prepare a Linux image for SSH.

Instance Method Summary collapse

Instance Method Details

#almalinux_platformString

Dockerfile lines installing an SSH server and sudo on AlmaLinux.

Returns:

  • (String)

    the RUN lines



178
179
180
181
182
183
184
185
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 178

def almalinux_platform
  <<-CODE
    ENV container=docker
    RUN yum clean all
    RUN yum install -y sudo openssh-server openssh-clients which
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  CODE
end

#amazonlinux_platformString

Dockerfile lines installing an SSH server and sudo on Amazon Linux.

Returns:

  • (String)

    the RUN lines



154
155
156
157
158
159
160
161
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 154

def amazonlinux_platform
  <<-CODE
    ENV container=docker
    RUN yum clean all
    RUN yum install -y --allowerasing sudo openssh-server openssh-clients which curl
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  CODE
end

#arch_platformString

Dockerfile lines installing an SSH server and sudo on Arch Linux.

Returns:

  • (String)

    the RUN lines



67
68
69
70
71
72
73
74
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 67

def arch_platform
  <<-CODE
    RUN pacman --noconfirm -Sy archlinux-keyring
    RUN pacman-db-upgrade
    RUN pacman --noconfirm -Syu openssl openssh sudo curl
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
  CODE
end

#centosstream_platformString

Dockerfile lines installing an SSH server and sudo on CentOS Stream.

Returns:

  • (String)

    the RUN lines



166
167
168
169
170
171
172
173
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 166

def centosstream_platform
  <<-CODE
    ENV container=docker
    RUN yum clean all
    RUN yum install -y sudo openssh-server openssh-clients which
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  CODE
end

#debian_platformString

Dockerfile lines installing an SSH server and sudo on Debian and Ubuntu.

Returns:

  • (String)

    the RUN lines



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 79

def debian_platform
  disable_upstart = <<-CODE
    RUN [ ! -f "/sbin/initctl" ] || dpkg-divert --local --rename --add /sbin/initctl \
        && ln -sf /bin/true /sbin/initctl
  CODE
  packages = <<-CODE
    ENV DEBIAN_FRONTEND=noninteractive
    ENV container=docker
    RUN apt-get update
    RUN apt-get install -y sudo openssh-server curl lsb-release
  CODE
  config[:disable_upstart] ? disable_upstart + packages : packages
end

#dockerfile_base_linux(username, homedir) ⇒ String

Dockerfile lines creating the login user and its SSH directory.

The user gets passwordless sudo and Defaults !requiretty, because Test Kitchen runs commands non-interactively and sudo would otherwise refuse.

Parameters:

  • username (String)

    the login user to create

  • homedir (String)

    that user's home directory

Returns:

  • (String)

    the RUN lines



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 220

def dockerfile_base_linux(username, homedir)
  <<-CODE
    RUN if ! getent passwd #{username}; then \
          useradd -d #{homedir} -m -s /bin/bash -p '*' #{username}; \
        fi
    RUN mkdir -p /etc/sudoers.d
    RUN chmod 0750 /etc/sudoers.d
    RUN echo "#{username} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/#{username}
    RUN echo "Defaults !requiretty" >> /etc/sudoers.d/#{username}
    RUN mkdir -p #{homedir}/.ssh
    RUN chown -R #{username} #{homedir}/.ssh
    RUN chmod 0700 #{homedir}/.ssh
    RUN touch #{homedir}/.ssh/authorized_keys
    RUN chown #{username} #{homedir}/.ssh/authorized_keys
    RUN chmod 0600 #{homedir}/.ssh/authorized_keys
    RUN mkdir -p /run/sshd
  CODE
end

#dockerfile_platformString

Dockerfile lines that prepare the configured platform for SSH.

Each distribution needs its own package manager invocation to install an SSH server and sudo, and its own host-key generation, which is why there is one method per family rather than a shared one.

Returns:

  • (String)

    the RUN lines for this platform

Raises:

  • (Kitchen::ActionFailed)

    if the platform is not recognised



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 33

def dockerfile_platform
  case config[:platform]
  when "arch"
    arch_platform
  when "debian", "ubuntu"
    debian_platform
  when "fedora"
    fedora_platform
  when "gentoo"
    gentoo_platform
  when "gentoo-paludis"
    gentoo_paludis_platform
  when "opensuse/tumbleweed", "opensuse/leap", "opensuse", "sles"
    opensuse_platform
  when "rhel", "centos", "oraclelinux"
    rhel_platform
  when "amazonlinux"
    amazonlinux_platform
  when "centosstream"
    centosstream_platform
  when "almalinux"
    almalinux_platform
  when "rockylinux"
    rockylinux_platform
  when "photon"
    photonos_platform
  else
    raise ActionFailed, "Unknown platform '#{config[:platform]}'"
  end
end

#fedora_platformString

Dockerfile lines installing an SSH server and sudo on Fedora.

Returns:

  • (String)

    the RUN lines



96
97
98
99
100
101
102
103
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 96

def fedora_platform
  <<-CODE
    ENV container=docker
    RUN dnf clean all
    RUN dnf install -y sudo openssh-server openssh-clients which curl
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  CODE
end

#gentoo_paludis_platformString

Dockerfile lines installing an SSH server and sudo on Gentoo with Paludis.

Returns:

  • (String)

    the RUN lines



119
120
121
122
123
124
125
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 119

def gentoo_paludis_platform
  <<-CODE
    RUN cave sync
    RUN cave resolve -zx net-misc/openssh app-admin/sudo
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
  CODE
end

#gentoo_platformString

Dockerfile lines installing an SSH server and sudo on Gentoo with Portage.

Returns:

  • (String)

    the RUN lines



108
109
110
111
112
113
114
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 108

def gentoo_platform
  <<-CODE
    RUN emerge-webrsync
    RUN emerge --quiet --noreplace net-misc/openssh app-admin/sudo
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
  CODE
end

#opensuse_platformString

Dockerfile lines installing an SSH server and sudo on openSUSE and SLES.

Returns:

  • (String)

    the RUN lines



130
131
132
133
134
135
136
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 130

def opensuse_platform
  <<-CODE
    ENV container=docker
    RUN zypper install -y sudo openssh which curl gawk
    RUN /usr/sbin/sshd-gen-keys-start
  CODE
end

#photonos_platformString

Dockerfile lines installing an SSH server and sudo on Photon OS.

Returns:

  • (String)

    the RUN lines



202
203
204
205
206
207
208
209
210
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 202

def photonos_platform
  <<-CODE
    ENV container=docker
    RUN tdnf clean all
    RUN tdnf install -y sudo openssh-server openssh-clients which curl
    RUN [ -f "/etc/ssh/ssh_host_ecdsa_key" ] || ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
    RUN [ -f "/etc/ssh/ssh_host_ed25519_key" ] || ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
  CODE
end

#rhel_platformString

Dockerfile lines installing an SSH server and sudo on RHEL, CentOS, and Oracle Linux.

Returns:

  • (String)

    the RUN lines



141
142
143
144
145
146
147
148
149
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 141

def rhel_platform
  <<-CODE
    ENV container=docker
    RUN yum clean all
    RUN yum install -y sudo openssh-server openssh-clients which
    RUN which curl || yum install -y curl
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  CODE
end

#rockylinux_platformString

Dockerfile lines installing an SSH server and sudo on Rocky Linux.

Returns:

  • (String)

    the RUN lines



190
191
192
193
194
195
196
197
# File 'lib/kitchen/docker/helpers/dockerfile_helper.rb', line 190

def rockylinux_platform
  <<-CODE
    ENV container=docker
    RUN yum clean all
    RUN yum install -y sudo openssh-server openssh-clients which
    RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  CODE
end