vcap_common 2.0.11 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,45 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__),'..'))
2
- $:.unshift(File.dirname(__FILE__))
3
- require 'user_pool_util'
4
- require 'user_ops'
5
- require 'subprocess'
6
-
7
- module VCAP
8
- class UserPool
9
- attr_accessor :free_users
10
- attr_accessor :busy_users
11
-
12
- def initialize(name, logger = nil)
13
- @logger = logger || Logger.new(STDOUT)
14
- UserPoolUtil.init
15
- @free_users = UserPoolUtil.open_pool(name)
16
- @busy_users = Hash.new
17
- @logger.debug("Initialized user pool #{name} with #{@free_users.size} users.")
18
- end
19
-
20
- def alloc_user
21
- user_name, user = @free_users.shift
22
- if user_name != nil
23
- @busy_users[user_name] = user
24
- else
25
- raise "out of users!!"
26
- end
27
- @logger.debug "alloc()'d user #{user_name}"
28
- user
29
- end
30
-
31
- def free_user(user)
32
- user_name = user[:user_name]
33
- if @busy_users.has_key?(user_name)
34
- VCAP::Subprocess.run("pkill -9 -u #{user_name}", 1)
35
- @busy_users.delete(user_name)
36
- @free_users[user_name] = user
37
- @logger.debug "free()'d user #{user_name}"
38
- else
39
- raise "invalid free user: #{user_name}"
40
- end
41
- end
42
- end
43
- end
44
-
45
-
@@ -1,107 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__))
2
-
3
- require 'logger'
4
- require 'vcap/subprocess'
5
- require 'user_ops'
6
-
7
- module VCAP
8
- module UserPoolUtil
9
- class << self
10
- def init(logger = nil)
11
- @logger = logger || Logger.new(STDOUT)
12
- end
13
-
14
- def user_from_num(name, num)
15
- "user-pool-#{name}-#{num}"
16
- end
17
-
18
- def group_from_name(name)
19
- "user-pool-#{name}"
20
- end
21
-
22
- def kill_group_procs(group_name)
23
- @logger.debug("killing all procs in group #{group_name}")
24
- #XXX -- fixme VCAP::Subprocess.run("pkill -9 -G #{group_name}" , 0)
25
- end
26
-
27
- def install_pool(name, size)
28
- raise ArgumentError("pool name must not contain dashes") if name =~ /-/
29
- group_name = group_from_name(name)
30
-
31
- @logger.info("Creating user pool #{name} with #{size} users.")
32
- if VCAP::UserOps.group_exists?(group_name)
33
- raise ArgumentError.new("group #{group_name} already exists")
34
- end
35
- VCAP::UserOps.install_group(group_name)
36
- kill_group_procs(group_name)
37
-
38
- begin
39
- 1.upto(size) do |number|
40
- user_name = user_from_num(name, number)
41
- if VCAP::UserOps.user_exists?(user_name)
42
- VCAP::UserOps.remove_user(user_name)
43
- @logger.warn("User reset occured for user #{user_name}!")
44
- end
45
- @logger.debug("installing user #{user_name}")
46
- VCAP::UserOps.install_user(user_name, group_name)
47
- end
48
- rescue => e
49
- @logger.error e.to_s
50
- @logger.error("pool creation failed, cleaning up")
51
- remove_pool(name)
52
- end
53
- end
54
-
55
- def remove_pool(name)
56
- @logger.info("Removing user pool #{name}.")
57
- group_name = group_from_name(name)
58
- kill_group_procs(group_name)
59
-
60
- Etc.passwd { |u|
61
- if u.name.split('-')[2] == name
62
- @logger.debug "removed user #{u.name}"
63
- VCAP::UserOps.remove_user(u.name)
64
- end
65
- }
66
- Etc.endpwent
67
-
68
- if VCAP::UserOps.group_exists?(group_name)
69
- VCAP::UserOps.remove_group(group_name)
70
- else
71
- @logger.warn "Pool group #{group_name} missing!!"
72
- end
73
- end
74
-
75
- def pool_exists?(name)
76
- group_name = group_from_name(name)
77
- VCAP::UserOps.group_exists?(group_name)
78
- end
79
-
80
- def open_pool(name)
81
- group_name = group_from_name(name)
82
- pool_users = Hash.new
83
- unless VCAP::UserOps.group_exists?(group_name)
84
- raise ArgumentError.new("no group named #{group_name} exists - can't open pool.")
85
- end
86
- Etc.passwd { |u|
87
- if u.name.split('-')[2] == name
88
- pool_users[u.name] = {:user_name => u.name, :uid => u.uid, :gid => u.gid}
89
- end
90
- }
91
- pool_users
92
- end
93
-
94
- def pool_list
95
- list = []
96
- Etc.group { |g|
97
- if ['user','pool'] == g.name.split('-')[0..1]
98
- list.push(g.name.split('-')[2])
99
- end
100
- }
101
- Etc.endgrent
102
- list.map {|name| "#{name} #{open_pool(name).size}"}
103
- end
104
-
105
- end
106
- end
107
- end