diff --git a/plugins/guests/centos/cap/flavor.rb b/plugins/guests/centos/cap/flavor.rb new file mode 100644 index 000000000..dfaeb3263 --- /dev/null +++ b/plugins/guests/centos/cap/flavor.rb @@ -0,0 +1,24 @@ +module VagrantPlugins + module GuestCentos + module Cap + class Flavor + def self.flavor(machine) + # Read the version file + output = "" + machine.communicate.sudo("cat /etc/centos-release") do |_, data| + output = data + end + + # Detect various flavors we care about + if output =~ /(CentOS)( .+)? 7/i + return :centos_7 + elsif output =~ /(CentOS)( .+)? 8/i + return :centos_8 + else + return :centos + end + end + end + end + end +end diff --git a/plugins/guests/centos/guest.rb b/plugins/guests/centos/guest.rb new file mode 100644 index 000000000..f62a6bd59 --- /dev/null +++ b/plugins/guests/centos/guest.rb @@ -0,0 +1,9 @@ +module VagrantPlugins + module GuestCentos + class Guest < Vagrant.plugin("2", :guest) + def detect?(machine) + machine.communicate.test("cat /etc/centos-release") + end + end + end +end diff --git a/plugins/guests/centos/plugin.rb b/plugins/guests/centos/plugin.rb new file mode 100644 index 000000000..fc07dafd6 --- /dev/null +++ b/plugins/guests/centos/plugin.rb @@ -0,0 +1,20 @@ +require "vagrant" + +module VagrantPlugins + module GuestCentos + class Plugin < Vagrant.plugin("2") + name "CentOS guest" + description "CentOS guest support." + + guest(:centos, :redhat) do + require_relative "guest" + Guest + end + + guest_capability(:centos, :flavor) do + require_relative "cap/flavor" + Cap::Flavor + end + end + end +end diff --git a/plugins/guests/redhat/cap/flavor.rb b/plugins/guests/redhat/cap/flavor.rb index 32dc1a7c2..bfe13d377 100644 --- a/plugins/guests/redhat/cap/flavor.rb +++ b/plugins/guests/redhat/cap/flavor.rb @@ -10,8 +10,10 @@ module VagrantPlugins end # Detect various flavors we care about - if output =~ /(CentOS|Red Hat Enterprise|Scientific|Cloud|Virtuozzo)\s*Linux( .+)? release 7/i + if output =~ /(Red Hat Enterprise|Scientific|Cloud|Virtuozzo)\s*Linux( .+)? release 7/i return :rhel_7 + elsif output =~ /(Red Hat Enterprise|Scientific|Cloud|Virtuozzo)\s*Linux( .+)? release 8/i + return :rhel_8 else return :rhel end diff --git a/plugins/provisioners/docker/cap/redhat/docker_install.rb b/plugins/provisioners/docker/cap/centos/docker_install.rb similarity index 61% rename from plugins/provisioners/docker/cap/redhat/docker_install.rb rename to plugins/provisioners/docker/cap/centos/docker_install.rb index 0a6868b8b..a755320f3 100644 --- a/plugins/provisioners/docker/cap/redhat/docker_install.rb +++ b/plugins/provisioners/docker/cap/centos/docker_install.rb @@ -1,31 +1,34 @@ module VagrantPlugins module DockerProvisioner module Cap - module Redhat + module Centos module DockerInstall def self.docker_install(machine) machine.communicate.tap do |comm| comm.sudo("yum -q -y update") comm.sudo("yum -q -y remove docker-io* || true") - comm.sudo("curl -sSL https://get.docker.com/ | sh") + comm.sudo("yum install -y -q yum-utils") + comm.sudo("yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo") + comm.sudo("yum makecache") + comm.sudo("yum install -y -q docker-ce") end case machine.guest.capability("flavor") - when :rhel_7 - docker_enable_rhel7(machine) + when :centos + docker_enable_service(machine) else - docker_enable_default(machine) + docker_enable_systemctl(machine) end end - def self.docker_enable_rhel7(machine) + def self.docker_enable_systemctl(machine) machine.communicate.tap do |comm| comm.sudo("systemctl start docker.service") comm.sudo("systemctl enable docker.service") end end - def self.docker_enable_default(machine) + def self.docker_enable_service(machine) machine.communicate.tap do |comm| comm.sudo("service docker start") comm.sudo("chkconfig docker on") diff --git a/plugins/provisioners/docker/cap/centos/docker_start_service.rb b/plugins/provisioners/docker/cap/centos/docker_start_service.rb new file mode 100644 index 000000000..116d86fca --- /dev/null +++ b/plugins/provisioners/docker/cap/centos/docker_start_service.rb @@ -0,0 +1,24 @@ +module VagrantPlugins + module DockerProvisioner + module Cap + module Centos + module DockerStartService + def self.docker_start_service(machine) + case machine.guest.capability("flavor") + when :centos + machine.communicate.tap do |comm| + comm.sudo("service docker start") + comm.sudo("chkconfig docker on") + end + else + machine.communicate.tap do |comm| + comm.sudo("systemctl start docker.service") + comm.sudo("systemctl enable docker.service") + end + end + end + end + end + end + end +end diff --git a/plugins/provisioners/docker/cap/redhat/docker_start_service.rb b/plugins/provisioners/docker/cap/redhat/docker_start_service.rb deleted file mode 100644 index 2e7662f00..000000000 --- a/plugins/provisioners/docker/cap/redhat/docker_start_service.rb +++ /dev/null @@ -1,16 +0,0 @@ -module VagrantPlugins - module DockerProvisioner - module Cap - module Redhat - module DockerStartService - def self.docker_start_service(machine) - machine.communicate.sudo("service docker start") - # TODO :: waiting to start - sleep 5 - machine.communicate.sudo("chkconfig docker on") - end - end - end - end - end -end diff --git a/plugins/provisioners/docker/plugin.rb b/plugins/provisioners/docker/plugin.rb index 6de03e727..e761ae400 100644 --- a/plugins/provisioners/docker/plugin.rb +++ b/plugins/provisioners/docker/plugin.rb @@ -29,14 +29,14 @@ module VagrantPlugins Cap::Fedora::DockerInstall end - guest_capability("redhat", "docker_install") do + guest_capability("centos", "docker_install") do require_relative "cap/redhat/docker_install" - Cap::Redhat::DockerInstall + Cap::Centos::DockerInstall end - guest_capability("redhat", "docker_start_service") do + guest_capability("centos", "docker_start_service") do require_relative "cap/redhat/docker_start_service" - Cap::Redhat::DockerStartService + Cap::Centos::DockerStartService end guest_capability("linux", "docker_installed") do diff --git a/test/unit/plugins/guests/centos/cap/flavor_test.rb b/test/unit/plugins/guests/centos/cap/flavor_test.rb new file mode 100644 index 000000000..b3bc89cb6 --- /dev/null +++ b/test/unit/plugins/guests/centos/cap/flavor_test.rb @@ -0,0 +1,37 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestCentos::Cap::Flavor" do + let(:caps) do + VagrantPlugins::GuestCentos::Plugin + .components + .guest_capabilities[:centos] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".flavor" do + let(:cap) { caps.get(:flavor) } + + { + "CentOS Linux 2.4 release 7" => :centos_7, + "CentOS Linux release 8.1.1911 (Core)" => :centos_8, + + "CentOS" => :centos, + "banana" => :centos, + }.each do |str, expected| + it "returns #{expected} for #{str}" do + comm.stub_command("cat /etc/centos-release", stdout: str) + expect(cap.flavor(machine)).to be(expected) + end + end + end +end diff --git a/test/unit/plugins/guests/redhat/cap/flavor_test.rb b/test/unit/plugins/guests/redhat/cap/flavor_test.rb index c711495fa..1676d54f2 100644 --- a/test/unit/plugins/guests/redhat/cap/flavor_test.rb +++ b/test/unit/plugins/guests/redhat/cap/flavor_test.rb @@ -22,12 +22,15 @@ describe "VagrantPlugins::GuestRedHat::Cap::Flavor" do let(:cap) { caps.get(:flavor) } { - "CentOS Linux 2.4 release 7" => :rhel_7, + "Red Hat Enterprise Linux 2.4 release 7" => :rhel_7, "Red Hat Enterprise Linux release 7" => :rhel_7, "Scientific Linux release 7" => :rhel_7, "CloudLinux release 7.2 (Valeri Kubasov)" => :rhel_7, - "CentOS" => :rhel, + "CloudLinux release 8.1.1911 (Valeri Kubasov)" => :rhel_8, + "Red Hat Enterprise Linux release 8" => :rhel_8, + + "Red Hat Enterprise Linux" => :rhel, "RHEL 6" => :rhel, "banana" => :rhel, }.each do |str, expected|