Class: Chef::Knife::EcBackup

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/ec_backup.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



39
40
41
42
43
# File 'lib/chef/knife/ec_backup.rb', line 39

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

#download_org(dest_dir, webui_key, name) ⇒ Object



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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/chef/knife/ec_backup.rb', line 178

def download_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}"

    ensure_dir(Chef::Config.chef_repo_path)

    # Download the billing-admins ACL and group as pivotal
    chef_fs_config = ::ChefFS::Config.new
    pattern = ::ChefFS::FilePattern.new('/acls/groups/billing-admins.json') 
    if ::ChefFS::FileSystem.copy_to(pattern, chef_fs_config.chef_fs, chef_fs_config.local_fs, nil, config, ui, proc { |entry| chef_fs_config.format_path(entry) })
      @error = true
    end
    pattern = ::ChefFS::FilePattern.new('/groups/billing-admins.json') 
    if ::ChefFS::FileSystem.copy_to(pattern, chef_fs_config.chef_fs, chef_fs_config.local_fs, nil, config, ui, proc { |entry| chef_fs_config.format_path(entry) })
      @error = true
    end
    pattern = ::ChefFS::FilePattern.new('/groups/admins.json') 
    if ::ChefFS::FileSystem.copy_to(pattern, chef_fs_config.chef_fs, chef_fs_config.local_fs, nil, config, ui, proc { |entry| chef_fs_config.format_path(entry) })
      @error = true
    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)
    admin_users = rest.get_rest('groups/admins')['users']
    org_members = rest.get_rest('users').map { |user| user['user']['username'] }
    admin_users.delete_if { |user| !org_members.include?(user) }
    Chef::Config.node_name = admin_users[0]
    Chef::Config.client_key = webui_key
    Chef::Config.custom_http_headers = (Chef::Config.custom_http_headers || {}).merge({'x-ops-request-source' => 'web'})

    # Download the entire org skipping the billing admins group ACL and the group itself
    chef_fs_config = ::ChefFS::Config.new
    top_level_paths = chef_fs_config.chef_fs.children.select { |entry| entry.name != 'acls' && entry.name != 'groups' }.map { |entry| entry.path }
    acl_paths = ::ChefFS::FileSystem.list(chef_fs_config.chef_fs, ::ChefFS::FilePattern.new('/acls/*')).select { |entry| entry.name != 'groups' }.map { |entry| entry.path }
    group_acl_paths = ::ChefFS::FileSystem.list(chef_fs_config.chef_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.chef_fs, ::ChefFS::FilePattern.new('/groups/*')).select { |entry| entry.name != 'billing-admins.json' }.map { |entry| entry.path }
    (top_level_paths + group_acl_paths + acl_paths + group_paths).each do |path|
      ::ChefFS::FileSystem.copy_to(::ChefFS::FilePattern.new(path), chef_fs_config.chef_fs, chef_fs_config.local_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

#ensure_dir(dir) ⇒ Object



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

def ensure_dir(dir)
  if !File.exist?(dir)
    Dir.mkdir(dir)
  end
end

#runObject



45
46
47
48
49
50
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
# File 'lib/chef/knife/ec_backup.rb', line 45

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
    Chef::Config.node_name = 'pivotal'
    Chef::Config.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

  rest = Chef::REST.new(Chef::Config.chef_server_root)
 
  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.1 are missing the GET User ACL helper in nginx
      if server_version_parts[0].to_i < 11 || (server_version_parts[0].to_i == 11 && server_version_parts[1].to_i == 0 && server_version_parts[0].to_i < 1)
        #Check to see if Opscode-Account can be directly from the local machine  
        begin
          user_acl_rest.get('users')
          ui.warn("Your version of Enterprise Chef Server does not support the downloading of User ACLs.  Using local connection to backup")
          user_acl_rest = Chef::REST.new("http://127.0.0.1:9465")
        rescue
          ui.warn("Your version of Enterprise Chef Server does not support the downloading of User ACLs.  Setting skip-useracl to TRUE")
          config[:skip_useracl] = true
          user_acl_rest = nil
        end
      else
        user_acl_rest = rest
      end

    else
      ui.warn("Unable to detect Chef Server version.")
    end
  end

  # Grab users
  puts "Grabbing users ..."

  ensure_dir("#{dest_dir}/users")
  ensure_dir("#{dest_dir}/user_acls")

  rest.get_rest('/users').each_pair do |name, url|
    File.open("#{dest_dir}/users/#{name}.json", 'w') do |file|
      file.write(Chef::JSONCompat.to_json_pretty(rest.get_rest(url)))
    end

    if config[:skip_useracl]
      ui.warn("Skipping user ACL download for #{name}. To download this ACL, remove --skip-useracl or upgrade your Enterprise Chef Server.")
      next
    end

    File.open("#{dest_dir}/user_acls/#{name}.json", 'w') do |file|
      file.write(Chef::JSONCompat.to_json_pretty(user_acl_rest.get_rest("users/#{name}/_acl")))
    end
  end

  # Download organizations
  ensure_dir("#{dest_dir}/organizations")
  rest.get_rest('/organizations').each_pair do |name, url|
    do_org = (config[:org].nil? || config[:org] == name)
    org = rest.get_rest(url)
    if org['assigned_at'] and do_org
      puts "Grabbing organization #{name} ..."
      ensure_dir("#{dest_dir}/organizations/#{name}")
      download_org(dest_dir, webui_key, name)
      File.open("#{dest_dir}/organizations/#{name}/org.json", 'w') do |file|
        file.write(Chef::JSONCompat.to_json_pretty(org))
      end
      File.open("#{dest_dir}/organizations/#{name}/members.json", 'w') do |file|
        file.write(Chef::JSONCompat.to_json_pretty(rest.get_rest("#{url}/users")))
      end
      File.open("#{dest_dir}/organizations/#{name}/invitations.json", 'w') do |file|
        file.write(Chef::JSONCompat.to_json_pretty(rest.get_rest("#{url}/association_requests")))
      end
    end
  end

  if @error
    exit 1
  end
end