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, #git

Constructor Details

#initialize(*args) ⇒ TableServe

Returns a new instance of TableServe.



68
69
70
71
# File 'lib/chef/knife/table_serve.rb', line 68

def initialize(*args)
  super
  @environments = config[:environments].to_s.split(",").map(&:strip)
end

Instance Method Details

#freeze_cookbook(cookbook) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/chef/knife/table_serve.rb', line 156

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)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/knife/table_serve.rb', line 137

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/chef/knife/table_serve.rb', line 183

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



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

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



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/chef/knife/table_serve.rb', line 202

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



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
130
131
132
133
134
135
# File 'lib/chef/knife/table_serve.rb', line 73

def run
  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)
  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



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

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

#upload_data_bags(bag) ⇒ Object



229
230
231
232
# File 'lib/chef/knife/table_serve.rb', line 229

def upload_data_bags(bag)
  data_bag_load = loader(:data_bags).load_from('data_bags', bag.split.first, bag.split.last)
  data_bag_load.save
end

#upload_environmentsObject



175
176
177
178
179
180
181
# File 'lib/chef/knife/table_serve.rb', line 175

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



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

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