Class: CliTool::Remote::Script

Inherits:
Object
  • Object
show all
Includes:
StdinOut
Defined in:
lib/cli_tool/remote.rb

Constant Summary

Constants included from StdinOut

StdinOut::ANSI_COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StdinOut

included

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

#commandsObject

Returns the value of attribute commands.



12
13
14
# File 'lib/cli_tool/remote.rb', line 12

def commands
  @commands
end

#environmentObject

Returns the value of attribute environment.



13
14
15
# File 'lib/cli_tool/remote.rb', line 13

def environment
  @environment
end

#indentObject

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

Returns:



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

Returns:



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

Returns:



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 =#

Returns:



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

Returns:



64
# File 'lib/cli_tool/remote.rb', line 64

def unless_installed?(*packages); check_installed(false, *packages) end

#updateObject



44
# File 'lib/cli_tool/remote.rb', line 44

def update;             apt(:update)             end

#upgradeObject



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