Class: CliTool::Remote::Script
- Inherits:
-
Object
- Object
- CliTool::Remote::Script
- Includes:
- StdinOut
- Defined in:
- lib/cli_tool/remote.rb
Constant Summary
Constants included from StdinOut
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#indent ⇒ Object
Returns the value of attribute indent.
Instance Method Summary collapse
- #aptkey(*keys) ⇒ Object
- #curl(from, to, sudo = false, sudouser = :root) ⇒ Object
- #directory_exist?(directory, exist = true, &block) ⇒ Boolean
- #dpkg_install(*packages) ⇒ Object
-
#exec(script, use_sudo = false, sudo_user = :root) ⇒ Object
Exec =#.
- #file_exist?(file, exist = true, &block) ⇒ Boolean
- #for_in(value, source, &block) ⇒ Object
- #if?(condition, &block) ⇒ Boolean
-
#if_installed?(*packages) ⇒ Boolean
DPKG =#.
-
#initialize(indent = 2, opts = {}) ⇒ Script
(also: #reset)
constructor
A new instance of Script.
-
#install(*packages) ⇒ Object
Apt Tools =#.
- #purge(*packages) ⇒ Object
- #remote_install(*packages) ⇒ Object
- #remove(*packages) ⇒ Object
- #service(name, action) ⇒ Object
- #sudo(user = :root, &block) ⇒ Object
- #to_s(opts = {}) ⇒ Object
- #unless_installed?(*packages) ⇒ Boolean
- #update ⇒ Object
- #upgrade ⇒ Object
- #upgrade! ⇒ Object
-
#wget(from, to, sudo = false, sudouser = :root) ⇒ Object
Tools =#.
Methods included from StdinOut
Constructor Details
#initialize(indent = 2, opts = {}) ⇒ Script Also known as: reset
Returns a new instance of Script.
16 17 18 19 20 21 22 23 |
# File 'lib/cli_tool/remote.rb', line 16 def initialize(indent = 2, opts = {}) @environment = opts[:environment] || {} @commands = [] @apt = {} @remote_installed = 0 @indent = indent @sudo = opts[:sudo] || false end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
12 13 14 |
# File 'lib/cli_tool/remote.rb', line 12 def commands @commands end |
#environment ⇒ Object
Returns the value of attribute environment.
13 14 15 |
# File 'lib/cli_tool/remote.rb', line 13 def environment @environment end |
#indent ⇒ Object
Returns the value of attribute indent.
14 15 16 |
# File 'lib/cli_tool/remote.rb', line 14 def indent @indent end |
Instance Method Details
#aptkey(*keys) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/cli_tool/remote.rb', line 50 def aptkey(*keys) @environment['DEBIAN_FRONTEND'] = %{noninteractive} keyserver = yield if block_given? keyserver ||= 'keyserver.ubuntu.com' exec("apt-key adv --keyserver #{keyserver} --recv-keys #{keys.join(' ')}", :sudo) self end |
#curl(from, to, sudo = false, sudouser = :root) ⇒ Object
96 97 98 99 100 |
# File 'lib/cli_tool/remote.rb', line 96 def curl(from, to, sudo = false, sudouser = :root) install(:curl) exec("curl -# -o #{to} #{from}", sudo, sudouser) self end |
#directory_exist?(directory, exist = true, &block) ⇒ Boolean
110 111 112 |
# File 'lib/cli_tool/remote.rb', line 110 def directory_exist?(directory, exist = true, &block) if?((exist ? '' : '! ') + %{-d "#{directory}"}, &block) end |
#dpkg_install(*packages) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/cli_tool/remote.rb', line 66 def dpkg_install(*packages) packages.each do |package| exec("dpkg -i #{package}", :sudo) end self end |
#exec(script, use_sudo = false, sudo_user = :root) ⇒ Object
Exec =#
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cli_tool/remote.rb', line 134 def exec(script, use_sudo = false, sudo_user = :root) if File.exist?(script) script = File.read(script) end # Remove unnecessary indentation if script.include?("\n") script = script.split("\n").reject { |x| x.strip.empty? } indents = script.first.match(/^([\t ]*)(.*)$/)[1] script = script.map { |x| x.gsub(/#{indents}/, '') }.join("\n") end # Use sudo if requested if use_sudo && ! @sudo return sudo(sudo_user) do exec(script) end end @commands << script.rstrip self end |
#file_exist?(file, exist = true, &block) ⇒ Boolean
106 107 108 |
# File 'lib/cli_tool/remote.rb', line 106 def file_exist?(file, exist = true, &block) if?((exist ? '' : '! ') + %{-f "#{file}"}, &block) end |
#for_in(value, source, &block) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/cli_tool/remote.rb', line 114 def for_in(value, source, &block) for_script = "for #{value} in #{source}\n#{"\t" * (@indent + 2)}do\n\n" for_script << Script.new(@indent + 2, environment: @environment).__send__(:instance_exec, &block).to_s(env: false) for_script << "\n#{"\t" * @indent}done" @commands << for_script << '' self end |
#if?(condition, &block) ⇒ Boolean
122 123 124 125 126 127 128 |
# File 'lib/cli_tool/remote.rb', line 122 def if?(condition, &block) if_script = "if [ #{condition} ]\n#{"\t" * (@indent + 2)}then\n\n" if_script << Script.new(@indent + 2, environment: @environment).__send__(:instance_exec, &block).to_s(env: false) if_script << "\n#{"\t" * @indent}fi" @commands << if_script << '' self end |
#if_installed?(*packages) ⇒ Boolean
DPKG =#
62 |
# File 'lib/cli_tool/remote.rb', line 62 def if_installed?(*packages); check_installed(true, *packages) end |
#install(*packages) ⇒ Object
Apt Tools =#
38 |
# File 'lib/cli_tool/remote.rb', line 38 def install(*packages); apt(:install, *packages) end |
#purge(*packages) ⇒ Object
40 |
# File 'lib/cli_tool/remote.rb', line 40 def purge(*packages); apt(:purge, *packages) end |
#remote_install(*packages) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cli_tool/remote.rb', line 73 def remote_install(*packages) @remote_installed = 0 packages.each do |package| @remote_installed += 1 num = "#{@remote_installed}".rjust(3,'0') tmp = "/tmp/package#{num}.deb" curl(package, tmp, :sudo) dpkg_install(tmp) exec("rm -f #{tmp}", :sudo) end self end |
#remove(*packages) ⇒ Object
42 |
# File 'lib/cli_tool/remote.rb', line 42 def remove(*packages); apt(:remove, *packages) end |
#service(name, action) ⇒ Object
102 103 104 |
# File 'lib/cli_tool/remote.rb', line 102 def service(name, action) exec("service #{name} #{action}", :sudo) end |
#sudo(user = :root, &block) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/cli_tool/remote.rb', line 157 def sudo(user = :root, &block) sudo_script = %{sudo su -c "/bin/bash -i -s --" #{user} <<-SCRIPT\n} sudo_script << Script.new(@indent + 2, sudo: true, environment: @environment).__send__(:instance_exec, &block).to_s(heredoc: true) sudo_script << "\n#{"\t" * @indent}SCRIPT" @commands << sudo_script << '' self end |
#to_s(opts = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/cli_tool/remote.rb', line 165 def to_s(opts = {}) env = opts[:env] == false ? [] : (get_environment_exports << '') result = env.concat(@commands).map! do |line| ("\t" * @indent) + line end # Join the commands result = result.join("\n") if @indent < 3 result = result.gsub('$', '\$') result << "\n" end if opts[:heredoc] == true result = result.gsub('$', '\\\\\$') end result end |
#unless_installed?(*packages) ⇒ Boolean
64 |
# File 'lib/cli_tool/remote.rb', line 64 def unless_installed?(*packages); check_installed(false, *packages) end |
#update ⇒ Object
44 |
# File 'lib/cli_tool/remote.rb', line 44 def update; apt(:update) end |
#upgrade ⇒ Object
46 |
# File 'lib/cli_tool/remote.rb', line 46 def upgrade; apt(:upgrade) end |
#upgrade! ⇒ Object
48 |
# File 'lib/cli_tool/remote.rb', line 48 def upgrade!; apt(:'dist-upgrade') end |
#wget(from, to, sudo = false, sudouser = :root) ⇒ Object
Tools =#
90 91 92 93 94 |
# File 'lib/cli_tool/remote.rb', line 90 def wget(from, to, sudo = false, sudouser = :root) install(:wget) exec("wget -O #{to} #{from}", sudo, sudouser) self end |