Class: Contained::Task::Setup

Inherits:
Base
  • Object
show all
Defined in:
lib/contained/task/setup.rb

Overview

Examples:

Contained::Task::Setup.execute(environment: "production", config: {})

Instance Method Summary collapse

Methods inherited from Base

execute!, #initialize

Constructor Details

This class inherits a constructor from Contained::Task::Base

Instance Method Details

#execute!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/contained/task/setup.rb', line 8

def execute!
  stack = @config.fetch("stack")
  username = ENV["DOCKER_LOGIN"]
  password = ENV["DOCKER_PASSWORD"]

  deploy_compose_yml = File.open("./config/deploy/compose.yml")
  deploy_env = File.open("./config/deploy/.env.#{@environment}")

  @stdout.puts("[setup] stack=#{stack} environment=#{@environment}")

  on hosts, in: :sequence do
    execute(:which, "curl") do |_, _, status|
      if status.exitstatus != 0
        @stdout.puts("missing curl")
        exit(1)
      end
    end

    execute(:which, "docker") do |_, _, status|
      if status.exitstatus != 0
        @stdout.puts("missing docker")
        execute("curl -sSL https://get.docker.com | sh")
      end
    end

    execute(:docker, :login, "-u", username, "-p", password) if username && password

    execute(:docker, :swarm, :init) unless capture(:docker, :info).include?("Swarm: active")

    execute("mkdir -p ./.contained/#{stack}")

    upload!(deploy_compose_yml, "./.contained/#{stack}/compose.yml")
    upload!(deploy_env, "./.contained/#{stack}/.env")

    execute(<<~BASH)
      source ./.contained/#{stack}/.env
      docker stack deploy -c ./.contained/#{stack}/compose.yml #{stack} --with-registry-auth
    BASH
  end
end