Module: Machines::Installation

Defined in:
lib/machines/installation.rb

Constant Summary collapse

APTGET_QUIET =
'apt-get -q -y'

Instance Method Summary collapse

Instance Method Details

#add_ppa(name, key_name) ⇒ Object

Adds a PPA source and updates apt

Parameters:

  • name (String)

    Name of the PPA

  • key_name (String)

    What to check in apt-key list to ensure it installed add_ppa 'mozillateam/firefox-stable', 'mozilla'



9
10
11
12
13
14
# File 'lib/machines/installation.rb', line 9

def add_ppa name, key_name
  [
    Command.new("add-apt-repository ppa:#{name}", "apt-key list | grep -i #{key_name} #{echo_result}"),
    update
  ]
end

#deb(source, options) ⇒ Object

Adds a DEB source

Parameters:

  • source (String)

    URL of the package. If YOUR_UBUNTU_VERSION_HERE is included then it is replaced by the Ubuntu version name

  • options (Hash)

Options Hash (options):



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/machines/installation.rb', line 25

def deb source, options
  command = "echo deb #{source} >> /etc/apt/sources.list"
  if source =~ /YOUR_UBUNTU_VERSION_HERE/
    command = "expr substr `cat /etc/lsb-release | grep DISTRIB_CODENAME` 18 20 | xargs -I YOUR_UBUNTU_VERSION_HERE #{command}"
  end
  [
    Command.new(command, check_string(source.gsub(/ .*$/, ''), '/etc/apt/sources.list')),
    Command.new("wget -q #{options[:key]} -O - | apt-key add -", "apt-key list | grep -i #{options[:name]} #{echo_result}"),
    update
  ]
end

#debconf(app, setting, type, value) ⇒ Object

Preseed debconf to allow silent installs

Parameters:

  • app (String)

    Name of application to configure

  • setting (String)

    The setting to set

  • type (String)

    Data type of the value

  • value

    The value to set (Ruby types supported)



42
43
44
45
46
# File 'lib/machines/installation.rb', line 42

def debconf app, setting, type, value
  command = "echo #{app} #{setting} #{type} #{value} | debconf-set-selections"
  check = "debconf-get-selections | grep #{app} #{echo_result}"
  Command.new(command, check)
end

#extract(package, options = {}) ⇒ Object

Download, extract, and remove an archive. Currently supports zip or tar.gz.

Parameters:

  • package (String)

    Package name to extract

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :to (Optional String)

    folder to clone or extract to (defaults to /usr/local/src)



52
53
54
55
56
57
58
# File 'lib/machines/installation.rb', line 52

def extract package, options = {}
  name = File.basename(package)
  cmd = package[/.zip/] ? 'unzip -qq' : 'tar -zxf'
  dir = cmd =~ /unzip/ ? File.basename(name, '.zip') : File.basename(name).gsub(/\.tar.*/, '')
  dest = options[:to] || '/usr/local/src'
  Command.new("cd #{dest} && wget #{package} && #{cmd} #{name} && rm #{name} && cd -", check_dir("#{File.join(dest, dir)}"))
end

#gem(package, options = {}) ⇒ Object

Install a gem

Parameters:

  • package (String)

    Name of the gem

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :version (String)

    Optional version number



64
65
66
67
# File 'lib/machines/installation.rb', line 64

def gem package, options = {}
  version =  " -v \"#{options[:version]}\"" if options[:version]
  Command.new("gem install #{package}#{version}", check_gem(package, options[:version]))
end

#gem_update(options = '') ⇒ Object

Update gems

Examples:

Update Rubygems

gem_update '--system'


72
73
74
# File 'lib/machines/installation.rb', line 72

def gem_update options = ''
  Command.new("gem update #{options}", nil)
end

#git_clone(url, options = {}) ⇒ Object

Clone a project from a Git repository

Parameters:

  • url (String)

    URL to clone

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :to (Optional String)

    Folder to clone to

  • :tag (Optional String)

    Checkout this tag after cloning (requires :to)

  • :branch (Optional String)

    Switch this branch when cloning

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
# File 'lib/machines/installation.rb', line 82

def git_clone url, options = {}
  raise ArgumentError.new('git_clone Must include a url and folder') if url.nil? || url.empty?
  raise ArgumentError.new('specifying :tag also requires :to') if options[:tag] && options[:to].nil?
  branch = "--branch #{options[:branch]} " if options[:branch]
  command = "git clone --quiet #{branch}#{url}"
  command << " #{options[:to]}" if options[:to]
  command = Command.new(command, check_dir(options[:to]))
  command = [command, Command.new("cd #{options[:to]} && git checkout #{options[:tag]}", "git name-rev --name-only HEAD | grep #{options[:tag]}")] if options[:tag]
  command
end

#install(packages, options = {}) ⇒ Object

Installs one or more packages using apt, deb or git clone and install.sh (Ignores architecture differences) (See extract to just uncompress tar.gz or zip files)

Parameters:

  • packages (Symbol, String, Array)

    can be: URL:: Download from the specified URL and run dpkg Array or string (with no URL):: Run apt to install specified packages in the array or string Packages are installed separately to aid progress feedback Ensure this is the main package as dpkg get-selections is used to validate installation install %w(build-essential libssl-dev mysql-server) #=> Installs apt packages install 'http://example.com/my_package.deb', :cleanup => true #=> Installs a deb using dpkg then removes the deb



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/machines/installation.rb', line 104

def install packages, options = {}
  if packages.is_a?(String)
    if packages =~ /^http:\/\//
      commands = []
      if packages =~ /\.deb$/i
        name = File.basename(packages)
        commands << Command.new("cd /tmp && wget #{packages} && dpkg -i --force-architecture #{name} && rm #{name} && cd -", nil)
      else
        commands << extract(packages, :to => '/tmp')
        name = File.basename(packages).gsub(/\.(tar|zip).*/, '')
        commands << Command.new("cd /tmp/#{name} && dpkg -i --force-architecture *.deb && cd - && rm -rf /tmp/#{name}", nil)
      end
      return commands
    else
      packages = [packages]
    end
  end

  if packages.is_a?(Array)
    commands = packages.map do |package|
      Command.new("#{APTGET_QUIET} install #{package}", check_package(package))
    end
  end
  commands
end

#uninstall(packages) ⇒ Object

Remove one or more packages

Parameters:

  • packages (Array)

    Packages to remove



132
133
134
135
136
# File 'lib/machines/installation.rb', line 132

def uninstall packages
  packages.map do |package|
    Command.new("#{APTGET_QUIET} purge #{package}", check_package(package, false))
  end
end

#updateObject



138
139
140
# File 'lib/machines/installation.rb', line 138

def update
  Command.new("#{APTGET_QUIET} update > /tmp/apt-update.log", check_string('Reading package lists', '/tmp/apt-update.log'))
end

#upgradeObject

Update, upgrade, autoremove, autoclean apt packages TODO: Check that check_command really checks the correct command with 'echo $?'



144
145
146
147
148
# File 'lib/machines/installation.rb', line 144

def upgrade
  %w(update upgrade autoremove autoclean).map do |command|
    Command.new("#{APTGET_QUIET} #{command}", check_command('echo $?', '0'))
  end
end