Class: Promote::Promoter

Inherits:
Object
  • Object
show all
Includes:
ChefServer
Defined in:
lib/promote/promoter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefServer

#with_chef_server

Constructor Details

#initialize(config = Config.new) ⇒ Promoter

Returns a new instance of Promoter.



10
11
12
# File 'lib/promote/promoter.rb', line 10

def initialize(config = Config.new)
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/promote/promoter.rb', line 8

def config
  @config
end

Instance Method Details

#monitor_promotion(source_environment, destination_environments, number_to_test, converge_interval, force = false, ui = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/promote/promoter.rb', line 26

def monitor_promotion(source_environment, destination_environments, number_to_test, converge_interval, force = false, ui = nil)
  ui.info "Waiting for #{source_environment} to finish" if ui
  check_promotion(source_environment, converge_interval, ui)

  test_environments = destination_environments[0, number_to_test]
  final_environments = []
  if destination_environments.count > number_to_test
    final_environments = destination_environments[number_to_test..-1]
  end

  test_environments.each do |env|
    promote_and_upload(source_environment, env, force, ui)
    check_promotion(env, converge_interval, ui)
  end

  final_environments.each do |env|
    promote_and_upload(source_environment, env, force, ui)
  end
  start = Time.now.to_i
  final_environments.each do |env|
    check_promotion(env, converge_interval, ui, start)
  end

  ui.info 'This promotion is complete' if ui
end

#promote_to(source_environment, destination_environment, ui = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/promote/promoter.rb', line 14

def promote_to(source_environment, destination_environment, ui = nil)
  source = EnvironmentFile.new(source_environment, config)
  dest = EnvironmentFile.new(destination_environment, config)

  if ui
    ui.info "Cookbook constraints being promoted to #{destination_environment}"
    ui.info source.cookbook_versions
  end

  dest.write_cookbook_versions(source.cookbook_versions)
end

#stage_promotion(promote_environment, destination_config, ui) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/promote/promoter.rb', line 52

def stage_promotion(promote_environment, destination_config, ui)
  ui.info "Staging artifacts from #{config.chef_server_url} to #{config.temp_directory}"
  config.reset_temp_dir

  deploy_file = "/environments/#{promote_environment}.json"
  ui.info "Downloading #{deploy_file}..."
  download_files(config.temp_directory, deploy_file)

  ui.info "Downloading data bags..."
  config.bags.each do |bag|
    download_files(config.temp_directory, "/data_bags/#{bag}")
  end

  ui.info "Downloading roles..."
  download_files(config.temp_directory, "/roles")

  ui.info "Downloading cookbooks missing on #{destination_config.chef_server_url}..."
  ui.info "Fetching all versions of cookbooks on #{destination_config.chef_server_url}..."
  server_versions = Promote::Utils.chef_server_cookbooks(destination_config)
  ui.info "Fetching the environment file content of #{promote_environment}"
  env_content = EnvironmentFile.new(promote_environment, Config.new({:repo_root => config.temp_directory}))
  env_content.cookbook_versions.each do |k,v|
    ui.info "#{k}-#{v}"
    if !server_versions.has_key?(k) || (server_versions[k]["versions"].select {|version| version['version'] == v}).empty?
      ui.info "Cookbook missing. Downloading..."
      download_files(config.temp_directory, "/cookbooks/#{k}", v)
    else
      ui.info "Cookbook already exists."
    end
  end
end

#upload_to(promote_environment, destination_config, ui) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/promote/promoter.rb', line 84

def upload_to(promote_environment, destination_config, ui)
  ui.info "Uploading staged artifacts to #{destination_config.chef_server_url}"
  uploader = Uploader.new(destination_config)
  Chef::Config[:verbosity] = 2
  ui.info("Uploading #{promote_environment} environment file...")
  uploader.upload_environment(promote_environment)
  ui.info("Uploading data bags...")
  uploader.upload_data_bags
  ui.info("Uploading roles...")
  uploader.upload_roles
  ui.info("Uploading cookbooks...")
  uploader.upload_cookbook_directory(destination_config.cookbook_directory, ui)
end