gluez 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gluez/context.rb CHANGED
@@ -8,16 +8,22 @@ module Gluez
8
8
  attr_reader :resources
9
9
 
10
10
  # A reference to the outer context
11
- attr_reader :parent
11
+ attr_reader :parent, :included_user_contexts
12
12
 
13
- def initialize(parent=nil, name=nil, &block)
13
+ def initialize(parent=nil, name=nil, user=nil, &block)
14
14
  @parent = parent
15
15
  @name = name
16
+ @user = user
16
17
 
17
18
  @resources = []
18
19
  @properties = {}
19
20
 
21
+ @included_user_contexts = []
22
+
20
23
  $gluez = self
24
+
25
+ self.include_user! "root" if self == self.root
26
+
21
27
  instance_eval(&block) if block
22
28
 
23
29
  if self.root == self
@@ -32,6 +38,18 @@ module Gluez
32
38
  self.parent ? self.parent.root : self
33
39
  end
34
40
 
41
+ def user
42
+ if @user
43
+ @user
44
+ else
45
+ @parent ? @parent.user : nil
46
+ end
47
+ end
48
+
49
+ def home_dir
50
+ self.user == "root" ? "/root" : "/home/#{self.user}"
51
+ end
52
+
35
53
  def default(name, name2)
36
54
  unless @properties.key?(name)
37
55
  if name2.is_a?(Symbol)
@@ -64,18 +82,98 @@ module Gluez
64
82
  @properties[name]
65
83
  end
66
84
 
67
- # Includes a recipe. A new context will be created. The passed block will be executed in the scope of the new context.
68
- def include(name, &block)
69
- Gluez::Context.new(self, name) do |c|
85
+ def include_library_recipe(library_dir, name, &block)
86
+ raise "include_recipe is only allowed in user context" if self == self.root
87
+ Gluez::Context.new(self, name, nil) do |c|
70
88
  c.instance_eval(&block) if block
71
- load "#{File.dirname($0)}/recipes/#{name}/#{name}.rb"
89
+ load "#{library_dir}/recipes/#{name}/#{name}.rb"
90
+ end
91
+ end
92
+
93
+ # Includes a recipe. A new context will be created. The passed block will be executed in the scope of the new context.
94
+ def include_recipe(name, &block)
95
+ self.include_library_recipe(File.dirname($0), name, &block)
96
+ end
97
+
98
+ def include_user!(name, &block)
99
+ self.include_user(name, true, &block)
100
+ end
101
+
102
+ def include_user(name, failsafe=false, &block)
103
+ raise "include_user is only allowed in root context" unless self == self.root
104
+ Gluez::Context.new(self, name, name) do |c|
105
+ if block
106
+ c.instance_eval(&block)
107
+ end
108
+
109
+ c.root.included_user_contexts << c
110
+
111
+ f = "#{File.dirname($0)}/users/#{name}.rb"
112
+
113
+ if File.exist?(f)
114
+ load(f)
115
+ else
116
+ raise "could not find user context file #{f}" unless failsafe
117
+ end
118
+
72
119
  end
73
120
  end
74
121
 
75
122
  # Loops through all resources, collect their generated code, format and return it.
76
123
  def generate(simulate)
77
124
  code = "#!/bin/bash"
125
+
126
+ root_user_context = @included_user_contexts.detect{|ctx| ctx.user == "root"}
127
+ raise "no root user context could be found" unless root_user_context
128
+
129
+ user_contexts = @included_user_contexts - [root_user_context]
130
+
131
+ existing_resources = Array.new(@resources)
132
+
133
+ root_user_context.instance_eval do |ctx|
134
+ user_contexts.each do |user_ctx|
135
+ create_group(user_ctx.user) do
136
+ gid user_ctx.get(:uid)
137
+ end
138
+
139
+ create_user(user_ctx.user) do
140
+ uid user_ctx.get(:uid)
141
+ gid user_ctx.get(:uid)
142
+ end
143
+
144
+ ['.gluez', '.gluez/path', 'tmp', 'backup', 'bin', '.ssh'].each do |dir_name|
145
+ dir(dir_name) do
146
+ as_user user_ctx.user
147
+ end
148
+ end
149
+
150
+ if user_ctx.get(:authorized_keys)
151
+ transfer "~/.ssh/authorized_keys" do
152
+ as_user user_ctx.user
153
+ chmod 400
154
+ content user_ctx.get(:authorized_keys).join("\n")
155
+ end
156
+ end
78
157
 
158
+ transfer "~/.profile" do
159
+ as_user user_ctx.user
160
+ chmod 644
161
+ content File.read("#{File.dirname(__FILE__)}/templates/profile.erb")
162
+ end
163
+
164
+ if user_ctx.get(:sudo)
165
+ transfer "/etc/sudoers.d/#{user_ctx.user}" do
166
+ chmod 440
167
+ content "#{user_ctx.user} ALL=(ALL) NOPASSWD: ALL"
168
+ end
169
+ end
170
+
171
+ end
172
+ end
173
+
174
+ # sort resources to always create users/groups first
175
+ @resources = (@resources - existing_resources) + existing_resources
176
+
79
177
  code += "\n" + @resources.collect do |res|
80
178
  res.generate(simulate).join("\n")
81
179
  end.join("\n")
@@ -84,19 +182,7 @@ module Gluez
84
182
  res.function_name
85
183
  end.join("\n")
86
184
 
87
- code = Gluez::format(code)
88
-
89
- if Gluez.options.include?("--ssh")
90
- code64 = Base64.encode64(code)
91
-
92
- cmd = %(code=\\"#{code64.strip}\\" && echo \\\$code | base64 -i -d - | /bin/bash)
93
- ssh = "ssh -t whale01 \"sudo su -l root -c '#{cmd}'\""
94
-
95
- puts ssh
96
- else
97
- puts code
98
- end
99
-
185
+ puts Gluez::format(code)
100
186
  end
101
187
 
102
188
  def self.load_resources
@@ -35,18 +35,32 @@ module Gluez
35
35
  @name = name
36
36
  @steps = []
37
37
 
38
+ @as_user = nil
39
+
38
40
  @notifies = []
39
41
  @subscriptions = []
40
42
 
41
43
  @mandatories = []
42
44
  @optionals = []
43
45
 
44
- self.optional :user, :default => "root"
46
+ # self.optional :user, :default => "root"
45
47
  self.optional :lazy, :default => false
46
48
 
47
49
  self.accessor :setup
48
50
  end
49
51
 
52
+ def as_user(user)
53
+ @as_user = user
54
+ end
55
+
56
+ def user
57
+ @as_user || @context.user
58
+ end
59
+
60
+ def home_dir
61
+ self.user == "root" ? "/root" : "/home/#{self.user}"
62
+ end
63
+
50
64
  def validate!
51
65
  @mandatories.each do |it|
52
66
  raise "no value for mandatory attribute #{it}" if self.send(it).nil?
@@ -1,4 +1,4 @@
1
- resource :group do
1
+ resource :create_group do
2
2
  mandatory :gid
3
3
 
4
4
  ready!
@@ -1,4 +1,4 @@
1
- resource :user do
1
+ resource :create_user do
2
2
  mandatory :uid
3
3
  mandatory :gid
4
4
 
@@ -2,8 +2,8 @@ resource :path do
2
2
  ready!
3
3
 
4
4
  steps do |step|
5
- link = self.name.gsub('/', '_').gsub('~', "/home/#{self.user}/.gluez/path/")
6
- target = self.name.gsub('~', "/home/#{self.user}")
5
+ link = self.name.gsub('/', '_').gsub('~', "#{home_dir}/.gluez/path/")
6
+ target = self.name.gsub('~', home_dir)
7
7
 
8
8
  step.checks << "-L #{link}"
9
9
  step.checks << "\\$(ls -al #{link} | awk '{print \\$10}' | grep #{target} | wc -l) -eq 1"
@@ -0,0 +1,33 @@
1
+ # ~/.profile: executed by the command interpreter for login shells.
2
+ # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
3
+ # exists.
4
+ # see /usr/share/doc/bash/examples/startup-files for examples.
5
+ # the files are located in the bash-doc package.
6
+
7
+ # the default umask is set in /etc/profile; for setting the umask
8
+ # for ssh logins, install and configure the libpam-umask package.
9
+ #umask 022
10
+
11
+ # if running bash
12
+ if [ -n "$BASH_VERSION" ]; then
13
+ # include .bashrc if it exists
14
+ if [ -f "$HOME/.bashrc" ]; then
15
+ . "$HOME/.bashrc"
16
+ fi
17
+ fi
18
+
19
+ # set PATH so it includes user's private bin if it exists
20
+ if [ -d "$HOME/bin" ] ; then
21
+ PATH="$HOME/bin:$PATH"
22
+ fi
23
+
24
+ if [ -d "$HOME/.gluez/path" ]; then
25
+ for entry in `ls $HOME/.gluez/path`; do
26
+ dir="$HOME/.gluez/path/$entry"
27
+ if [ -d "$dir" ]; then
28
+ PATH="${dir}:$PATH"
29
+ fi
30
+ done
31
+ fi
32
+
33
+ export PATH
data/lib/gluez.rb CHANGED
@@ -5,6 +5,9 @@ class Object
5
5
  def recipe(&block)
6
6
  $gluez.instance_eval(&block)
7
7
  end
8
+ def user(&block)
9
+ $gluez.instance_eval(&block)
10
+ end
8
11
  def resource(name, &block)
9
12
  Gluez::Context.register(name.to_s.underscore, &block)
10
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gluez
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-27 00:00:00.000000000Z
12
+ date: 2011-11-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: a server configuration toolkit
15
15
  email: jan.zimmek@web.de
@@ -21,12 +21,13 @@ files:
21
21
  - lib/gluez/resource.rb
22
22
  - lib/gluez/resources/bash.rb
23
23
  - lib/gluez/resources/bash_once.rb
24
+ - lib/gluez/resources/create_group.rb
25
+ - lib/gluez/resources/create_user.rb
24
26
  - lib/gluez/resources/dir.rb
25
27
  - lib/gluez/resources/disable.rb
26
28
  - lib/gluez/resources/enable.rb
27
29
  - lib/gluez/resources/file.rb
28
30
  - lib/gluez/resources/gem.rb
29
- - lib/gluez/resources/group.rb
30
31
  - lib/gluez/resources/link.rb
31
32
  - lib/gluez/resources/mount.rb
32
33
  - lib/gluez/resources/package.rb
@@ -37,7 +38,7 @@ files:
37
38
  - lib/gluez/resources/stop.rb
38
39
  - lib/gluez/resources/transfer.rb
39
40
  - lib/gluez/resources/umount.rb
40
- - lib/gluez/resources/user.rb
41
+ - lib/gluez/templates/profile.erb
41
42
  - lib/gluez.rb
42
43
  homepage:
43
44
  licenses: []
@@ -59,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  version: '0'
60
61
  requirements: []
61
62
  rubyforge_project:
62
- rubygems_version: 1.8.6
63
+ rubygems_version: 1.8.10
63
64
  signing_key:
64
65
  specification_version: 3
65
66
  summary: a system configuration toolkit