Class: UploadCmd

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

Instance Attribute Summary

Attributes inherited from MofaCmd

#cookbook, #options, #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

#execute(_ssh_port) ⇒ Object



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

def execute(_ssh_port)
  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
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
# 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']}"

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

  begin
    # 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,
                     :use_agent => false) 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

    # only upload if the file does not exist on the target host:
    already_uploaded = false
    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,
                   :use_agent => false) do |ssh|
      out = ssh_exec!(ssh, "if [ -f #{import_dir}/#{cookbook.pkg_name} ];then echo -n already_exists;fi")
      fail "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
      already_uploaded = true if out[1] == 'already_exists'
    end

    unless already_uploaded
      puts "Uploading cookbook pkg #{cookbook.pkg_name} to binrepo import folder #{Mofa::Config.config['binrepo_host']}:#{Mofa::Config.config['binrepo_import_dir']}..."
      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,
                      :use_agent => false) do |sftp|
        sftp.upload!("#{cookbook.pkg_dir}/#{cookbook.pkg_name}", "#{import_dir}/#{cookbook.pkg_name}")
      end
      puts "OK."
    else
      puts "Cookbook pkg #{cookbook.pkg_name} already exists in the binrepo. Will NOT upload it again."
    end
  rescue RuntimeError => e
    puts "Error: #{e.message}"
    raise "Failed to upload cookbook #{cookbook.name}!"
  end
end