clc-promote 0.4.0.dev.15 → 0.4.0.dev.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fbec88c6ee783cfda0c4dde10e77efe5158cdd4
4
- data.tar.gz: e409cf9cf81cd1a632e41755f355398d9e8f9d89
3
+ metadata.gz: 24a8ea1aec1e9dd86817d1eb9c74051e26329491
4
+ data.tar.gz: a1ba2706c78a8b4e6be0a1aa806919b0e54c18f4
5
5
  SHA512:
6
- metadata.gz: ae4afb75ef882b373ebd7e8614dde5bfbd9acf6544902518533ce5ea4dc469c80622b4c999733cf18b48d37007f036393f3ee81014523e5a22eb6e563a7aec24
7
- data.tar.gz: c157119d21822944b319a74cbefdfd9aa7d4b657e8235f8a938b65e8ca1c6060f06df006f61d6b7292f60c1c354a8fa7894d15299e13829031ab73f9c5736f07
6
+ metadata.gz: d2289943c16adc648ca954430e775441df4e84cdd81d0e424d1a6b204937fd7869210f84405467f978f2329ecc4cd8014b9b508c3276c6cee28e6341c512a7a6
7
+ data.tar.gz: 3ed76680c8c3a07cd6162a45803a126a9e3b5320fa7d83e1df887385f3e38bf0c000849fce98bf45d84ec6b9d219cf906cc9c30c6a382909c952da96c8142aa7
@@ -1,4 +1,5 @@
1
1
  require 'promote/config'
2
+ require 'promote/cookbook'
2
3
  require 'promote/environment_file'
3
4
  require 'promote/git_repo'
4
5
  require 'promote/uploader'
@@ -0,0 +1,114 @@
1
+ require 'berkshelf'
2
+ require 'json'
3
+
4
+ module Promote
5
+ class Cookbook
6
+ def initialize(cookbook_name, config)
7
+ @name = cookbook_name
8
+ @config = config
9
+ @metadata_path = File.join(path, 'metadata.rb')
10
+ @berks_path = File.join(path, "Berksfile")
11
+ end
12
+
13
+ def dependencies
14
+ @dependencies ||= get_dependencies_from_lockfile
15
+ end
16
+
17
+ def metadata_dependencies
18
+ metadata.dependencies
19
+ end
20
+
21
+ def version(version=nil)
22
+ metadata.version
23
+ end
24
+
25
+ def version=(version)
26
+ version_line = raw_metadata[/^\s*version\s.*$/]
27
+ current_version = version_line[/('|").*("|')/].gsub(/('|")/,"")
28
+ if current_version != version.to_s
29
+ new_version_line = version_line.gsub(current_version, version.to_s)
30
+ new_content = raw_metadata.gsub(version_line, new_version_line)
31
+ save_metadata(new_content)
32
+ end
33
+ version
34
+ end
35
+
36
+ def stamp_commit(commit)
37
+ commit_line = "#sha1 '#{commit}'"
38
+ new_content = raw_metadata.gsub(/#sha1.*$/, commit_line)
39
+ if new_content[/#sha1.*$/].nil?
40
+ new_content += "\n#{commit_line}"
41
+ end
42
+ save_metadata(new_content)
43
+ end
44
+
45
+ def raw_metadata
46
+ @raw_metadata ||= File.read(@metadata_path)
47
+ end
48
+
49
+ def metadata
50
+ @metadata ||=get_metadata_from_file
51
+ end
52
+
53
+ def path
54
+ File.join(@config.cookbook_directory, @name)
55
+ end
56
+
57
+ def sync_berksfile(update=false)
58
+ return if berksfile.nil?
59
+
60
+ update = false unless File.exist?("#{@berks_path}.lock")
61
+ update ? berksfile.update : berksfile.install
62
+ end
63
+
64
+ def sync_latest_app_cookbooks(environment_cookbook_name)
65
+ result = {}
66
+ latest_server_cookbooks = Utils.chef_server_cookbooks(@config, 1)
67
+ env_cookbook = Cookbook.new(environment_cookbook_name, @config)
68
+ env_cookbook.metadata_dependencies do |key|
69
+ latest_version = latest_server_cookbooks[key]['versions'][0]
70
+ if dependencies.keys.include?(key) && latest_version > dependencies[key].to_s
71
+ berksfile.update(key)
72
+ result[key] = latest_version
73
+ end
74
+ end
75
+ @dependencies = nil unless result.keys.empty?
76
+ result
77
+ end
78
+
79
+ private
80
+
81
+ def berksfile
82
+ @berksfile ||= get_berksfile
83
+ end
84
+
85
+ def save_metadata(content)
86
+ File.open(@metadata_path, 'w') do |out|
87
+ out << content
88
+ end
89
+ @metadata=nil
90
+ @raw_metadata = nil
91
+ end
92
+
93
+ def get_metadata_from_file
94
+ metadata = Chef::Cookbook::Metadata.new
95
+ metadata.from_file(@metadata_path)
96
+ metadata
97
+ end
98
+
99
+ def get_dependencies_from_lockfile
100
+ berksfile.install
101
+ berks_deps = berksfile.list
102
+ deps = {}
103
+
104
+ berks_deps.each {|dep| deps[dep.name] = dep.locked_version}
105
+ deps
106
+ end
107
+
108
+ def get_berksfile
109
+ return unless File.exist?(@berks_path)
110
+ Berkshelf.set_format :null
111
+ Berkshelf::Berksfile.from_file(@berks_path)
112
+ end
113
+ end
114
+ end
@@ -53,6 +53,7 @@ module Promote
53
53
  end
54
54
 
55
55
  def git_root(start_leaf)
56
+ raise "no git repo found for #{scope_path}" if start_leaf == '/'
56
57
  return start_leaf if File.exist?(File.join(start_leaf, '.git'))
57
58
  git_root(File.expand_path('..', start_leaf))
58
59
  end
@@ -6,11 +6,11 @@ module Promote
6
6
  class RakeTasks < ::Rake::TaskLib
7
7
 
8
8
  def initialize(config)
9
- config = Config.new(config) if config.is_a?(Hash)
10
- config = Config.new({ :repo_root => config }) if config.is_a?(String)
11
- @versioner = Versioner.new(config)
12
- @uploader = Uploader.new(config)
13
- @promoter = Promoter.new(config)
9
+ @config = Config.new(config) if config.is_a?(Hash)
10
+ @config = Config.new({ :repo_root => config }) if config.is_a?(String)
11
+ @versioner = Versioner.new(@config)
12
+ @uploader = Uploader.new(@config)
13
+ @promoter = Promoter.new(@config)
14
14
  yield self if block_given?
15
15
  define_version_cookbook
16
16
  define_version_cookbooks
@@ -54,7 +54,7 @@ module Promote
54
54
  desc "Syncs a Berksfile.lock file"
55
55
  task "sync_berksfile", :cookbook, :update do |task, args|
56
56
  puts "Syncing berksfile.lock for #{args.cookbook}..."
57
- @versioner.sync_berksfile(args.cookbook, args.update)
57
+ Cookbook.new(args.cookbook, @config).sync_berksfile(args.update)
58
58
  end
59
59
  end
60
60
  end
@@ -64,7 +64,7 @@ module Promote
64
64
  desc "Syncs all Berksfile.lock files"
65
65
  task "sync_berksfiles" do
66
66
  puts "Syncing berksfile.locks..."
67
- @versioner.sync_berksfiles
67
+ Utils.sync_berksfiles(@config)
68
68
  end
69
69
  end
70
70
  end
@@ -84,8 +84,8 @@ module Promote
84
84
  task "constrain_environment", :environment, :environment_cookbook do |task, args|
85
85
  puts "Checking for cookbok constraints for the #{args.environment} environment using the #{args.environment_cookbook} cookbook..."
86
86
  deps = @versioner.constrain_environment(args.environment, args.environment_cookbook)
87
- deps.each do |dep|
88
- puts "#{dep.name} -> #{dep.locked_version.to_s}"
87
+ deps.each do |k,v|
88
+ puts "#{k} -> #{v}"
89
89
  end
90
90
  end
91
91
  end
@@ -2,25 +2,15 @@ require 'chef'
2
2
 
3
3
  module Promote
4
4
  class Utils
5
- def self.chef_server_cookbooks(config)
5
+ def self.chef_server_cookbooks(config, versions = 'all')
6
6
  rest = Chef::REST.new(config.chef_server_url, config.node_name, config.client_key)
7
- rest.get_rest("/cookbooks?num_versions=all")
7
+ rest.get_rest("/cookbooks?num_versions=#{versions}")
8
8
  end
9
9
 
10
- def self.sync_berksfiles(cookbook_directory, update)
11
- repo_cookbooks = Dir.glob(File.join(cookbook_directory, "*"))
10
+ def self.sync_berksfiles(config, update = false)
11
+ repo_cookbooks = Dir.glob(File.join(config.cookbook_directory, "*"))
12
12
  repo_cookbooks.each do |cookbook|
13
- sync_berksfile(cookbook, update)
14
- end
15
- end
16
-
17
- def self.sync_berksfile(cookbook_root, update)
18
- berks_name = File.join(cookbook_root, "Berksfile")
19
- if File.exist?(berks_name)
20
- update = false unless File.exist?("#{berks_name}.lock")
21
- Berkshelf.set_format :null
22
- berksfile = Berkshelf::Berksfile.from_file(berks_name)
23
- update ? berksfile.update : berksfile.install
13
+ Cookbook.new(File.basename(cookbook), config).sync_berksfile(update)
24
14
  end
25
15
  end
26
16
  end
@@ -1,4 +1,3 @@
1
- require 'berkshelf'
2
1
  require 'json'
3
2
 
4
3
  module Promote
@@ -8,23 +7,12 @@ module Promote
8
7
  end
9
8
 
10
9
  def version_cookbook(cookbook_name)
11
- dir = File.join(config.cookbook_directory, cookbook_name)
12
- repo = GitRepo.new(dir)
13
- cookbook_name = File.basename(dir)
10
+ cookbook = Cookbook.new(cookbook_name, config)
11
+ repo = GitRepo.new(cookbook.path)
14
12
  version = repo.version_number
15
- metadata_file = File.join(dir, "metadata.rb")
16
- metadata_content = File.read(metadata_file)
17
- version_line = metadata_content[/^\s*version\s.*$/]
18
- current_version = version_line[/('|").*("|')/].gsub(/('|")/,"")
19
- if current_version != version
20
- metadata_content = metadata_content.gsub(current_version, version)
21
- outdata = metadata_content.gsub(/#sha1.*$/, "#sha1 '#{repo.sha1}'")
22
- if outdata[/#sha1.*$/].nil?
23
- outdata += "#sha1 '#{repo.sha1}'"
24
- end
25
- File.open(metadata_file, 'w') do |out|
26
- out << outdata
27
- end
13
+ if cookbook.version.to_s != version
14
+ cookbook.version = Semverse::Version.new(version)
15
+ cookbook.stamp_commit(repo.sha1)
28
16
  return {
29
17
  :cookbook => cookbook_name,
30
18
  :version => version,
@@ -36,8 +24,7 @@ module Promote
36
24
  def version_cookbooks
37
25
  results = []
38
26
  Dir.glob(File.join(config.cookbook_directory, "*")).each do |file|
39
- cookbook = File.basename(file)
40
- result = version_cookbook(cookbook)
27
+ result = version_cookbook(File.basename(file))
41
28
  if !result.nil?
42
29
  results << result
43
30
  end
@@ -45,14 +32,6 @@ module Promote
45
32
  results
46
33
  end
47
34
 
48
- def sync_berksfile(cookbook_name, update = false)
49
- Utils.sync_berksfile(File.join(config.cookbook_directory, cookbook_name), update)
50
- end
51
-
52
- def sync_berksfiles(update = false)
53
- Utils.sync_berksfiles(config.cookbook_directory, update)
54
- end
55
-
56
35
  def version_environment(environment_name)
57
36
  version_json config.environment_directory, environment_name do | content |
58
37
  content['override_attributes']
@@ -62,8 +41,7 @@ module Promote
62
41
  def version_environments
63
42
  results = []
64
43
  Dir.glob(File.join(config.environment_directory, "*.json")).each do |file|
65
- environment = File.basename(file ,File.extname(file))
66
- result = version_environment(environment)
44
+ result = version_environment(File.basename(file ,File.extname(file)))
67
45
  if !result.nil?
68
46
  results << result
69
47
  end
@@ -72,14 +50,9 @@ module Promote
72
50
  end
73
51
 
74
52
  def constrain_environment(environment_name, cookbook_name)
75
- dependencies = environment_dependencies(cookbook_name)
53
+ dependencies = Cookbook.new(cookbook_name, config).dependencies
76
54
  env_file = EnvironmentFile.new(environment_name, config)
77
- versions = {}
78
- dependencies.each do | dep |
79
- versions[dep.name] = dep.locked_version.to_s
80
- end
81
-
82
- env_file.write_cookbook_versions(versions)
55
+ env_file.write_cookbook_versions(dependencies)
83
56
  dependencies
84
57
  end
85
58
 
@@ -95,13 +68,6 @@ module Promote
95
68
 
96
69
  private
97
70
 
98
- def environment_dependencies(cookbook_name)
99
- berks_name = File.join(config.cookbook_directory, cookbook_name, "Berksfile")
100
- berksfile = Berkshelf::Berksfile.from_file(berks_name)
101
- berksfile.install
102
- berksfile.list
103
- end
104
-
105
71
  def version_json(parent_directory, file_name, &block)
106
72
  version_string = "version"
107
73
  file = "#{parent_directory}/#{file_name}.json"
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clc-promote
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.dev.15
4
+ version: 0.4.0.dev.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - CenturyLink Cloud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-13 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: chef
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: clc-git
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +101,7 @@ files:
115
101
  - lib/chef/knife/promote.rb
116
102
  - lib/promote.rb
117
103
  - lib/promote/config.rb
104
+ - lib/promote/cookbook.rb
118
105
  - lib/promote/environment_file.rb
119
106
  - lib/promote/git_repo.rb
120
107
  - lib/promote/promoter.rb