Class: VPS::CLI::Playbook::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/vps/cli/playbook/tasks.rb

Defined Under Namespace

Classes: InvalidTaskError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks) ⇒ Tasks

Returns a new instance of Tasks.



12
13
14
# File 'lib/vps/cli/playbook/tasks.rb', line 12

def initialize(tasks)
  @tasks = [tasks].flatten.compact
end

Class Method Details

.availableObject



8
9
10
# File 'lib/vps/cli/playbook/tasks.rb', line 8

def self.available
  public_instance_methods(false) - [:run]
end

Instance Method Details

#confirm(state, options) ⇒ Object



95
96
97
98
99
100
# File 'lib/vps/cli/playbook/tasks.rb', line 95

def confirm(state, options)
  answer = Ask.confirm(question(options)) ? "y" : "n"
  tasks = options[answer]
  set(state, options, answer)
  run_tasks(state, {:tasks => tasks})
end

#ensure(state, options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vps/cli/playbook/tasks.rb', line 33

def ensure(state, options)
  argument = state.resolve(options[:argument])

  if state[argument].blank?
    options[:fallbacks].each do |task|
      unless (value = run_task(state, task.merge(as: argument))).blank?
        set(state, argument, value)
        break
      end
    end
  end
end

#execute(state, options) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vps/cli/playbook/tasks.rb', line 152

def execute(state, options)
  output = [options[:command]].flatten.inject(nil) do |_, command|
    command = state.resolve(command)
    puts "☕ ~> ".gray + command.yellow
    unless state.dry_run?
      start = Time.now
      result = []

      IO.popen(command).each do |data|
        unless data.blank?
          data = data.split("\n").reject(&:blank?)
          puts "   " + data.join("\n   ")
          result.concat data
        end
      end

      puts "   #{(Time.now - start).round(3)}s".gray
      result.join("\n")
    end
  end
  set(state, options, output)
end

#generate_file(state, options) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/vps/cli/playbook/tasks.rb', line 134

def generate_file(state, options)
  erb = VPS.read_template(state.resolve(options[:template]))
  template = Erubis::Eruby.new(erb)
  content = template.result(state.to_binding)

  unless state.dry_run?
    if target = state.resolve(options[:target])
      target = File.expand_path(target)
      FileUtils.mkdir_p(File.dirname(target))
      File.open(target, "w") do |file|
        file.write(content)
      end
    end
  end

  content
end

#input(state, options) ⇒ Object



129
130
131
132
# File 'lib/vps/cli/playbook/tasks.rb', line 129

def input(state, options)
  answer = Ask.input(question(options), default: state.resolve(options[:default]))
  set(state, options, answer)
end

#loop(state, options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vps/cli/playbook/tasks.rb', line 76

def loop(state, options)
  if (collection = (state.resolve(options[:through]) || []).compact).any?
    puts_description(state, options)
    as = state.resolve(options[:as])
    collection.each do |item|
      state.scope({as => item}) do
        run_tasks(state, {:tasks => options[:run]})
      end
    end
  end
end

#multiselect(state, options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vps/cli/playbook/tasks.rb', line 108

def multiselect(state, options)
  names, labels, defaults = [], [], []

  options[:options].inject([names, labels, defaults]) do |_, (name, label)|
    default = true
    label = label.gsub(/ \[false\]$/) do
      default = false
      ""
    end
    names.push(name)
    labels.push(label)
    defaults.push(default)
  end

  selected = Ask.checkbox(question(options), labels, default: defaults)
  selected.each_with_index do |value, index|
    name = names[index]
    state[name] = value
  end
end

#obtain_config(state, options) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/vps/cli/playbook/tasks.rb', line 46

def obtain_config(state, options)
  VPS.read_config(state[:host]).tap do |config|
    config.each do |key, value|
      set(state, key, value)
    end
  end
end

#playbook(state, options) ⇒ Object



213
214
215
# File 'lib/vps/cli/playbook/tasks.rb', line 213

def playbook(state, options)
  Playbook.run(state.resolve(options[:playbook]), state)
end


217
218
219
# File 'lib/vps/cli/playbook/tasks.rb', line 217

def print(state, options)
  puts state.resolve(options[:message])
end

#read_config(state, options) ⇒ Object



54
55
56
# File 'lib/vps/cli/playbook/tasks.rb', line 54

def read_config(state, options)
  VPS.read_config(state[:host], state.resolve(options[:key]))
end

#remote_execute(state, options) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/vps/cli/playbook/tasks.rb', line 175

def remote_execute(state, options)
  user = state.resolve(options[:user])
  output = [options[:command]].flatten.inject(nil) do |_, command|
    command = state.resolve(command)
    state.execute(command, user)
  end
  set(state, options, output)
end

#run(state) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vps/cli/playbook/tasks.rb', line 16

def run(state)
  @tasks.collect do |task|
    case task
    when :continue # next
    when :abort
      raise Interrupt
    else
      run_task(state, task)
    end
  end
end

#run_tasks(state, options) ⇒ Object



28
29
30
31
# File 'lib/vps/cli/playbook/tasks.rb', line 28

def run_tasks(state, options)
  tasks = (state.resolve(options[:tasks]) || []).compact
  Tasks.new(tasks).run(state)
end

#select(state, options) ⇒ Object



102
103
104
105
106
# File 'lib/vps/cli/playbook/tasks.rb', line 102

def select(state, options)
  list = state.resolve(options[:options])
  index = Ask.list(question(options), list)
  set(state, options, list[index])
end

#sync(state, options) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/vps/cli/playbook/tasks.rb', line 199

def sync(state, options)
  host = state[:host]

  directory = state.resolve(options[:directory])
  remote_path = options[:remote_path] ? state.resolve(options[:remote_path]) : directory

  return if directory.blank?

  remote_path = remote_path.gsub("~", state.home_directory)

  remote_execute(state, {:command => "mkdir -p #{File.dirname(remote_path)}"})
  execute(state, {:command => "rsync #{options[:options]} #{directory} #{host}:#{remote_path} > /dev/tty"})
end

#upload(state, options) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/vps/cli/playbook/tasks.rb', line 184

def upload(state, options)
  host = state[:host]

  file = state.resolve(options[:file])
  remote_path = options[:remote_path] ? state.resolve(options[:remote_path]) : file
  file = "-r #{file}" if File.directory?(file)

  return if file.blank?

  remote_path = remote_path.gsub("~", state.home_directory)

  remote_execute(state, {:command => "mkdir -p #{File.dirname(remote_path)}"})
  execute(state, {:command => "scp #{file} #{host}:#{remote_path} > /dev/tty"})
end

#when(state, options) ⇒ Object



88
89
90
91
92
93
# File 'lib/vps/cli/playbook/tasks.rb', line 88

def when(state, options)
  if state[options[:boolean]]
    puts_description(state, options)
    run_tasks(state, {:tasks => options[:run]})
  end
end

#write_config(state, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vps/cli/playbook/tasks.rb', line 58

def write_config(state, options)
  config = {}

  options[:config].each do |key, spec|
    spec = spec.with_indifferent_access if spec.is_a?(Hash)

    if spec.is_a?(Hash) && spec[:task]
      config[key] = run_task(state, spec)
    else
      config[key] = state.resolve(spec)
    end
  end

  unless state.dry_run?
    VPS.write_config(state[:host], config)
  end
end