Class: KnifePromote::PromoteEnvironment

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/promote.rb

Instance Method Summary collapse

Instance Method Details

#runObject



24
25
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
51
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
83
84
85
86
# File 'lib/chef/knife/promote.rb', line 24

def run
  if @name_args.length < 2
    show_usage
    ui.error("You must specify a source and destination environment")
    exit 1
  end

  source_env = @name_args[0]
  dest_env = @name_args[1]

  promote_config = Promote::Config.new({
    :repo_root => File.expand_path("../",Chef::Config[:cookbook_path][0]),
    :node_name => Chef::Config[:node_name],
    :client_key => Chef::Config[:client_key],
    :chef_server_url => Chef::Config[:chef_server_url],
    :temp_directory => '/tmp/promote_staging',
    :bags => config[:bags]
  })
  destination_config = Promote::Config.new({
    :repo_root => promote_config.temp_directory,
    :node_name => Chef::Config[:knife][:promote_prod_user],
    :client_key => Chef::Config[:knife][:promote_prod_client_key],
    :chef_server_url => Chef::Config[:knife][:promote_prod_url]
  })

  # Confirm local git repo is in sync with origin/master
  source = "environments/#{source_env}.json"
  dest = "environments/#{dest_env}.json"
  r = Promote::GitRepo.new(promote_config.repo_root)
  r.check_sync(source, dest)

  ui.info "*** #{source_env} is being promoted to #{dest_env}"
  ui.confirm "Are you sure this is what you want to do"

  promoter = Promote::Promoter.new(promote_config)
  uploader = Promote::Uploader.new(promote_config)
  Chef::Config[:verbosity] = 2

  # Locally mirror the environment constraints over
  promoter.promote_to(source_env, dest_env, ui)

  if !config[:dry]
    ui.info "Committing and pushing changes to #{dest_env} back to git"
    env_file = File.join(promote_config.environment_directory, "#{dest_env}.json")
    repo = Promote::GitRepo.new(env_file)

    # If there were any pushes to the repo between the sync check
    # and here the commit will throw. So we do a pull to avoid that.
    repo.git.pull 
    repo.commit("promoted new constraints from #{source_env} to #{dest_env}", true)
  end

  # Uploads the environment to QA
  ui.info "uploading #{dest_env} to #{promote_config.chef_server_url}"
  uploader.upload_environment(dest_env)

  promoter.stage_promotion(dest_env, destination_config, ui)

  if !config[:dry]
    promoter.upload_to(dest_env, destination_config, ui)
    ui.info "promotion complete. Congratulations #{dest_env} on a well deserved promotion!!"
  end
end