Class: KnifeSpork::SporkPromote

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

Instance Method Summary collapse

Methods included from Runner

included

Instance Method Details

#check_cookbook_uploaded(cookbook_name) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chef/knife/spork-promote.rb', line 143

def check_cookbook_uploaded(cookbook_name)
  validate_version!(config[:version])
  version = config[:version] || load_cookbook(cookbook_name).version

  environment = config[:environment]
  api_endpoint = environment ? "environments/#{environment}/cookbooks/#{cookbook_name}/#{version}" : "cookbooks/#{cookbook_name}/#{version}"

  begin
    cookbooks = rest.get_rest(api_endpoint)
  rescue Net::HTTPServerException => e
    ui.error "#{cookbook_name}@#{version} does not exist on Chef Server! Upload the cookbook first by running:\n\n\tknife spork upload #{cookbook_name}\n\n"
    exit(1)
  end
end

#promote(environment, cookbook_names) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/knife/spork-promote.rb', line 131

def promote(environment, cookbook_names)
  cookbook_names = [cookbook_names].flatten

  cookbook_names.each do |cookbook_name|
    validate_version!(config[:version])
    version = config[:version] || load_cookbook(cookbook_name).version

    ui.msg "Adding version constraint #{cookbook_name} = #{version}"
    update_version_constraints(environment, cookbook_name, version)
  end
end

#runObject



22
23
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
# File 'lib/chef/knife/spork-promote.rb', line 22

def run
  self.config = Chef::Config.merge!(config)

  if @name_args.empty?
    show_usage
    ui.error("You must specify the cookbook and environment to promote to")
    exit 1
  end

  #First load so plugins etc know what to work with
  @environments, @cookbook = load_environments_and_cookbook

  run_plugins(:before_promote)

  #Reload cookbook and env in case a VCS plugin found updates
  @environments, @cookbook = load_environments_and_cookbook

  check_cookbook_uploaded(@cookbook)

  @environments.each do |e|
    environment = load_environment(e)

    if @cookbook == 'all'
      ui.msg "Promoting ALL cookbooks to environment #{environment}"
      promote(environment, all_cookbooks)
    else
      promote(environment, @cookbook)
    end

    ui.msg "Saving changes to #{e}.json"

    new_environment_json = pretty_print_json(environment)
    save_environment_changes(e, new_environment_json)

    if config[:remote]
      ui.msg "Uploading #{environment.name}.json to Chef Server"
      save_environment_changes_remote(e)
      ui.info "Promotion complete at #{Time.now}!"
    else
      ui.info "Promotion complete. Don't forget to upload your changed #{environment.name}.json to Chef Server"
    end
  end
  run_plugins(:after_promote_local)
  if config[:remote]
    run_plugins(:after_promote_remote)
  end
end

#save_environment_changes(environment, json) ⇒ Object



124
125
126
127
128
129
# File 'lib/chef/knife/spork-promote.rb', line 124

def save_environment_changes(environment, json)
  environments_path = cookbook_path.gsub('cookbooks', 'environments')
  environment_path = File.expand_path( File.join(environments_path, "#{environment}.json") )

  File.open(environment_path, 'w'){ |f| f.puts(json) }
end

#save_environment_changes_remote(environment) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chef/knife/spork-promote.rb', line 75

def save_environment_changes_remote(environment)
  local_environment = load_environment(environment)
  remote_environment = load_remote_environment(environment)
  @environment_diffs ||= Hash.new
  @environment_diffs["#{environment}"] = environment_diff(local_environment, remote_environment)
  
  version_change_threshold = spork_config.version_change_threshold || 2
  env_constraints_diff = constraints_diff(@environment_diffs["#{environment}"]).select{|k,v| v > version_change_threshold}
  
  if env_constraints_diff.size !=0 then
    ui.warn 'You\'re about to promote a significant version number change to 1 or more cookbooks:'
    ui.warn @environment_diffs["#{environment}"].select{|k,v|env_constraints_diff.has_key?(k)}.collect{|k,v| "\t#{k}: #{v}"}.join("\n")

    begin
      ui.confirm('Are you sure you want to continue?')
    rescue SystemExit => e
      if e.status == 3
        ui.confirm("Would you like to reset your local #{environment}.json to match the remote server?")
        tmp = Chef::Environment.load(environment)
        save_environment_changes(environment, pretty_print_json(tmp))
        ui.info "#{environment}.json was reset"
      end

      raise
    end
  end
  
  if  @environment_diffs["#{environment}"].size > 1
    ui.msg ""
    ui.warn "You're about to promote changes to several cookbooks at once:"
    ui.warn @environment_diffs["#{environment}"].collect{|k,v| "\t#{k}: #{v}"}.join("\n")

    begin
      ui.confirm('Are you sure you want to continue?')
    rescue SystemExit => e
      if e.status == 3
        ui.confirm("Would you like to reset your local #{environment}.json to match the remote server?")
        tmp = Chef::Environment.load(environment)
        save_environment_changes(environment, pretty_print_json(tmp))
        ui.info "#{environment}.json was reset"
      end

      raise
    end
  end

  local_environment.save
end

#update_version_constraints(environment, cookbook, new_version) ⇒ Object



70
71
72
73
# File 'lib/chef/knife/spork-promote.rb', line 70

def update_version_constraints(environment, cookbook, new_version)
  validate_version!(new_version)
  environment.cookbook_versions[cookbook] = "= #{new_version}"
end