ood_support 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +165 -0
- data/Rakefile +2 -0
- data/lib/ood_support.rb +9 -0
- data/lib/ood_support/group.rb +54 -0
- data/lib/ood_support/process.rb +28 -0
- data/lib/ood_support/user.rb +89 -0
- data/lib/ood_support/version.rb +4 -0
- data/ood_support.gemspec +23 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ab9a0231c41e21de5992f0ae3bcf0be3ed19638
|
4
|
+
data.tar.gz: fce3ab5ef3dc87c42cb8a8af09136aa80705ed7e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0611200202ea6a276b89927499a45c130d54a06d5a6eb865bcf2c413ddefa85458ea3cb887f639e3dea7d8b1a8616d5cdf27dec54b05f8a3a834cf8a86f51878
|
7
|
+
data.tar.gz: 98f312e86afbe33ea1b6d1b5d1e9830feb10a28e149d3cfdbe7692744a7280397b2d2911f55b946daea9cc17a6ebc5ba25cf0e27bf8c0f5843261cdb9d8ef38e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015-2016 Ohio Supercomputer Center
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
# OodSupport
|
2
|
+
|
3
|
+
Open OnDemand gem that provides a set of support objects to interface with the
|
4
|
+
local OS installed on the HPC Center's web node.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'ood_support'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install ood_support
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### User
|
25
|
+
|
26
|
+
Provides a simplified system-level user object that can be used to determine
|
27
|
+
user's id, groups, shell, home directory...
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'ood_support'
|
31
|
+
|
32
|
+
# Generate OodSupport::User object from user name
|
33
|
+
u = OodSupport::User.new 'user1'
|
34
|
+
|
35
|
+
# User name
|
36
|
+
u.name
|
37
|
+
#=> "user1"
|
38
|
+
|
39
|
+
# User id
|
40
|
+
u.id
|
41
|
+
#=> 1000
|
42
|
+
|
43
|
+
# User shell
|
44
|
+
u.shell
|
45
|
+
#=> "/bin/bash"
|
46
|
+
|
47
|
+
# Array of groups user is in
|
48
|
+
u.groups
|
49
|
+
#=> [<OodSupport::Group ...>, <OodSupport::Group ...>, ...]
|
50
|
+
|
51
|
+
# Names of groups user is in
|
52
|
+
u.groups.map(&:name)
|
53
|
+
#=> ["primary_group", "group1", "group2"]
|
54
|
+
|
55
|
+
# Name of user's primary group
|
56
|
+
u.group.name
|
57
|
+
#=> "primary_group"
|
58
|
+
|
59
|
+
# Whether user is in group called "group15"
|
60
|
+
u.in_group? "group15"
|
61
|
+
#=> false
|
62
|
+
|
63
|
+
# Use it in a string
|
64
|
+
puts "Hello #{u}!"
|
65
|
+
#=> "Hello user1!"
|
66
|
+
```
|
67
|
+
|
68
|
+
You can generate the `OodSupport::User` object from a user name, user id,
|
69
|
+
another `OodSupport::User`, or from the running process:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
require 'ood_support'
|
73
|
+
|
74
|
+
# Generate OodSupport::User object from user name
|
75
|
+
u1 = OodSupport::User.new 'user1'
|
76
|
+
|
77
|
+
# Generate OodSupport::User object from user id
|
78
|
+
u2 = OodSupport::User.new 1000
|
79
|
+
|
80
|
+
# Generate OodSupport::User object from another object
|
81
|
+
u3 = OodSupport::User.new u1
|
82
|
+
|
83
|
+
# Generate OodSupport::User from running process
|
84
|
+
me = OodSupport::User.new
|
85
|
+
```
|
86
|
+
|
87
|
+
### Group
|
88
|
+
|
89
|
+
Provides a simplified system-level group object that can be used to determine
|
90
|
+
group id and group name.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'ood_support'
|
94
|
+
|
95
|
+
# Generate OodSupport::Group object from group name
|
96
|
+
g = OodSupport::Group.new 'group1'
|
97
|
+
|
98
|
+
# Get group id
|
99
|
+
g.id
|
100
|
+
#=> 100
|
101
|
+
|
102
|
+
# Get group name
|
103
|
+
g.name
|
104
|
+
#=> 'group1'
|
105
|
+
|
106
|
+
# Generate OodSupport::User object from user name
|
107
|
+
u = OodSupport::User.new 'user1'
|
108
|
+
|
109
|
+
# Sort the list of groups user is in
|
110
|
+
u.groups.sort.map(&:name)
|
111
|
+
#=> ["a_group", "b_group", "c_group"]
|
112
|
+
```
|
113
|
+
|
114
|
+
You can generate the `OodSupport::Group` object from a group name, group id,
|
115
|
+
another `OodSupport::Group` object, or from the running process:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
require 'ood_support'
|
119
|
+
|
120
|
+
# Generate OodSupport::Group object from group name
|
121
|
+
g1 = OodSupport::Group.new 'group1'
|
122
|
+
|
123
|
+
# Generate OodSupport::Group object from group id
|
124
|
+
g2 = OodSupport::Group.new 100
|
125
|
+
|
126
|
+
# Generate OodSupport::Group object from another object
|
127
|
+
g3 = OodSupport::Group.new g1
|
128
|
+
|
129
|
+
# Generate OodSupport::Group from running process
|
130
|
+
me = OodSupport::Group.new
|
131
|
+
```
|
132
|
+
|
133
|
+
### Process
|
134
|
+
|
135
|
+
Provides a simplified interface to the running process that can be used to
|
136
|
+
determine owner of the process as well as whether the owner's groups have
|
137
|
+
changed since the process started.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
require 'ood_support'
|
141
|
+
|
142
|
+
# Get owner of process
|
143
|
+
OodSupport::Process.user
|
144
|
+
#=> <OodSupport::User ...>
|
145
|
+
|
146
|
+
# Get primary group of process
|
147
|
+
OodSupport::Process.group
|
148
|
+
#=> <OodSupport::Group ...>
|
149
|
+
|
150
|
+
# Get list of groups process is currently in
|
151
|
+
OodSupport::Process.groups
|
152
|
+
#=> [<OodSupport::Group ...>, <OodSupport::Group ...>, ...]
|
153
|
+
|
154
|
+
# Whether owner's groups changed since process started
|
155
|
+
OodSupport::Process.groups_changed?
|
156
|
+
#=> false
|
157
|
+
```
|
158
|
+
|
159
|
+
## Contributing
|
160
|
+
|
161
|
+
1. Fork it ( https://github.com/[my-github-username]/ood_support/fork )
|
162
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
163
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
164
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
165
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/ood_support.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'etc'
|
2
|
+
|
3
|
+
module OodSupport
|
4
|
+
# A helper object describing a Unix group's details
|
5
|
+
class Group
|
6
|
+
include Comparable
|
7
|
+
|
8
|
+
# The id of the group
|
9
|
+
# @return [Fixnum] the group id
|
10
|
+
attr_reader :id
|
11
|
+
|
12
|
+
# The name of the group
|
13
|
+
# @return [String] the group name
|
14
|
+
attr_reader :name
|
15
|
+
|
16
|
+
# @param group [Fixnum, #to_s] the group id or name
|
17
|
+
def initialize(group = Process.group)
|
18
|
+
if group.is_a?(Fixnum)
|
19
|
+
@id = group
|
20
|
+
@name = Etc.getgrgid(@id).name
|
21
|
+
else
|
22
|
+
@name = group.to_s
|
23
|
+
@id = Etc.getgrnam(@name).gid
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# The comparison operator for sorting values
|
28
|
+
# @param other [Group] group to compare against
|
29
|
+
# @return [Fixnum] how groups compare
|
30
|
+
def <=>(other)
|
31
|
+
name <=> other
|
32
|
+
end
|
33
|
+
|
34
|
+
# Checks whether two Group objects have the same group as well as that the
|
35
|
+
# object is in the Group class
|
36
|
+
# @param other [Group] group to compare against
|
37
|
+
# @return [Boolean] whether same objects
|
38
|
+
def eql?(other)
|
39
|
+
other.is_a?(Group) && id == other.id
|
40
|
+
end
|
41
|
+
|
42
|
+
# Generates a hash value for this object
|
43
|
+
# @return [Fixnum] hash value of object
|
44
|
+
def hash
|
45
|
+
id.hash
|
46
|
+
end
|
47
|
+
|
48
|
+
# Convert object to string using group name as string value
|
49
|
+
# @return [String] the group name
|
50
|
+
def to_s
|
51
|
+
name
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module OodSupport
|
2
|
+
# A helper module that describes the current running process
|
3
|
+
module Process
|
4
|
+
# Owner of current process
|
5
|
+
# @return [User] owner of process
|
6
|
+
def self.user
|
7
|
+
User.new ::Process.uid
|
8
|
+
end
|
9
|
+
|
10
|
+
# Primary group of current process
|
11
|
+
# @return [Group] group of process
|
12
|
+
def self.group
|
13
|
+
Group.new ::Process.gid
|
14
|
+
end
|
15
|
+
|
16
|
+
# List of groups current process is in
|
17
|
+
# @return [Array<Group>] list of groups for process
|
18
|
+
def self.groups
|
19
|
+
::Process.groups.map {|g| Group.new g}
|
20
|
+
end
|
21
|
+
|
22
|
+
# Whether user's groups changed since running process
|
23
|
+
# @return [Boolean] whether groups changed
|
24
|
+
def self.groups_changed?
|
25
|
+
groups.sort != user.groups.sort
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'etc'
|
3
|
+
|
4
|
+
module OodSupport
|
5
|
+
# A helper object used to query information about a system user from the
|
6
|
+
# local host
|
7
|
+
class User
|
8
|
+
include Comparable
|
9
|
+
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
# @!method name
|
13
|
+
# The user name
|
14
|
+
# @return [String] the user name
|
15
|
+
# @!method uid
|
16
|
+
# The user's id
|
17
|
+
# @return [Integer] the user id
|
18
|
+
# @!method gecos
|
19
|
+
# The user's real name
|
20
|
+
# @return [String] the real name
|
21
|
+
# @!method dir
|
22
|
+
# The user's home directory
|
23
|
+
# @return [String] the home path
|
24
|
+
# @!method shell
|
25
|
+
# The user's shell
|
26
|
+
# @return [String] the shell
|
27
|
+
delegate [:name, :uid, :gecos, :dir, :shell] => :@passwd
|
28
|
+
|
29
|
+
alias_method :id, :uid
|
30
|
+
|
31
|
+
# @param user [Fixnum, #to_s] user id or name
|
32
|
+
def initialize(user = Process.user)
|
33
|
+
@passwd = user.is_a?(Fixnum) ? Etc.getpwuid(user) : Etc.getpwnam(user.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Determine whether user is part of specified group
|
37
|
+
# @param group [Group] group to check
|
38
|
+
# @return [Boolean] whether user is in group
|
39
|
+
def in_group?(group)
|
40
|
+
groups.include? Group.new(group)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Provide primary group of user
|
44
|
+
# @return [Group] primary group of user
|
45
|
+
def group
|
46
|
+
groups.first
|
47
|
+
end
|
48
|
+
|
49
|
+
# List of all groups that user belongs to
|
50
|
+
# @return [Array<String>] list of groups user is in
|
51
|
+
def groups
|
52
|
+
@groups ||= get_groups
|
53
|
+
end
|
54
|
+
|
55
|
+
# The comparison operator for sorting values
|
56
|
+
# @param other [User] user to compare against
|
57
|
+
# @return [Fixnum] how users compare
|
58
|
+
def <=>(other)
|
59
|
+
name <=> other
|
60
|
+
end
|
61
|
+
|
62
|
+
# Checks whether two User objects have the same user as well as that the
|
63
|
+
# object is in the User class
|
64
|
+
# @param other [User] user to compare against
|
65
|
+
# @return [Boolean] whether same objects
|
66
|
+
def eql?(other)
|
67
|
+
other.is_a?(User) && id == other.id
|
68
|
+
end
|
69
|
+
|
70
|
+
# Generates a hash value for this object
|
71
|
+
# @return [Fixnum] hash value of object
|
72
|
+
def hash
|
73
|
+
id.hash
|
74
|
+
end
|
75
|
+
|
76
|
+
# Convert object to string using user name as string value
|
77
|
+
# @return [String] the user name
|
78
|
+
def to_s
|
79
|
+
name
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
# Use `id` to get list of groups as the /etc/group file can give
|
84
|
+
# erroneous results
|
85
|
+
def get_groups
|
86
|
+
`id -G #{name}`.split(' ').map {|g| Group.new(g.to_i)}
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/ood_support.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ood_support/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ood_support"
|
8
|
+
spec.version = OodSupport::VERSION
|
9
|
+
spec.authors = ["Jeremy Nicklas"]
|
10
|
+
spec.email = ["jnicklas@osc.edu"]
|
11
|
+
spec.summary = %q{Open OnDemand gem that adds useful support methods for an HPC Center.}
|
12
|
+
spec.description = %q{Provides an interface to working with the local OS installed on the HPC Center's web node.}
|
13
|
+
spec.homepage = "https://github.com/OSC/ood_support"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ood_support
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Nicklas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Provides an interface to working with the local OS installed on the HPC
|
42
|
+
Center's web node.
|
43
|
+
email:
|
44
|
+
- jnicklas@osc.edu
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/ood_support.rb
|
55
|
+
- lib/ood_support/group.rb
|
56
|
+
- lib/ood_support/process.rb
|
57
|
+
- lib/ood_support/user.rb
|
58
|
+
- lib/ood_support/version.rb
|
59
|
+
- ood_support.gemspec
|
60
|
+
homepage: https://github.com/OSC/ood_support
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Open OnDemand gem that adds useful support methods for an HPC Center.
|
84
|
+
test_files: []
|
85
|
+
has_rdoc:
|