Created
April 16, 2015 18:41
-
-
Save mwrock/6f56a3e66d574214d75f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'json' | |
| module Promote | |
| class Versioner | |
| def initialize(config = Config.new) | |
| @config = config | |
| end | |
| def version_cookbook(cookbook_name) | |
| cookbook = Cookbook.new(cookbook_name, config) | |
| repo = GitRepo.new(cookbook.path) | |
| version = repo.version_number | |
| if cookbook.version.to_s != version | |
| cookbook.version = Semverse::Version.new(version) | |
| cookbook.stamp_commit(repo.sha1) | |
| return { | |
| :cookbook => cookbook_name, | |
| :version => version, | |
| :sha1 => repo.sha1 | |
| } | |
| end | |
| end | |
| def version_cookbooks | |
| results = [] | |
| Dir.glob(File.join(config.cookbook_directory, "*")).each do |file| | |
| result = version_cookbook(File.basename(file)) | |
| if !result.nil? | |
| results << result | |
| end | |
| end | |
| results | |
| end | |
| def version_environment(environment_name) | |
| version_json config.environment_directory, environment_name do | content | | |
| content['override_attributes'] | |
| end | |
| end | |
| def version_environments | |
| results = [] | |
| Dir.glob(File.join(config.environment_directory, "*.json")).each do |file| | |
| result = version_environment(File.basename(file ,File.extname(file))) | |
| if !result.nil? | |
| results << result | |
| end | |
| end | |
| results | |
| end | |
| def version_role(role_name) | |
| # if role doesn't include an override_attributes key, create it | |
| file = File.join(config.role_directory, "#{role_name}.json") | |
| role = JSON.parse(File.read(file)) | |
| if !role.has_key?("override_attributes") | |
| overrides_hash = { "override_attributes" => {} } | |
| role.merge!(overrides_hash) | |
| File.open(file, 'w') do |out| | |
| out << JSON.pretty_generate(role) | |
| end | |
| end | |
| version_json config.role_directory, role_name do | content | | |
| content['override_attributes'] | |
| end | |
| end | |
| def version_roles | |
| results = [] | |
| Dir.glob(File.join(config.role_directory, "*.json")).each do |file| | |
| result = version_role(File.basename(file ,File.extname(file))) | |
| if !result.nil? | |
| results << result | |
| end | |
| end | |
| results | |
| end | |
| def constrain_environment(environment_name, cookbook_name) | |
| dependencies = Cookbook.new(cookbook_name, config).dependencies | |
| env_file = EnvironmentFile.new(environment_name, config) | |
| env_file.write_cookbook_versions(dependencies) | |
| dependencies | |
| end | |
| def is_dirty(source_environment, target_environment, cookbook_name, dependency_hash = nil) | |
| target_env_file = EnvironmentFile.new(target_environment, config) | |
| return true unless target_env_file.cookbook_versions.has_key?(cookbook_name) | |
| source_env_file = EnvironmentFile.new(source_environment, config) | |
| target_version_parts = target_env_file.cookbook_versions[cookbook_name].split('.') | |
| target_version = target_version_parts[0..2].join('.') | |
| target_hash = nil | |
| if target_version_parts.count > 3 | |
| target_hash = target_version_parts[3] | |
| end | |
| return source_env_file.cookbook_versions[cookbook_name] != target_version || dependency_hash != target_hash | |
| end | |
| attr_accessor :config | |
| private | |
| def version_json(parent_directory, file_name, &block) | |
| version_string = "version" | |
| file = "#{parent_directory}/#{file_name}.json" | |
| repo = GitRepo.new(file) | |
| version = repo.version_number | |
| content = JSON.parse(File.read(file)) | |
| current_version = '0.0' | |
| if block_given? | |
| content_hash = yield content | |
| else | |
| content_hash = content | |
| end | |
| if !content_hash.nil? && content_hash.has_key?(version_string) | |
| current_version = content_hash[version_string] | |
| end | |
| if !content_hash.nil? && current_version != version | |
| content_hash[version_string] = version | |
| content_hash['sha1'] = repo.sha1 | |
| File.open(file, 'w') do |out| | |
| out << JSON.pretty_generate(content) | |
| end | |
| { | |
| :artifact => file_name, | |
| :version => version, | |
| :sha1 => repo.sha1 | |
| } | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment