Class: VPS::CLI::Playbook::Tasks
- Inherits:
-
Object
- Object
- VPS::CLI::Playbook::Tasks
- Defined in:
- lib/vps/cli/playbook/tasks.rb
Defined Under Namespace
Classes: InvalidTaskError
Class Method Summary collapse
Instance Method Summary collapse
- #confirm(state, options) ⇒ Object
- #ensure(state, options) ⇒ Object
- #execute(state, options) ⇒ Object
- #generate_file(state, options) ⇒ Object
-
#initialize(tasks) ⇒ Tasks
constructor
A new instance of Tasks.
- #input(state, options) ⇒ Object
- #loop(state, options) ⇒ Object
- #multiselect(state, options) ⇒ Object
- #obtain_config(state, options) ⇒ Object
- #playbook(state, options) ⇒ Object
- #print(state, options) ⇒ Object
- #read_config(state, options) ⇒ Object
- #remote_execute(state, options) ⇒ Object
- #run(state) ⇒ Object
- #run_tasks(state, options) ⇒ Object
- #select(state, options) ⇒ Object
- #sync(state, options) ⇒ Object
- #upload(state, options) ⇒ Object
- #when(state, options) ⇒ Object
- #write_config(state, options) ⇒ Object
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
.available ⇒ Object
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, ) answer = Ask.confirm(question()) ? "y" : "n" tasks = [answer] set(state, , 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, ) argument = state.resolve([:argument]) if state[argument].blank? [: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, ) output = [[: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, , 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, ) erb = VPS.read_template(state.resolve([:template])) template = Erubis::Eruby.new(erb) content = template.result(state.to_binding) unless state.dry_run? if target = state.resolve([:target]) target = File.(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, ) answer = Ask.input(question(), default: state.resolve([:default])) set(state, , 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, ) if (collection = (state.resolve([:through]) || []).compact).any? puts_description(state, ) as = state.resolve([:as]) collection.each do |item| state.scope({as => item}) do run_tasks(state, {:tasks => [: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, ) names, labels, defaults = [], [], [] [: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(), 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, ) 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, ) Playbook.run(state.resolve([:playbook]), state) end |
#print(state, options) ⇒ Object
217 218 219 |
# File 'lib/vps/cli/playbook/tasks.rb', line 217 def print(state, ) puts state.resolve([:message]) end |
#read_config(state, options) ⇒ Object
54 55 56 |
# File 'lib/vps/cli/playbook/tasks.rb', line 54 def read_config(state, ) VPS.read_config(state[:host], state.resolve([: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, ) user = state.resolve([:user]) output = [[:command]].flatten.inject(nil) do |_, command| command = state.resolve(command) state.execute(command, user) end set(state, , 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, ) tasks = (state.resolve([: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, ) list = state.resolve([:options]) index = Ask.list(question(), list) set(state, , 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, ) host = state[:host] directory = state.resolve([:directory]) remote_path = [:remote_path] ? state.resolve([: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]} #{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, ) host = state[:host] file = state.resolve([:file]) remote_path = [:remote_path] ? state.resolve([: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, ) if state[[:boolean]] puts_description(state, ) run_tasks(state, {:tasks => [: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, ) config = {} [: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 |