Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-9212) Allow for image entry point CMDs #11

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions lib/beaker/hypervisor/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,19 @@ def provision
@logger.notify "provisioning #{host.name}"

@logger.debug("Creating image")
image = ::Docker::Image.build(dockerfile_for(host), {
:rm => true, :buildargs => buildargs_for(host)
})
unless host['use_image_entry_point']
image = ::Docker::Image.build(dockerfile_for(host), {
:rm => true, :buildargs => buildargs_for(host)
})
else
df = <<-DF
FROM #{host['image']}
EXPOSE 22
DF
image = ::Docker::Image.build(df, {
:rm => true, :buildargs => buildargs_for(host)
})
end

if @docker_type == 'swarm'
image_name = "#{@registry}/beaker/#{image.id}"
Expand Down Expand Up @@ -129,6 +139,12 @@ def provision
@logger.debug("Starting container #{container.id}")
container.start

if host['use_image_entry_point']
@logger.notify('Installing ssh components and starting ssh daemon in container')
install_ssh_components(container, host)
# run fixssh to configure and start the ssh service
fix_ssh(container)
end
# Find out where the ssh port is from the container
# When running on swarm DOCKER_HOST points to the swarm manager so we have to get the
# IP of the swarm slave via the container data
Expand Down Expand Up @@ -171,6 +187,48 @@ def provision

end

# When using the entrypoint of an image, run this method to download and install ssh
# alongside your container's CMD
def install_ssh_components(container, host)
case host['platform']
when /ubuntu/, /debian/
container.exec(%w(apt-get update))
container.exec(%w(apt-get install -y openssh-server openssh-client))
when /cumulus/
container.exec(%w(apt-get update))
container.exec(%w(apt-get install -y openssh-server openssh-client))
when /fedora-(2[2-9])/
container.exec(%w(dnf clean all))
container.exec(%w(dnf install -y sudo openssh-server openssh-clients))
container.exec(%w(ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key))
container.exec(%w(ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key))
when /^el-/, /centos/, /fedora/, /redhat/, /eos/
container.exec(%w(yum clean all))
container.exec(%w(yum install -y sudo openssh-server openssh-clients))
container.exec(%w(ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key))
container.exec(%w(ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key))
when /opensuse/, /sles/
container.exec(%w(zypper -n in openssh))
container.exec(%w(ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key))
container.exec(%w(ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key))
container.exec(%w(sed -ri 's/^#?UsePAM .*/UsePAM no/' /etc/ssh/sshd_config))
when /archlinux/
container.exec(%w(pacman --noconfirm -Sy archlinux-keyring))
container.exec(%w(pacman --noconfirm -Syu))
container.exec(%w(pacman -S --noconfirm openssh))
container.exec(%w(ssh-keygen -A))
container.exec(%w(sed -ri 's/^#?UsePAM .*/UsePAM no/' /etc/ssh/sshd_config))
container.exec(%w(systemctl enable sshd))
else
# TODO add more platform steps here
raise "platform #{host['platform']} not yet supported on docker"
end

# Make sshd directory, set root password
container.exec(%w(mkdir -p /var/run/sshd))
container.exec(['/bin/sh', '-c', "echo root:#{root_password} | chpasswd"])
end

def cleanup
@logger.notify "Cleaning up docker"
@hosts.each do |host|
Expand Down
18 changes: 18 additions & 0 deletions spec/beaker/hypervisor/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,26 @@ module Beaker
allow( docker ).to receive(:dockerfile_for)
end

context 'when the host has "use_image_entry_point" set to true on the host' do

before :each do
hosts.each do |host|
host['use_image_entry_point'] = true
end
end

it 'should not call #dockerfile_for but run methods necessary for ssh installation' do
expect( docker ).not_to receive(:dockerfile_for)
expect( docker ).to receive(:install_ssh_components).exactly(3).times #once per host
expect( docker ).to receive(:fix_ssh).exactly(3).times #once per host
docker.provision
end
end

it 'should call dockerfile_for with all the hosts' do
hosts.each do |host|
expect( docker ).not_to receive(:install_ssh_components)
expect( docker ).not_to receive(:fix_ssh)
expect( docker ).to receive(:dockerfile_for).with(host).and_return('')
end

Expand Down