Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements to the exported files controller #292

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions crowbar_framework/app/controllers/support_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,24 @@ def destroy

def export_supportconfig
begin
base = "supportconfig-#{Time.now.strftime("%Y%m%d-%H%M%S")}"
filename = "#{base}.tbz"
filename = "supportconfig-#{Time.now.strftime("%Y%m%d-%H%M%S")}.tbz"

pid = Process.fork do
begin
tmp = Rails.root.join("tmp", base).to_s
tmpdir = Dir.mktmpdir

supportconfig = ["sudo", "-i", "supportconfig", "-Q", "-R", tmp]
chown = ["sudo", "-i", "chown", "-R", "#{Process.uid}:#{Process.gid}", tmp]
supportconfig = ["sudo", "-i", "supportconfig", "-Q", "-R", tmpdir]
chown = ["sudo", "-i", "chown", "-R", "#{Process.uid}:#{Process.gid}", tmpdir]

ok = system(*supportconfig)
ok &= system(*chown)

tarball = Dir.glob("#{tmp}/*.tbz").first
tarball = Dir.glob("#{tmpdir}/*.tbz").first
File.rename tarball, export_dir.join(filename) if tarball && ok
rescue => e
Rails.logger.warn(e.message)
ensure
FileUtils.rm_rf(tmp)
FileUtils.remove_entry_secure tmpdir
end
end

Expand All @@ -103,22 +102,29 @@ def export_supportconfig

def export_chef
begin
Rails.root.join("db").children.each do |file|
file.unlink if file.extname == ".json"
end

NodeObject.all.each { |n| n.export }
RoleObject.all.each { |r| r.export }
Proposal.all.each { |p| p.export }

filename = "crowbar-chef-#{Time.now.strftime("%Y%m%d-%H%M%S")}.tgz"

pid = Process.fork do
exports = Dir.glob(Rails.root.join("db", "*.json").to_s)
cmd = ["tar", "-czf", Rails.root.join("tmp", filename).to_s, *exports]
tmpdir = Dir.mktmpdir
tmpdirpath = Pathname.new(tmpdir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR: i would call it tmpdir_path for consistency

or as an alternative just use tmpdir = Dir.mktmpdir(Pathname.new(tmpdir)) and reference it with .to_s where needed

tmpfile_path = tmpdirpath.join(filename)

begin
NodeObject.all.each { |n| n.export(tmpdirpath) }
RoleObject.all.each { |r| r.export(tmpdirpath) }
Proposal.all.each { |p| p.export(tmpdirpath) }
rescue StandardError => e
FileUtils.remove_entry_secure tmpdir
raise e
end

ok = system(*cmd)
File.rename(Rails.root.join("tmp", filename), export_dir.join(filename)) if ok
pid = Process.fork do
begin
cmd = ["tar", "czf", tmpfile_path.to_s, "*"]
_stdout, ok = Open3.capture2(*cmd, chdir: tmpdir)
File.rename(tmpfile_path, export_dir.join(filename)) if ok
ensure
FileUtils.remove_entry tmpdir
end
end

Process.detach(pid)
Expand Down
10 changes: 5 additions & 5 deletions crowbar_framework/app/models/chef_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ def self.chef_escape(str)
str.gsub("-:") { |c| '\\' + c }
end

# FIXME: the second argument was added so that the Proposal model
# FIXME: the third argument was added so that the Proposal model
# can be exported in a same way as the ProposalObject. When the replacement
# is completed, remove it and implement the export in the Proposal.
# Also check the logging barclamp that it did not break.
def self.export(obj, name = nil)
def self.export(obj, dirpath, name = nil)
name ||= obj.respond_to?(:name) ? obj.name : "unknown"
file = Rails.root.join("db", "#{obj.chef_type}_#{name}.json")
file = dirpath.join("#{obj.chef_type}_#{name}.json")
File.open(file, "w") { |f| f.write(obj.to_json) }
end

def export(name = nil)
self.class.export(self, name)
def export(dirpath, name = nil)
self.class.export(self, dirpath, name)
end
end
4 changes: 2 additions & 2 deletions crowbar_framework/app/models/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def key
end
end

def export
ChefObject.export(self)
def export(dirpath)
ChefObject.export(self, dirpath)
end

# FIXME: this is not correct, the item of ProposalObject returns
Expand Down
2 changes: 1 addition & 1 deletion crowbar_framework/app/views/support/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- else
= link_to file, "/export/#{file}"

= link_to icon_tag("trash"), utils_files_path(:id => file), :title => t(".delete_hint"), :class => "pull-right"
= link_to icon_tag("trash"), utils_files_path(:id => file), :title => t(".delete_hint"), :class => "pull-right", :method => :delete
- else
.alert.alert-info
= t(".none_exported")
2 changes: 1 addition & 1 deletion crowbar_framework/config/locales/crowbar/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ en:
utils:
title: 'Utilities'
queue: 'Deployment Queue'
logs: 'Exported Items'
logs: 'Exported Files'
repositories: 'Repositories'
backup: 'Backup & Restore'
help: 'Help'
Expand Down
4 changes: 1 addition & 3 deletions crowbar_framework/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@

#support paths
get "utils(.:format)", controller: "support", action: "index", as: "utils"
get "utils/files/:id(.:format)", controller: "support", action: "destroy", constraints: { id: /[^\/]+/ }, as: "utils_files"
delete "utils/files/:id(.:format)", controller: "support", action: "destroy", constraints: { id: /[^\/]+/ }, as: "utils_files"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. 128/100

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this require some change in the cli?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not part of the CLI yet.

get "utils/chef(.:format)", controller: "support", action: "export_chef", as: "export_chef"
get "utils/supportconfig(.:format)", controller: "support", action: "export_supportconfig", as: "export_supportconfig"
get "utils/:controller/1.0/export(.:format)", action: "export", as: "utils_export"
get "utils/:controller/1.0(.:format)", action: "utils", as: "utils_barclamp"
get "utils/import/:id(.:format)", controller: "support", action: "import", constraints: { id: /[^\/]+/ }, as: "utils_import"
get "utils/upload/:id(.:format)", controller: "support", action: "upload", constraints: { id: /[^\/]+/ }, as: "utils_upload"
get "utils/repositories(.:format)", controller: "repositories", action: "index", as: "repositories"
post "utils/repositories/sync(.:format)", controller: "repositories", action: "sync", as: "sync_repositories"
post "utils/repositories/activate(.:format)", controller: "repositories", action: "activate", as: "activate_repository"
Expand Down
9 changes: 6 additions & 3 deletions crowbar_framework/spec/controllers/support_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@
it "exports known data into db dir" do
begin
now = Time.now
tmpdir = Rails.root.join("tmp", "chef-export")
tmpdir.mkpath

allow(Time).to receive(:now).and_return(now)
allow(Process).to receive(:fork).and_return(0)
allow(Dir).to receive(:mktmpdir).and_return(tmpdir.to_s)

filename = "crowbar-chef-#{now.strftime("%Y%m%d-%H%M%S")}.tgz"
export = Rails.root.join("db", filename)

get :export_chef
expect(flash[:alert]).to be_nil
expect(response).to redirect_to(utils_url(waiting: true, file: filename))

expect(Dir.glob(Rails.root.join("db", "*.json")).count).to_not be_zero
expect(Dir.glob(tmpdir.join("*.json")).count).to_not be_zero
ensure
Dir.glob(Rails.root.join("db", "*.json")).each { |json| FileUtils.rm(json) }
tmpdir.rmtree
end
end
end
Expand Down