Class: Minke::Tasks::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/minke/tasks/task.rb

Overview

Task is a base implementation of a rake task such as fetch, build, etc

Direct Known Subclasses

Build, BuildImage, Cucumber, Fetch, Push, Run, Shell, Test

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Task

Returns a new instance of Task.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/minke/tasks/task.rb', line 7

def initialize args
  @config                 = args[:config]
  @task_name              = args[:task_name]
  @docker_runner          = args[:docker_runner]
  @task_runner            = args[:task_runner]
  @shell_helper           = args[:shell_helper]
  @logger                 = args[:logger_helper]
  @generator_config       = args[:generator_config]
  @docker_compose_factory = args[:docker_compose_factory]
  @consul                 = args[:consul]
  @docker_network         = args[:docker_network]
  @health_check           = args[:health_check]
  @service_discovery      = args[:service_discovery]
  @task_settings          = @config.send(@task_name)
end

Instance Method Details

#create_container_imageObject

Pulls the build image for the container from the registry if one is supplied, if a build file is specified an image is built.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/minke/tasks/task.rb', line 112

def create_container_image
  build_image = @generator_config.build_settings.docker_settings.image
  build_image = @config.build_image_for(@task_name) unless @config.build_image_for(@task_name) == nil

  build_file = @config.build_docker_file_for(@task_name)

  if build_file != nil
    build_image = "#{@config.application_name}-buildimage"
    
    @logger.debug "Building image: #{build_image} from file #{build_file}"
    @docker_runner.build_image build_file, build_image
  else
    if (@docker_runner.find_image build_image) == nil
      @logger.debug "Pulling image: #{build_image}"
      @docker_runner.pull_image build_image         
    end
  end

  build_image
end

#create_working_directoryObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/minke/tasks/task.rb', line 133

def create_working_directory
  base_path = @generator_config.build_settings.docker_settings.working_directory
  override_path = @task_settings.docker.working_directory unless @task_settings.docker == nil

  if override_path != nil
    path = Pathname.new(base_path)
    return (path + override_path).to_s
  else 
    return base_path
  end
end

#run_command_in_container(command, blocking = false, links = nil, ports = nil) ⇒ Object

runs the given command in a docker container



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/minke/tasks/task.rb', line 66

def run_command_in_container(command, blocking = false, links = nil, ports = nil)
  begin
    @logger.info "Running command: #{command}"
    settings          = @generator_config.build_settings.docker_settings
    volumes           = settings.binds.clone unless settings.binds == nil
    environment       = settings.env.clone unless settings.env == nil
    build_image       = create_container_image
    working_directory = create_working_directory

    if ENV['AGENT_SOCK'] != nil
      volumes.push "#{ENV['AGENT_SOCK']}:/ssh-agent"
      environment.push "SSH_AUTH_SOCK=/ssh-agent"
      environment.push "GIT_SSH_COMMAND=ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
    end

    if @task_settings.consul_loader != nil && links != nil
      links.push "consul"
    end


    args = {
      :image             => build_image,
      :command           => command,
      :volumes           => volumes,
      :environment       => environment,
      :working_directory => working_directory,
      :links             => links,
      :ports             => ports,
    }

    if blocking == false
      container, success = @docker_runner.create_and_run_container args
    else 
      container, success = @docker_runner.create_and_run_blocking_container args
    end

    # throw exception if failed
    raise "Unable to run command #{command}" unless success
  ensure
    @docker_runner.delete_container container
  end
end

#run_with_blockObject

run_with_config executes the task steps for the given

  • block containing custom actions



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/minke/tasks/task.rb', line 26

def run_with_block
  success = true

  begin
    @docker_network.create
    @consul.start_and_load_data @task_settings.consul_loader unless @task_settings.consul_loader == nil
    
    pre_func = -> {
      @task_runner.run_steps(@task_settings.pre) unless @task_settings == nil || @task_settings.pre == nil
    }

    post_func = -> {
      @task_runner.run_steps(@task_settings.post) unless @task_settings == nil || @task_settings.post == nil
    } 
    
    if block_given?
      yield(pre_func, post_func)
    else
      pre_func.call
      post_func.call
    end
  rescue Exception => e
    @logger.error e.message
    success = false
  ensure
    @consul.stop unless @task_settings.consul_loader == nil
    begin
      @docker_network.remove
    rescue Exception => e
      # Trap removing a network as minke may have been called with an existing network and containers
      # may still be attached.
      @logger.error e.message
    end
  end

  abort unless success
end