Class: Management::Server::Run

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

Instance Method Summary collapse

Methods included from Helper

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

Methods inherited from Command

all, #arg_list, #call_with, #command_name, #fn, #help_string, inherited, maxlen, #true_arity

Instance Method Details

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



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
# File 'lib/management/commands/server/run.rb', line 48

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

  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
      server.chmod(remote_path, custom_chmod) if custom_chmod
    end
  end
end

#missing_local_files(script) ⇒ Object



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

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



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

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



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
# File 'lib/management/commands/server/run.rb', line 11

def run(server_name, script_name)
  require 'tmpdir'
  require 'fileutils'
  require 'erb'
  require 'shellwords'

  server = get_server(server_name)
  script = get_script(script_name)

  server.private_key_path = config[: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
      local_path, remote_path, opts = *data
      puts "############ Copying #{local_path} -> #{remote_path}"

      copy_file(server, local_path, remote_path, opts)
    when :run
      cmd = data
      puts "############ Running #{cmd}"

      code = run_remote_command(server, cmd)
      if code != 0
        abort "############ Failed. Exit code: #{code}"
      end
    end
  end

  puts "############ Success!"
end

#run_remote_command(server, cmd) ⇒ Object

returns error code



83
84
85
86
# File 'lib/management/commands/server/run.rb', line 83

def run_remote_command(server, cmd)
  result = server.ssh("#{cmd}").first
  return result.status
end