Class: SSHKit::Backend::Netssh

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

Constant Summary collapse

SUDO_PASSWORD_KEY =
:'__cronicle_sudo_password__'
SUDO_PROMPT =
'__cronicle_sudo_prompt__'
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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cronicle/ext/sshkit_ext.rb', line 80

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



73
74
75
76
77
78
# File 'lib/cronicle/ext/sshkit_ext.rb', line 73

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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cronicle/ext/sshkit_ext.rb', line 44

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



63
64
65
66
67
68
69
70
71
# File 'lib/cronicle/ext/sshkit_ext.rb', line 63

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cronicle/ext/sshkit_ext.rb', line 25

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



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

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

#list_crontabsObject



37
38
39
40
41
42
# File 'lib/cronicle/ext/sshkit_ext.rb', line 37

def list_crontabs
  cron_dir = find_cron_dir
  @crontab_list ||= sudo(:capture, :bash, '-c',
                      Shellwords.shellescape("find #{cron_dir} -type f 2> /dev/null"),
                      :raise_on_non_zero_exit => false).each_line.map(&:strip)
end

#list_libexec_scriptsObject



58
59
60
61
# File 'lib/cronicle/ext/sshkit_ext.rb', line 58

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



137
138
139
140
# File 'lib/cronicle/ext/sshkit_ext.rb', line 137

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

#mktemp(user = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cronicle/ext/sshkit_ext.rb', line 100

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



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

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

#sudo(command, *args) ⇒ Object



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

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

  retval = with_sudo_password(host.options[:sudo_password] || '') do
    with_sudo = [:sudo, '-p', SUDO_PROMPT, '-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)
    send(command, *with_sudo, :raise_on_non_zero_exit => raise_on_non_zero_exit)
  end

  Cronicle::Utils.remove_prompt!(retval) if retval.kind_of?(String)
  retval
end

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

Yields:

  • (temp_script)


93
94
95
96
97
98
# File 'lib/cronicle/ext/sshkit_ext.rb', line 93

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



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

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

#user_libexec_dir(user) ⇒ Object



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

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