Class: KnifeTable::TableServe

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

Instance Method Summary collapse

Methods included from Helpers

#cookbook_path, #discover_changed, #git

Instance Method Details

#freeze_cookbook(cookbook) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/knife/table_serve.rb', line 150

def freeze_cookbook(cookbook)
  unless(frozen_cookbook?(cookbook))
    spork_upload = KnifeSpork::SporkUpload.new
    spork_upload.config[:freeze] = true
    spork_upload.config[:cookbook_path] = cookbook_path
    repo_cookbook = spork_upload.cookbook_repo[cookbook]
    repo_cookbook.freeze_version
    Chef::CookbookUploader.new(repo_cookbook, cookbook_path).upload_cookbook
    ui.highline.say "#{cookbook} "
  else
    ui.highline.say "#{ui.highline.color("#{cookbook} (already frozen)", HighLine::RED)} "
  end
end

#frozen_cookbook?(cookbook) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chef/knife/table_serve.rb', line 131

def frozen_cookbook?(cookbook)
  begin
    spork_check = KnifeSpork::SporkCheck.new
    spork_check.check_frozen(
      cookbook,
      spork_check.get_version(
        cookbook_path,
        cookbook
      )
    )
  rescue  Net::HTTPServerException => e
    if(e.response.is_a?(Net::HTTPNotFound))
      false
    else
      raise
    end
  end
end

#git_commit_environments(cookbooks) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/chef/knife/table_serve.rb', line 177

def git_commit_environments(cookbooks)
  commit = false
  @environments.each do |environment|
    status = git.status.changed[::File.join('environments', "#{environment}.json")]
    if(status && !git.diff('HEAD', status.path).none?)
      git.add(status.path)
      commit_message = "Environment #{environment} cookbook version updates\n"
      cookbooks.sort.each do |cookbook|
        commit_message << "\n#{cookbook}: #{spork_promote.get_version(cookbook_path, cookbook)}"
      end
      git.commit(commit_message)
      ui.highline.say "#{environment} "
      @git_changed = true
      commit = true
    end
  end
  ui.highline.say "nothing to commit " unless commit
end

#git_pushObject



209
210
211
212
213
214
215
216
# File 'lib/chef/knife/table_serve.rb', line 209

def git_push
  if(@git_changed)
    git.push(config[:git_remote_name], config[:git_branch], true)
    ui.highline.say "pushed #{config[:git_branch]} to #{config[:git_remote_name]} "
  else
    ui.highline.say "nothing to push "
  end
end

#git_tag(cookbooks) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/chef/knife/table_serve.rb', line 196

def git_tag(cookbooks)
  cookbooks.each do |cookbook|
    tag_string = "#{cookbook}-v#{spork_promote.get_version(cookbook_path, cookbook)}"
    unless(git.tags.map(&:name).include?(tag_string))
      git.add_tag(tag_string)
      ui.highline.say "#{tag_string} "
      @git_changed = true
    else
      ui.highline.say "#{ui.highline.color("#{tag_string} (exists)", HighLine::RED)} "
    end
  end
end

#runObject



66
67
68
69
70
71
72
73
74
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
123
124
125
126
127
128
129
# File 'lib/chef/knife/table_serve.rb', line 66

def run
  check_config_options
  sanity_checks
  cookbooks = discover_changed(:cookbooks, *determine_commit_span).map{|c|c.split('/').first}
  roles = discover_changed(:roles, *determine_commit_span) if config[:upload_roles]
  data_bags = discover_changed(:data_bags, *determine_commit_span) if config[:upload_data_bags]

  ui.msg ui.highline.color("#{' ' * 10}** Knife Table: Service started  **", [HighLine::GREEN, HighLine::BOLD])
  ui.highline.say ui.highline.color("Discovered cookbooks staged for freeze: #{cookbooks.join(', ')}", HighLine::CYAN) unless cookbooks.nil? || cookbooks.empty?
  ui.highline.say ui.highline.color("Environments staged to be updated: #{@environments.join(', ')}", HighLine::CYAN) unless @environments.empty?
  ui.highline.say ui.highline.color("Roles staged to be uploaded: #{roles.sort.map{|r|r.sub(/\.(rb|json)/, '')}.join(', ')}", HighLine::CYAN) unless roles.nil? || roles.empty?
  ui.highline.say ui.highline.color("Data Bags staged to be uploaded: #{data_bags.sort.join(', ')}", HighLine::CYAN) unless data_bags.nil? || data_bags.empty?

  ui.highline.say "\n"

  if(config[:autoproceed])
    ui.warn "Autoproceeding based on config (ctrl+c to halt)"
    sleep(3)
  else
    ui.confirm "Proceed"
  end
  
  ui.highline.say "\n"

  ui.highline.say "#{ui.highline.color("Freezing cookbooks:", HighLine::GREEN)} "
  cookbooks.each{|c| freeze_cookbook(c) }
  ui.highline.say "\n"

  unless(@environments.empty?)
    ui.msg ui.highline.color("Updating environments:", HighLine::GREEN)
    @environments.each do |env|
      ui.highline.say "  #{ui.highline.color(env, HighLine::BLUE)}: "
      cookbooks.each{|c| update_environments(env, c) }
      ui.highline.say "\n"
    end

    ui.highline.say "#{ui.highline.color("Uploading environments:", HighLine::GREEN)} "
    upload_environments
    ui.highline.say "\n"
  end

  upload_changes(:roles, roles) if roles && !roles.empty?
  upload_changes(:data_bags, data_bags) if data_bags && !data_bags.empty?

  if(config[:git_autocommit])
    ui.highline.say "#{ui.highline.color("Committing environments:", HighLine::GREEN)} "
    git_commit_environments(cookbooks)
    ui.highline.say "\n"
  end

  if(config[:git_tag])
    ui.highline.say "#{ui.highline.color("Tagging cookbooks:", HighLine::GREEN)} "
    git_tag(cookbooks)
    ui.highline.say "\n"
  end

  if(config[:git_autopush])
    ui.highline.say "#{ui.highline.color("Pushing changes to remote repo:", HighLine::GREEN)} "
    git_push
    ui.highline.say "\n"
  end

  ui.msg ui.highline.color("#{' ' * 10}** Knife Table: Service complete **", [HighLine::GREEN, HighLine::BOLD])
end

#update_environments(environment, cookbook) ⇒ Object



164
165
166
167
# File 'lib/chef/knife/table_serve.rb', line 164

def update_environments(environment, cookbook)
  promote_environment(environment, cookbook)
  ui.highline.say "#{cookbook} "
end

#upload_data_bags(bag) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/chef/knife/table_serve.rb', line 224

def upload_data_bags(bag)
  data_bag_load = loader(:data_bags).load_from('data_bags', bag.split('/').first, bag.split('/').last)
  dbag = Chef::DataBagItem.new
  dbag.data_bag(bag.split('/').first)
  dbag.raw_data = data_bag_load
  dbag.save
  dbag
end

#upload_environmentsObject



169
170
171
172
173
174
175
# File 'lib/chef/knife/table_serve.rb', line 169

def upload_environments
  @environments.each do |environment|
    spork_promote.config[:cookbook_path] = [cookbook_path]
    spork_promote.save_environment_changes_remote(environment)
    ui.highline.say "#{environment} "
  end
end

#upload_roles(role) ⇒ Object



218
219
220
221
222
# File 'lib/chef/knife/table_serve.rb', line 218

def upload_roles(role)
  role_load = loader(:roles).load_from('roles', role)
  role_load.save
  role_load
end