librarian-chef-nochef 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $:.unshift(lib) unless $:.include?(lib)
5
+
6
+ require 'librarian/chef/cli'
7
+ Librarian::Chef::Cli.bin!
@@ -0,0 +1 @@
1
+ require "librarian/chef"
@@ -0,0 +1 @@
1
+ require 'librarian/chef/extension'
@@ -0,0 +1,47 @@
1
+ require 'librarian/helpers'
2
+
3
+ require 'librarian/cli'
4
+ require 'librarian/chef'
5
+
6
+ module Librarian
7
+ module Chef
8
+ class Cli < Librarian::Cli
9
+
10
+ module Particularity
11
+ def root_module
12
+ Chef
13
+ end
14
+ end
15
+
16
+ extend Particularity
17
+
18
+ source_root Pathname.new(__FILE__).dirname.join("templates")
19
+
20
+ def init
21
+ copy_file environment.specfile_name
22
+ end
23
+
24
+ desc "install", "Resolves and installs all of the dependencies you specify."
25
+ option "quiet", :type => :boolean, :default => false
26
+ option "verbose", :type => :boolean, :default => false
27
+ option "line-numbers", :type => :boolean, :default => false
28
+ option "clean", :type => :boolean, :default => false
29
+ option "strip-dot-git", :type => :boolean
30
+ option "path", :type => :string
31
+ def install
32
+ ensure!
33
+ clean! if options["clean"]
34
+ if options.include?("strip-dot-git")
35
+ strip_dot_git_val = options["strip-dot-git"] ? "1" : nil
36
+ environment.config_db.local["install.strip-dot-git"] = strip_dot_git_val
37
+ end
38
+ if options.include?("path")
39
+ environment.config_db.local["path"] = options["path"]
40
+ end
41
+ resolve!
42
+ install!
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ require 'librarian/dsl'
2
+ require 'librarian/chef/source'
3
+
4
+ module Librarian
5
+ module Chef
6
+ class Dsl < Librarian::Dsl
7
+
8
+ dependency :cookbook
9
+
10
+ source :site => Source::Site
11
+ source :git => Source::Git
12
+ source :github => Source::Github
13
+ source :path => Source::Path
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require "librarian/environment"
2
+ require "librarian/chef/dsl"
3
+ require "librarian/chef/source"
4
+ require "librarian/chef/version"
5
+
6
+ module Librarian
7
+ module Chef
8
+ class Environment < Environment
9
+
10
+ def adapter_name
11
+ "chef"
12
+ end
13
+
14
+ def adapter_version
15
+ VERSION
16
+ end
17
+
18
+ def install_path
19
+ part = config_db["path"] || "cookbooks"
20
+ project_path.join(part)
21
+ end
22
+
23
+ def config_keys
24
+ super + %w[
25
+ install.strip-dot-git
26
+ path
27
+ ]
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ require 'librarian/chef/environment'
2
+
3
+ module Librarian
4
+ module Chef
5
+ extend self
6
+ extend Librarian
7
+
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ require 'pathname'
2
+ require 'securerandom'
3
+ require 'highline'
4
+
5
+ require 'librarian'
6
+ require 'librarian/action/install'
7
+ require 'librarian/chef'
8
+
9
+ module Librarian
10
+ module Chef
11
+
12
+ class Environment
13
+ def install_path
14
+ @install_path ||= begin
15
+ has_home = ENV["HOME"] && File.directory?(ENV["HOME"])
16
+ tmp_dir = Pathname.new(has_home ? "~/.librarian/tmp" : "/tmp/librarian").expand_path
17
+ enclosing = tmp_dir.join("chef/integration/knife/install")
18
+ enclosing.mkpath unless enclosing.exist?
19
+ dir = enclosing.join(SecureRandom.hex(16))
20
+ dir.mkpath
21
+ at_exit { dir.rmtree }
22
+ dir
23
+ end
24
+ end
25
+ end
26
+
27
+ def environment
28
+ @environment ||= environment_class.new
29
+ end
30
+
31
+ def install_path
32
+ environment.install_path
33
+ end
34
+
35
+ hl = HighLine.new
36
+
37
+ begin
38
+ Action::Install.new(environment).run
39
+ rescue Error => e
40
+ message = hl.color(e.message, HighLine::RED)
41
+ hl.say(message)
42
+ Process.exit!(1)
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,56 @@
1
+ require 'json'
2
+ require 'yaml'
3
+
4
+ require 'librarian/manifest'
5
+
6
+ require_relative 'metadata'
7
+
8
+ module Librarian
9
+ module Chef
10
+ module ManifestReader
11
+ extend self
12
+
13
+ MANIFESTS = %w(metadata.json metadata.yml metadata.yaml metadata.rb)
14
+
15
+ def manifest_path(path)
16
+ MANIFESTS.map{|s| path.join(s)}.find{|s| s.exist?}
17
+ end
18
+
19
+ def read_manifest(name, manifest_path)
20
+ case manifest_path.extname
21
+ when ".json" then JSON.parse(binread(manifest_path))
22
+ when ".yml", ".yaml" then YAML.load(binread(manifest_path))
23
+ when ".rb" then compile_manifest(name, manifest_path.dirname)
24
+ end
25
+ end
26
+
27
+ def compile_manifest(name, path)
28
+ md = Metadata.new(path.join('metadata.rb').to_s)
29
+ { "name" => name, "version" => md.version, "dependencies" => md.dependencies }
30
+ end
31
+
32
+ def manifest?(name, path)
33
+ path = Pathname.new(path)
34
+ !!manifest_path(path)
35
+ end
36
+
37
+ def check_manifest(name, manifest_path)
38
+ manifest = read_manifest(name, manifest_path)
39
+ manifest["name"] == name
40
+ end
41
+
42
+ private
43
+
44
+ if File.respond_to?(:binread)
45
+ def binread(path)
46
+ File.binread(path)
47
+ end
48
+ else
49
+ def binread(path)
50
+ File.read(path)
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,70 @@
1
+ module Librarian
2
+ module Chef
3
+ # Read metadata from a Chef cookbook in a similar way
4
+ # as Chef::Cookbook::Metadata does.
5
+ #
6
+ # It reads only necessary items (version and dependencies),
7
+ # silently ignoring the other entries.
8
+ class Metadata
9
+
10
+ attr_reader :dependencies
11
+
12
+ def initialize(file_name)
13
+ @dependencies = {}
14
+ @version = "0.0.0"
15
+ self.instance_eval File.read(file_name)
16
+ end
17
+
18
+ def depends(cookbook, version_constraint = nil)
19
+ version_constraint ||= ">= 0.0.0"
20
+ @dependencies[cookbook] = parse_version_constraint(version_constraint)
21
+ end
22
+
23
+ def version(number = nil)
24
+ @version = parse_version(number) if number
25
+ @version
26
+ end
27
+
28
+ # Ignore any other definition
29
+ def method_missing(name, *args, &block)
30
+ nil
31
+ end
32
+
33
+ private
34
+
35
+ # Simple version parser
36
+ #
37
+ # Allowed formats:
38
+ # x.x
39
+ # x.x.x
40
+ def parse_version(version)
41
+ raise ::Librarian::Error, "Invalid cookbook version" unless version =~ /^(\d+)\.(\d+)(\.(\d+))?$/
42
+ version
43
+ end
44
+
45
+ # Parse version constraint
46
+ #
47
+ # Allowed operators:
48
+ # <
49
+ # >
50
+ # =
51
+ # <=
52
+ # >=
53
+ # ~>
54
+ def parse_version_constraint(version_constraint)
55
+ if version_constraint.index(" ").nil?
56
+ version = parse_version(version_constraint)
57
+ operator = "="
58
+ "#{operator} #{version}"
59
+ elsif version_constraint =~ /^(<|>|=|<=|>=|~>) (.+)$/
60
+ version = parse_version($2)
61
+ operator = $1
62
+ "#{operator} #{version}"
63
+ else
64
+ raise ::Librarian::Error, "Invalid version constraint"
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ require 'librarian/chef/source/path'
2
+ require 'librarian/chef/source/git'
3
+ require 'librarian/chef/source/github'
4
+ require 'librarian/chef/source/site'
@@ -0,0 +1,25 @@
1
+ require 'librarian/source/git'
2
+ require 'librarian/chef/source/local'
3
+
4
+ module Librarian
5
+ module Chef
6
+ module Source
7
+ class Git < Librarian::Source::Git
8
+ include Local
9
+
10
+ private
11
+
12
+ def install_perform_step_copy!(found_path, install_path)
13
+ debug { "Copying #{relative_path_to(found_path)} to #{relative_path_to(install_path)}" }
14
+ FileUtils.cp_r(found_path, install_path)
15
+
16
+ if environment.config_db["install.strip-dot-git"] == "1"
17
+ dot_git = install_path.join(".git")
18
+ dot_git.rmtree if dot_git.directory?
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'librarian/chef/source/git'
2
+
3
+ module Librarian
4
+ module Chef
5
+ module Source
6
+ class Github
7
+
8
+ class << self
9
+
10
+ def lock_name
11
+ Git.lock_name
12
+ end
13
+
14
+ def from_lock_options(environment, options)
15
+ Git.from_lock_options(environment, options)
16
+ end
17
+
18
+ def from_spec_args(environment, uri, options)
19
+ Git.from_spec_args(environment, "https://github.com/#{uri}", options)
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,74 @@
1
+ require 'librarian/chef/manifest_reader'
2
+
3
+ module Librarian
4
+ module Chef
5
+ module Source
6
+ module Local
7
+
8
+ def install!(manifest)
9
+ manifest.source == self or raise ArgumentError
10
+
11
+ info { "Installing #{manifest.name} (#{manifest.version})" }
12
+
13
+ debug { "Installing #{manifest}" }
14
+
15
+ name, version = manifest.name, manifest.version
16
+ found_path = found_path(name)
17
+
18
+ install_path = environment.install_path.join(name)
19
+ if install_path.exist?
20
+ debug { "Deleting #{relative_path_to(install_path)}" }
21
+ install_path.rmtree
22
+ end
23
+
24
+ install_perform_step_copy!(found_path, install_path)
25
+ end
26
+
27
+ def fetch_version(name, extra)
28
+ manifest_data(name)["version"]
29
+ end
30
+
31
+ def fetch_dependencies(name, version, extra)
32
+ manifest_data(name)["dependencies"]
33
+ end
34
+
35
+ private
36
+
37
+ def install_perform_step_copy!(found_path, install_path)
38
+ debug { "Copying #{relative_path_to(found_path)} to #{relative_path_to(install_path)}" }
39
+ FileUtils.mkdir_p(install_path)
40
+ FileUtils.cp_r(filter_path(found_path), install_path)
41
+ end
42
+
43
+ def filter_path(path)
44
+ Dir.glob("#{path}/*").reject { |e| e == environment.install_path.to_s }
45
+ end
46
+
47
+ def manifest_data(name)
48
+ @manifest_data ||= { }
49
+ @manifest_data[name] ||= fetch_manifest_data(name)
50
+ end
51
+
52
+ def fetch_manifest_data(name)
53
+ expect_manifest!(name)
54
+
55
+ found_path = found_path(name)
56
+ manifest_path = ManifestReader.manifest_path(found_path)
57
+ ManifestReader.read_manifest(name, manifest_path)
58
+ end
59
+
60
+ def manifest?(name, path)
61
+ ManifestReader.manifest?(name, path)
62
+ end
63
+
64
+ def expect_manifest!(name)
65
+ found_path = found_path(name)
66
+ return if found_path && ManifestReader.manifest_path(found_path)
67
+
68
+ raise Error, "No metadata file found for #{name} from #{self}! If this should be a cookbook, you might consider contributing a metadata file upstream or forking the cookbook to add your own metadata file."
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,12 @@
1
+ require 'librarian/source/path'
2
+ require 'librarian/chef/source/local'
3
+
4
+ module Librarian
5
+ module Chef
6
+ module Source
7
+ class Path < Librarian::Source::Path
8
+ include Local
9
+ end
10
+ end
11
+ end
12
+ end