Class: Chef::TarFile

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/tar_file.rb

Defined Under Namespace

Classes: InvalidStructureError, MissingChefComponentError

Constant Summary collapse

COOKBOOKS_PATH =

The named conventions for chef components

"cookbooks"
ROLES_PATH =
"roles"
ENVIRONMENTS_PATH =
"environments"
DATA_BAGS_PATH =
"data_bags"
API_CLIENTS_PATH =
"api_clients"
WEB_USERS_PATH =
"web_users"
NODES_PATH =
"nodes"
CHEF_FILE_EXTENSIONS =

A list of valid chef file extensions

[".js", ".json", ".rb"]

Instance Method Summary collapse

Constructor Details

#initialize(tarPath, create = false) ⇒ TarFile

Returns a new instance of TarFile.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/chef/tar_file.rb', line 23

def initialize tarPath, create=false
  
  if tarPath==nil
    raise ArgumentError, "A tar file path must be given"
  end
  
  @create_tar = create
  
  unless create
    @temp_directory = TmpDirectory.new.path
    
    #Assume for now that the components live directly inside the tar file
    @tar_contents_path = @temp_directory
    
    localTarFile = File.join(@temp_directory, 'cookbooks.tgz')
  
    #Move/Download tar file to tmp directory
    File.open(localTarFile, 'wb') do |f|
      open(tarPath) do |r|
        f.write(r.read)
      end
    end
  
    #Untar file
    Chef::Mixin::Command.run_command(:command => "tar zxfC #{localTarFile} #{@temp_directory}")
    
    #Verify tar file structure and update tar_contents_path if necessary
    
    dirList = get_directories_names @tar_contents_path
    
    if !is_tar_valid? dirList
      #The tar does not contain any immediate chef component directories, check to see if there is a top-level project folder
      #that contains any chef component directories
      
      if dirList.size!=1 or !is_tar_valid? get_directories_names(File.join(@tar_contents_path, dirList.first))
        raise InvalidStructureError, "The tar file has an invalid structure"
      end
      
      #The tar file is valid but contains a top-level project folder so update the @tar_contents_path
      @tar_contents_path = File.join(@tar_contents_path, dirList.first)
    end
    
    #Remove tar file
    FileUtils.rm_f localTarFile
    
  else
    
    # Verify that the tarPath doesn't already exist
    if File.exists? tarPath
      raise ArgumentError, "We cannot create a tar file at path : #{tarPath} because it already exists"
    end
    
    @temp_directory = TmpDirectory.new("chef-server").path
    
    #We will create the tar file such that their is no single root directory inside the tar file
    @tar_contents_path = @temp_directory
    
    FileUtils.mkdir_p @temp_directory
    
    @create_tar_path = File.absolute_path tarPath
    
    # We are setting up a directory to become a Chef TarFile so create the resource directories
    FileUtils.mkdir_p File.join @tar_contents_path, COOKBOOKS_PATH
    FileUtils.mkdir_p File.join @tar_contents_path, ROLES_PATH
    FileUtils.mkdir_p File.join @tar_contents_path, ENVIRONMENTS_PATH
    FileUtils.mkdir_p File.join @tar_contents_path, DATA_BAGS_PATH
    FileUtils.mkdir_p File.join @tar_contents_path, API_CLIENTS_PATH
    FileUtils.mkdir_p File.join @tar_contents_path, WEB_USERS_PATH
    FileUtils.mkdir_p File.join @tar_contents_path, NODES_PATH
    
  end
  
end

Instance Method Details

#api_clientsObject

Returns a list of absolute paths to the api_clients json files



163
164
165
# File 'lib/chef/tar_file.rb', line 163

def api_clients
  get_chef_files_absolute_paths api_clients_path
end

#api_clients_pathObject

Returns the absolute path to the api_clients directory



157
158
159
160
# File 'lib/chef/tar_file.rb', line 157

def api_clients_path
  verify_path API_CLIENTS_PATH
  File.join @tar_contents_path, API_CLIENTS_PATH
end

#cookbooksObject

Returns list of absolute paths of the cookbooks



104
105
106
# File 'lib/chef/tar_file.rb', line 104

def cookbooks
  get_directories_absolute_paths cookbooks_path
end

#cookbooks_pathObject

Returns the absolute path to the cookbooks directory



98
99
100
101
# File 'lib/chef/tar_file.rb', line 98

def cookbooks_path
  verify_path COOKBOOKS_PATH
  File.join @tar_contents_path, COOKBOOKS_PATH
end

#data_bagsObject

Returns a list of absolute paths to the data_bags json files



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/tar_file.rb', line 137

def data_bags
  
  #Data bags follow a different structure then the other components, their structure is
  #|- data_bags
  #\ \- data_bag_1
  #| | |- values_1.json
  #| ...
  
  dir_list = get_directories_absolute_paths(data_bags_path)
  
  data_bags_absolute_paths = Array.new
  
  dir_list.each do |dir_path|
    data_bags_absolute_paths = data_bags_absolute_paths | get_chef_files_absolute_paths(dir_path)
  end
  
  data_bags_absolute_paths
end

#data_bags_pathObject

Returns the absolute path to the data_bags directory



131
132
133
134
# File 'lib/chef/tar_file.rb', line 131

def data_bags_path
  verify_path DATA_BAGS_PATH
  File.join @tar_contents_path, DATA_BAGS_PATH
end

#environmentsObject

Returns a list of absolute paths to the environemnts json files



126
127
128
# File 'lib/chef/tar_file.rb', line 126

def environments
  get_chef_files_absolute_paths environments_path
end

#environments_pathObject

Returns the absolute path to the environments directory



120
121
122
123
# File 'lib/chef/tar_file.rb', line 120

def environments_path
  verify_path ENVIRONMENTS_PATH
  File.join @tar_contents_path, ENVIRONMENTS_PATH
end

#nodesObject

Returns a list of absolute paths to the nodes json files



174
175
176
# File 'lib/chef/tar_file.rb', line 174

def nodes
  get_chef_files_absolute_paths nodes_path
end

#nodes_pathObject

Returns the absolute path of the nodes directory



168
169
170
171
# File 'lib/chef/tar_file.rb', line 168

def nodes_path
  verify_path NODES_PATH
  File.join @tar_contents_path, NODES_PATH
end

#rolesObject

Returns a list of absolute paths to the roles json files



115
116
117
# File 'lib/chef/tar_file.rb', line 115

def roles
  get_chef_files_absolute_paths roles_path
end

#roles_pathObject

Returns the absolute path to the roles directory



109
110
111
112
# File 'lib/chef/tar_file.rb', line 109

def roles_path
  verify_path ROLES_PATH
  File.join @tar_contents_path, ROLES_PATH
end

#saveObject



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/chef/tar_file.rb', line 189

def save
  if @create_tar
    
    #Tar up the directory from the parent directory of the tar's contents (this will really be /tmp)
    Chef::Mixin::Command.run_command(:cwd => File.expand_path("..",@tar_contents_path), :command => "tar zfc #{@create_tar_path} #{File.basename @tar_contents_path}")
    
    @create_tar = false
  else
    raise StandardError, "The tar file is not in the correct state to be saved"
  end
end

#web_usersObject

Returns a list of absolute paths to the web_users json files



185
186
187
# File 'lib/chef/tar_file.rb', line 185

def web_users
  get_chef_files_absolute_paths web_users_path
end

#web_users_pathObject

Returns the absolute path of the web_users directory



179
180
181
182
# File 'lib/chef/tar_file.rb', line 179

def web_users_path
  verify_path WEB_USERS_PATH
  File.join @tar_contents_path, WEB_USERS_PATH
end