Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source 'https://rubygems.org'

gem 'veil', git: 'https://github.com/chef/chef_secrets', branch: 'main'
gem "chef", git: 'https://github.com/chef/chef.git', branch: 'praj/add_frozen_info_to_metadata_chef_18'
gemspec

group :development do
Expand All @@ -9,7 +10,7 @@ group :development do
gem 'fakefs'
gem 'simplecov'
gem "chef-zero", "~> 15" # eval when we drop ruby 2.6
gem "chef", "~> 18"
gem "ohai", "~> 18" # eval when we drop ruby 2.6
gem "knife", "~> 18"
# gem "chef", "~> 18"
gem "ohai" # eval when we drop ruby 2.6
gem "knife"
end
3 changes: 2 additions & 1 deletion knife-ec-backup.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Gem::Specification.new do |s|
# s.add_dependency "mixlib-cli", ">= 1.2.2"
s.add_dependency "sequel", "~> 5.9"
s.add_dependency "pg"
s.add_dependency "chef", "~> 18.0"
s.add_dependency "chef"
# s.add_dependency "chef", git: "https://github.com/chef/chef.git", branch: "praj/add_frozen_info_to_metadata"
s.add_dependency "veil"
s.add_dependency "knife-tidy"

Expand Down
1 change: 1 addition & 0 deletions lib/chef/knife/ec_backup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'chef'
require 'chef/knife'
require_relative 'ec_base'

Expand Down
1 change: 1 addition & 0 deletions lib/chef/knife/ec_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#

require 'chef'
require 'chef/knife'
require 'chef/server_api'
require 'veil' unless defined?(Veil)
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/ec_key_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef'
require 'chef/knife'
require 'veil'

Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/ec_key_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef'
require 'chef/knife'
require_relative 'ec_key_base'

Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/ec_key_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef'
require 'chef/knife'
require_relative 'ec_key_base'
require_relative '../org_id_cache'
Expand Down
49 changes: 47 additions & 2 deletions lib/chef/knife/ec_restore.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'chef'
require 'chef/knife'
require_relative 'ec_base'

Expand Down Expand Up @@ -214,6 +215,48 @@ def restore_key_sql
k.config[:clients_only] = true
k.run
end
def upload_cookbooks_custom(cookbooks_dir)
Dir.glob("#{cookbooks_dir}/*/metadata.json").each do |metadata_path|
begin
metadata = JSON.parse(File.read(metadata_path))
cookbook_dir = File.dirname(metadata_path)
cookbook_name = metadata["name"]

if cookbook_name.nil? || cookbook_name.strip.empty?
ui.warn "Skipping cookbook in #{cookbook_dir} — no name in metadata.json"
next
end

frozen = metadata.delete("frozen")

upload_cmd = [
"knife", "cookbook", "upload", cookbook_name,
"-o", cookbooks_dir
]

if frozen
upload_cmd += ["--freeze", "--force"]
end

ui.msg "Uploading cookbook #{cookbook_name} with command: #{upload_cmd.join(' ')}"
unless system(*upload_cmd)
ui.warn "Failed to upload cookbook #{cookbook_name}"
next
end

# Remove `"frozen": true` and rewrite metadata.json
if frozen
File.write(metadata_path, JSON.pretty_generate(metadata))
ui.msg "Removed 'frozen': true from #{metadata_path}"
end

rescue JSON::ParserError => e
ui.warn "Skipping invalid JSON in #{metadata_path}: #{e}"
rescue => e
ui.warn "Error processing cookbook at #{metadata_path}: #{e}"
end
end
end

PATHS = %w(chef_repo_path cookbook_path environment_path data_bag_path role_path node_path client_path acl_path group_path container_path)
def upload_org_data(name)
Expand Down Expand Up @@ -275,11 +318,14 @@ def upload_org_data(name)
else
server.supports_defaulting_to_pivotal? ? 'pivotal' : org_admin
end
# Upload cookbooks with custom logic
cookbooks_dir = "#{dest_dir}/organizations/#{name}/cookbooks"
upload_cookbooks_custom(cookbooks_dir)

# Restore the entire org skipping the admin data and restoring groups and acls last
ui.msg "Restoring the rest of the org"
chef_fs_config = Chef::ChefFS::Config.new
top_level_paths = chef_fs_config.local_fs.children.select { |entry| entry.name != 'acls' && entry.name != 'groups' }.map { |entry| entry.path }
top_level_paths = chef_fs_config.local_fs.children.select { |entry| entry.name != 'acls' && entry.name != 'groups' && entry.name != 'cookbooks' }.map { |entry| entry.path }

# Topologically sort groups for upload
filenames = ['billing-admins.json', 'public_key_read_access.json']
Expand Down Expand Up @@ -316,7 +362,6 @@ def upload_org_data(name)
# abstract it inside EcBase.
def chef_fs_copy_pattern(pattern_str, chef_fs_config)
ui.msg "Copying #{pattern_str}"
pattern = Chef::ChefFS::FilePattern.new(pattern_str)
Chef::ChefFS::FileSystem.copy_to(pattern, chef_fs_config.local_fs,
chef_fs_config.chef_fs, nil,
config, ui,
Expand Down
1 change: 1 addition & 0 deletions spec/chef/knife/ec_base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
require 'chef/knife/ec_base'
require 'chef'
require 'chef/knife'
require 'chef/config'
require 'stringio' unless defined?(StringIO)
Expand Down
Loading