Class: UploadCmd

Inherits:
MofaCmd show all
Defined in:
lib/mofa/upload_cmd.rb

Instance Attribute Summary

Attributes inherited from MofaCmd

#cookbook, #token

Instance Method Summary collapse

Methods inherited from MofaCmd

generate_token, #ssh_exec!

Constructor Details

#initialize(token, cookbook) ⇒ UploadCmd

Returns a new instance of UploadCmd.



6
7
8
# File 'lib/mofa/upload_cmd.rb', line 6

def initialize(token, cookbook)
  super(token, cookbook)
end

Instance Method Details

#binrepo_up?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mofa/upload_cmd.rb', line 27

def binrepo_up?
  binrepo_up = true

  exit_status = system("ping -q -c 1 #{Mofa::Config.config['binrepo_host']} >/dev/null 2>&1")
  unless exit_status then
    puts "  --> Binrepo host #{Mofa::Config.config['binrepo_host']} is unavailable!"
    binrepo_up = false
  end

  puts "Binrepo #{ Mofa::Config.config['binrepo_ssh_user']}@#{Mofa::Config.config['binrepo_host']}:#{Mofa::Config.config['binrepo_import_dir']} not present or not reachable!" unless binrepo_up
  binrepo_up

end

#cleanupObject



23
24
25
# File 'lib/mofa/upload_cmd.rb', line 23

def cleanup
  cookbook.cleanup
end

#executeObject



18
19
20
21
# File 'lib/mofa/upload_cmd.rb', line 18

def execute
  cookbook.execute
  upload_cookbook_pkg
end

#prepareObject



10
11
12
13
14
15
16
# File 'lib/mofa/upload_cmd.rb', line 10

def prepare
  fail unless binrepo_up?

  # upload always means: package a release
  cookbook.pkg_name = "#{cookbook.name}_#{cookbook.version}-full.tar.gz"
  cookbook.prepare
end

#upload_cookbook_pkgObject



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
# File 'lib/mofa/upload_cmd.rb', line 41

def upload_cookbook_pkg
  puts "Will use ssh_user #{Mofa::Config.config['binrepo_ssh_user']} and ssh_key_file #{Mofa::Config.config['binrepo_ssh_keyfile']}"
  puts "Uploading cookbook pkg #{cookbook.pkg_name} to binrepo import folder #{Mofa::Config.config['binrepo_host']}:#{Mofa::Config.config['binrepo_import_dir']}..."

  fail unless binrepo_up?
  import_dir = Mofa::Config.config['binrepo_import_dir']

  # if the upload target is not a proper binrepo with a designated ".../import" folder -> create the "right" folder structure
  unless Mofa::Config.config['binrepo_import_dir'].match(/import$/)
    Net::SSH.start(Mofa::Config.config['binrepo_host'], Mofa::Config.config['binrepo_ssh_user'], :keys => [Mofa::Config.config['binrepo_ssh_keyfile']], :port => Mofa::Config.config['binrepo_ssh_port'], :verbose => :error) do |ssh|
      puts "Remotely creating target dir \"#{import_dir}/#{cookbook.name}/#{cookbook.version}\""
      out = ssh_exec!(ssh, "[ -d #{import_dir}/#{cookbook.name}/#{cookbook.version} ] || mkdir -p #{import_dir}/#{cookbook.name}/#{cookbook.version}")
      fail "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
      import_dir = "#{import_dir}/#{cookbook.name}/#{cookbook.version}"
    end
  end

  begin
    Net::SFTP.start(Mofa::Config.config['binrepo_host'], Mofa::Config.config['binrepo_ssh_user'], :keys => [Mofa::Config.config['binrepo_ssh_keyfile']], :port => Mofa::Config.config['binrepo_ssh_port'], :verbose => :error) do |sftp|
      sftp.upload!("#{cookbook.pkg_dir}/#{cookbook.pkg_name}", "#{import_dir}/#{cookbook.pkg_name}")
    end
    puts "OK."
  rescue RuntimeError => e
    puts "Error: #{e.message}"
    raise "Failed to upload cookbook #{cookbook.name}!"
  end
end