Class: Deplomat::Node
- Inherits:
-
Object
- Object
- Deplomat::Node
- Defined in:
- lib/deplomat/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_path ⇒ Object
readonly
Returns the value of attribute current_path.
-
#log_to ⇒ Object
Returns the value of attribute log_to.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#raise_exceptions ⇒ Object
Returns the value of attribute raise_exceptions.
Instance Method Summary collapse
- #cd(path) ⇒ Object
- #clean(path: @current_path, except: [], leave: [0, :last]) ⇒ Object
- #copy(what, where) ⇒ Object (also: #cp)
- #create_dir(dirname) ⇒ Object (also: #mkdir)
- #create_file(filename) ⇒ Object (also: #touch)
- #create_symlink(source, target) ⇒ Object (also: #ln)
- #execute(command, path = @current_path, message: [], stdout_source: :stdout, log_command: true) ⇒ Object
- #git_checkout(target) ⇒ Object
- #git_merge(source = "origin", target = "master") ⇒ Object
- #git_pull(remote = "origin", branch = "master") ⇒ Object
- #git_push(remote = "origin", branch = "master") ⇒ Object
-
#initialize(logfile: "#{Dir.pwd}/deplomat.log", log_to: [:stdout]) ⇒ Node
constructor
A new instance of Node.
- #move(what, where) ⇒ Object (also: #mv)
- #remove(what) ⇒ Object (also: #rm)
Constructor Details
#initialize(logfile: "#{Dir.pwd}/deplomat.log", log_to: [:stdout]) ⇒ Node
Returns a new instance of Node.
8 9 10 11 |
# File 'lib/deplomat/node.rb', line 8 def initialize(logfile: "#{Dir.pwd}/deplomat.log", log_to: [:stdout]) @log_to = log_to @logfile = logfile end |
Instance Attribute Details
#current_path ⇒ Object (readonly)
Returns the value of attribute current_path.
6 7 8 |
# File 'lib/deplomat/node.rb', line 6 def current_path @current_path end |
#log_to ⇒ Object
Returns the value of attribute log_to.
5 6 7 |
# File 'lib/deplomat/node.rb', line 5 def log_to @log_to end |
#logfile ⇒ Object
Returns the value of attribute logfile.
5 6 7 |
# File 'lib/deplomat/node.rb', line 5 def logfile @logfile end |
#raise_exceptions ⇒ Object
Returns the value of attribute raise_exceptions.
5 6 7 |
# File 'lib/deplomat/node.rb', line 5 def raise_exceptions @raise_exceptions end |
Instance Method Details
#cd(path) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/deplomat/node.rb', line 89 def cd(path) @current_path = if path =~ /\A[\/~]/ || path.nil? path else File.("#{@current_path}#{path}") end raise Deplomat::NoSuchPathError, @current_path if !@current_path.nil? && !path_exist?(@current_path) # Making sure we don't end up with double // at the end of the path @current_path = @current_path.chomp("/") + "/" end |
#clean(path: @current_path, except: [], leave: [0, :last]) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/deplomat/node.rb', line 117 def clean(path: @current_path, except: [], leave: [0, :last]) # Gets us all entries sorted by date, most recent ones first entries_by_date = execute("ls -t", path)[:out].split("\n") # Don't do anything with entries listed in :except entries_by_date = entries_by_date - except if leave entries_by_date.reverse! if leave[1] == :first entries_by_date = entries_by_date[leave[0]..entries_by_date.length] end entries_by_date.each { |entry| remove("#{path}#{entry}") } end |
#copy(what, where) ⇒ Object Also known as: cp
59 60 61 |
# File 'lib/deplomat/node.rb', line 59 def copy(what, where) execute("rsync -ar #{what} #{where}") end |
#create_dir(dirname) ⇒ Object Also known as: mkdir
79 80 81 |
# File 'lib/deplomat/node.rb', line 79 def create_dir(dirname) execute("mkdir #{dirname}") end |
#create_file(filename) ⇒ Object Also known as: touch
74 75 76 |
# File 'lib/deplomat/node.rb', line 74 def create_file(filename) execute("touch #{filename}") end |
#create_symlink(source, target) ⇒ Object Also known as: ln
84 85 86 |
# File 'lib/deplomat/node.rb', line 84 def create_symlink(source, target) execute("ln -s #{source} #{target}") end |
#execute(command, path = @current_path, message: [], stdout_source: :stdout, log_command: true) ⇒ Object
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 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/deplomat/node.rb', line 13 def execute(command, path=@current_path, message: [], stdout_source: :stdout, log_command: true) = [] if .kind_of?(String) log([0] + "\n", color: 'white') unless .empty? || .nil? # Respect current_path command_to_log = command if path command_to_log = "#{command}\n(in #{path})" command = "cd #{path} && #{command}" end out = "" status = nil Open3.popen3(command) do |stdin, stdout, stderr, thread| # Sometimes, programs write in stderr, although there are no errors. # rake assets:precompile does that, for example. stdout_source_object = (stdout_source == :stderr ? stderr : stdout) log("--> " + command_to_log + "\n", color: "white") if log_command while line=stdout_source_object.gets do out << line log(" #{line}") end error_out = "" status = thread.value.exitstatus.to_i if status > 0 while o = stderr.gets error_out += o end log(error_out + "\n", color: 'red') if @raise_exceptions self.close if self.respond_to?(:close) raise Deplomat::ExecutionError end end yield if block_given? end log([1] + "\n", color: 'white') unless .empty? || .nil? return { status: status, out: out } end |
#git_checkout(target) ⇒ Object
113 114 115 |
# File 'lib/deplomat/node.rb', line 113 def git_checkout(target) execute("git checkout #{target}") end |
#git_merge(source = "origin", target = "master") ⇒ Object
109 110 111 |
# File 'lib/deplomat/node.rb', line 109 def git_merge(source="origin", target="master") execute("git merge #{source} #{target}") end |
#git_pull(remote = "origin", branch = "master") ⇒ Object
105 106 107 |
# File 'lib/deplomat/node.rb', line 105 def git_pull(remote="origin", branch="master") execute("git pull #{remote} #{branch}") end |
#git_push(remote = "origin", branch = "master") ⇒ Object
101 102 103 |
# File 'lib/deplomat/node.rb', line 101 def git_push(remote="origin", branch="master") execute("git push -u #{remote} #{branch}") end |
#move(what, where) ⇒ Object Also known as: mv
64 65 66 |
# File 'lib/deplomat/node.rb', line 64 def move(what, where) execute("mv #{what} #{where}") end |
#remove(what) ⇒ Object Also known as: rm
69 70 71 |
# File 'lib/deplomat/node.rb', line 69 def remove(what) execute("rm -rf #{what}") end |