wright 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/NEWS +5 -0
- data/README.md +3 -3
- data/Rakefile +0 -14
- data/lib/wright.rb +2 -1
- data/lib/wright/config.rb +11 -14
- data/lib/wright/dry_run.rb +7 -8
- data/lib/wright/dsl.rb +13 -11
- data/lib/wright/logger.rb +17 -16
- data/lib/wright/provider.rb +8 -8
- data/lib/wright/provider/directory.rb +5 -5
- data/lib/wright/provider/file.rb +5 -5
- data/lib/wright/provider/package.rb +9 -8
- data/lib/wright/provider/package/apt.rb +7 -9
- data/lib/wright/provider/symlink.rb +9 -9
- data/lib/wright/resource.rb +24 -24
- data/lib/wright/resource/directory.rb +15 -14
- data/lib/wright/resource/file.rb +15 -14
- data/lib/wright/resource/package.rb +14 -13
- data/lib/wright/resource/symlink.rb +11 -10
- data/lib/wright/util.rb +17 -19
- data/lib/wright/util/color.rb +14 -15
- data/lib/wright/util/file.rb +50 -55
- data/lib/wright/util/file_permissions.rb +30 -23
- data/lib/wright/util/recursive_autoloader.rb +10 -12
- data/lib/wright/util/stolen_from_activesupport.rb +42 -43
- data/lib/wright/util/user.rb +19 -22
- data/lib/wright/version.rb +2 -2
- metadata +6 -5
@@ -3,11 +3,11 @@ require 'fileutils'
|
|
3
3
|
|
4
4
|
module Wright
|
5
5
|
class Provider
|
6
|
-
#
|
6
|
+
# Symlink provider. Used as a provider for {Resource::Symlink}.
|
7
7
|
class Symlink < Wright::Provider
|
8
|
-
#
|
8
|
+
# Creates or updates the symlink.
|
9
9
|
#
|
10
|
-
#
|
10
|
+
# @return [void]
|
11
11
|
def create
|
12
12
|
if exist?
|
13
13
|
symlink = symlink_to_s(@resource.name, @resource.to)
|
@@ -20,9 +20,9 @@ module Wright
|
|
20
20
|
@updated = true
|
21
21
|
end
|
22
22
|
|
23
|
-
#
|
23
|
+
# Removes the symlink.
|
24
24
|
#
|
25
|
-
#
|
25
|
+
# @return [void]
|
26
26
|
def remove
|
27
27
|
if ::File.exist?(link_name) && !::File.symlink?(link_name)
|
28
28
|
fail "'#{link_name}' is not a symlink"
|
@@ -38,11 +38,11 @@ module Wright
|
|
38
38
|
|
39
39
|
private
|
40
40
|
|
41
|
-
#
|
41
|
+
# Checks if the specified link exists.
|
42
42
|
#
|
43
|
-
# Returns true if the link exists and points to the specified
|
44
|
-
# and false otherwise.
|
45
|
-
def exist?
|
43
|
+
# Returns true if the link exists and points to the specified
|
44
|
+
# target and false otherwise.
|
45
|
+
def exist?
|
46
46
|
::File.symlink?(link_name) &&
|
47
47
|
::File.readlink(link_name) == link_to
|
48
48
|
end
|
data/lib/wright/resource.rb
CHANGED
@@ -4,11 +4,11 @@ require 'wright/logger'
|
|
4
4
|
require 'wright/dry_run'
|
5
5
|
|
6
6
|
module Wright
|
7
|
-
#
|
7
|
+
# Resource base class.
|
8
8
|
class Resource
|
9
|
-
#
|
9
|
+
# Initializes a Resource.
|
10
10
|
#
|
11
|
-
# name
|
11
|
+
# @param name [String] the name of the resource
|
12
12
|
def initialize(name = nil)
|
13
13
|
@name = name
|
14
14
|
@resource_name = Util.class_to_resource_name(self.class).to_sym
|
@@ -18,16 +18,15 @@ module Wright
|
|
18
18
|
@ignore_failure = false
|
19
19
|
end
|
20
20
|
|
21
|
-
#
|
21
|
+
# @return [Symbol] the name of the method to be run by {#run_action}
|
22
22
|
attr_accessor :action
|
23
23
|
|
24
|
-
#
|
24
|
+
# @return [Bool] the ignore_failure attribute
|
25
25
|
attr_accessor :ignore_failure
|
26
26
|
|
27
|
-
#
|
28
|
-
#
|
29
|
-
# Examples
|
27
|
+
# @return [String] the resource's name attribute
|
30
28
|
#
|
29
|
+
# @example
|
31
30
|
# foo = Wright::Resource::Symlink.new('/tmp/fstab')
|
32
31
|
# foo.name
|
33
32
|
# # => "/tmp/fstab"
|
@@ -38,22 +37,21 @@ module Wright
|
|
38
37
|
# # => "/tmp/passwd"
|
39
38
|
attr_accessor :name
|
40
39
|
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# Examples
|
40
|
+
# @return [Symbol] a compact resource name
|
44
41
|
#
|
42
|
+
# @example
|
45
43
|
# foo = Wright::Resource::Symlink.new
|
46
44
|
# foo.resource_name
|
47
45
|
# # => :symlink
|
48
46
|
attr_reader :resource_name
|
49
47
|
|
50
|
-
#
|
48
|
+
# Sets an update action for a resource.
|
51
49
|
#
|
52
|
-
# on_update
|
53
|
-
#
|
50
|
+
# @param on_update [Proc, #call] the block that is called when the
|
51
|
+
# resource is updated.
|
54
52
|
#
|
55
|
-
#
|
56
|
-
#
|
53
|
+
# @return [void]
|
54
|
+
# @raise [ArgumentError] if on_update is not callable
|
57
55
|
def on_update=(on_update)
|
58
56
|
if on_update.respond_to?(:call) || on_update.nil?
|
59
57
|
@on_update = on_update
|
@@ -62,28 +60,29 @@ module Wright
|
|
62
60
|
end
|
63
61
|
end
|
64
62
|
|
65
|
-
#
|
66
|
-
#
|
67
|
-
# Examples
|
63
|
+
# Runs the resource's current action.
|
68
64
|
#
|
65
|
+
# @example
|
69
66
|
# fstab = Wright::Resource::Symlink.new('/tmp/fstab')
|
70
67
|
# fstab.action = :remove
|
71
68
|
# fstab.run_action
|
69
|
+
#
|
70
|
+
# @return the return value of the current action
|
72
71
|
def run_action
|
73
72
|
send @action if @action
|
74
73
|
end
|
75
74
|
|
76
75
|
private
|
77
76
|
|
78
|
-
#
|
77
|
+
# @api public
|
78
|
+
# Marks a code block that might update a resource.
|
79
79
|
#
|
80
80
|
# Usually this method is called in the definition of a new
|
81
81
|
# resource class in order to mark those methods that should be
|
82
82
|
# able to trigger update actions. Runs the current update action
|
83
83
|
# if the provider was updated by the block method.
|
84
84
|
#
|
85
|
-
#
|
86
|
-
#
|
85
|
+
# @example
|
87
86
|
# class BalloonAnimal < Wright::Provider
|
88
87
|
# def inflate
|
89
88
|
# puts "It's a giraffe!"
|
@@ -101,8 +100,9 @@ module Wright
|
|
101
100
|
# balloon = Balloon.new.inflate
|
102
101
|
# # => true
|
103
102
|
#
|
104
|
-
#
|
105
|
-
|
103
|
+
# @return [Bool] true if the provider was updated and false
|
104
|
+
# otherwise
|
105
|
+
def might_update_resource
|
106
106
|
begin
|
107
107
|
yield
|
108
108
|
rescue => e
|
@@ -3,16 +3,15 @@ require 'wright/dsl'
|
|
3
3
|
|
4
4
|
module Wright
|
5
5
|
class Resource
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# Examples
|
6
|
+
# Directory resource, represents a directory.
|
9
7
|
#
|
8
|
+
# @example
|
10
9
|
# dir = Wright::Resource::Directory.new('/tmp/foobar')
|
11
10
|
# dir.create
|
12
11
|
class Directory < Wright::Resource
|
13
|
-
#
|
12
|
+
# Initializes a Directory.
|
14
13
|
#
|
15
|
-
# name
|
14
|
+
# @param name [String] the directory's name
|
16
15
|
def initialize(name)
|
17
16
|
super
|
18
17
|
@mode = nil
|
@@ -21,13 +20,13 @@ module Wright
|
|
21
20
|
@action = :create
|
22
21
|
end
|
23
22
|
|
24
|
-
#
|
23
|
+
# @return [String, Integer] the directory's mode
|
25
24
|
attr_accessor :mode
|
26
25
|
|
27
|
-
#
|
26
|
+
# @return [String] the directory's owner
|
28
27
|
attr_reader :owner
|
29
28
|
|
30
|
-
#
|
29
|
+
# Sets the directory's owner.
|
31
30
|
def owner=(owner)
|
32
31
|
target_owner, target_group =
|
33
32
|
Wright::Util::User.owner_to_owner_group(owner)
|
@@ -35,26 +34,28 @@ module Wright
|
|
35
34
|
@group = target_group unless target_group.nil?
|
36
35
|
end
|
37
36
|
|
38
|
-
#
|
37
|
+
# @return [String] the directory's group
|
39
38
|
attr_reader :group
|
40
39
|
|
41
|
-
#
|
40
|
+
# Sets the directory's group.
|
42
41
|
def group=(group)
|
43
42
|
@group = Wright::Util::User.group_to_gid(group)
|
44
43
|
end
|
45
44
|
|
46
|
-
#
|
45
|
+
# Creates or updates the directory.
|
47
46
|
#
|
48
|
-
#
|
47
|
+
# @return [Bool] true if the directory was updated and false
|
48
|
+
# otherwise
|
49
49
|
def create
|
50
50
|
might_update_resource do
|
51
51
|
@provider.create
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
#
|
55
|
+
# Removes the directory.
|
56
56
|
#
|
57
|
-
#
|
57
|
+
# @return [Bool] true if the directory was updated and false
|
58
|
+
# otherwise
|
58
59
|
def remove
|
59
60
|
might_update_resource do
|
60
61
|
@provider.remove
|
data/lib/wright/resource/file.rb
CHANGED
@@ -3,29 +3,28 @@ require 'wright/dsl'
|
|
3
3
|
|
4
4
|
module Wright
|
5
5
|
class Resource
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# Examples
|
6
|
+
# Symlink resource, represents a symlink.
|
9
7
|
#
|
8
|
+
# @example
|
10
9
|
# file = Wright::Resource::File.new('/tmp/foo')
|
11
10
|
# file.content = 'bar'
|
12
11
|
# file.create
|
13
12
|
class File < Wright::Resource
|
14
|
-
#
|
13
|
+
# @return [String] the file's intended content
|
15
14
|
attr_accessor :content
|
16
15
|
|
17
|
-
#
|
16
|
+
# @return [String] the file's intended group.
|
18
17
|
attr_accessor :group
|
19
18
|
|
20
|
-
#
|
19
|
+
# @return [String, Integer] the file's intended mode
|
21
20
|
attr_accessor :mode
|
22
21
|
|
23
|
-
#
|
22
|
+
# @return [String] the file's intended owner
|
24
23
|
attr_reader :owner
|
25
24
|
|
26
|
-
#
|
25
|
+
# Initializes a File.
|
27
26
|
#
|
28
|
-
# name
|
27
|
+
# @param name [String] the file's name
|
29
28
|
def initialize(name)
|
30
29
|
super
|
31
30
|
@content = nil
|
@@ -35,7 +34,7 @@ module Wright
|
|
35
34
|
@action = :create
|
36
35
|
end
|
37
36
|
|
38
|
-
#
|
37
|
+
# Sets the file's owner.
|
39
38
|
def owner=(owner)
|
40
39
|
target_owner, target_group =
|
41
40
|
Wright::Util::User.owner_to_owner_group(owner)
|
@@ -43,18 +42,20 @@ module Wright
|
|
43
42
|
@group = target_group unless target_group.nil?
|
44
43
|
end
|
45
44
|
|
46
|
-
#
|
45
|
+
# Creates or updates the file.
|
47
46
|
#
|
48
|
-
#
|
47
|
+
# @return [Bool] true if the file was updated and false
|
48
|
+
# otherwise
|
49
49
|
def create
|
50
50
|
might_update_resource do
|
51
51
|
@provider.create
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
#
|
55
|
+
# Removes the file.
|
56
56
|
#
|
57
|
-
#
|
57
|
+
# @return [Bool] true if the file was updated and false
|
58
|
+
# otherwise
|
58
59
|
def remove
|
59
60
|
might_update_resource do
|
60
61
|
@provider.remove
|
@@ -3,10 +3,9 @@ require 'wright/dsl'
|
|
3
3
|
|
4
4
|
module Wright
|
5
5
|
class Resource
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# Examples
|
6
|
+
# Package resource, represents a package.
|
9
7
|
#
|
8
|
+
# @example
|
10
9
|
# vim = Wright::Resource::Package.new('vim')
|
11
10
|
# vim.installed_versions
|
12
11
|
# # => []
|
@@ -21,42 +20,44 @@ module Wright
|
|
21
20
|
# htop.installed_versions
|
22
21
|
# # => []
|
23
22
|
class Package < Wright::Resource
|
24
|
-
#
|
23
|
+
# @return [String] the package version to install or remove
|
25
24
|
attr_accessor :version
|
26
25
|
|
27
|
-
#
|
26
|
+
# Initializes a Package.
|
28
27
|
#
|
29
|
-
# name
|
28
|
+
# @param name [String] the package's name
|
30
29
|
def initialize(name)
|
31
30
|
super
|
32
31
|
@version = nil
|
33
32
|
@action = :install
|
34
33
|
end
|
35
34
|
|
36
|
-
#
|
37
|
-
#
|
38
|
-
# Returns an array of installed package version Strings.
|
35
|
+
# @return [Array<String>] the installed package versions
|
39
36
|
def installed_versions
|
40
37
|
@provider.installed_versions
|
41
38
|
end
|
42
39
|
|
43
|
-
#
|
40
|
+
# Installs the Package.
|
44
41
|
#
|
45
|
-
#
|
42
|
+
# @return [Bool] true if the package was updated and false
|
43
|
+
# otherwise
|
46
44
|
def install
|
47
45
|
might_update_resource do
|
48
46
|
@provider.install
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
52
|
-
#
|
50
|
+
# Removes the Package.
|
53
51
|
#
|
54
|
-
#
|
52
|
+
# @return [Bool] true if the package was updated and false
|
53
|
+
# otherwise
|
55
54
|
def remove
|
56
55
|
might_update_resource do
|
57
56
|
@provider.remove
|
58
57
|
end
|
59
58
|
end
|
59
|
+
|
60
|
+
alias_method :uninstall, :remove
|
60
61
|
end
|
61
62
|
end
|
62
63
|
end
|
@@ -3,38 +3,39 @@ require 'wright/dsl'
|
|
3
3
|
|
4
4
|
module Wright
|
5
5
|
class Resource
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# Examples
|
6
|
+
# Symlink resource, represents a symlink.
|
9
7
|
#
|
8
|
+
# @example
|
10
9
|
# link = Wright::Resource::Symlink.new('/tmp/fstab')
|
11
10
|
# link.to = '/etc/fstab'
|
12
11
|
# link.create
|
13
12
|
class Symlink < Wright::Resource
|
14
|
-
#
|
13
|
+
# Initializes a Symlink.
|
15
14
|
#
|
16
|
-
# name
|
15
|
+
# @param name [String] the symlink's name
|
17
16
|
def initialize(name)
|
18
17
|
super
|
19
18
|
@to = nil
|
20
19
|
@action = :create
|
21
20
|
end
|
22
21
|
|
23
|
-
#
|
22
|
+
# @return [String] the symlink's intended target
|
24
23
|
attr_accessor :to
|
25
24
|
|
26
|
-
#
|
25
|
+
# Creates or updates the symlink.
|
27
26
|
#
|
28
|
-
#
|
27
|
+
# @return [Bool] true if the symlink was updated and false
|
28
|
+
# otherwise
|
29
29
|
def create
|
30
30
|
might_update_resource do
|
31
31
|
@provider.create
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
#
|
35
|
+
# Removes the symlink.
|
36
36
|
#
|
37
|
-
#
|
37
|
+
# @return [Bool] true if the symlink was updated and false
|
38
|
+
# otherwise
|
38
39
|
def remove
|
39
40
|
might_update_resource do
|
40
41
|
@provider.remove
|
data/lib/wright/util.rb
CHANGED
@@ -1,38 +1,37 @@
|
|
1
1
|
require 'wright/util/stolen_from_activesupport'
|
2
2
|
|
3
3
|
module Wright
|
4
|
-
#
|
4
|
+
# @api private
|
5
|
+
# Various utility functions.
|
5
6
|
module Util
|
6
|
-
#
|
7
|
+
# Converts a class constant into its corresponding resource name.
|
7
8
|
#
|
8
|
-
# klass
|
9
|
-
#
|
10
|
-
# Examples
|
9
|
+
# @param klass [Class] the class constant
|
11
10
|
#
|
11
|
+
# @example
|
12
12
|
# Wright::Util.class_to_resource_name(Wright::Resource::Package)
|
13
13
|
# # => "package"
|
14
14
|
#
|
15
15
|
# Wright::Util.class_to_resource_name(Foo::Bar::BazQux)
|
16
16
|
# # => "baz_qux"
|
17
17
|
#
|
18
|
-
#
|
18
|
+
# @return [String] the resource name of the given class
|
19
19
|
def self.class_to_resource_name(klass)
|
20
20
|
ActiveSupport.underscore(klass.name).split('/').last
|
21
21
|
end
|
22
22
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
# filename - The filename for which to get the class name.
|
23
|
+
# Converts a file path into its corresponding class name.
|
26
24
|
#
|
27
|
-
#
|
25
|
+
# @param filename [String] the filename
|
28
26
|
#
|
29
|
-
#
|
27
|
+
# @example
|
28
|
+
# Wright::Util.filename_to_classname('foo/bar/baz.rb')
|
30
29
|
# # => "Foo::Bar::Baz"
|
31
30
|
#
|
32
|
-
# Wright::Util.filename_to_classname(
|
31
|
+
# Wright::Util.filename_to_classname('foo/bar/')
|
33
32
|
# # => "Foo::Bar"
|
34
33
|
#
|
35
|
-
#
|
34
|
+
# @return [String] the class name for the given filename
|
36
35
|
def self.filename_to_classname(filename)
|
37
36
|
ActiveSupport.camelize(filename.chomp('.rb').chomp('/'))
|
38
37
|
end
|
@@ -45,18 +44,17 @@ module Wright
|
|
45
44
|
end
|
46
45
|
private_class_method :distro
|
47
46
|
|
48
|
-
#
|
49
|
-
#
|
50
|
-
# Examples
|
47
|
+
# Determines the system's OS family.
|
51
48
|
#
|
49
|
+
# @example
|
52
50
|
# Wright::Util.os_family
|
53
51
|
# # => "debian"
|
54
|
-
#
|
52
|
+
# @example
|
55
53
|
# Wright::Util.os_family
|
56
54
|
# # => "macosx"
|
57
55
|
#
|
58
|
-
#
|
59
|
-
#
|
56
|
+
# @return [String] the system's OS family (base distribution for
|
57
|
+
# GNU/Linux systems) or 'other' for unknown operating systems
|
60
58
|
def self.os_family
|
61
59
|
system_arch = RbConfig::CONFIG['target_os']
|
62
60
|
case system_arch
|