# Defines Docker image suitable for testing cookbooks on CentOS 7.
#
# This handles a number of idiosyncrasies with systemd so it can be 
# run as the root process of the container, making it behave like a 
# normal VM but without the overhead. 

# Because we could use different aws accounts, ecr can differs
# You can set chef_ci_image variable in Manage Jenkins -> Configure System -> Global Properties
FROM centos:latest

# Systemd needs to be able to access cgroups
VOLUME /sys/fs/cgroup 

# Setup container to run Systemd as root process, start an SSH 
# daemon, and provision a user for test-kitchen to connect as.
RUN yum clean all && \ 
    yum -y swap — remove fakesystemd — install systemd systemd-libs && \ 

  # Remove unneeded unit files as this container isn't a proper machine 
  (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done) && \ 
  rm -f /lib/systemd/system/multi-user.target.wants/* && \ 
  rm -f /etc/systemd/system/*.wants/* && \ 
  rm -f /lib/systemd/system/local-fs.target.wants/* && \ 
  rm -f /lib/systemd/system/sockets.target.wants/*udev* && \ 
  rm -f /lib/systemd/system/sockets.target.wants/*initctl* && \ 
  rm -f /lib/systemd/system/basic.target.wants/* && \ 
  rm -f /lib/systemd/system/anaconda.target.wants/* && \ 

  # Setup kitchen user with passwordless sudo 
  useradd -d /home/kitchen -m -s /bin/bash kitchen && \ 
  (echo kitchen:kitchen | chpasswd) && \ 
  mkdir -p /etc/sudoers.d && \ 
  echo 'kitchen ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers.d/kitchen && \ 

  # Setup SSH daemon so test-kitchen can access the container 
  yum -y install openssh-server openssh-clients && \ 
  ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' && \ 
  ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' && \ 
  echo 'OPTIONS="-o UseDNS=no -o UsePAM=no -o PasswordAuthentication=yes"' >> /etc/sysconfig/sshd && \ 
  systemctl enable sshd.service && \
  yum -y install firewalld && \
  yum clean all && \
  systemctl enable firewalld.service && \
  yum clean all

# Docker installation 
RUN yum-config-manager --add-repo https://yum.dockerproject.org/repo/main/centos/7/ && \
    rpm --import https://yum.dockerproject.org/gpg && \
    yum makecache && \
    yum -y install docker-engine && \
    systemctl enable docker && \
    # To avoid restart of docker service we creates .dockerenabled file
    touch /var/cache/.dockerenabled && \
    yum clean all && \
    rm -rf /var/cache/yum