|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from shutil import rmtree |
| 4 | +from multiprocessing import cpu_count |
| 5 | +from shell import shell |
| 6 | +import requests |
| 7 | + |
| 8 | +from charmhelpers.core import hookenv |
| 9 | +from charmhelpers.core.host import chdir |
| 10 | +from charmhelpers.fetch import apt_install |
| 11 | + |
| 12 | +config = hookenv.config() |
| 13 | +WORKDIR = '/tmp/ruby' |
| 14 | + |
| 15 | + |
| 16 | +# HELPERS --------------------------------------------------------------------- |
| 17 | +def set_proxy(cmd): |
| 18 | + """ Take a string to be executed by shell and add proxy if needed |
| 19 | + """ |
| 20 | + http_proxy = config.get('http-proxy') |
| 21 | + if http_proxy: |
| 22 | + cmd = "env http_proxy='{}' {}".format(http_proxy, cmd) |
| 23 | + return cmd |
| 24 | + |
| 25 | + |
| 26 | +def git(cmd): |
| 27 | + """ simple git wrapper |
| 28 | + """ |
| 29 | + if not os.path.isfile('/usr/bin/git'): |
| 30 | + apt_install(['git']) |
| 31 | + shell_cmd = set_proxy("git {}".format(cmd)) |
| 32 | + sh = shell(shell_cmd) |
| 33 | + if sh.code > 0: |
| 34 | + hookenv.status_set('blocked', |
| 35 | + 'Problem with Ruby: {}'.format(sh.errors())) |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + |
| 39 | +def tarball_exists(url): |
| 40 | + """ Checks that ruby tarball exists on mirror |
| 41 | + """ |
| 42 | + resp = requests.head(url) |
| 43 | + if resp.status_code == 200: |
| 44 | + return True |
| 45 | + return False |
| 46 | + |
| 47 | + |
| 48 | +def compile_ruby(): |
| 49 | + with chdir(WORKDIR): |
| 50 | + cmds = [ |
| 51 | + 'env RUBY_CFLAGS="-O3" ./configure --prefix=/usr ' |
| 52 | + '--disable-install-rdoc', |
| 53 | + 'make -j{}'.format(cpu_count()), |
| 54 | + 'make install' |
| 55 | + ] |
| 56 | + |
| 57 | + for cmd in cmds: |
| 58 | + hookenv.log('Running compile command: {}'.format(cmd)) |
| 59 | + sh = shell(cmd, record_output=False) |
| 60 | + if sh.code > 0: |
| 61 | + hookenv.status_set('blocked', |
| 62 | + 'Problem with Ruby: {}'.format(sh.errors())) |
| 63 | + hookenv.log("Problem with Ruby: {}".format(sh.errors())) |
| 64 | + sys.exit(1) |
| 65 | + hookenv.status_set('maintenance', 'Installing Ruby completed.') |
| 66 | + |
| 67 | + |
| 68 | +def download_ruby(): |
| 69 | + """ Downloads ruby tarball, puts it in /tmp |
| 70 | + """ |
| 71 | + url = os.path.join(config['ruby-mirror'], |
| 72 | + 'ruby-{}.tar.gz'.format(config['ruby-version'])) |
| 73 | + if not tarball_exists(url): |
| 74 | + hookenv.status_set( |
| 75 | + 'blocked', |
| 76 | + 'Unable to find {} for download, please check your ' |
| 77 | + 'mirror and version'.format(url)) |
| 78 | + hookenv.log('Unable to find {} for download, please check your ' |
| 79 | + 'mirror and version'.format(url)) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + hookenv.status_set('maintenance', |
| 83 | + 'Installing Ruby {}'.format(url)) |
| 84 | + |
| 85 | + shell_cmd = set_proxy('wget -q -O /tmp/ruby.tar.gz {}'.format(url)) |
| 86 | + sh = shell(shell_cmd) |
| 87 | + if sh.code > 0: |
| 88 | + hookenv.status_set('blocked', |
| 89 | + 'Problem downlading Ruby: {}'.format(sh.errors())) |
| 90 | + hookenv.log('Problem downlading Ruby: {}'.format(sh.errors())) |
| 91 | + sys.exit(1) |
| 92 | + |
| 93 | + |
| 94 | +def extract_ruby(): |
| 95 | + if os.path.exists(WORKDIR): |
| 96 | + rmtree(WORKDIR) |
| 97 | + os.makedirs(WORKDIR) |
| 98 | + with chdir('/tmp'): |
| 99 | + cmd = ('tar xf ruby.tar.gz -C {} --strip-components=1'.format(WORKDIR)) |
| 100 | + sh = shell(cmd) |
| 101 | + if sh.code > 0: |
| 102 | + hookenv.status_set( |
| 103 | + 'blocked', |
| 104 | + 'Problem extracting ruby: {}:{}'.format(cmd, |
| 105 | + sh.errors())) |
| 106 | + hookenv.log('Problem extracting ruby: {}:{}'.format(cmd, |
| 107 | + sh.errors())) |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + |
| 111 | +def ruby_dist_dir(): |
| 112 | + """ Absolute path of Ruby application dir |
| 113 | +
|
| 114 | + Returns: |
| 115 | + Absolute string of ruby application directory |
| 116 | + """ |
| 117 | + config = hookenv.config() |
| 118 | + return os.path.join(config['app-path']) |
| 119 | + |
| 120 | + |
| 121 | +def bundle(cmd): |
| 122 | + """ Runs bundle |
| 123 | +
|
| 124 | + Usage: |
| 125 | +
|
| 126 | + bundle('install') |
| 127 | + bundle('exec rails s') |
| 128 | + bundle('rake db:create RAILS_ENV=production') |
| 129 | +
|
| 130 | + Arguments: |
| 131 | + cmd: Command to run can be string or list |
| 132 | +
|
| 133 | + Returns: |
| 134 | + Will halt on error |
| 135 | + """ |
| 136 | + sh = shell('which bundler') |
| 137 | + if sh.code > 0: |
| 138 | + gem('install -N bundler') |
| 139 | + hookenv.status_set('maintenance', 'Running Bundler') |
| 140 | + with chdir(ruby_dist_dir()): |
| 141 | + if not isinstance(cmd, str): |
| 142 | + hookenv.log('{} must be a string'.format(cmd), 'error') |
| 143 | + sys.exit(1) |
| 144 | + shell_cmd = set_proxy("bundle {} -j{}".format(cmd, cpu_count())) |
| 145 | + sh = shell(shell_cmd, record_output=False) |
| 146 | + |
| 147 | + if sh.code > 0: |
| 148 | + hookenv.status_set("blocked", "Ruby error: {}".format(sh.errors())) |
| 149 | + hookenv.log("Ruby error: {}".format(sh.errors())) |
| 150 | + sys.exit(1) |
| 151 | + |
| 152 | + |
| 153 | +def gem(cmd): |
| 154 | + """ Runs gem |
| 155 | +
|
| 156 | + Usage: |
| 157 | +
|
| 158 | + gem('install bundler') |
| 159 | +
|
| 160 | + Arguments: |
| 161 | + cmd: Command to run can be string or list |
| 162 | +
|
| 163 | + Returns: |
| 164 | + Will halt on error |
| 165 | + """ |
| 166 | + hookenv.status_set('maintenance', 'Running Gem') |
| 167 | + if not isinstance(cmd, str): |
| 168 | + hookenv.log('{} must be a string'.format(cmd), 'error') |
| 169 | + sys.exit(1) |
| 170 | + shell_cmd = set_proxy("gem {}".format(cmd)) |
| 171 | + with chdir(ruby_dist_dir()): |
| 172 | + sh = shell(shell_cmd, record_output=False) |
| 173 | + if sh.code > 0: |
| 174 | + hookenv.status_set("blocked", "Ruby error: {}".format(sh.errors())) |
| 175 | + hookenv.log("Ruby error: {}".format(sh.errors())) |
| 176 | + sys.exit(1) |
0 commit comments