Skip to content
This repository was archived by the owner on Jan 8, 2019. It is now read-only.

Commit 402b01f

Browse files
vt0raespinosa
authored andcommitted
Copy jRuby users/groups script to new projects one (#1259)
* Copy jRuby users/groups script to new projects one * Cleanup on dirinfo_hash assignments - feedback
1 parent 7e3169b commit 402b01f

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

cookbooks/bcpc-hadoop/attributes/hdfs.rb

+20
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,23 @@
123123
hdfs['user']['space_quota'] = '30G'
124124
hdfs['groups']['space_quota'] = '50G'
125125
end
126+
127+
# Default HDFS Projects Configuration
128+
default['bcpc']['hadoop']['hdfs']['projects'].tap do |projects|
129+
projects['owner'] = 'hdfs'
130+
projects['group'] = 'supergroup'
131+
projects['perms'] = '1771'
132+
projects['space_quota'] =
133+
node['bcpc']['hadoop']['hdfs']['groups']['space_quota']
134+
projects['ns_quota'] = 'NO_QUOTA'
135+
projects['dirinfo'] = {
136+
### EXAMPLE:
137+
# project: {
138+
# owner: 'user',
139+
# group: 'group',
140+
# perms: '1710',
141+
# space_quota: '300T',
142+
# ns_quota: '10000000'
143+
# }
144+
}
145+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# create_projects_dirs.rb
2+
# -------
3+
# Creates HDFS directories and manages quota (if applicable)
4+
# Run this in an HBase jruby shell
5+
# -------
6+
# General usage:
7+
# hbase shell create_projects_dirs.rb dirinfo_file
8+
9+
include Java
10+
11+
require 'yaml'
12+
13+
import org.apache.hadoop.conf.Configuration
14+
import org.apache.hadoop.fs.FileSystem
15+
import org.apache.hadoop.fs.Path
16+
import org.apache.hadoop.fs.permission.FsPermission
17+
import org.apache.hadoop.hdfs.protocol.HdfsConstants
18+
19+
usage = 'hbase shell '\
20+
'create_projects_dirs.rb dirinfo_file'
21+
banner = "usage: #{usage}\n -- need "
22+
23+
config = {
24+
'dirinfo_file' => (ARGV[0] or raise "#{banner} dirinfo_file")
25+
}
26+
27+
# hdfs file system handle
28+
fs = FileSystem.newInstance(Configuration.new)
29+
30+
# load dirinfo from file
31+
dirinfo = YAML.load_file(config['dirinfo_file'])
32+
33+
dirinfo.each_pair do |dir, info|
34+
path = Path.new("/projects/#{dir}")
35+
puts "updating path: #{path}"
36+
37+
# enforce ownership and permissions
38+
owner = info[:owner]
39+
group = info[:groups]
40+
perms = FsPermission.new(info[:perms].to_i(8))
41+
42+
# create the directory
43+
fs.mkdirs(path)
44+
fs.setOwner(path, owner, group)
45+
fs.setPermission(path, perms)
46+
47+
# Takes a string with a unit-prefix
48+
# returns the result as a "long"
49+
# Examples are:
50+
# string2long(600) => 600
51+
# string2long('1') => 1
52+
# string2long('1k') => 1024
53+
# string2long('30T') => 32985348833280
54+
def string2long(s)
55+
s = s.to_s.downcase
56+
prefixes = {
57+
'' => 1,
58+
'k' => 1024,
59+
'm' => 1024**2,
60+
'g' => 1024**3,
61+
't' => 1024**4,
62+
'p' => 1024**5
63+
}
64+
65+
base = s[/[0-9.]*/].to_i
66+
prefix = prefixes[s[/[kmgtp]/] || '']
67+
68+
base * prefix
69+
end
70+
71+
# set space quota
72+
max_quota = HdfsConstants::QUOTA_RESET
73+
space_quota =
74+
if info[:space_quota] == 'NO_QUOTA'
75+
max_quota
76+
else
77+
string2long(info[:space_quota])
78+
end
79+
ns_quota =
80+
if info[:ns_quota] == 'NO_QUOTA'
81+
max_quota
82+
else
83+
string2long(info[:ns_quota])
84+
end
85+
fs.setQuota(path, ns_quota, space_quota)
86+
end
87+
88+
exit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Bcpc_Hadoop
2+
module Helper
3+
require 'yaml'
4+
include Chef::Mixin::ShellOut
5+
6+
def projects_dir_creation(dirinfo)
7+
file_cache = File.join(
8+
Chef::Config[:file_cache_path],
9+
'cookbooks/bcpc-hadoop',
10+
'files/default'
11+
)
12+
13+
# path to creation script and caches
14+
jruby_script = File.join(file_cache, 'create_projects_dirs.rb')
15+
dirinfo_cache = File.join(file_cache, 'dirinfo.yml')
16+
17+
# Build dirinfo cache
18+
# -------------------
19+
# use default space/ns quota if dir-specific quota unset
20+
dirinfo_hash = dirinfo.map do |dir, info|
21+
[
22+
dir,
23+
{
24+
owner: info['owner'] ||
25+
node['bcpc']['hadoop']['hdfs']['projects']['owner'],
26+
group: info['group'] ||
27+
node['bcpc']['hadoop']['hdfs']['projects']['group'],
28+
perms: info['perms'] ||
29+
node['bcpc']['hadoop']['hdfs']['projects']['perms'],
30+
space_quota: info['space_quota'] ||
31+
node['bcpc']['hadoop']['hdfs']['projects']['space_quota'],
32+
ns_quota: info['ns_quota'] ||
33+
node['bcpc']['hadoop']['hdfs']['projects']['ns_quota']
34+
}
35+
]
36+
end.to_h
37+
38+
# Save dirinfo cache
39+
File.open(dirinfo_cache, 'w+') { |f| f.puts(dirinfo_hash.to_yaml) }
40+
41+
# allow the hdfs user to read them
42+
File.chmod(0o0644, jruby_script)
43+
File.chmod(0o0644, dirinfo_cache)
44+
45+
# execute the jruby script
46+
jruby_shell_cmd = "hbase shell #{jruby_script} #{dirinfo_cache}"
47+
jruby_shell =
48+
Mixlib::ShellOut.new(jruby_shell_cmd, user: 'hdfs', timeout: 120)
49+
jruby_shell.run_command
50+
jruby_shell.error!
51+
end
52+
end
53+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Chef::Resource::RubyBlock.send(:include, Bcpc_Hadoop::Helper)
2+
3+
ruby_block 'hdfs_projects_directories' do
4+
block do
5+
projects_dir_creation(node['bcpc']['hadoop']['hdfs']['projects']['dirinfo'])
6+
end
7+
end

0 commit comments

Comments
 (0)