Module: Local

Extended by:
Local
Included in:
Local
Defined in:
lib/local.rb

Constant Summary collapse

DEFAULT_TARGET =
{ user: "root", port: 22 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#as_user_blockObject

Returns the value of attribute as_user_block.



8
9
10
# File 'lib/local.rb', line 8

def as_user_block
  @as_user_block
end

#local_filesObject

Returns the value of attribute local_files.



65
66
67
# File 'lib/local.rb', line 65

def local_files
  @local_files
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/local.rb', line 5

def target
  @target
end

Instance Method Details

#check_mode(mode) ⇒ Object



60
61
62
63
# File 'lib/local.rb', line 60

def check_mode(mode)
  mode = mode.to_i(8) if mode.is_a? String
  die "please supply up to 4 digits before the leading zero, example: 00644" if mode && mode >= 010000
end

#copy_local_files_to(dir) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/local.rb', line 69

def copy_local_files_to(dir)
  @local_files.each do |local_file|
    # dublicating the whole directory stucture
    acc = []
    Pathname.new(local_file).each_filename do |chunk|
      source_path = File.join(*(['/'] + acc << chunk))
      dst = File.join([dir] + acc << chunk)
      if File.directory? source_path
        unless File.directory? dst
          debug "mkdir #{dst}"
          FileUtils.mkdir_p(dst)
        end
      else
        debug "cp #{source_path} #{dst}"
        FileUtils.cp(source_path, dst)
      end
      acc << chunk
    end
  end
end

#debug(s) ⇒ Object



134
135
136
# File 'lib/local.rb', line 134

def debug(s)
  info s if @verbose
end

#die(message) ⇒ Object



21
22
23
24
25
# File 'lib/local.rb', line 21

def die(message)
  $stderr.puts "Script error: #{message}"
  print_location
  exit 1
end

#info(s) ⇒ Object



138
139
140
141
# File 'lib/local.rb', line 138

def info(s)
  $stdout.puts s
  $stdout.flush
end

#perform!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/local.rb', line 91

def perform!
  die "no target host specified!" if (@target[:host] || "").empty?
  script_path = File.expand_path(ENV["_"])
  script_dir = File.dirname(File.expand_path(ENV["_"]))
  status = nil
  Dir.mktmpdir "hatecf" do |tmp_dir|
    debug "using temporary dir #{tmp_dir}"
    FileUtils.cp(script_path, File.join(tmp_dir, "script.rb"))
    File.chmod(00777, File.join(tmp_dir, "script.rb"))
    FileUtils.cp(File.join(__dir__, "../remote/hatecf.rb"    ), tmp_dir)
    FileUtils.cp(File.join(__dir__, "../remote/remote_dsl.rb"), tmp_dir)
    FileUtils.cp(File.join(__dir__, "../remote/remote.rb"    ), tmp_dir)
    FileUtils.cp(File.join(__dir__, "../bootstrap_ruby"      ), tmp_dir)
    File.chmod(00777, File.join(tmp_dir, "bootstrap_ruby"))
    unless @local_files.empty?
      local_files_dir = File.join(tmp_dir, "local_files")
      FileUtils.mkdir local_files_dir
      copy_local_files_to(local_files_dir)
    end
    script_opts = []
    script_opts << "--dry" if @dry_run
    script_opts << "-v" if @verbose
    script_opts << "--local-home #{ENV["HOME"]}"
    script_opts << "--local-script-dir #{script_dir}"
    cmd = "tar -czP #{tmp_dir} | ssh #{@target[:user] || DEFAULT_TARGET[:user]}@#{@target[:host]} \"tar -xzP -C / && (cd #{tmp_dir} && ./bootstrap_ruby && RUBYLIB=. ./script.rb #{script_opts.join(" ")}); rm -r #{tmp_dir}\""
    debug "executing: #{cmd}"
    pid, status = Process.wait2(Process.spawn(cmd))
    debug "exit status: #{status.exitstatus}"
  end
  # FIXME: this always returns the status of "; rm" above, doesn't it?
  exit status.exitstatus
end


11
12
13
14
15
16
17
18
19
# File 'lib/local.rb', line 11

def print_location
  exclude = [
    "local.rb",
    "local_dsl.rb",
  ]
  caller.each do |line|
    $stderr.puts line unless exclude.find{|x| line.include? x}
  end
end

#type_check(name, value, *types) ⇒ Object



27
28
29
30
31
32
33
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
# File 'lib/local.rb', line 27

def type_check(name, value, *types)
  unless types.find{|x| value.is_a? x}
    case name
    when 1
      arg_dsc = "first argument"
    when 2
      arg_dsc = "second argument"
    when 3
      arg_dsc = "third argument"
    when Symbol
      arg_dsc = %{argument "#{name}:"}
    else
      arg_dsc = "argument"
    end
    if types.length > 1
      types_dsc = "types #{types.map(&:to_s).join(" or ")}"
    else
      types_dsc = "type #{types.first}"
    end
    hints = types.map do |t|
      case t.to_s
      when "LocalFile"
        %{LocalFile is a file on this very computer and could be defined as local_file("~/hello")}
      when "Regexp"
        "Regexp is a regular expression wrapped in /slashes/"
      else
        nil
      end
    end.compact.map{|x|"\nHint: #{x}"}.join
    die "#{arg_dsc} should be of #{types_dsc}, but #{value.class.to_s} here#{hints}"
  end
end