Skip to content

Instantly share code, notes, and snippets.

@jeremiahsnapp
Last active March 25, 2016 21:37
Show Gist options
  • Save jeremiahsnapp/84e6339a3c569ccb364c to your computer and use it in GitHub Desktop.
Save jeremiahsnapp/84e6339a3c569ccb364c to your computer and use it in GitHub Desktop.
Manually download Open Source Chef Server 11 data to migrate to separate Chef Server 12 server
require 'pg'
require 'json'
def pull_chef11_db_credentials
puts "Pulling open source Chef 11 database credentials"
if !File.exists?("/etc/chef-server/chef-server-running.json")
puts "Failed to find /etc/chef-server/chef-server-running.json"
exit 1
end
running_config = JSON.parse(File.read("/etc/chef-server/chef-server-running.json"))
sql_host = running_config['chef_server']['postgresql']['vip']
sql_port = running_config['chef_server']['postgresql']['port']
sql_user = running_config['chef_server']['postgresql']['sql_user']
sql_password = running_config['chef_server']['postgresql']['sql_password']
[sql_host, sql_port, sql_user, sql_password]
end
def create_chef11_key_file(key_file)
sql_host, sql_port, sql_user, sql_password = pull_chef11_db_credentials
db = PG.connect(:dbname => 'opscode_chef', :user => sql_user, :password => sql_password, :host => sql_host, :port => sql_port)
result = db.exec("select username, id, public_key, hashed_password, salt, hash_type from osc_users")
sql_users_json = JSON.generate(result.entries)
IO.write(key_file, sql_users_json)
end
chef11_data_dir = ARGV.shift
key_file = "#{chef11_data_dir}/key_dump.json"
create_chef11_key_file(key_file)

There are two methods for downloading OSC data so it can be migrated to a separate CS12 server.

Ref: http://docs.chef.io/upgrade_server_open_source_notes.html#subcommand-reference

Method 1: Install CS12 and use chef12-upgrade-download

Installing CS12 package on the OSC server makes the chef12-upgrade-download command available. The CS12 package installs to /opt/opscode so it doesn't overwrite any of the OSC package. The only impactful change is that the CS12 package install points the /usr/bin/chef-server-ctl link to the CS12 command. After using chef12-upgrade-download to download the OSC data the CS12 package can be uninstalled and the /usr/bin/chef-server-ctl link can be pointed back to the OSC command.

Now you can tar that entire data download directory up and untar it on the new Chef Server 12 server. Then run the transform and upload commands.

Method 2: Use knife download and a custom script

If the customer absolutely does not want to install CS12 on the OSC server then they can use the following instructions to download the data.

To manually extract all data from the Open Source Chef Server you need to create a directory on the server where you want to store all the extracted data.

Create a /tmp/knife.rb file that has the following parameters. Set chef_repo_path to the path of the directory you just created.

chef_server_url 'https://localhost'
node_name 'admin'
client_key '/etc/chef-server/admin.pem'
repo_mode 'everything'
versioned_cookbooks true
chef_repo_path '/directory/where/data/will/be/extracted'

Now use knife to download all data except users.

/opt/chef-server/embedded/bin/knife download -c /tmp/knife-config.rb /

To extract the users data you need to upload the create_chef11_key_file.rb script to the Chef server. Any location such as root user's home directory will be fine.

Run the create_chef11_key_file.rb script and specify the path to the data extraction directory.

/opt/chef-server/embedded/bin/ruby create_chef11_key_file.rb /directory/where/data/will/be/extracted

After running the script you should find a file named /directory/where/data/will/be/extracted/key_dump.json with the users data in it.

Now you can tar that entire data extraction directory up and untar it on the new Chef Server 12 server and run the transform and upload commands.

Code Reference:

https://github.com/opscode/opscode-omnibus/blob/12.0.0/files/private-chef-ctl-commands/chef12_upgrade_download.rb#L81-L84

https://github.com/opscode/opscode-omnibus/blob/12.0.0/files/private-chef-ctl-commands/open_source_chef12_upgrade.rb#L337-L388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment