Skip to content

Commit

Permalink
Update Docker install and Docker Compose usage (#43)
Browse files Browse the repository at this point in the history
Also removing some cruft around some old Fedora versions
  • Loading branch information
maxfierke authored Oct 4, 2023
1 parent f010ddd commit 4eeba2f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/mstrap/paths.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module MStrap
# :nodoc:
SERVICES_INTERNAL_YML = File.join(RC_DIR, "services-internal.yml")

# Path to default profile `services.yml`, a docker-compose file.
# Path to default profile `services.yml`, a Docker Compose file.
SERVICES_YML = File.join(RC_DIR, "services.yml")

# :nodoc:
Expand Down
22 changes: 11 additions & 11 deletions src/mstrap/steps/compose_step.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module MStrap
module Steps
# Runnable as `mstrap compose`, the Compose step provides a wrapper around
# docker-compose that loads all profile's services.yml, to allow managing
# Runnable as `mstrap compose`, the compose step provides a wrapper around
# `docker compose` that loads all profile's services.yml, to allow managing
# all mstrap-managed Docker services.
#
# NOTE: Due to limitations with how step arguments work, any arguments
# intended for `docker-compose` must be seperated by `--` so as to not get
# intended for `docker compose` must be seperated by `--` so as to not get
# interpretted by the `mstrap` CLI options parser. For example, using
# `mstrap compose -- --version` to print the docker-compose version.
# `mstrap compose -- --version` to print the docker compose version.
class ComposeStep < Step
REMOVED_FLAGS = [
"-c",
Expand All @@ -18,11 +18,11 @@ module MStrap
]

def self.description
"Wrapper around `docker-compose` and all loaded profile's services.yml"
"Wrapper around `docker compose` and all loaded profile's services.yml"
end

def self.setup_cmd!(cmd)
# HACK: Remove persistent flags that overlap with docker-compose
# HACK: Remove persistent flags that overlap with docker compose
cmd.flags.flags.reject! do |flag|
REMOVED_FLAGS.includes?(flag.short) || REMOVED_FLAGS.includes?(flag.long)
end
Expand All @@ -38,15 +38,15 @@ module MStrap
logc "No services.yml found. Please create one at #{Paths::SERVICES_YML}, or within a profile."
end

compose_args = file_args + args
docker_args = ["compose"] + file_args + args

logn "# mstrap: executing docker-compose #{compose_args.join(' ')}"
logn "# mstrap: executing docker #{docker_args.join(' ')}"

if docker.requires_sudo?
logw "mstrap: #{ENV["USER"]} is not in 'docker' group, so invoking docker-compose with sudo"
Process.exec("sudo", ["docker-compose"] + compose_args)
logw "mstrap: #{ENV["USER"]} is not in 'docker' group, so invoking docker compose with sudo"
Process.exec("sudo", ["docker"] + docker_args)
else
Process.exec("docker-compose", compose_args)
Process.exec("docker", docker_args)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/mstrap/steps/services_step.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module MStrap
# mstrap-managed Docker services.
class ServicesStep < Step
def self.description
"(Re)creates mstrap-managed docker-compose services"
"(Re)creates mstrap-managed Docker Compose services"
end

def bootstrap
Expand Down Expand Up @@ -33,8 +33,8 @@ module MStrap
end

private def start_services(file_args)
logw "#{ENV["USER"]} is not in 'docker' group (or change hasn't taken affect), so invoking docker-compose with sudo" if docker.requires_sudo?
cmd("docker-compose", file_args + ["up", "-d"], sudo: docker.requires_sudo?)
logw "#{ENV["USER"]} is not in 'docker' group (or change hasn't taken effect), so invoking docker compose with sudo" if docker.requires_sudo?
cmd("docker", ["compose"] + file_args + ["up", "-d"], sudo: docker.requires_sudo?)
end
end
end
Expand Down
73 changes: 25 additions & 48 deletions src/mstrap/supports/docker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ module MStrap

@app_path : String? = nil
@requires_sudo : Bool? = nil
@postinstall_reboot_required = false

# :nodoc:
APT_KEY_FINGERPRINT = "9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88"

# Returns the path to an installed Docker for Mac application
# :nodoc:
DOCKER_CE_PACKAGE_NAMES = %w[
docker-ce
docker-ce-cli
containerd.io
docker-buildx-plugin
docker-compose-plugin
]

# Returns the path to an installed Docker Desktop application
def app_path
{% if flag?(:darwin) %}
@app_path ||=
Expand All @@ -25,7 +33,7 @@ module MStrap
{% end %}
end

# Returns a collection of flags for `docker-compose` to use `services.yml`
# Returns a collection of flags for `docker compose` to use `services.yml`
# for the loaded profiles.
def compose_file_args(config)
file_args = [] of String
Expand Down Expand Up @@ -68,11 +76,11 @@ module MStrap
{% end %}
end

# Check for docker-compose and raise if not found. On macOS, this will loop until you confirm the
# command line tools have been installed for Docker for Mac
# Check for `docker compose` and raise if not found. On macOS, this will loop until you confirm the
# command line tools have been installed for Docker Desktop
def ensure_compose!
while !cmd("docker-compose version", quiet: true, sudo: requires_sudo?)
logw "Could not find 'docker-compose'."
while !cmd("docker compose version", quiet: true, sudo: requires_sudo?)
logw "Could not execute 'docker compose'"

if app_path && STDIN.tty?
cmd "open -a #{app_path}", quiet: true
Expand Down Expand Up @@ -100,10 +108,9 @@ module MStrap
logc "Could not install docker via Homebrew cask"
end
{% elsif flag?(:linux) %}
if !cmd("docker version", quiet: true, sudo: requires_sudo?)
logn "Docker has not been installed. Attempting to install Docker now."
if !cmd("docker version", quiet: true, sudo: requires_sudo?) || !cmd("docker compose version", quiet: true, sudo: requires_sudo?)
logn "Docker and/or Docker Compose has not been installed. Attempting to install Docker now."
logn "You may be prompted by sudo"
require_reboot = false

success =
if MStrap::Linux.arch_distro?
Expand Down Expand Up @@ -131,46 +138,21 @@ module MStrap
else
success "OK. You may need to log-out and back in, or restart for it to take effect."
end

if postinstall_reboot_required?
logw "Docker install successfully, but unfortunately a reboot is required for Docker to work correctly."
logw "You may run this again to continue anyway, but Docker may fail to launch containers."
exit
end
end

if !cmd("docker-compose version", quiet: true, sudo: requires_sudo?)
logn "docker-compose has not been installed. Attempting to install docker-compose now."

unless cmd("brew install docker-compose")
logc "Could not install docker-compose successfully"
end
end
{% end %}
end

private def fedora_disable_cgroups_v2!
logn "Enabling cgroup backwards compatiblity (requires reboot)"
success = cmd("sudo grubby --update-kernel=ALL --args=\"systemd.unified_cgroup_hierarchy=0\"")

if success
@postinstall_reboot_required = true
end

success
end

private def install_docker_archlinux!
logn "Installing Docker from ArchLinux repos"
MStrap::Platform.install_packages!(["docker", "docker-compose"])
MStrap::Platform.install_packages!(["docker", "docker-buildx", "docker-compose"])
end

private def install_docker_centos!
# https://docs.docker.com/engine/install/centos/#installation-methods
logn "Installing Docker from Official Docker Repos"
success = cmd("sudo yum install -y yum-utils") &&
cmd("sudo yum-config-manager -y --add-repo https://download.docker.com/linux/centos/docker-ce.repo") &&
cmd("sudo yum install -y docker-ce docker-ce-cli containerd.io")
MStrap::Platform.install_packages!(DOCKER_CE_PACKAGE_NAMES)

success
end
Expand Down Expand Up @@ -198,18 +180,17 @@ module MStrap
cmd("curl -fsSL https://download.docker.com/linux/#{distro_name}/gpg | sudo apt-key add -") &&
cmd("sudo add-apt-repository \"deb [arch=#{docker_arch}] https://download.docker.com/linux/#{distro_name} #{distro_codename} stable\"") &&
cmd("sudo apt-get update") &&
cmd("sudo apt-get -y install docker-ce docker-ce-cli containerd.io")
MStrap::Platform.install_packages!(DOCKER_CE_PACKAGE_NAMES)

success
end

private def install_docker_fedora!
# https://docs.docker.com/engine/install/fedora/#installation-methods
logn "Installing Docker from Official Docker Repos"
success = cmd("sudo dnf -y install dnf-plugins-core grubby") &&
success = cmd("sudo dnf -y install dnf-plugins-core") &&
cmd("sudo dnf config-manager -y --add-repo https://download.docker.com/linux/fedora/docker-ce.repo") &&
cmd("sudo dnf install -y docker-ce docker-ce-cli containerd.io") &&
fedora_disable_cgroups_v2!
MStrap::Platform.install_packages!(DOCKER_CE_PACKAGE_NAMES)

success
end
Expand All @@ -221,16 +202,12 @@ module MStrap
install_docker_fedora!
else
logc <<-REDHAT
docker-ce (community edition) is not officially supported on RHEL.
You'll need to install it manually via supported channels or via RedHat
directly.
docker-ce (community edition) is not officially supported on RHEL.
You'll need to install it manually via supported channels or via RedHat
directly.
REDHAT
false
end
end

private def postinstall_reboot_required?
@postinstall_reboot_required
end
end
end

0 comments on commit 4eeba2f

Please sign in to comment.