Class: Chef::Knife::EcRestore

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/ec_restore.rb

Constant Summary collapse

PATHS =
%w(chef_repo_path cookbook_path environment_path data_bag_path role_path node_path client_path acl_path group_path container_path)
CONFIG_VARS =
%w(chef_server_url chef_server_root custom_http_headers node_name client_key versioned_cookbooks) + PATHS

Instance Method Summary collapse

Instance Method Details

#configure_chefObject



45
46
47
48
49
# File 'lib/chef/knife/ec_restore.rb', line 45

def configure_chef
  super
  Chef::Config[:concurrency] = config[:concurrency].to_i if config[:concurrency]
  ::ChefFS::Parallelizer.threads = (Chef::Config[:concurrency] || 10) - 1
end

#parallelize(entries, options = {}, &block) ⇒ Object



280
281
282
# File 'lib/chef/knife/ec_restore.rb', line 280

def parallelize(entries, options = {}, &block)
  ::ChefFS::Parallelizer.parallelize(entries, options, &block)
end

#put_acl(rest, url, acls) ⇒ Object



284
285
286
287
288
289
290
291
292
293
# File 'lib/chef/knife/ec_restore.rb', line 284

def put_acl(rest, url, acls)
  old_acls = rest.get_rest(url)
  old_acls = ::ChefFS::DataHandler::AclDataHandler.new.normalize(old_acls, nil)
  acls = ::ChefFS::DataHandler::AclDataHandler.new.normalize(acls, nil)
  if acls != old_acls
    ::ChefFS::FileSystem::AclEntry::PERMISSIONS.each do |permission|
      rest.put_rest("#{url}/#{permission}", { permission => acls[permission] })
    end
  end
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/chef/knife/ec_restore.rb', line 51

def run
  #Check for destination directory argument
  if name_args.length <= 0
    ui.error("Must specify backup directory as an argument.")
    exit 1
  end
  dest_dir = name_args[0]

  #Check for pivotal user and key
  node_name = Chef::Config.node_name
  client_key = Chef::Config.client_key
  if node_name != "pivotal"
    if !File.exist?("/etc/opscode/pivotal.pem")
      ui.error("Username not configured as pivotal and /etc/opscode/pivotal.pem does not exist.  It is recommended that you run this plugin from your Chef server.")
      exit 1
    end
    node_name = 'pivotal'
    client_key = '/etc/opscode/pivotal.pem'
  end

  #Check for WebUI Key
  if config[:webui_key] == nil
    if !File.exist?("/etc/opscode/webui_priv.pem")
      ui.error("WebUI not specified and /etc/opscode/webui_priv.pem does not exist.  It is recommended that you run this plugin from your Chef server.")
      exit 1
    end
    ui.warn("WebUI not specified. Using /etc/opscode/webui_priv.pem")
    webui_key = '/etc/opscode/webui_priv.pem'
  else
    webui_key = config[:webui_key]
  end

  #Set the server root
  server_root = Chef::Config.chef_server_root
  if server_root == nil
    server_root = Chef::Config.chef_server_url.gsub(/\/organizations\/+[^\/]+\/*$/, '')
    ui.warn("chef_server_root not found in knife configuration. Setting root to: #{server_root}")
    Chef::Config.chef_server_root = server_root
  end

  if config[:skip_version] && config[:skip_useracl]
    ui.warn("Skipping the Chef Server version check.  This will also skip any auto-configured options")
    user_acl_rest = nil
  elsif config[:skip_version] && !config[:skip_useracl]
    ui.warn("Skipping the Chef Server version check.  This will also skip any auto-configured options")
    user_acl_rest = rest
  else # Grab Chef Server version number so that we can auto set options
    uri = URI.parse("#{Chef::Config.chef_server_root}/version")
    server_version = open(uri, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).each_line.first.split(' ').last
    server_version_parts = server_version.split('.')

    if server_version_parts.count == 3
      puts "Detected Enterprise Chef Server version: #{server_version}"

      # All versions of Chef Server below 11.0.X are unable to update user acls
      if server_version_parts[0].to_i < 11 || (server_version_parts[0].to_i == 11 && server_version_parts[1].to_i == 0)
        ui.warn("Your version of Enterprise Chef Server does not support the updating of User ACLs.  Setting skip-useracl to TRUE")
        config[:skip_useracl] = true
        user_acl_rest = nil
      else
        user_acl_rest = rest 
      end             
    else
      ui.warn("Unable to detect Chef Server version.")
    end
  end

  # Restore users
  puts "Restoring users ..."

  rest = Chef::REST.new(Chef::Config.chef_server_root)

  Dir.foreach("#{dest_dir}/users") do |filename|
    next if filename !~ /(.+)\.json/
    name = $1
    if name == 'pivotal' && !config[:overwrite_pivotal]
      ui.warn("Skipping pivotal update.  To overwrite pivotal, pass --overwrite-pivotal.")
      next
    end

    # Update user object
    user = JSONCompat.from_json(IO.read("#{dest_dir}/users/#{name}.json"))
    begin
      # Supply password for new user
      user_with_password = user.dup
      user_with_password['password'] = SecureRandom.hex
      rest.post_rest('users', user_with_password)
    rescue Net::HTTPServerException => e
      if e.response.code == "409"
        rest.put_rest("users/#{name}", user)
      else
        raise
      end
    end

  end

  # Restore organizations
  Dir.foreach("#{dest_dir}/organizations") do |name|
    next if name == '..' || name == '.' || !File.directory?("#{dest_dir}/organizations/#{name}")
    puts "Restoring org #{name} ..."

    # Create organization
    org = JSONCompat.from_json(IO.read("#{dest_dir}/organizations/#{name}/org.json"))
    begin
      rest.post_rest('organizations', org)
    rescue Net::HTTPServerException => e
      if e.response.code == "409"
        rest.put_rest("organizations/#{name}", org)
      else
        raise
      end
    end

    # Restore open invitations
    invitations = JSONCompat.from_json(IO.read("#{dest_dir}/organizations/#{name}/invitations.json"))
    invitations.each do |invitation|
      begin
        rest.post_rest("organizations/#{name}/association_requests", { 'user' => invitation['username'] })
      rescue Net::HTTPServerException => e
        if e.response.code != "409"
          raise
        end
      end
    end

    # Repopulate org members
    members = JSONCompat.from_json(IO.read("#{dest_dir}/organizations/#{name}/members.json"))
    members.each do |member|
      username = member['user']['username']
      begin
        response = rest.post_rest("organizations/#{name}/association_requests", { 'user' => username })
        association_id = response["uri"].split("/").last
        rest.put_rest("users/#{username}/association_requests/#{association_id}", { 'response' => 'accept' })
      rescue Net::HTTPServerException => e
        if e.response.code != "409"
          raise
        end
      end
    end

    # Upload org data
    upload_org(dest_dir, webui_key, name)
  end

  # Restore user ACLs
  puts "Restoring user ACLs ..."
  Dir.foreach("#{dest_dir}/users") do |filename|
    next if filename !~ /(.+)\.json/
    name = $1
    if config[:skip_useracl]
      ui.warn("Skipping user ACL update for #{name}. To update this ACL, remove --skip-useracl or upgrade your Enterprise Chef Server.")
      next
    end
    if name == 'pivotal' && !config[:overwrite_pivotal]
      ui.warn("Skipping pivotal update.  To overwrite pivotal, pass --overwrite-pivotal.")
      next
    end

    # Update user acl
    user_acl = JSONCompat.from_json(IO.read("#{dest_dir}/user_acls/#{name}.json"))
    put_acl(rest, "users/#{name}/_acl", user_acl)          
  end


  if @error
    exit 1
  end
end

#upload_org(dest_dir, webui_key, name) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/chef/knife/ec_restore.rb', line 223

def upload_org(dest_dir, webui_key, name)
  old_config = {}
  CONFIG_VARS.each do |key|
    old_config[key] = Chef::Config[key.to_sym]
  end
  begin
    # Clear out paths
    PATHS.each do |path_var|
      Chef::Config[path_var.to_sym] = nil
    end
    Chef::Config.chef_repo_path = "#{dest_dir}/organizations/#{name}"
    Chef::Config.versioned_cookbooks = true

    Chef::Config.chef_server_url = "#{Chef::Config.chef_server_root}/organizations/#{name}"

    # Upload the admins group and billing-admins acls
    puts "Restoring the org admin data"
    chef_fs_config = ::ChefFS::Config.new
    %w(/groups/admins.json /groups/billing-admins.json /acls/groups/billing-admins.json).each do |name|
      pattern = ::ChefFS::FilePattern.new(name)
      if ::ChefFS::FileSystem.copy_to(pattern, chef_fs_config.local_fs, chef_fs_config.chef_fs, nil, config, ui, proc { |entry| chef_fs_config.format_path(entry) })
        @error = true
      end
    end

    # Figure out who the admin is so we can spoof him and retrieve his stuff
    rest = Chef::REST.new(Chef::Config.chef_server_url)
    org_admins = rest.get_rest('groups/admins')['users']
    org_members = rest.get_rest('users').map { |user| user['user']['username'] }
    org_admins.delete_if { |user| !org_members.include?(user) || user == 'pivotal' }
    if org_admins[0] != nil
      # Using an org admin already on the destination server
      Chef::Config.node_name = org_admins[0]
      Chef::Config.client_key = webui_key
    else
      # No suitable org admins found, defaulting to pivotal
      ui.warn("No suitable Organizational Admins found.  Defaulting to pivotal for org creation")
    end
    Chef::Config.custom_http_headers = (Chef::Config.custom_http_headers || {}).merge({'x-ops-request-source' => 'web'})

    # Restore the entire org skipping the admin data and restoring groups and acls last
    puts "Restoring the rest of the org"
    chef_fs_config = ::ChefFS::Config.new
    top_level_paths = chef_fs_config.local_fs.children.select { |entry| entry.name != 'acls' && entry.name != 'groups' }.map { |entry| entry.path }
    acl_paths = ::ChefFS::FileSystem.list(chef_fs_config.local_fs, ::ChefFS::FilePattern.new('/acls/*')).select { |entry| entry.name != 'groups' }.map { |entry| entry.path }
    group_acl_paths = ::ChefFS::FileSystem.list(chef_fs_config.local_fs, ::ChefFS::FilePattern.new('/acls/groups/*')).select { |entry| entry.name != 'billing-admins.json' }.map { |entry| entry.path }
    group_paths = ::ChefFS::FileSystem.list(chef_fs_config.local_fs, ::ChefFS::FilePattern.new('/groups/*')).select { |entry| entry.name != 'billing-admins.json' }.map { |entry| entry.path }
    (top_level_paths + group_paths + group_acl_paths + acl_paths).each do |path|
      ::ChefFS::FileSystem.copy_to(::ChefFS::FilePattern.new(path), chef_fs_config.local_fs, chef_fs_config.chef_fs, nil, config, ui, proc { |entry| chef_fs_config.format_path(entry) })
    end
   ensure
    CONFIG_VARS.each do |key|
      Chef::Config[key.to_sym] = old_config[key]
    end
  end
end