Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Modernize layer to use new primitives #11

Merged
merged 1 commit into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ is ready to be used.

# api

All helper modules are found in `lib/rubylib.py`
All helper modules are found in `lib/charms/layer/ruby.py` available `from charms.layer import ruby`

Example,

```python

from rubylib import bundle, gem, ruby_dist_dir
from charms.layer.ruby import bundle, gem, ruby_dist_dir

print(ruby_dist_dir())
# /var/lib/juju/agents/unit-ruby-0/charm/dist
Expand All @@ -31,11 +31,14 @@ def install_deps():
# configuration

You can add additional debian packages to your ruby install by editing a
`dependencies.txt` and placing the package names seperated by newline.
`layer.yaml` and placing the package names as follows

```
libxml2-dev
libyaml-dev
options:
basic:
packages:
- libxml2-dev
- libyaml-dev
```

This layer will pick up those dependencies in addition to the required packages
Expand Down
21 changes: 20 additions & 1 deletion layer.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
includes: ['layer:basic']
includes: ['layer:basic']
tactics: ['tactics.ruby.DependenciesTxtTactic']
options:
basic:
packages:
- build-essential
- libreadline-dev
- libssl-dev
- libgmp-dev
- libffi-dev
- libyaml-dev
- libxslt-dev
- zlib1g-dev
- libgdbm-dev
- openssl
- libicu-dev
- cmake
- pkg-config
- libxml2-dev
- libncurses5-dev
176 changes: 176 additions & 0 deletions lib/charms/layer/ruby.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import os
import sys
from shutil import rmtree
from multiprocessing import cpu_count
from shell import shell
import requests

from charmhelpers.core import hookenv
from charmhelpers.core.host import chdir
from charmhelpers.fetch import apt_install

config = hookenv.config()
WORKDIR = '/tmp/ruby'


# HELPERS ---------------------------------------------------------------------
def set_proxy(cmd):
""" Take a string to be executed by shell and add proxy if needed
"""
http_proxy = config.get('http-proxy')
if http_proxy:
cmd = "env http_proxy='{}' {}".format(http_proxy, cmd)
return cmd


def git(cmd):
""" simple git wrapper
"""
if not os.path.isfile('/usr/bin/git'):
apt_install(['git'])
shell_cmd = set_proxy("git {}".format(cmd))
sh = shell(shell_cmd)
if sh.code > 0:
hookenv.status_set('blocked',
'Problem with Ruby: {}'.format(sh.errors()))
sys.exit(1)


def tarball_exists(url):
""" Checks that ruby tarball exists on mirror
"""
resp = requests.head(url)
if resp.status_code == 200:
return True
return False


def compile_ruby():
with chdir(WORKDIR):
cmds = [
'env RUBY_CFLAGS="-O3" ./configure --prefix=/usr '
'--disable-install-rdoc',
'make -j{}'.format(cpu_count()),
'make install'
]

for cmd in cmds:
hookenv.log('Running compile command: {}'.format(cmd))
sh = shell(cmd, record_output=False)
if sh.code > 0:
hookenv.status_set('blocked',
'Problem with Ruby: {}'.format(sh.errors()))
hookenv.log("Problem with Ruby: {}".format(sh.errors()))
sys.exit(1)
hookenv.status_set('maintenance', 'Installing Ruby completed.')


def download_ruby():
""" Downloads ruby tarball, puts it in /tmp
"""
url = os.path.join(config['ruby-mirror'],
'ruby-{}.tar.gz'.format(config['ruby-version']))
if not tarball_exists(url):
hookenv.status_set(
'blocked',
'Unable to find {} for download, please check your '
'mirror and version'.format(url))
hookenv.log('Unable to find {} for download, please check your '
'mirror and version'.format(url))
sys.exit(1)

hookenv.status_set('maintenance',
'Installing Ruby {}'.format(url))

shell_cmd = set_proxy('wget -q -O /tmp/ruby.tar.gz {}'.format(url))
sh = shell(shell_cmd)
if sh.code > 0:
hookenv.status_set('blocked',
'Problem downlading Ruby: {}'.format(sh.errors()))
hookenv.log('Problem downlading Ruby: {}'.format(sh.errors()))
sys.exit(1)


def extract_ruby():
if os.path.exists(WORKDIR):
rmtree(WORKDIR)
os.makedirs(WORKDIR)
with chdir('/tmp'):
cmd = ('tar xf ruby.tar.gz -C {} --strip-components=1'.format(WORKDIR))
sh = shell(cmd)
if sh.code > 0:
hookenv.status_set(
'blocked',
'Problem extracting ruby: {}:{}'.format(cmd,
sh.errors()))
hookenv.log('Problem extracting ruby: {}:{}'.format(cmd,
sh.errors()))
sys.exit(1)


def ruby_dist_dir():
""" Absolute path of Ruby application dir

Returns:
Absolute string of ruby application directory
"""
config = hookenv.config()
return os.path.join(config['app-path'])


def bundle(cmd):
""" Runs bundle

Usage:

bundle('install')
bundle('exec rails s')
bundle('rake db:create RAILS_ENV=production')

Arguments:
cmd: Command to run can be string or list

Returns:
Will halt on error
"""
sh = shell('which bundler')
if sh.code > 0:
gem('install -N bundler')
hookenv.status_set('maintenance', 'Running Bundler')
with chdir(ruby_dist_dir()):
if not isinstance(cmd, str):
hookenv.log('{} must be a string'.format(cmd), 'error')
sys.exit(1)
shell_cmd = set_proxy("bundle {} -j{}".format(cmd, cpu_count()))
sh = shell(shell_cmd, record_output=False)

if sh.code > 0:
hookenv.status_set("blocked", "Ruby error: {}".format(sh.errors()))
hookenv.log("Ruby error: {}".format(sh.errors()))
sys.exit(1)


def gem(cmd):
""" Runs gem

Usage:

gem('install bundler')

Arguments:
cmd: Command to run can be string or list

Returns:
Will halt on error
"""
hookenv.status_set('maintenance', 'Running Gem')
if not isinstance(cmd, str):
hookenv.log('{} must be a string'.format(cmd), 'error')
sys.exit(1)
shell_cmd = set_proxy("gem {}".format(cmd))
with chdir(ruby_dist_dir()):
sh = shell(shell_cmd, record_output=False)
if sh.code > 0:
hookenv.status_set("blocked", "Ruby error: {}".format(sh.errors()))
hookenv.log("Ruby error: {}".format(sh.errors()))
sys.exit(1)
Loading