Class: Management::RunScript

Inherits:
Command
  • Object
show all
Includes:
Helper
Defined in:
lib/management/commands/run_script.rb

Instance Method Summary collapse

Methods included from Helper

#cloud, #config, #get_env, #get_script, #get_server, #get_type, #live_servers, #system_verbose

Methods inherited from Command

all, #call_with, #command_name, #fn, #help_string, inherited

Instance Method Details

#copy_file(server, local_path, remote_path, opts = nil) ⇒ Object



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
# File 'lib/management/commands/run_script.rb', line 36

def copy_file(server, local_path, remote_path, opts = nil)
  should_template = opts && opts[:template]
  custom_chown = opts && opts[:chown]

  puts "Copying #{local_path} -> #{remote_path}"

  Dir.mktmpdir('management-file-dir') do |file_tmpdir|

    # copy to the fake "remote" path locally
    remote_looking_path = File.join(file_tmpdir, remote_path)
    FileUtils.mkdir_p File.dirname(remote_looking_path)
    FileUtils.cp_r local_path, remote_looking_path, preserve: true

    # overwrite the fake "remote" file with its own templated contents if necessary
    if should_template
      new_contents = ERB.new(File.read(remote_looking_path)).result(binding)
      File.write(remote_looking_path, new_contents)
    end

    Dir.mktmpdir('management-tar-dir') do |tar_tmpdir|

      # zip this file up, starting from its absolute path
      local_tar_path = File.join(tar_tmpdir, "__management__.tar.gz")
      zip_relevant_files(file_tmpdir, local_tar_path)

      # copy tar file to remote and extract
      remote_tar_path = "/tmp/__management__.tar.gz"
      server.copy_file(local_tar_path, remote_tar_path)
      server.extract_tar(remote_tar_path)
      server.chown_r(remote_path, custom_chown) if custom_chown

    end

  end

end

#missing_local_files(script) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/management/commands/run_script.rb', line 89

def missing_local_files(script)
  script.find_all do |tuple|
    type, data = *tuple.first
    if type == :copy
      local, remote = *data
      ! File.exists?(local)
    end
  end.map do |tuple|
    type, data = *tuple.first
    local, remote = *data
    local
  end
end

#relevant_files(at_dir) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/management/commands/run_script.rb', line 103

def relevant_files(at_dir)
  abort unless at_dir.start_with? "/"

  Dir[File.join(at_dir, "**/*")].select do |path|
    File.file?(path) || (File.directory?(path) && Dir.entries(path) == [".", ".."])
  end.map do |path|
    path.slice! at_dir.end_with?("/") ? at_dir : "#{at_dir}/"
    "./#{path}"
  end
end

#run(server_name, script_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/management/commands/run_script.rb', line 13

def run(server_name, script_name)
  server = get_server(server_name)
  script = get_script(script_name)

  server.private_key_path = config[:types][server.type.to_sym][:ssh_key_path]

  missing = missing_local_files(script)
  abort "The following files are missing:" + (["\n"] + missing).join("\n - ") if !missing.empty?

  script.each do |tuple|
    type, data = *tuple.first

    case type.to_sym
    when :copy
      copy_file(server, *data)
    when :run
      run_remote_command(server, data)
    end

  end

end

#run_remote_command(server, cmd) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/management/commands/run_script.rb', line 73

def run_remote_command(server, cmd)
  puts "Running #{cmd}"

  result = server.ssh("#{cmd}").first

  if result.respond_to?(:status)
    puts
    puts "---------------------------"
    if result.status == 0
      puts "Success!"
    else
      puts "Failed. Exit code: #{result.status}"
    end
  end
end