Class: Management::RunScript

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

Instance Method Summary collapse

Methods inherited from Command

all, #cloud, command_name, #config, #get_env, #get_script, #get_server, #get_type, help_string, inherited

Instance Method Details

#call(server_name, script_name) ⇒ Object



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

def call(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_command(server, data)
    end

  end

end

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



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

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



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

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



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

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_command(server, cmd) ⇒ Object



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

def run_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