PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/salt/modules/rvm.py

https://gitlab.com/ricardo.hernandez/salt
Python | 458 lines | 419 code | 19 blank | 20 comment | 8 complexity | 6723df5437e2d8f835a64541bdb82494 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Manage ruby installations and gemsets with RVM, the Ruby Version Manager.
  4. '''
  5. from __future__ import absolute_import
  6. # Import python libs
  7. import re
  8. import os
  9. import logging
  10. # Import salt libs
  11. import salt.utils
  12. from salt.exceptions import CommandExecutionError
  13. log = logging.getLogger(__name__)
  14. # Don't shadow built-in's.
  15. __func_alias__ = {
  16. 'list_': 'list'
  17. }
  18. __opts__ = {
  19. 'rvm.runas': None,
  20. }
  21. def _get_rvm_location(runas=None):
  22. if runas:
  23. runas_home = os.path.expanduser('~{0}'.format(runas))
  24. rvmpath = '{0}/.rvm/bin/rvm'.format(runas_home)
  25. if os.path.exists(rvmpath):
  26. return [rvmpath]
  27. return ['/usr/local/rvm/bin/rvm']
  28. def _rvm(command, runas=None, cwd=None):
  29. if runas is None:
  30. runas = __salt__['config.option']('rvm.runas')
  31. if not is_installed(runas):
  32. return False
  33. cmd = _get_rvm_location(runas) + command
  34. ret = __salt__['cmd.run_all'](cmd,
  35. runas=runas,
  36. cwd=cwd,
  37. python_shell=False)
  38. if ret['retcode'] == 0:
  39. return ret['stdout']
  40. return False
  41. def _rvm_do(ruby, command, runas=None, cwd=None):
  42. return _rvm([ruby or 'default', 'do'] + command, runas=runas, cwd=cwd)
  43. def is_installed(runas=None):
  44. '''
  45. Check if RVM is installed.
  46. CLI Example:
  47. .. code-block:: bash
  48. salt '*' rvm.is_installed
  49. '''
  50. try:
  51. return __salt__['cmd.has_exec'](_get_rvm_location(runas)[0])
  52. except IndexError:
  53. return False
  54. def install(runas=None):
  55. '''
  56. Install RVM system-wide
  57. runas
  58. The user under which to run the rvm installer script. If not specified,
  59. then it be run as the user under which Salt is running.
  60. CLI Example:
  61. .. code-block:: bash
  62. salt '*' rvm.install
  63. '''
  64. # RVM dependencies on Ubuntu 10.04:
  65. # bash coreutils gzip bzip2 gawk sed curl git-core subversion
  66. installer = 'https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer'
  67. ret = __salt__['cmd.run_all'](
  68. # the RVM installer automatically does a multi-user install when it is
  69. # invoked with root privileges
  70. 'curl -Ls {installer} | bash -s stable'.format(installer=installer),
  71. runas=runas,
  72. python_shell=True
  73. )
  74. if ret['retcode'] > 0:
  75. msg = 'Error encountered while downloading the RVM installer'
  76. if ret['stderr']:
  77. msg += '. stderr follows:\n\n' + ret['stderr']
  78. raise CommandExecutionError(msg)
  79. return True
  80. def install_ruby(ruby, runas=None):
  81. '''
  82. Install a ruby implementation.
  83. ruby
  84. The version of ruby to install
  85. runas
  86. The user under which to run rvm. If not specified, then rvm will be run
  87. as the user under which Salt is running.
  88. CLI Example:
  89. .. code-block:: bash
  90. salt '*' rvm.install_ruby 1.9.3-p385
  91. '''
  92. # MRI/RBX/REE dependencies for Ubuntu 10.04:
  93. # build-essential openssl libreadline6 libreadline6-dev curl
  94. # git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0
  95. # libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libc6-dev
  96. # libncurses5-dev automake libtool bison subversion ruby
  97. if runas and runas != 'root':
  98. _rvm(['autolibs', 'disable', ruby], runas=runas)
  99. return _rvm(['install', '--disable-binary', ruby], runas=runas)
  100. else:
  101. return _rvm(['install', ruby], runas=runas)
  102. def reinstall_ruby(ruby, runas=None):
  103. '''
  104. Reinstall a ruby implementation
  105. ruby
  106. The version of ruby to reinstall
  107. runas
  108. The user under which to run rvm. If not specified, then rvm will be run
  109. as the user under which Salt is running.
  110. CLI Example:
  111. .. code-block:: bash
  112. salt '*' rvm.reinstall_ruby 1.9.3-p385
  113. '''
  114. return _rvm(['reinstall', ruby], runas=runas)
  115. def list_(runas=None):
  116. '''
  117. List all rvm-installed rubies
  118. runas
  119. The user under which to run rvm. If not specified, then rvm will be run
  120. as the user under which Salt is running.
  121. CLI Example:
  122. .. code-block:: bash
  123. salt '*' rvm.list
  124. '''
  125. rubies = []
  126. output = _rvm(['list'], runas=runas)
  127. if output:
  128. regex = re.compile(r'^[= ]([*> ]) ([^- ]+)-([^ ]+) \[ (.*) \]')
  129. for line in output.splitlines():
  130. match = regex.match(line)
  131. if match:
  132. rubies.append([
  133. match.group(2), match.group(3), match.group(1) == '*'
  134. ])
  135. return rubies
  136. def set_default(ruby, runas=None):
  137. '''
  138. Set the default ruby
  139. ruby
  140. The version of ruby to make the default
  141. runas
  142. The user under which to run rvm. If not specified, then rvm will be run
  143. as the user under which Salt is running.
  144. CLI Example:
  145. .. code-block:: bash
  146. salt '*' rvm.set_default 2.0.0
  147. '''
  148. return _rvm(['alias', 'create', 'default', ruby], runas=runas)
  149. def get(version='stable', runas=None):
  150. '''
  151. Update RVM
  152. version : stable
  153. Which version of RVM to install, (e.g. stable or head)
  154. CLI Example:
  155. .. code-block:: bash
  156. salt '*' rvm.get
  157. '''
  158. return _rvm(['get', version], runas=runas)
  159. def wrapper(ruby_string, wrapper_prefix, runas=None, *binaries):
  160. '''
  161. Install RVM wrapper scripts
  162. ruby_string
  163. Ruby/gemset to install wrappers for
  164. wrapper_prefix
  165. What to prepend to the name of the generated wrapper binaries
  166. runas
  167. The user under which to run rvm. If not specified, then rvm will be run
  168. as the user under which Salt is running.
  169. binaries : None
  170. The names of the binaries to create wrappers for. When nothing is
  171. given, wrappers for ruby, gem, rake, irb, rdoc, ri and testrb are
  172. generated.
  173. CLI Example:
  174. .. code-block:: bash
  175. salt '*' rvm.wrapper <ruby_string> <wrapper_prefix>
  176. '''
  177. cmd = ['wrapper', ruby_string, wrapper_prefix]
  178. cmd.extend(binaries)
  179. return _rvm(cmd, runas=runas)
  180. def rubygems(ruby, version, runas=None):
  181. '''
  182. Installs a specific rubygems version in the given ruby
  183. ruby
  184. The ruby for which to install rubygems
  185. version
  186. The version of rubygems to install, or 'remove' to use the version that
  187. ships with 1.9
  188. runas
  189. The user under which to run rvm. If not specified, then rvm will be run
  190. as the user under which Salt is running.
  191. CLI Example:
  192. .. code-block:: bash
  193. salt '*' rvm.rubygems 2.0.0 1.8.24
  194. '''
  195. return _rvm_do(ruby, ['rubygems', version], runas=runas)
  196. def gemset_create(ruby, gemset, runas=None):
  197. '''
  198. Creates a gemset.
  199. ruby
  200. The ruby version for which to create the gemset
  201. gemset
  202. The name of the gemset to create
  203. runas
  204. The user under which to run rvm. If not specified, then rvm will be run
  205. as the user under which Salt is running.
  206. CLI Example:
  207. .. code-block:: bash
  208. salt '*' rvm.gemset_create 2.0.0 foobar
  209. '''
  210. return _rvm_do(ruby, ['rvm', 'gemset', 'create', gemset], runas=runas)
  211. def gemset_list(ruby='default', runas=None):
  212. '''
  213. List all gemsets for the given ruby.
  214. ruby : default
  215. The ruby version for which to list the gemsets
  216. runas
  217. The user under which to run rvm. If not specified, then rvm will be run
  218. as the user under which Salt is running.
  219. CLI Example:
  220. .. code-block:: bash
  221. salt '*' rvm.gemset_list
  222. '''
  223. gemsets = []
  224. output = _rvm_do(ruby, ['rvm', 'gemset', 'list'], runas=runas)
  225. if output:
  226. regex = re.compile('^ ([^ ]+)')
  227. for line in output.splitlines():
  228. match = regex.match(line)
  229. if match:
  230. gemsets.append(match.group(1))
  231. return gemsets
  232. def gemset_delete(ruby, gemset, runas=None):
  233. '''
  234. Delete a gemset
  235. ruby
  236. The ruby version to which the gemset belongs
  237. gemset
  238. The gemset to delete
  239. runas
  240. The user under which to run rvm. If not specified, then rvm will be run
  241. as the user under which Salt is running.
  242. CLI Example:
  243. .. code-block:: bash
  244. salt '*' rvm.gemset_delete 2.0.0 foobar
  245. '''
  246. return _rvm_do(ruby,
  247. ['rvm', '--force', 'gemset', 'delete', gemset],
  248. runas=runas)
  249. def gemset_empty(ruby, gemset, runas=None):
  250. '''
  251. Remove all gems from a gemset.
  252. ruby
  253. The ruby version to which the gemset belongs
  254. gemset
  255. The gemset to empty
  256. runas
  257. The user under which to run rvm. If not specified, then rvm will be run
  258. as the user under which Salt is running.
  259. CLI Example:
  260. .. code-block:: bash
  261. salt '*' rvm.gemset_empty 2.0.0 foobar
  262. '''
  263. return _rvm_do(ruby,
  264. ['rvm', '--force', 'gemset', 'empty', gemset],
  265. runas=runas)
  266. def gemset_copy(source, destination, runas=None):
  267. '''
  268. Copy all gems from one gemset to another.
  269. source
  270. The name of the gemset to copy, complete with ruby version
  271. destination
  272. The destination gemset
  273. runas
  274. The user under which to run rvm. If not specified, then rvm will be run
  275. as the user under which Salt is running.
  276. CLI Example:
  277. .. code-block:: bash
  278. salt '*' rvm.gemset_copy foobar bazquo
  279. '''
  280. return _rvm(['gemset', 'copy', source, destination], runas=runas)
  281. def gemset_list_all(runas=None):
  282. '''
  283. List all gemsets for all installed rubies.
  284. Note that you must have set a default ruby before this can work.
  285. runas
  286. The user under which to run rvm. If not specified, then rvm will be run
  287. as the user under which Salt is running.
  288. CLI Example:
  289. .. code-block:: bash
  290. salt '*' rvm.gemset_list_all
  291. '''
  292. gemsets = {}
  293. current_ruby = None
  294. output = _rvm_do('default', ['rvm', 'gemset', 'list_all'], runas=runas)
  295. if output:
  296. gems_regex = re.compile('^ ([^ ]+)')
  297. gemset_regex = re.compile('^gemsets for ([^ ]+)')
  298. for line in output.splitlines():
  299. match = gemset_regex.match(line)
  300. if match:
  301. current_ruby = match.group(1)
  302. gemsets[current_ruby] = []
  303. match = gems_regex.match(line)
  304. if match:
  305. gemsets[current_ruby].append(match.group(1))
  306. return gemsets
  307. def do(ruby, command, runas=None, cwd=None): # pylint: disable=C0103
  308. '''
  309. Execute a command in an RVM controlled environment.
  310. ruby
  311. Which ruby to use
  312. command
  313. The rvm command to execute
  314. runas
  315. The user under which to run rvm. If not specified, then rvm will be run
  316. as the user under which Salt is running.
  317. cwd
  318. The directory from which to run the rvm command. Defaults to the user's
  319. home directory.
  320. CLI Example:
  321. .. code-block:: bash
  322. salt '*' rvm.do 2.0.0 <command>
  323. '''
  324. try:
  325. command = salt.utils.shlex_split(command)
  326. except AttributeError:
  327. command = salt.utils.shlex_split(str(command))
  328. return _rvm_do(ruby, command, runas=runas, cwd=cwd)