clc-promote 0.3.1 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chef/knife/promote.rb +68 -0
- data/lib/promote.rb +2 -0
- data/lib/promote/config.rb +12 -0
- data/lib/promote/git_helper.rb +16 -0
- data/lib/promote/promoter.rb +48 -0
- data/lib/promote/uploader.rb +15 -7
- data/lib/promote/utils.rb +10 -0
- data/lib/promote/versioner.rb +2 -5
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cc35177c05853e5ebedbc92108bfad0fe143355
|
4
|
+
data.tar.gz: c2f0586fedd03adff7a45cdf65b7d72b8008eba5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44ed3925abcc6c61d15d6e56c99fde9bd16956c9533fabc2ecfe2d3fba3f57e453c6e51877f15976523aab5f5146b88cfc96852e870cba2eb55aab3844d995d4
|
7
|
+
data.tar.gz: c7fd0aace5ab30dd38fda484d208e1db7f890b036512fbeca59acd49fdad23d6c6a2d85fdfbeb1df780f2acca985cda91b6bae41a3f08ca2a9df1dc22cac3da8
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'chef/knife'
|
2
|
+
require 'git'
|
3
|
+
require 'promote'
|
4
|
+
|
5
|
+
module KnifePromote
|
6
|
+
class PromoteEnvironment < Chef::Knife
|
7
|
+
|
8
|
+
banner "knife promote environment SOURCE_ENVIRONMENT DESTINATION_ENVIRONMENT"
|
9
|
+
|
10
|
+
def run
|
11
|
+
if @name_args.length < 2
|
12
|
+
show_usage
|
13
|
+
ui.error("You must specify a source and destination environment")
|
14
|
+
exit 1
|
15
|
+
end
|
16
|
+
|
17
|
+
source_env = @name_args[0]
|
18
|
+
dest_env = @name_args[1]
|
19
|
+
|
20
|
+
ui.info "#{source_env} is being promoted to #{dest_env}"
|
21
|
+
ui.confirm "Are you sure #{source_env} deserves this promotion"
|
22
|
+
|
23
|
+
config = Promote::Config.new({
|
24
|
+
:repo_root => File.expand_path("../",Chef::Config[:cookbook_path][0]),
|
25
|
+
:node_name => Chef::Config[:node_name],
|
26
|
+
:client_key => Chef::Config[:client_key],
|
27
|
+
:chef_server_url => Chef::Config[:chef_server_url]
|
28
|
+
})
|
29
|
+
destination_config = Promote::Config.new({
|
30
|
+
:repo_root => [config.temp_directory],
|
31
|
+
:node_name => Chef::Config[:knife][:promote_prod_user],
|
32
|
+
:client_key => Chef::Config[:knife][:promote_prod_client_key],
|
33
|
+
:chef_server_url => Chef::Config[:knife][:promote_prod_url]
|
34
|
+
})
|
35
|
+
|
36
|
+
promoter = Promote::Promoter.new(config)
|
37
|
+
uploader = Promote::Uploader.new(config)
|
38
|
+
#Chef::Config[:verbosity] = 2
|
39
|
+
|
40
|
+
promoter.promote_to(source_env, dest_env)
|
41
|
+
|
42
|
+
ui.info "Commiting and pushing changes to #{dest_env} back to git"
|
43
|
+
commit(config, source_env, dest_env)
|
44
|
+
|
45
|
+
ui.info "uploading #{dest_env} to #{config.chef_server_url}"
|
46
|
+
uploader.upload_environment(dest_env)
|
47
|
+
|
48
|
+
promoter.stage_promotion(dest_env, destination_config, ui)
|
49
|
+
promoter.upload_to(dest_env, destination_config, ui)
|
50
|
+
|
51
|
+
ui.info "promotion complete. Congratulations #{dest_env} on a well deserved promotion!!"
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
include Promote::GitHelper
|
57
|
+
|
58
|
+
def commit(config, source_env, dest_env)
|
59
|
+
git = git_chef(config)
|
60
|
+
dest_path = File.join(config.environment_directory, "#{dest_env}.json")
|
61
|
+
git.add(dest_path)
|
62
|
+
if !(git.status.changed.select{|s| dest_path.end_with?(s)}).keys.empty?
|
63
|
+
git.commit("promoted new constraints from #{source_env} to #{dest_env}")
|
64
|
+
git.push
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/promote.rb
CHANGED
data/lib/promote/config.rb
CHANGED
@@ -11,6 +11,18 @@ module Promote
|
|
11
11
|
self.temp_directory ||= "/tmp/promote"
|
12
12
|
end
|
13
13
|
|
14
|
+
def to_hash
|
15
|
+
hash = {}
|
16
|
+
instance_variables.each do |key|
|
17
|
+
hash[key[1..-1].to_sym] = instance_variable_get key
|
18
|
+
end
|
19
|
+
hash
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
to_hash.to_s
|
24
|
+
end
|
25
|
+
|
14
26
|
attr_accessor :repo_root
|
15
27
|
attr_accessor :cookbook_directory
|
16
28
|
attr_accessor :environment_directory
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'git'
|
2
|
+
|
3
|
+
module Promote
|
4
|
+
module GitHelper
|
5
|
+
|
6
|
+
def git_chef(config)
|
7
|
+
Git.open(git_root(config.repo_root))
|
8
|
+
end
|
9
|
+
|
10
|
+
def git_root(start_leaf)
|
11
|
+
return start_leaf if File.exist?(File.join(start_leaf, '.git'))
|
12
|
+
git_root(File.expand_path('..', start_leaf))
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
data/lib/promote/promoter.rb
CHANGED
@@ -21,5 +21,53 @@ module Promote
|
|
21
21
|
out << JSON.pretty_generate(dest_content)
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
def stage_promotion(promote_environment, destination_config, ui)
|
26
|
+
ui.info "Staging artifacts from #{config.chef_server_url} to #{config.temp_directory}"
|
27
|
+
FileUtils.rm_rf(config.temp_directory) if Dir.exist?(config.temp_directory)
|
28
|
+
Dir.mkdir(config.temp_directory)
|
29
|
+
|
30
|
+
deploy_file = "/environments/#{promote_environment}.json"
|
31
|
+
ui.info "Downloading #{deploy_file}..."
|
32
|
+
download_files(config.temp_directory, deploy_file)
|
33
|
+
ui.info "Downloading data bags..."
|
34
|
+
download_files(config.temp_directory, "/data_bags/*")
|
35
|
+
|
36
|
+
ui.info "Downloading cookbooks..."
|
37
|
+
server_versions = Promote::Utils.chef_server_cookbooks(destination_config)
|
38
|
+
env_content = JSON.parse(File.read(File.join(config.temp_directory, deploy_file)))
|
39
|
+
env_content['cookbook_versions'].each do |k,v|
|
40
|
+
if !server_versions.has_key?(k) || (server_versions[k]["versions"].select {|version| version['version'] == v}).empty?
|
41
|
+
ui.info "#{k}-#{v}"
|
42
|
+
download_files(config.temp_directory, "/cookbooks/#{k}", v)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def upload_to(promote_environment, destination_config, ui)
|
48
|
+
ui.info "Uploading staged artifacts to #{destination_config.chef_server_url}"
|
49
|
+
uploader = Uploader.new(destination_config)
|
50
|
+
ui.info("Uploading #{promote_environment} environment file...")
|
51
|
+
uploader.upload_environment(promote_environment)
|
52
|
+
ui.info("Uploading data bags...")
|
53
|
+
uploader.upload_data_bags
|
54
|
+
ui.info("Uploading cookbooks...")
|
55
|
+
uploader.upload_cookbook_directory(destination_config.cookbook_directory)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def download_files(local_root, path, cookbook_version = nil)
|
61
|
+
fs_config = Chef::ChefFS::Config.new(Chef::Config, cwd = Dir.pwd, {:cookbook_version => cookbook_version})
|
62
|
+
pattern = Chef::ChefFS::FilePattern.new(path)
|
63
|
+
local = Chef::ChefFS::FileSystem::ChefRepositoryFileSystemRootDir.new(
|
64
|
+
{
|
65
|
+
'environments' => ["#{local_root}/environments"],
|
66
|
+
'data_bags' => ["#{local_root}/data_bags"],
|
67
|
+
'cookbooks' => ["#{local_root}/cookbooks"]
|
68
|
+
}
|
69
|
+
)
|
70
|
+
Chef::ChefFS::FileSystem.copy_to(pattern, fs_config.chef_fs, local, 1, Chef::Config)
|
71
|
+
end
|
24
72
|
end
|
25
73
|
end
|
data/lib/promote/uploader.rb
CHANGED
@@ -4,8 +4,11 @@ require 'chef/chef_fs/config'
|
|
4
4
|
require 'chef/chef_fs/file_pattern'
|
5
5
|
require 'chef/chef_fs/file_system'
|
6
6
|
require 'chef/config'
|
7
|
-
require 'chef/knife/cookbook_upload'
|
8
7
|
require 'chef/chef_fs/path_utils'
|
8
|
+
require 'chef/knife'
|
9
|
+
unless defined? Chef::Knife::CookbookUpload
|
10
|
+
require 'chef/knife/cookbook_upload'
|
11
|
+
end
|
9
12
|
|
10
13
|
module Promote
|
11
14
|
class Uploader
|
@@ -21,8 +24,7 @@ module Promote
|
|
21
24
|
|
22
25
|
def upload_cookbooks
|
23
26
|
nothing_to_upload = true
|
24
|
-
|
25
|
-
server_cookbooks = rest.get_rest("/cookbooks?num_versions=all")
|
27
|
+
server_cookbooks = Utils.chef_server_cookbooks(config)
|
26
28
|
FileUtils.rm_rf(config.temp_directory) if Dir.exist?(config.temp_directory)
|
27
29
|
Dir.mkdir(config.temp_directory)
|
28
30
|
Dir.glob(File.expand_path("~/.berkshelf/cookbooks/*")).each do | dir |
|
@@ -44,20 +46,26 @@ module Promote
|
|
44
46
|
end
|
45
47
|
|
46
48
|
if !nothing_to_upload
|
49
|
+
upload_cookbook_directory(config.temp_directory)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def upload_cookbook_directory(directory)
|
54
|
+
if !Dir.glob(File.join(config.cookbook_directory, "*")).empty?
|
47
55
|
knife = Chef::Knife::CookbookUpload.new()
|
48
56
|
knife.config[:all] = true
|
49
57
|
knife.config[:freeze] = true
|
50
|
-
knife.config[:cookbook_path] =
|
58
|
+
knife.config[:cookbook_path] = directory
|
51
59
|
knife.run
|
52
60
|
end
|
53
61
|
end
|
54
|
-
|
62
|
+
|
55
63
|
def upload_environment(environment_name)
|
56
64
|
upload_file("/environments/#{environment_name}.json")
|
57
65
|
end
|
58
66
|
|
59
67
|
def upload_data_bags
|
60
|
-
upload_file("/data_bags")
|
68
|
+
upload_file("/data_bags/**/*.json")
|
61
69
|
end
|
62
70
|
|
63
71
|
attr_accessor :config
|
@@ -66,7 +74,7 @@ module Promote
|
|
66
74
|
|
67
75
|
def upload_file(file_path)
|
68
76
|
fs_config = Chef::ChefFS::Config.new
|
69
|
-
pattern = Chef::ChefFS::FilePattern.new(
|
77
|
+
pattern = Chef::ChefFS::FilePattern.new(file_path)
|
70
78
|
Chef::ChefFS::FileSystem.copy_to(pattern, fs_config.local_fs, fs_config.chef_fs, 1, Chef::Config)
|
71
79
|
file_path
|
72
80
|
end
|
data/lib/promote/versioner.rb
CHANGED
@@ -6,7 +6,7 @@ module Promote
|
|
6
6
|
class Versioner
|
7
7
|
def initialize(config = Config.new)
|
8
8
|
@config = config
|
9
|
-
@git =
|
9
|
+
@git = git_chef(config)
|
10
10
|
version_tags = @git.tags.select { |t| t.name[/^[0-9\.]+/] }
|
11
11
|
@current_tag = version_tags[-1]
|
12
12
|
@sha1 = @git.log(10000).between(current_tag.sha).first.sha
|
@@ -171,9 +171,6 @@ module Promote
|
|
171
171
|
"#{current_tag.name}.#{commit_count}"
|
172
172
|
end
|
173
173
|
|
174
|
-
|
175
|
-
return start_leaf if File.exist?(File.join(start_leaf, '.git'))
|
176
|
-
git_root(File.expand_path('..', start_leaf))
|
177
|
-
end
|
174
|
+
include Promote::GitHelper
|
178
175
|
end
|
179
176
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clc-promote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
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-10-
|
11
|
+
date: 2014-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chef
|
@@ -112,11 +112,14 @@ extra_rdoc_files:
|
|
112
112
|
- README.md
|
113
113
|
files:
|
114
114
|
- README.md
|
115
|
+
- lib/chef/knife/promote.rb
|
115
116
|
- lib/promote.rb
|
116
117
|
- lib/promote/config.rb
|
118
|
+
- lib/promote/git_helper.rb
|
117
119
|
- lib/promote/promoter.rb
|
118
120
|
- lib/promote/rake_tasks.rb
|
119
121
|
- lib/promote/uploader.rb
|
122
|
+
- lib/promote/utils.rb
|
120
123
|
- lib/promote/versioner.rb
|
121
124
|
homepage: https://github.com/tier3/DevOps/gems/clc-promote
|
122
125
|
licenses: []
|