Skip to content

Commit 415b1a8

Browse files
marcoceppiAdam Stokes
authored and
Adam Stokes
committed
Modernize layer to use new primitives (#11)
1 parent 027c9e8 commit 415b1a8

File tree

8 files changed

+236
-211
lines changed

8 files changed

+236
-211
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ is ready to be used.
99

1010
# api
1111

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

1414
Example,
1515

1616
```python
1717

18-
from rubylib import bundle, gem, ruby_dist_dir
18+
from charms.layer.ruby import bundle, gem, ruby_dist_dir
1919

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

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

3636
```
37-
libxml2-dev
38-
libyaml-dev
37+
options:
38+
basic:
39+
packages:
40+
- libxml2-dev
41+
- libyaml-dev
3942
```
4043

4144
This layer will pick up those dependencies in addition to the required packages

layer.yaml

+20-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
includes: ['layer:basic']
1+
includes: ['layer:basic']
2+
tactics: ['tactics.ruby.DependenciesTxtTactic']
3+
options:
4+
basic:
5+
packages:
6+
- build-essential
7+
- libreadline-dev
8+
- libssl-dev
9+
- libgmp-dev
10+
- libffi-dev
11+
- libyaml-dev
12+
- libxslt-dev
13+
- zlib1g-dev
14+
- libgdbm-dev
15+
- openssl
16+
- libicu-dev
17+
- cmake
18+
- pkg-config
19+
- libxml2-dev
20+
- libncurses5-dev

lib/charms/layer/ruby.py

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

Comments
 (0)