Class: Kitchen::Yansible::Tools::Install

Inherits:
Object
  • Object
show all
Includes:
Exec
Defined in:
lib/kitchen-yansible/tools/install.rb,
lib/kitchen-yansible/tools/install/rhel.rb,
lib/kitchen-yansible/tools/install/amazon.rb,
lib/kitchen-yansible/tools/install/darwin.rb,
lib/kitchen-yansible/tools/install/debian.rb,
lib/kitchen-yansible/tools/install/fedora.rb,
lib/kitchen-yansible/tools/install/windows.rb

Direct Known Subclasses

Darwin, Debian, RHEL, Windows

Defined Under Namespace

Classes: Amazon, Darwin, Debian, Fedora, RHEL, Windows

Constant Summary collapse

BINARY_INSTALL_PREFIX =
'/usr/local/bin'
BINARY_DEFAULT_PREFIX =
'/usr/bin'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Exec

#check_command, #command_exists, #detect_platform, #execute_local_command, #local_command_exists, #local_command_path, #print_cmd_error, #print_cmd_parameters, #sudo, #sudo_env, #unindent

Constructor Details

#initialize(config, platform) ⇒ Install

Returns a new instance of Install.



42
43
44
45
# File 'lib/kitchen-yansible/tools/install.rb', line 42

def initialize(config, platform)
  @config = config
  @platform = platform
end

Class Method Details

.make(config, platform) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kitchen-yansible/tools/install.rb', line 47

def self.make(config, platform)

  case platform.downcase
  when /.*(debian|ubuntu).*/
    return Debian.new(config, platform)
  when /.*(redhat|centos|oracle).*/
    return RHEL.new(config, platform)
  when /.*fedora.*/
    return Fedora.new(config, platform)
  when /.*amazon.*/
    return Amazon.new(config, platform)
  # when 'suse', 'opensuse', 'sles'
  #   return Suse.new(platform, config)
   when 'darwin', 'mac', 'macos', 'macosx'
     return Darwin.new(config, platform)
  # when 'alpine'
  #   return Alpine.new(config, platform)
  # when 'openbsd'
  #   return Openbsd.new(config, platform)
  # when 'freebsd'
  #   return Freebsd.new(config, platform)
  when /.*windows.*/
    return Windows.new(config, platform)
  else
    raise "Unsupported platform - '#{platform.to_s}'!"
  end
end

Instance Method Details

#alternatives_commandObject



150
151
152
# File 'lib/kitchen-yansible/tools/install.rb', line 150

def alternatives_command
  "#{sudo('alternatives')}"
end

#ansible_alternatives(install_prefix, sandbox_path) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/kitchen-yansible/tools/install.rb', line 154

def ansible_alternatives(install_prefix, sandbox_path)
  """
    #{command_exists("#{sandbox_path}/venv/bin/ansible")} && {
      #{alternatives_command} --install #{install_prefix}/ansible ansible #{sandbox_path}/venv/bin/ansible 100 \\
        --slave #{install_prefix}/ansible-config ansible-config #{sandbox_path}/venv/bin/ansible-config \\
        --slave #{install_prefix}/ansible-connection ansible-connection #{sandbox_path}/venv/bin/ansible-connection \\
        --slave #{install_prefix}/ansible-console ansible-console #{sandbox_path}/venv/bin/ansible-console \\
        --slave #{install_prefix}/ansible-doc ansible-doc #{sandbox_path}/venv/bin/ansible-doc \\
        --slave #{install_prefix}/ansible-galaxy ansible-galaxy #{sandbox_path}/venv/bin/ansible-galaxy \\
        --slave #{install_prefix}/ansible-inventory ansible-inventory #{sandbox_path}/venv/bin/ansible-inventory \\
        --slave #{install_prefix}/ansible-playbook ansible-playbook #{sandbox_path}/venv/bin/ansible-playbook \\
        --slave #{install_prefix}/ansible-pull ansible-pull #{sandbox_path}/venv/bin/ansible-pull \\
        --slave #{install_prefix}/ansible-test ansible-test #{sandbox_path}/venv/bin/ansible-test \\
        --slave #{install_prefix}/ansible-vault ansible-vault #{sandbox_path}/venv/bin/ansible-vault
    } || {
      echo '===> Ansible is not installed, exiting now. <==='
      exit 1
    }

    echo 'Check alternatives validity'
    #{command_exists("#{install_prefix}/ansible")} || {
      echo '===> Ansible alternative is incorrectly installed, exiting now. <==='
      exit 1
    }
  """
end

#ansible_versionObject



107
108
109
# File 'lib/kitchen-yansible/tools/install.rb', line 107

def ansible_version
  (@config[:ansible_version] && @config[:ansible_version] != 'latest') ? @config[:ansible_version].to_s : ''
end

#get_python_versionObject



121
122
123
# File 'lib/kitchen-yansible/tools/install.rb', line 121

def get_python_version
  "python -c 'import sys; print(\"\".join(map(str, sys.version_info[:#{python_version_size}])))'"
end

#install_ansible_pip(sandbox_path) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kitchen-yansible/tools/install.rb', line 137

def install_ansible_pip(sandbox_path)
  """
    installAnsiblePip () {
      echo \"Installing Ansible via Pip\"

      virtualenv #{sandbox_path}/venv
      #{sandbox_path}/venv/bin/pip install $PIP_ARGS --upgrade pip setuptools
      #{sandbox_path}/venv/bin/pip install $PIP_ARGS #{pip_required_packages.join(' ')}
      #{ansible_alternatives(BINARY_INSTALL_PREFIX, sandbox_path)}
    }
  """
end

#install_packageObject



133
134
135
# File 'lib/kitchen-yansible/tools/install.rb', line 133

def install_package
  "#{package_manager} install -y"
end

#local_installObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kitchen-yansible/tools/install.rb', line 94

def local_install
  """
    #{preinstall_command}
    #{search_alternatives}
    #{install_python}
    #{install_ruby}

    preInstall
    installPython
    installRuby
  """
end

#package_managerObject



125
126
127
# File 'lib/kitchen-yansible/tools/install.rb', line 125

def package_manager
  "#{sudo_env('yum')}"
end

#pip_required_packagesObject



115
116
117
118
119
# File 'lib/kitchen-yansible/tools/install.rb', line 115

def pip_required_packages
  [
    "ansible#{pip_version(ansible_version)}",
  ]
end

#pip_version(version) ⇒ Object



111
112
113
# File 'lib/kitchen-yansible/tools/install.rb', line 111

def pip_version(version)
  version.empty? ? '' : "==#{version}"
end

#python_version_sizeObject



38
39
40
# File 'lib/kitchen-yansible/tools/install.rb', line 38

def python_version_size
  1
end

#remote_installObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kitchen-yansible/tools/install.rb', line 75

def remote_install
  """
    #{preinstall_command}
    #{search_alternatives}
    #{install_python}
    #{install_virtualenv}
    #{install_ruby}
    #{install_ansible_pip('/tmp/ansible')}

    preInstall
    installPython
    installVirtualenv
    installRuby
    #{command_exists('ansible')} && {
      ansible --version|head -n1|grep -i 'ansible #{ansible_version}' &>/dev/null || installAnsiblePip
    } || installAnsiblePip
  """
end

#ruby_alternatives(install_prefix, alternative_path) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/kitchen-yansible/tools/install.rb', line 181

def ruby_alternatives(install_prefix, alternative_path)
  """
    #{command_exists("#{alternative_path}/ruby")} && {
      #{alternatives_command} --install #{install_prefix}/ruby ruby #{alternative_path}/ruby 100 \\
        --slave #{install_prefix}/erb erb #{alternative_path}/erb \\
        --slave #{install_prefix}/gem gem #{alternative_path}/gem \\
        --slave #{install_prefix}/irb irb #{alternative_path}/irb \\
        --slave #{install_prefix}/rdoc rdoc #{alternative_path}/rdoc \\
        --slave #{install_prefix}/ri ri #{alternative_path}/ri

      #{alternatives_command} --set ruby #{alternative_path}/ruby
    } || {
      echo '===> Ruby is not installed, exiting now. <==='
      exit 1
    }

    echo 'Check alternatives validity'
    #{command_exists("#{install_prefix}/ruby")} || {
      echo '===> Ruby alternative is incorrectly installed, exiting now. <==='
      exit 1
    }
  """
end

#search_alternativesObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/kitchen-yansible/tools/install.rb', line 205

def search_alternatives
  """
    searchAlternatives() {
      binaryCmd=$1
      #{command_exists("${binaryCmd}")} || {
        alternateCmd=$(ls -1A #{BINARY_DEFAULT_PREFIX}/${binaryCmd}*|grep -v \"${binaryCmd}$\"|sort|head -n1)
        test -n \"${alternateCmd}\" && {
          echo \"Attempt to install '${alternateCmd}' as an alternative.\"
          #{command_exists("${alternateCmd}")} && {
            #{alternatives_command} --install #{BINARY_DEFAULT_PREFIX}/${binaryCmd} ${binaryCmd} $(#{check_command("${alternateCmd}")}) 100
            #{alternatives_command} --set ${binaryCmd} $(#{check_command("${alternateCmd}")})
          }
        }
      }
    }
  """
end

#update_cacheObject



129
130
131
# File 'lib/kitchen-yansible/tools/install.rb', line 129

def update_cache
  "#{package_manager} makecache"
end

#update_pathObject



223
224
225
226
227
228
229
230
231
# File 'lib/kitchen-yansible/tools/install.rb', line 223

def update_path
  """
    updatePath () {
      #{sudo('grep')} secure_path /etc/sudoers.d/ansible &> /dev/null || {
        #{sudo('echo')} 'Defaults    secure_path = /usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' | #{sudo('tee')} -a /etc/sudoers.d/ansible
      }
    }
  """
end