Class: SSHKit::Backend::Netssh

Inherits:
Object
  • Object
show all
Defined in:
lib/cronicle/ext/sshkit_ext.rb

Constant Summary collapse

CRON_DIRS =
%w(/var/spool/cron/crontabs /var/spool/cron)

Instance Method Summary collapse

Instance Method Details

#add_cron_entry(user, name, schedule, temp_dir) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cronicle/ext/sshkit_ext.rb', line 76

def add_cron_entry(user, name, schedule, temp_dir)
  script = script_path(user, name)
  temp_entry = [temp_dir, name + '.entry'].join('/')

  cron_entry = "#{schedule}\\t#{script} 2>&1 | logger -t cronicle/#{user}/#{name}"
  cron_entry = Shellwords.shellescape(cron_entry)
  sudo(:execute, :echo, '-e', cron_entry, '>', temp_entry)

  entry_cat = "cat #{temp_entry} >> #{user_crontab(user)}"
  entry_cat = Shellwords.shellescape(entry_cat)
  sudo(:execute, :bash, '-c', entry_cat)
end

#delete_cron_entry(user, name = nil) ⇒ Object



69
70
71
72
73
74
# File 'lib/cronicle/ext/sshkit_ext.rb', line 69

def delete_cron_entry(user, name = nil)
  sed_cmd = '/' + Cronicle::Utils.sed_escape(script_path(user, name)) + ' /d'
  sed_cmd = Shellwords.shellescape(sed_cmd)

  sudo(:execute, :sed, '-i', sed_cmd, user_crontab(user), :raise_on_non_zero_exit => false)
end

#fetch_crontabsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cronicle/ext/sshkit_ext.rb', line 40

def fetch_crontabs
  return @crontabs if @crontabs

  @crontabs = {}

  list_crontabs.each do |path|
    user = File.basename(path)
    crontab = sudo(:capture, :cat, path)
    @crontabs[user] = crontab
  end

  @crontabs
end

#fetch_libexec_scriptsObject



59
60
61
62
63
64
65
66
67
# File 'lib/cronicle/ext/sshkit_ext.rb', line 59

def fetch_libexec_scripts
  script_contents = {}

  list_libexec_scripts.each do |script|
    script_contents[script] = crlf_to_lf(capture(:cat, script))
  end

  script_contents
end

#find_cron_dirObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cronicle/ext/sshkit_ext.rb', line 22

def find_cron_dir
  @cron_dir ||= CRON_DIRS.find do |path|
    execute(:test, '-d', path, :raise_on_non_zero_exit => false)
  end

  unless @cron_dir
    raise "Cannot find cron directory: #{CRON_DIRS.join(', ')}"
  end

  @cron_dir
end

#libexec_dirObject



116
117
118
# File 'lib/cronicle/ext/sshkit_ext.rb', line 116

def libexec_dir
  host.options.fetch(:libexec)
end

#list_crontabsObject



34
35
36
37
38
# File 'lib/cronicle/ext/sshkit_ext.rb', line 34

def list_crontabs
  cron_dir = find_cron_dir
  @crontab_list ||= sudo(:capture, :find, cron_dir, '-type', :f, '-maxdepth', 1, '2> /dev/null',
                      :raise_on_non_zero_exit => false).each_line.map(&:strip)
end

#list_libexec_scriptsObject



54
55
56
57
# File 'lib/cronicle/ext/sshkit_ext.rb', line 54

def list_libexec_scripts
  @libexec_scripts ||= capture(:find, libexec_dir, '-type', :f, '2> /dev/null',
                         :raise_on_non_zero_exit => false).each_line.map(&:strip)
end

#log_for_cronicle(level, message, opts = {}) ⇒ Object



133
134
135
136
# File 'lib/cronicle/ext/sshkit_ext.rb', line 133

def log_for_cronicle(level, message, opts = {})
  opts = host.options.merge(opts)
  Cronicle::Logger.log(level, message, opts)
end

#mktemp(user = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cronicle/ext/sshkit_ext.rb', line 96

def mktemp(user = nil)
  temp_dir = capture(:mktemp, '-d', '/var/tmp/cronicle.XXXXXXXXXX')
  block_args = [temp_dir]

  begin
    execute(:chmod, 755, temp_dir)

    if user
      user_temp_dir = [temp_dir, user].join('/')
      execute(:mkdir, '-p', user_temp_dir)
      execute(:chmod, 755, user_temp_dir)
      block_args << user_temp_dir
    end

    yield(*block_args)
  ensure
    execute(:rm, '-rf', temp_dir, :raise_on_non_zero_exit => false) rescue nil
  end
end

#script_path(user, name) ⇒ Object



129
130
131
# File 'lib/cronicle/ext/sshkit_ext.rb', line 129

def script_path(user, name)
  [libexec_dir, user, name].join('/')
end

#sudo(command, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cronicle/ext/sshkit_ext.rb', line 4

def sudo(command, *args)
  opts = args.last.kind_of?(Hash) ? args.pop : {}

  password = host.options[:sudo_password] || ''
  password = Shellwords.shellescape(password)

  with_sudo = [:echo, password, '|', :sudo, '-S']
  with_sudo << '-u' << opts[:user] if opts[:user]
  with_sudo.concat(args)

  raise_on_non_zero_exit = opts.fetch(:raise_on_non_zero_exit, true)
  retval = send(command, *with_sudo, :raise_on_non_zero_exit => raise_on_non_zero_exit)
  Cronicle::Utils.remove_prompt!(retval) if retval.kind_of?(String)
  retval
end

#upload_script(temp_dir, name, content) {|temp_script| ... } ⇒ Object

Yields:

  • (temp_script)


89
90
91
92
93
94
# File 'lib/cronicle/ext/sshkit_ext.rb', line 89

def upload_script(temp_dir, name, content)
  temp_script = [temp_dir, name].join
  upload!(StringIO.new(content), temp_script)
  execute(:chmod, 755, temp_script)
  yield(temp_script)
end

#user_crontab(user) ⇒ Object



124
125
126
127
# File 'lib/cronicle/ext/sshkit_ext.rb', line 124

def user_crontab(user)
  cron_dir = find_cron_dir
  [cron_dir, user].join('/')
end

#user_libexec_dir(user) ⇒ Object



120
121
122
# File 'lib/cronicle/ext/sshkit_ext.rb', line 120

def user_libexec_dir(user)
  [libexec_dir, user].join('/')
end