class VPS::CLI::Playbook::State
Constants
- SERVER_VERSION
Public Class Methods
new(hash = {})
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 27 def initialize(hash = {}) @stack = [hash.with_indifferent_access] end
Public Instance Methods
[](path)
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 55 def [](path) to_domain = !!(path = path.dup).gsub!("domain:", "") if path.is_a?(String) path.to_s.split(".").inject(self) do |hash, key| (hash || {}).fetch(key) end.tap do |value| if to_domain && value if (domain = (value[:domains] || ["-"]).first) return domain.gsub(/https?:\/\//, "") end end end end
[]=(key, value)
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 68 def []=(key, value) stack.first[key] = value end
dry_run?()
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 31 def dry_run? !!fetch(:d) || skip? end
execute(command, user = nil)
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 108 def execute(command, user = nil) if user command = "sudo -u #{user} -H sh -c #{command.inspect}" end puts "🏄♀️ ~> ".gray + command.yellow if dry_run? puts " skipped".gray if skip? else start = Time.now result = [] channel = ssh.open_channel do |ch| ch.exec(command) do |ch| ch.on_data do |_, data| unless data.blank? data = data.split("\n").reject(&:blank?) puts " " + data.join("\n ") result.concat data end end end end channel.wait puts " #{(Time.now - start).round(3)}s".gray result.join("\n") end end
fetch(key, default = nil)
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 48 def fetch(key, default = nil) stack.each do |hash| return hash[key] if hash.key?(key) end default end
home_directory()
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 137 def home_directory ssh.exec!("pwd").strip end
resolve(arg)
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 93 def resolve(arg) if arg.is_a?(String) if arg.match(/^<<\s*(.*?)\s*>>$/) self[$1] else arg.gsub(/\{\{(\{?)\s*(.*?)\s*\}\}\}?/) do value = self[$2] ($1 == "{") ? value.inspect : value end end else arg end end
scope(constants = {}) { || ... }
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 39 def scope(constants = {}) stack.unshift(constants.with_indifferent_access) constants.keys.each do |key| self[key] = resolve(self[key]) end yield stack.shift end
server_version()
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 141 def server_version release = ssh.exec!("cat /etc/lsb-release") distribution = release.match(/DISTRIB_ID=(.*)/)[1].underscore release = release.match(/DISTRIB_RELEASE=(.*)/)[1] [distribution, release].join("-") end
skip?()
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 35 def skip? !!fetch(:_skip_) end
to_binding(object = self)
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 72 def to_binding(object = self) case object when State keys = stack.collect(&:keys).flatten.uniq keys.inject({state: object}) do |hash, key| hash[key] = to_binding(self[key]) hash end when Hash hash = object.inject({}) do |hash, (key, value)| hash[key] = to_binding(resolve(value)) hash end OpenStruct.new(hash) when Array object.collect{|object| to_binding(object)} else object end end
Private Instance Methods
ssh()
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 156 def ssh if dry_run? SSHMock.new else Net::SSH.start(fetch(:host), fetch(:user)) end rescue StandardError => e raise AuthenticationFailedError, e.message end
stack()
click to toggle source
# File lib/vps/cli/playbook/state.rb, line 152 def stack @stack end