Module: Genesis::Framework::Task::TaskDslMethods

Defined in:
lib/genesisframework/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



16
17
18
# File 'lib/genesisframework/task.rb', line 16

def blocks
  @blocks
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/genesisframework/task.rb', line 16

def options
  @options
end

Instance Method Details

#add_block(sym, description, block) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/genesisframework/task.rb', line 184

def add_block sym, description, block
  self.init_defaults
  if self.blocks[sym].has_key?(description)
    raise "Task defines multiple conditions with the same description"
  end
  self.blocks[sym][description] = block
end

#collinsObject



58
59
60
# File 'lib/genesisframework/task.rb', line 58

def collins
  Genesis::Framework::Utils.collins
end

#condition(description, &block) ⇒ Object



30
31
32
# File 'lib/genesisframework/task.rb', line 30

def condition description, &block
  add_block :condition, description, block
end

#configObject



93
94
95
96
97
98
# File 'lib/genesisframework/task.rb', line 93

def config
  # We are intentionally causing a deep copy here so one task
  # can't pollute another task's config setup
  # TODO: consider possibly patching hash to not allow setting members?
  @config ||= Marshal.load(Marshal.dump(Genesis::Framework::Utils.config_cache))
end

#description(desc) ⇒ Object



18
19
20
# File 'lib/genesisframework/task.rb', line 18

def description desc
  set_option :description, desc
end

#facterObject



62
63
64
65
66
67
# File 'lib/genesisframework/task.rb', line 62

def facter
  # lets cache the facts on first use
  # TODO symbolize these keys?
  # TODO implement method_missing? on this hash for easy access
  Genesis::Framework::Utils.facter
end

#fetch(what, filename, base_url: ) ⇒ Object



165
166
167
168
# File 'lib/genesisframework/task.rb', line 165

def fetch what, filename, base_url: ENV['GENESIS_URL']
  filepath = tmp_path(filename)
  Genesis::RetryingFetcher.get(what, base_url) {|data| File.open(filepath, "w", 0755) { |file| file.write data }}
end

#init(&block) ⇒ Object



26
27
28
# File 'lib/genesisframework/task.rb', line 26

def init &block
  set_block :init, block
end

#init_defaultsObject



197
198
199
200
# File 'lib/genesisframework/task.rb', line 197

def init_defaults
  self.blocks ||= { :precondition => {}, :init => nil, :condition => {}, :run => nil, :rollback => nil, :success => nil }
  self.options ||= { :retries => 3.times.to_a, :timeout => 0, :description => nil }
end

#install(provider, *what) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/genesisframework/task.rb', line 108

def install provider, *what
  if provider == :rpm
    Kernel.system("yum", "install", "-y", *what)
    if $?.exitstatus != 0
      raise 'yum install exited with status: ' + $?.exitstatus.to_s
    end
  elsif provider == :gem
    # convert arguments in "what" to a gem_name => [requires_list] structure
    gems = {}
    what.each { |item|
      if item.is_a?(Hash)
        gems.merge! item
      else
        gems[item] = [item]
      end
    }

    # We give a decent try at detecting if the gem is
    # installed before trying to reinstall again.
    # If it contains a - (aka you are specifying a specific version
    # or a / (aka you are specifying a path to find it) then
    # we punt on trying to determine if the gem is already
    # installed and just pass it to install anyway.
    install_gems = gems.select do |gem, requires|
      gem.include?("-") \
        || gem.include?("/") \
        || Gem::Dependency.new(gem).matching_specs.count == 0
    end

    if install_gems.size > 0    # make sure we still have something to do
      options = config.fetch(:gem_args, '').split
      args = (options << install_gems.keys).flatten
      Kernel.system('gem', 'install', *args)
      if $?.exitstatus != 0
        raise "gem install #{args.join(' ')} exited with status: " \
          + $?.exitstatus.to_s
      end
    else                # be noisy that we aren't doing anything
      puts "already installed gems: #{gems.keys.join(' ')}"
    end

    # now need to clear out the Gem cache so we can load it
    Gem.clear_paths

    # Attempt to require loaded gems, print a message if we can't.
    gems.each { |gem, requires|
      begin
        requires.each {|r| require r }
      rescue LoadError
        raise "Could not load gem #{gem} automatically. Maybe the gem name differs from its load path? Please specify the name to require."
      end
    }
  else
    raise 'Unknown install provider: ' + provider.to_s
  end
end

#log(message) ⇒ Object



100
101
102
# File 'lib/genesisframework/task.rb', line 100

def log message
  Genesis::Framework::Utils.log(self.class.name, message)
end

#precondition(description, &block) ⇒ Object



22
23
24
# File 'lib/genesisframework/task.rb', line 22

def precondition description, &block
  add_block :precondition, description, block
end

#prompt(message, seconds = 15, default = false) ⇒ Object



104
105
106
# File 'lib/genesisframework/task.rb', line 104

def prompt message, seconds=15, default=false
  Genesis::PromptCLI.ask message, seconds, default
end

#retries(count) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/genesisframework/task.rb', line 50

def retries count
  if count.is_a? Enumerator then
    set_option :retries, count.to_a
  else
    set_option :retries, count.times.to_a
  end
end

#rollback(&block) ⇒ Object



38
39
40
# File 'lib/genesisframework/task.rb', line 38

def rollback &block
  set_block :rollback, block
end

#run(&block) ⇒ Object



34
35
36
# File 'lib/genesisframework/task.rb', line 34

def run &block
  set_block :run, block
end

#run_cmd(*cmd, stdin_data: '', return_both_streams: false, return_merged_streams: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/genesisframework/task.rb', line 69

def run_cmd *cmd, stdin_data: '', return_both_streams: false, return_merged_streams: false
  if return_both_streams && return_merged_streams
    raise "Invalid run_cmd invocation, can's specify both and merged together"
  end

  if return_merged_streams
    output, status = Open3.capture2e(*cmd, stdin_data: stdin_data)
  else
    stdout, stderr, status = Open3.capture3(*cmd, stdin_data: stdin_data)
    if return_both_streams
      output = [stdout, stderr]
    else
      output = stdout
    end
  end

  if status.exitstatus != 0
    log("Run Command failed for '%s' with exit status '%d' and output: %s" % [cmd.to_s, status.exitstatus, output.to_s])
    raise 'run_cmd exited with status: ' + status.exitstatus.to_s
  end

  return output
end

#set_block(sym, block) ⇒ Object

These methods are private and not part of the exposed DSL. Use at your own risk.



179
180
181
182
# File 'lib/genesisframework/task.rb', line 179

def set_block sym, block
  self.init_defaults
  self.blocks[sym] = block
end

#set_option(sym, option) ⇒ Object



192
193
194
195
# File 'lib/genesisframework/task.rb', line 192

def set_option sym, option
  self.init_defaults
  self.options[sym] = option
end

#success(&block) ⇒ Object



42
43
44
# File 'lib/genesisframework/task.rb', line 42

def success &block
  set_block :success, block
end

#timeout(secs) ⇒ Object



46
47
48
# File 'lib/genesisframework/task.rb', line 46

def timeout secs
  set_option :timeout, secs
end

#tmp_path(filename) ⇒ Object



170
171
172
# File 'lib/genesisframework/task.rb', line 170

def tmp_path filename
  Genesis::Framework::Utils.tmp_path(filename, self.class.name)
end