test-kitchen / kitchen-docker
Showing 2 of 6 files from the diff.

@@ -17,11 +17,14 @@
Loading
17 17
require 'shellwords'
18 18
19 19
require_relative '../container'
20 +
require_relative '../helpers/dockerfile_helper'
20 21
21 22
module Kitchen
22 23
  module Docker
23 24
    class Container
24 25
      class Linux < Kitchen::Docker::Container
26 +
        include Kitchen::Docker::Helpers::DockerfileHelper
27 +
25 28
        MUTEX_FOR_SSH_KEYS = Mutex.new
26 29
27 30
        def initialize(config)
@@ -107,88 +110,11 @@
Loading
107 110
          return dockerfile_template if @config[:dockerfile]
108 111
109 112
          from = "FROM #{@config[:image]}"
110 -
111 -
          platform = case @config[:platform]
112 -
                     when 'debian', 'ubuntu'
113 -
                       disable_upstart = <<-CODE
114 -
                         RUN [ ! -f "/sbin/initctl" ] || dpkg-divert --local --rename --add /sbin/initctl && ln -sf /bin/true /sbin/initctl
115 -
                       CODE
116 -
                       packages = <<-CODE
117 -
                         ENV DEBIAN_FRONTEND noninteractive
118 -
                         ENV container docker
119 -
                         RUN apt-get update
120 -
                         RUN apt-get install -y sudo openssh-server curl lsb-release
121 -
                       CODE
122 -
                       @config[:disable_upstart] ? disable_upstart + packages : packages
123 -
                     when 'rhel', 'centos', 'oraclelinux', 'amazonlinux'
124 -
                       <<-CODE
125 -
                         ENV container docker
126 -
                         RUN yum clean all
127 -
                         RUN yum install -y sudo openssh-server openssh-clients which curl
128 -
                         RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
129 -
                         RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
130 -
                       CODE
131 -
                     when 'fedora'
132 -
                       <<-CODE
133 -
                         ENV container docker
134 -
                         RUN dnf clean all
135 -
                         RUN dnf install -y sudo openssh-server openssh-clients which curl
136 -
                         RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
137 -
                         RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
138 -
                       CODE
139 -
                     when 'opensuse/tumbleweed', 'opensuse/leap', 'opensuse', 'sles'
140 -
                       <<-CODE
141 -
                         ENV container docker
142 -
                         RUN zypper install -y sudo openssh which curl
143 -
                         RUN /usr/sbin/sshd-gen-keys-start
144 -
                       CODE
145 -
                     when 'arch'
146 -
                       # See https://bugs.archlinux.org/task/47052 for why we
147 -
                       # blank out limits.conf.
148 -
                       <<-CODE
149 -
                         RUN pacman --noconfirm -Sy archlinux-keyring
150 -
                         RUN pacman-db-upgrade
151 -
                         RUN pacman --noconfirm -Syu openssl openssh sudo curl
152 -
                         RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
153 -
                         RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
154 -
                         RUN echo >/etc/security/limits.conf
155 -
                       CODE
156 -
                     when 'gentoo'
157 -
                       <<-CODE
158 -
                         RUN emerge --sync
159 -
                         RUN emerge net-misc/openssh app-admin/sudo
160 -
                         RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
161 -
                         RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
162 -
                       CODE
163 -
                     when 'gentoo-paludis'
164 -
                       <<-CODE
165 -
                         RUN cave sync
166 -
                         RUN cave resolve -zx net-misc/openssh app-admin/sudo
167 -
                         RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
168 -
                         RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
169 -
                       CODE
170 -
                     else
171 -
                       raise ActionFailed, "Unknown platform '#{@config[:platform]}'"
172 -
                     end
173 -
113 +
          platform = dockerfile_platform
174 114
          username = @config[:username]
175 115
          public_key = IO.read(@config[:public_key]).strip
176 116
          homedir = username == 'root' ? '/root' : "/home/#{username}"
177 -
178 -
          base = <<-CODE
179 -
            RUN if ! getent passwd #{username}; then \
180 -
                  useradd -d #{homedir} -m -s /bin/bash -p '*' #{username}; \
181 -
                fi
182 -
            RUN echo "#{username} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
183 -
            RUN echo "Defaults !requiretty" >> /etc/sudoers
184 -
            RUN mkdir -p #{homedir}/.ssh
185 -
            RUN chown -R #{username} #{homedir}/.ssh
186 -
            RUN chmod 0700 #{homedir}/.ssh
187 -
            RUN touch #{homedir}/.ssh/authorized_keys
188 -
            RUN chown #{username} #{homedir}/.ssh/authorized_keys
189 -
            RUN chmod 0600 #{homedir}/.ssh/authorized_keys
190 -
            RUN mkdir -p /run/sshd
191 -
          CODE
117 +
          base = dockerfile_base_linux(username, homedir)
192 118
193 119
          custom = ''
194 120
          Array(@config[:provision_command]).each do |cmd|

@@ -0,0 +1,136 @@
Loading
1 +
#
2 +
# Licensed under the Apache License, Version 2.0 (the "License");
3 +
# you may not use this file except in compliance with the License.
4 +
# You may obtain a copy of the License at
5 +
#
6 +
#    http://www.apache.org/licenses/LICENSE-2.0
7 +
#
8 +
# Unless required by applicable law or agreed to in writing, software
9 +
# distributed under the License is distributed on an "AS IS" BASIS,
10 +
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 +
# See the License for the specific language governing permissions and
12 +
# limitations under the License.
13 +
14 +
require 'kitchen'
15 +
require 'kitchen/configurable'
16 +
17 +
module Kitchen
18 +
  module Docker
19 +
    module Helpers
20 +
      module DockerfileHelper     
21 +
        include Configurable
22 +
23 +
        def dockerfile_platform
24 +
          case config[:platform]
25 +
          when 'arch'
26 +
            arch_platform
27 +
          when 'debian', 'ubuntu'
28 +
            debian_platform
29 +
          when 'fedora'
30 +
            fedora_platform
31 +
          when 'gentoo'
32 +
            gentoo_platform
33 +
          when 'gentoo-paludis'
34 +
            gentoo_paludis_platform
35 +
          when 'opensuse/tumbleweed', 'opensuse/leap', 'opensuse', 'sles'
36 +
            opensuse_platform
37 +
          when 'rhel', 'centos', 'oraclelinux', 'amazonlinux'
38 +
            rhel_platform
39 +
          else
40 +
            raise ActionFailed, "Unknown platform '#{config[:platform]}'"
41 +
          end
42 +
        end
43 +
44 +
        def arch_platform
45 +
          # See https://bugs.archlinux.org/task/47052 for why we
46 +
          # blank out limits.conf.
47 +
          <<-CODE
48 +
            RUN pacman --noconfirm -Sy archlinux-keyring
49 +
            RUN pacman-db-upgrade
50 +
            RUN pacman --noconfirm -Syu openssl openssh sudo curl
51 +
            RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
52 +
            RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
53 +
            RUN echo >/etc/security/limits.conf
54 +
          CODE
55 +
        end
56 +
57 +
        def debian_platform
58 +
          disable_upstart = <<-CODE
59 +
            RUN [ ! -f "/sbin/initctl" ] || dpkg-divert --local --rename --add /sbin/initctl \
60 +
                && ln -sf /bin/true /sbin/initctl
61 +
          CODE
62 +
          packages = <<-CODE
63 +
            ENV DEBIAN_FRONTEND noninteractive
64 +
            ENV container docker
65 +
            RUN apt-get update
66 +
            RUN apt-get install -y sudo openssh-server curl lsb-release
67 +
          CODE
68 +
          config[:disable_upstart] ? disable_upstart + packages : packages
69 +
        end
70 +
71 +
        def fedora_platform
72 +
          <<-CODE
73 +
            ENV container docker
74 +
            RUN dnf clean all
75 +
            RUN dnf install -y sudo openssh-server openssh-clients which curl
76 +
            RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
77 +
            RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
78 +
          CODE
79 +
        end
80 +
81 +
        def gentoo_platform
82 +
          <<-CODE
83 +
            RUN emerge --sync
84 +
            RUN emerge net-misc/openssh app-admin/sudo
85 +
            RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
86 +
            RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
87 +
          CODE
88 +
        end
89 +
90 +
        def gentoo_paludis_platform
91 +
          <<-CODE
92 +
            RUN cave sync
93 +
            RUN cave resolve -zx net-misc/openssh app-admin/sudo
94 +
            RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
95 +
            RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
96 +
          CODE
97 +
        end
98 +
99 +
        def opensuse_platform
100 +
          <<-CODE
101 +
            ENV container docker
102 +
            RUN zypper install -y sudo openssh which curl
103 +
            RUN /usr/sbin/sshd-gen-keys-start
104 +
          CODE
105 +
        end
106 +
107 +
        def rhel_platform
108 +
          <<-CODE
109 +
            ENV container docker
110 +
            RUN yum clean all
111 +
            RUN yum install -y sudo openssh-server openssh-clients which curl
112 +
            RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
113 +
            RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
114 +
          CODE
115 +
        end
116 +
117 +
        def dockerfile_base_linux(username, homedir)
118 +
          <<-CODE
119 +
            RUN if ! getent passwd #{username}; then \
120 +
                  useradd -d #{homedir} -m -s /bin/bash -p '*' #{username}; \
121 +
                fi
122 +
            RUN echo "#{username} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
123 +
            RUN echo "Defaults !requiretty" >> /etc/sudoers
124 +
            RUN mkdir -p #{homedir}/.ssh
125 +
            RUN chown -R #{username} #{homedir}/.ssh
126 +
            RUN chmod 0700 #{homedir}/.ssh
127 +
            RUN touch #{homedir}/.ssh/authorized_keys
128 +
            RUN chown #{username} #{homedir}/.ssh/authorized_keys
129 +
            RUN chmod 0600 #{homedir}/.ssh/authorized_keys
130 +
            RUN mkdir -p /run/sshd
131 +
          CODE
132 +
        end
133 +
      end
134 +
    end
135 +
  end
136 +
end
Files Coverage
lib/kitchen 36.76%
Project Totals (10 files) 36.76%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading