Class: SSC::Handler::OverlayFile

Inherits:
Base
  • Object
show all
Includes:
DirectoryManager
Defined in:
lib/handlers/file.rb

Constant Summary

Constants inherited from Base

Base::API_URL

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helper

included

Constructor Details

This class inherits a constructor from SSC::Handler::Base

Instance Method Details

#add(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/handlers/file.rb', line 22

def add(path)
  absolute_path= File.expand_path(path)
  optional_file_params= {:permissions => options.permissions, 
                         :group       => options.group,
                         :owner       => options.owner}
  file_dir, file_name= File.split(absolute_path)
  file_dir = options.path == '' ? file_dir : options.path
  file_name = options.name == '' ? file_name : options.name
  file_params= ({:path => file_dir, :filename => file_name})
  file_params.merge!(optional_file_params)
  id= nil
  if options.remote?
    require_appliance do |appliance|
      File.open(absolute_path) do |file|
        file= StudioApi::File.upload(file, appliance.id, file_params)
        id= file.id.to_i
      end
      say "Overlay file saved. Id: #{id}"
    end
  end
  if ApplianceDirectory.new.valid?
    local_copy= FileListFile.new.initiate_file(absolute_path, file_params)
    say "Created #{local_copy}"
  end
end

#diff(file_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/handlers/file.rb', line 104

def diff(file_name)
  require_appliance_directory do |appliance, files|
    id= files[:file_list].is_uploaded?(file_name)
    raise Thor::Error, "File hasn't been uploaded" unless id
    response= StudioApi::File.find(id)
    remote_content= response.content
    local_file= File.join(Dir.pwd, 'files', file_name)
    tempfile=Tempfile.new('ssc_file') 
    tempfile.write(remote_content)
    say find_diff(tempfile.path, local_file)
    tempfile.close; tempfile.unlink
  end
rescue ApplianceDirectoryError
  raise Thor::Error, "diff can only be performed in the appliance directory"
end

#listObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/handlers/file.rb', line 123

def list
  require_appliance_directory do |appliance, files|
    file_list= files[:file_list]
    out= if options.remote? || file_list.empty_list?
      response= StudioApi::File.find(:all, :params => {:appliance_id => appliance.id})
      response= response.collect do |file|
        item= {file.filename => {"id" => file.id, "path" => file.path}}
        file_list.push('list', item)
      end
      file_list.save
      response
    else
      file_list["list"]
    end
    say out.to_yaml
  end
rescue ApplianceDirectoryError
  require_appliance do |appliance|
    print_table StudioApi::File.find(:all, :params => {:appliance_id => appliance.id}).collect do |file|
      [file.id, File.join(file.path, file.filename)]
    end
  end
end

#remove(file_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/handlers/file.rb', line 51

def remove(file_name)
  file_list= FileListFile.new
  file_id= file_list.is_uploaded?(file_name)
  if options.remote? && file_id
    begin
      StudioApi::File.find(file_id).destroy
      say "File '#{file_name}' removed"
    rescue
      raise Thor::Error, "Couldn't remove file #{file_name} (id: #{file_id}"
    end
  elsif options.remote? && !file_id
    raise Thor::Error, "File '#{file_name}' not found"
  else
    file_list.push('remove', {file_name => nil})
    file_list.save
    say "File '#{file_name}' marked for removal"
  end
end

#show(file_name) ⇒ Object



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
# File 'lib/handlers/file.rb', line 74

def show(file_name)
  if options.remote?
    begin
      require_appliance_directory do |appliance, files|
        id= files[:file_list].is_uploaded?(file_name)
        if id
          response= StudioApi::File.find(id)
          say response.content
        else
          say "File hasn't been uploaded.\nLocal Copy:", :red
          say ApplianceDirectory.show_file(File.join('files',file_name))
        end
      end
    rescue ApplianceDirectoryError
      if options.file_id
        say StudioApi::File.find(options.file_id).content
      else
        files= StudioApi::File.find(:all)
        files= files.select {|f| f.filename == file_name}
        raise Thor::Error, "File not found or ambiguous file name " unless files.length == 1
        say files[0].content
      end
    end
  else
    say ApplianceDirectory.show_file(File.join('files', file_name))
  end
end