knife-tar 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+ require 'chef/data_bag'
5
+ require 'chef/data_bag_item'
6
+
7
+ class Chef
8
+ class Knife
9
+ class DataBagTarDownload < Chef::Knife
10
+
11
+ banner "knife data bag tar download tarPath [options]"
12
+ category "data bag tar"
13
+
14
+ def run
15
+ #Get Arguments
16
+ if @name_args.size != 1
17
+ ui.info("Please specify a tar path")
18
+ show_usage
19
+ exit 1
20
+ end
21
+
22
+ tar_file = Chef::TarFile.new(@name_args.first, true)
23
+ DataBagTarDownload.download_data_bags tar_file
24
+ tar_file.save
25
+
26
+ end
27
+
28
+ def self.download_data_bags(tar_file)
29
+ dir = tar_file.data_bags_path
30
+ Chef::DataBag.list.each do |bag_name, url|
31
+ system("mkdir -p #{File.join(dir, bag_name)}")
32
+ Chef::DataBag.load(bag_name).each do |item_name, itemUrl|
33
+ Chef::Log.info("Backing up data bag #{bag_name} item #{item_name}")
34
+ item = Chef::DataBagItem.load(bag_name, item_name)
35
+ File.open(File.join(dir, bag_name, "#{item_name}.json"), "w") do |dbag_file|
36
+ dbag_file.print(item.raw_data.to_json)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+
5
+ class Chef
6
+ class Knife
7
+ class DataBagTarUpload < Chef::Knife
8
+
9
+ banner "knife data bag tar upload tarPath [options]"
10
+ category "data bag tar"
11
+
12
+ def run
13
+ #Get Arguments
14
+ if @name_args.size != 1
15
+ ui.info("Please specify a tar path")
16
+ show_usage
17
+ exit 1
18
+ end
19
+
20
+ tar_file = Chef::TarFile.new(@name_args.first)
21
+ DataBagTarUpload.upload_data_bags tar_file
22
+
23
+ end
24
+
25
+ def self.upload_data_bags(tar_file)
26
+ data_bag_from_file = Chef::Knife::DataBagFromFile.new
27
+
28
+ tar_file.data_bags.each do |databag_path|
29
+
30
+ #TODO: May want to consider moving this logic into TarFile.
31
+
32
+ databag = File.basename(File.expand_path("..", databag_path))
33
+
34
+ #In order to upload a data bag value the data bag itself must exist
35
+ #so attempt to create it now
36
+
37
+ #We make the assumption here that the parent directory of each data bag
38
+ #file is the data bag name.
39
+
40
+ databag_create = Chef::Knife::DataBagCreate.new
41
+ databag_create.name_args = [databag]
42
+ databag_create.run
43
+
44
+ #To upload a data bag we must know the data bag name and the path to the file
45
+
46
+ data_bag_from_file.name_args = [databag, databag_path]
47
+ data_bag_from_file.run
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+ require 'chef/environment'
5
+
6
+ class Chef
7
+ class Knife
8
+ class EnvironmentTarDownload < Chef::Knife
9
+
10
+ @@DEFAULT_ENVIRONMENT = "_default"
11
+
12
+ banner "knife environment tar download tarPath [options]"
13
+ category "environment tar"
14
+
15
+ def run
16
+ #Get Arguments
17
+ if @name_args.size != 1
18
+ ui.info("Please specify a tar path")
19
+ show_usage
20
+ exit 1
21
+ end
22
+
23
+ tar_file = Chef::TarFile.new(@name_args.first, true)
24
+ EnvironmentTarDownload.download_environments tar_file
25
+ tar_file.save
26
+
27
+ end
28
+
29
+ def self.download_environments(tar_file)
30
+ dir = tar_file.environments_path
31
+ Chef::Environment.list.each do |component_name, url|
32
+ if component_name != @@DEFAULT_ENVIRONMENT
33
+ Chef::Log.info("Backing up environment #{component_name}")
34
+ component_obj = Chef::Environment.load(component_name)
35
+ File.open(File.join(dir, "#{component_name}.json"), "w") do |component_file|
36
+ component_file.print(component_obj.to_json)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+
5
+ class Chef
6
+ class Knife
7
+ class EnvironmentTarUpload < Chef::Knife
8
+
9
+ banner "knife environment tar upload tarPath [options]"
10
+ category "environment tar"
11
+
12
+ def run
13
+ #Get Arguments
14
+ if @name_args.size != 1
15
+ ui.info("Please specify a tar path")
16
+ show_usage
17
+ exit 1
18
+ end
19
+
20
+ tar_file = Chef::TarFile.new(@name_args.first)
21
+ EnvironmentTarUpload.upload_environments tar_file
22
+
23
+ end
24
+
25
+ def self.upload_environments(tar_file)
26
+ environment_from_file = Chef::Knife::EnvironmentFromFile.new
27
+
28
+ tar_file.environments.each do |environment_path|
29
+ environment_from_file.name_args = [environment_path]
30
+ environment_from_file.run
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+ require 'chef/node'
5
+
6
+ class Chef
7
+ class Knife
8
+ class NodeTarDownload < Chef::Knife
9
+
10
+ banner "knife node tar download tarPath [options]"
11
+ category "node tar"
12
+
13
+ def run
14
+ #Get Arguments
15
+ if @name_args.size != 1
16
+ ui.info("Please specify a tar path")
17
+ show_usage
18
+ exit 1
19
+ end
20
+
21
+ tar_file = Chef::TarFile.new(@name_args.first, true)
22
+ NodeTarDownload.download_nodes tar_file
23
+ tar_file.save
24
+
25
+ end
26
+
27
+ def self.download_nodes(tar_file)
28
+ dir = tar_file.nodes_path
29
+ Chef::Node.list.each do |component_name, url|
30
+ Chef::Log.info("Backing up node #{component_name}")
31
+ component_obj = Chef::Node.load(component_name)
32
+ File.open(File.join(dir, "#{component_name}.json"), "w") do |component_file|
33
+ component_file.print(component_obj.to_json)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ require 'chef/knife'
2
+ require 'chef/node'
3
+ require 'chef/knife/core/object_loader'
4
+ require 'chef/json_compat'
5
+
6
+ class Chef
7
+ class Knife
8
+ class NodeTarUpload < Chef::Knife
9
+
10
+ banner "knife node tar upload tarPath [options]"
11
+ category "node tar"
12
+
13
+ def run
14
+ #Get Arguments
15
+ if @name_args.size != 1
16
+ ui.info("Please specify a tar path")
17
+ show_usage
18
+ exit 1
19
+ end
20
+
21
+ tar_file = Chef::TarFile.new(@name_args.first)
22
+ NodeTarUpload.upload_nodes tar_file
23
+
24
+ end
25
+
26
+ def self.upload_nodes(tar_file)
27
+ node_from_file = Chef::Knife::NodeFromFile.new
28
+
29
+ tar_file.nodes.each do |nodes_path|
30
+ node_from_file.name_args = [nodes_path]
31
+ node_from_file.run
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+ require 'chef/role'
5
+
6
+ class Chef
7
+ class Knife
8
+ class RoleTarDownload < Chef::Knife
9
+
10
+ banner "knife role tar download tarPath [options]"
11
+ category "role tar"
12
+
13
+ def run
14
+ #Get Arguments
15
+ if @name_args.size != 1
16
+ ui.info("Please specify a tar path")
17
+ show_usage
18
+ exit 1
19
+ end
20
+
21
+ tar_file = Chef::TarFile.new(@name_args.first, true)
22
+ RoleTarDownload.download_roles tar_file
23
+ tar_file.save
24
+
25
+ end
26
+
27
+ def self.download_roles(tar_file)
28
+ dir = tar_file.roles_path
29
+ Chef::Role.list.each do |component_name, url|
30
+ Chef::Log.info("Backing up role #{component_name}")
31
+ component_obj = Chef::Role.load(component_name)
32
+ File.open(File.join(dir, "#{component_name}.json"), "w") do |component_file|
33
+ component_file.print(component_obj.to_json)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ # coding: UTF-8
2
+
3
+ require 'chef/knife'
4
+ require 'chef/role'
5
+ require 'chef/knife/core/object_loader'
6
+ require 'chef/json_compat'
7
+
8
+ class Chef
9
+ class Knife
10
+ class RoleTarUpload < Chef::Knife
11
+
12
+ banner "knife role tar upload TARPATH [options]"
13
+
14
+ def run
15
+ #Get Arguments
16
+ if @name_args.size != 1
17
+ ui.info("Please specify a tar path")
18
+ show_usage
19
+ exit 1
20
+ end
21
+
22
+ tar_file = Chef::TarFile.new(@name_args[0])
23
+ RoleTarUpload.upload_roles tar_file
24
+
25
+ end
26
+
27
+ def self.upload_roles tar_file
28
+ role_from_file = Chef::Knife::RoleFromFile.new
29
+ role_from_file.name_args = tar_file.roles
30
+ role_from_file.config[:print_after] = true
31
+ role_from_file.run
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+
5
+ class Chef
6
+ class Knife
7
+ class TarDownload < Chef::Knife
8
+
9
+ banner "knife tar download tarPath [options]"
10
+ category "tar"
11
+
12
+ def run
13
+ #Get Arguments
14
+ if @name_args.size != 1
15
+ ui.info("Please specify a tar path")
16
+ show_usage
17
+ exit 1
18
+ end
19
+
20
+ tar_file = Chef::TarFile.new(@name_args.first, true)
21
+
22
+ ClientTarDownload.download_clients tar_file
23
+ CookbookTarDownload.download_cookbooks tar_file
24
+ DataBagTarDownload.download_data_bags tar_file
25
+ EnvironmentTarDownload.download_environments tar_file
26
+ NodeTarDownload.download_nodes tar_file
27
+ RoleTarDownload.download_roles tar_file
28
+ UserTarDownload.download_users tar_file
29
+
30
+ tar_file.save
31
+
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,70 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+
5
+ class Chef
6
+ class Knife
7
+ class TarUpload < Chef::Knife
8
+
9
+ banner "knife tar upload tarPath [options]"
10
+ category "tar"
11
+
12
+ def run
13
+ #Get Arguments
14
+ if @name_args.size != 1
15
+ ui.info("Please specify a tar path")
16
+ show_usage
17
+ exit 1
18
+ end
19
+
20
+ tar_file = Chef::TarFile.new @name_args.first
21
+
22
+ # Attempt to upload all the components in the tar file
23
+ # If our tar file does not contain a component ignore the error and skip it
24
+ begin
25
+ ClientTarUpload.upload_clients tar_file
26
+ rescue TarFile::MissingChefComponentError => e
27
+ ui.info("No Client files to upload")
28
+ end
29
+
30
+ begin
31
+ CookbookTarUpload.upload_cookbooks tar_file
32
+ rescue TarFile::MissingChefComponentError => e
33
+ ui.info("No Cookbooks to upload")
34
+ end
35
+
36
+ begin
37
+ DataBagTarUpload.upload_data_bags tar_file
38
+ rescue TarFile::MissingChefComponentError => e
39
+ ui.info("No data bag files to upload")
40
+ end
41
+
42
+ begin
43
+ EnvironmentTarUpload.upload_environments tar_file
44
+ rescue TarFile::MissingChefComponentError => e
45
+ ui.info("No Environment files to upload")
46
+ end
47
+
48
+ begin
49
+ NodeTarUpload.upload_nodes tar_file
50
+ rescue TarFile::MissingChefComponentError => e
51
+ ui.info("No Node files to upload")
52
+ end
53
+
54
+ begin
55
+ RoleTarUpload.upload_roles tar_file
56
+ rescue TarFile::MissingChefComponentError => e
57
+ ui.info("No Role files to upload")
58
+ end
59
+
60
+ begin
61
+ UserTarUpload.upload_users tar_file
62
+ rescue TarFile::MissingChefComponentError => e
63
+ ui.info("No User files to upload")
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,40 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+ require 'chef/webui_user'
5
+
6
+ class Chef
7
+ class Knife
8
+ class UserTarDownload < Chef::Knife
9
+
10
+ banner "knife user tar download tarPath [options]"
11
+ category "user tar"
12
+
13
+ def run
14
+ #Get Arguments
15
+ if @name_args.size != 1
16
+ ui.info("Please specify a tar path")
17
+ show_usage
18
+ exit 1
19
+ end
20
+
21
+ tar_file = Chef::TarFile.new(@name_args.first, true)
22
+ UserTarDownload.download_users tar_file
23
+ tar_file.save
24
+
25
+ end
26
+
27
+ def self.download_users(tar_file)
28
+ dir = tar_file.web_users_path
29
+ Chef::WebUIUser.list.each do |component_name, url|
30
+ Chef::Log.info("Backing up user #{component_name}")
31
+ component_obj = Chef::WebUIUser.load(component_name)
32
+ File.open(File.join(dir, "#{component_name}.json"), "w") do |component_file|
33
+ component_file.print(component_obj.to_json)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,49 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/core/object_loader'
3
+ require 'chef/json_compat'
4
+ require 'chef/webui_user'
5
+
6
+ class Chef
7
+ class Knife
8
+ class UserTarUpload < Chef::Knife
9
+
10
+ banner "knife user tar upload tarPath [options]"
11
+ category "user tar"
12
+
13
+ def run
14
+ #Get Arguments
15
+ if @name_args.size != 1
16
+ ui.info("Please specify a tar path")
17
+ show_usage
18
+ exit 1
19
+ end
20
+
21
+ tar_file = Chef::TarFile.new(@name_args.first)
22
+ UserTarUpload.upload_users tar_file
23
+
24
+ end
25
+
26
+ def self.upload_users(tar_file)
27
+ current_users = Chef::WebUIUser.list.keys
28
+ users_loader = Chef::Knife::Core::ObjectLoader.new(Chef::WebUIUser, ui)
29
+
30
+ tar_file.web_users.each do |web_user_path|
31
+
32
+ user = users_loader.load_from("users", web_user_path)
33
+
34
+ # In order to 'update' a user we have to remove it first, so if the user exists destroy it
35
+ if current_users.include? user.name
36
+ ui.info("Deleting Chef User [#{user.name}] in order to update it")
37
+ WebUIUser.load(user.name).destroy
38
+ end
39
+
40
+ user.save
41
+ ui.info("Updated User : #{user.name}")
42
+ end
43
+ end
44
+
45
+
46
+
47
+ end
48
+ end
49
+ end