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, Test

Instance Method Summary collapse

Constructor Details

#initialize(config, task, generator_settings, docker_runner, docker_compose_factory, service_discovery, logger, helper) ⇒ Task

Returns a new instance of Task.



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

def initialize config, task, generator_settings, docker_runner, docker_compose_factory, service_discovery, logger, helper
  @config = config
  @task = task
  @generator_settings = generator_settings
  @docker_runner = docker_runner
  @service_discovery = service_discovery
  @logger = logger
  @helper = helper
  @task_settings = config.send(task)

  @build_image = @generator_settings.build_settings.docker_settings.image
  @build_image = config.build_image_for(task) unless config.build_image_for(task) == nil

  @build_file = config.build_docker_file_for(task)

  @compose_file = config.compose_file_for(task)

  @compose = docker_compose_factory.create @compose_file unless @compose_file == nil
end

Instance Method Details

#build_address(url) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/minke/tasks/task.rb', line 87

def build_address url
  if url.type == 'external'
    "#{url.protocol}://#{url.address}:#{url.port}#{url.path}"
  elsif url.type == 'bridge'
    address = @service_discovery.bridge_address_for ENV['DOCKER_NETWORK'], url.address, url.port
    "#{url.protocol}://#{address}#{url.path}"
  elsif url.type == 'public'
    address = @service_discovery.public_address_for url.address, url.port

    # if running on docker for mac we need to replace the ip address with the docker hosts
    ip = @docker_runner.get_docker_ip_address
    if  ip != "127.0.0.1" && ip != "0.0.0.0" && ip != nil
      address.gsub!('0.0.0.0', ip)
    end

    "#{url.protocol}://#{address}#{url.path}"
  end
end

#copy_assets(assets) ⇒ Object



64
65
66
# File 'lib/minke/tasks/task.rb', line 64

def copy_assets assets
  assets.each { |a| @helper.copy_assets a.from, a.to }
end

#execute_rake_tasks(tasks) ⇒ Object

execute an array of rake tasks



50
51
52
# File 'lib/minke/tasks/task.rb', line 50

def execute_rake_tasks tasks
  tasks.each { |t| @helper.invoke_task t }
end

#load_consul_data(config) ⇒ Object

load consul config



56
57
58
# File 'lib/minke/tasks/task.rb', line 56

def load_consul_data config
  @helper.load_consul_data build_address(config.url), config.config_file
end

#log(message, level) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/minke/tasks/task.rb', line 106

def log message, level
  ## implement logger implementation
  case level
  when :error
    @logger.error message
  when :info
    @logger.info message
  when :debug
    @logger.debug message
  end
end

#run_command_in_container(command) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/minke/tasks/task.rb', line 68

def run_command_in_container command
  begin
    settings = @generator_settings.build_settings.docker_settings
    if @build_file != nil
      @build_image = "#{@config.application_name}-buildimage"
      @docker_runner.build_image @build_file, @build_image
    else
      @docker_runner.pull_image @build_image unless @docker_runner.find_image @build_image
    end

    container, success = @docker_runner.create_and_run_container @build_image, settings.binds, settings.env, settings.working_directory, command

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

#run_steps(steps) ⇒ Object

execute the defined steps in the given Minke::Config::TaskRunSettings



41
42
43
44
45
46
# File 'lib/minke/tasks/task.rb', line 41

def run_steps steps
  execute_rake_tasks steps.tasks unless steps.tasks == nil
  load_consul_data steps.consul_loader unless steps.consul_loader == nil
  wait_for_health_check steps.health_check unless steps.health_check == nil
  copy_assets steps.copy unless steps.copy == nil
end

#run_with_blockObject

run_with_config executes the task steps for the given

  • block containing custom actions



30
31
32
33
34
35
36
37
# File 'lib/minke/tasks/task.rb', line 30

def run_with_block
  #TODO: Need to add some tests for this stuff
  run_steps @task_settings.pre unless @task_settings == nil || @task_settings.pre == nil

  yield if block_given?

  run_steps @task_settings.post unless @task_settings == nil || @task_settings.post == nil
end

#wait_for_health_check(url) ⇒ Object



60
61
62
# File 'lib/minke/tasks/task.rb', line 60

def wait_for_health_check url
  @helper.wait_for_HTTPOK build_address(url), 0, 3
end