aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/remote/plugin.rb
blob: 6dafbd865e489f6b2a1b4f1cb36d604f87a25e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
96
97
98
99
#
# these methods are made available in capistrano tasks as 'leap.method_name'
#

module LeapCli; module Remote; module Plugin

  def required_packages
    "puppet ruby-hiera-puppet rsync lsb-release"
  end

  def log(*args, &block)
    LeapCli::Util::log(*args, &block)
  end

  #
  # creates directories that are owned by root and 700 permissions
  #
  def mkdirs(*dirs)
    raise ArgumentError.new('illegal dir name') if dirs.grep(/[\' ]/).any?
    run dirs.collect{|dir| "mkdir -m 700 -p #{dir}; "}.join
  end

  def assert_initialized

    begin
      test_initialized_file = "test -f /srv/leap/initialized"
      check_required_packages = "! dpkg-query -W --showformat='${Status}\n' #{required_packages} 2>&1 | grep -q -E '(deinstall|no packages)'"
      run "#{test_initialized_file} && #{check_required_packages}"
    rescue Capistrano::CommandError => exc
      LeapCli::Util.bail! do
        exc.hosts.each do |host|
          LeapCli::Util.log :error, "running deploy: node not initialized. Run 'leap init-node #{host}'", :host => host
        end
      end
    end
  end

  def mark_initialized
    run "touch /srv/leap/initialized"
  end

  #def mkdir(dir)
  #  run "mkdir -p #{dir}"
  #end

  #def chown_root(dir)
  #  run "chown root -R #{dir} && chmod -R ag-rwx,u+rwX #{dir}"
  #end

  #
  # takes a block, yielded a server, that should return {:source => '', :dest => ''}
  #
  def rsync_update
    SupplyDrop::Util.thread_pool_size = puppet_parallel_rsync_pool_size
    servers = SupplyDrop::Util.optionally_async(find_servers, puppet_parallel_rsync)

    # rsync to each server
    failed_servers = []
    servers.each do |server|
      # build rsync command
      paths       = yield server
      remote_user = server.user || fetch(:user, ENV['USER'])
      rsync_cmd = SupplyDrop::Rsync.command(
        paths[:source],
        SupplyDrop::Rsync.remote_address(remote_user, server.host, paths[:dest]),
        {:ssh => ssh_options.merge(server.options[:ssh_options]||{})}
      )

      # run command
      logger.debug rsync_cmd
      ok = system(rsync_cmd)
      if ok
        logger.log 1, "rsync #{paths[:source]} #{paths[:dest]}", server.host, :color => :green
      else
        failed_servers << server.host
      end
    end

    raise "rsync failed on #{failed_servers.join(',')}" if failed_servers.any?
  end

  #def logrun(cmd)
  #  @streamer ||= LeapCli::Remote::LogStreamer.new
  #  run cmd do |channel, stream, data|
  #    @streamer.collect_output(channel[:host], data)
  #  end
  #end

#    return_code = nil
#    run "something; echo return code: $?" do |channel, stream, data|
#      if data =~ /return code: (\d+)/
#        return_code = $1.to_i
#      else
#        Capistrano::Configuration.default_io_proc.call(channel, stream, data)
#      end
#    end
#    puts "finished with return code: #{return_code}"

end; end; end