Class: KnifePlayground::PgClientnodeDelete::PgGitCookbookUpload

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

Overview

Most of this code comes from Opscode Knife cookbook_upload.rb plugin:

github.com/opscode/chef/blob/master/chef/lib/chef/knife/cookbook_upload.rb

Minor modifications to add Git support added

Constant Summary collapse

CHECKSUM =
"checksum"
MATCH_CHECKSUM =
/[0-9a-f]{32,}/

Instance Method Summary collapse

Instance Method Details

#cookbook_repoObject



160
161
162
163
164
165
# File 'lib/chef/knife/pg.rb', line 160

def cookbook_repo
  @cookbook_loader ||= begin
    Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest, config[:cookbook_path]) }
    Chef::CookbookLoader.new(config[:cookbook_path])
  end
end

#environmentObject



175
176
177
# File 'lib/chef/knife/pg.rb', line 175

def environment
  @environment ||= config[:environment] ? Environment.load(config[:environment]) : nil
end

#runObject



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/knife/pg.rb', line 106

def run
  git_urls = @name_args.dup
  git_urls.each do |n| 
    if n =~ /^git:\/\//
      @name_args.delete n
      git_repo = n
      git_clone(git_repo)
    end
  end
  
  config[:cookbook_path] ||= Chef::Config[:cookbook_path]

  assert_environment_valid!
  version_constraints_to_update = {}
  # Get a list of cookbooks and their versions from the server
  # for checking existence of dependending cookbooks.
  @server_side_cookbooks = Chef::CookbookVersion.list

  if config[:all]
    justify_width = cookbook_repo.cookbook_names.map {|name| name.size}.max.to_i + 2
    cookbook_repo.each do |cookbook_name, cookbook|
      cookbook.freeze_version if config[:freeze]
      upload(cookbook, justify_width)
      version_constraints_to_update[cookbook_name] = cookbook.version
    end
  else
    if @name_args.empty?
      show_usage
      ui.error("You must specify the --all flag or at least one cookbook name")
      exit 1
    end
    justify_width = @name_args.map {|name| name.size }.max.to_i + 2
    @name_args.each do |cookbook_name|
      begin
        cookbook = cookbook_repo[cookbook_name]
        if config[:depends]
          cookbook..dependencies.each do |dep, versions|
            @name_args.push dep
          end
        end
        cookbook.freeze_version if config[:freeze]
        upload(cookbook, justify_width)
        version_constraints_to_update[cookbook_name] = cookbook.version
      rescue Chef::Exceptions::CookbookNotFoundInRepo => e
        ui.error("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
        Chef::Log.debug(e)
      end
    end
  end

  ui.info "upload complete"
  update_version_constraints(version_constraints_to_update) if config[:environment]
end

#update_version_constraints(new_version_constraints) ⇒ Object



167
168
169
170
171
172
# File 'lib/chef/knife/pg.rb', line 167

def update_version_constraints(new_version_constraints)
  new_version_constraints.each do |cookbook_name, version|
    environment.cookbook_versions[cookbook_name] = "= #{version}"
  end
  environment.save
end