Class: Deplomat::Node
- Inherits:
-
Object
- Object
- Deplomat::Node
- Defined in:
- lib/deplomat/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_path ⇒ Object
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.
- #stdout_lines_to_ignore ⇒ Object
-
#wrap_in ⇒ Object
Returns the value of attribute wrap_in.
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)
- #current_requisite_number(fn = "#{@current_path}/.deployment_requisites_counter") ⇒ Object
- #execute(command, path = @current_path, message: [], stdout_source: :stdout, log_command: true, _raise_exceptions: @raise_exceptions) ⇒ 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], path: nil, log_tasks_status: true, raise_exceptions: true) ⇒ Node
constructor
A new instance of Node.
- #log(line, color: 'light_black') ⇒ Object
- #move(what, where) ⇒ Object (also: #mv)
- #path_exist?(path) ⇒ Boolean (also: #path_exists?, #file_exists?)
- #remove(what) ⇒ Object (also: #rm)
- #update_requisite_number!(n, counter_file_path: "#{@current_path}/.deployment_requisites_counter") ⇒ Object
Constructor Details
#initialize(logfile: "#{Dir.pwd}/deplomat.log", log_to: [:stdout], path: nil, log_tasks_status: true, raise_exceptions: true) ⇒ Node
Returns a new instance of Node.
16 17 18 19 20 21 22 |
# File 'lib/deplomat/node.rb', line 16 def initialize(logfile: "#{Dir.pwd}/deplomat.log", log_to: [:stdout], path: nil, log_tasks_status: true, raise_exceptions: true) @log_to = log_to @log_tasks_status = log_tasks_status @logfile = logfile @raise_exceptions = raise_exceptions @current_path = path.sub(/\/+\Z/, '') if !path.nil? && path_exist?(path) end |
Instance Attribute Details
#current_path ⇒ Object
Returns the value of attribute current_path.
5 6 7 |
# File 'lib/deplomat/node.rb', line 5 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 |
#stdout_lines_to_ignore ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/deplomat/node.rb', line 8 def stdout_lines_to_ignore if @stdout_lines_to_ignore.kind_of?(Array) @stdout_lines_to_ignore else [@stdout_lines_to_ignore] end.compact end |
#wrap_in ⇒ Object
Returns the value of attribute wrap_in.
5 6 7 |
# File 'lib/deplomat/node.rb', line 5 def wrap_in @wrap_in end |
Instance Method Details
#cd(path) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/deplomat/node.rb', line 120 def cd(path) raise Deplomat::NoSuchPathError, path if !path.nil? && !path_exist?(path) @current_path = if path =~ /\A[\/~]/ || path.nil? path else File.("#{@current_path}/#{path}") end # Remove / from the end of the current path @current_path.sub!(/\/+\Z/, '') end |
#clean(path: @current_path, except: [], leave: [0, :last]) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/deplomat/node.rb', line 177 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, log_command: false)[:out].split("\n") if entries_by_date # 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}") } if entries_by_date end end |
#copy(what, where) ⇒ Object Also known as: cp
82 83 84 |
# File 'lib/deplomat/node.rb', line 82 def copy(what, where) execute("rsync -ar #{what} #{where}") end |
#create_dir(dirname) ⇒ Object Also known as: mkdir
102 103 104 |
# File 'lib/deplomat/node.rb', line 102 def create_dir(dirname) execute("mkdir -p #{dirname}") end |
#create_file(filename) ⇒ Object Also known as: touch
97 98 99 |
# File 'lib/deplomat/node.rb', line 97 def create_file(filename) execute("touch #{filename}") end |
#create_symlink(source, target) ⇒ Object Also known as: ln
107 108 109 |
# File 'lib/deplomat/node.rb', line 107 def create_symlink(source, target) execute("ln -s #{source} #{target}") end |
#current_requisite_number(fn = "#{@current_path}/.deployment_requisites_counter") ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/deplomat/node.rb', line 161 def current_requisite_number(fn="#{@current_path}/.deployment_requisites_counter") if file_exists?(fn) contents = execute("cat #{fn}")[:out].chomp("\n") if contents =~ /\A\d+\Z/ return contents.to_i else 0 end else log "Requisite counter file `#{fn}` doesn't exist. " + "Please create it manually and symlink it in the deployment script if necessary.", color: "red" self.close if self.respond_to?(:close) exit 1 end end |
#execute(command, path = @current_path, message: [], stdout_source: :stdout, log_command: true, _raise_exceptions: @raise_exceptions) ⇒ Object
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 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/deplomat/node.rb', line 24 def execute(command, path=@current_path, message: [], stdout_source: :stdout, log_command: true, _raise_exceptions: @raise_exceptions) original_command = command if @wrap_in command = @wrap_in.sub("_command_", command) end = [] if .kind_of?(String) log([0] + "\n", color: 'white') unless .empty? || .nil? # Respect current_path command_to_log = original_command if path command_to_log = "#{original_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 stdout_source_object.readlines.each_with_index do |line, i| self.stdout_lines_to_ignore.each do |line_to_ignore| line_to_ignore = line_to_ignore.to_a if line_to_ignore.kind_of?(Range) line_to_ignore = [line_to_ignore] if line_to_ignore.kind_of?(Integer) line = nil if line_to_ignore.include?(i+1) end if line out << line log(" #{line}") if log_command end 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
145 146 147 |
# File 'lib/deplomat/node.rb', line 145 def git_checkout(target) execute("git checkout #{target}") end |
#git_merge(source = "origin", target = "master") ⇒ Object
141 142 143 |
# File 'lib/deplomat/node.rb', line 141 def git_merge(source="origin", target="master") execute("git merge #{source} #{target}") end |
#git_pull(remote = "origin", branch = "master") ⇒ Object
137 138 139 |
# File 'lib/deplomat/node.rb', line 137 def git_pull(remote="origin", branch="master") execute("git pull #{remote} #{branch}") end |
#git_push(remote = "origin", branch = "master") ⇒ Object
133 134 135 |
# File 'lib/deplomat/node.rb', line 133 def git_push(remote="origin", branch="master") execute("git push #{remote} #{branch}") end |
#log(line, color: 'light_black') ⇒ Object
191 192 193 194 195 |
# File 'lib/deplomat/node.rb', line 191 def log(line, color: 'light_black') @message_color = color # Only calls log methods mentioned in the @log_to property @log_to.each { |logger| self.send("log_to_#{logger}", line) } end |
#move(what, where) ⇒ Object Also known as: mv
87 88 89 |
# File 'lib/deplomat/node.rb', line 87 def move(what, where) execute("mv #{what} #{where}") end |
#path_exist?(path) ⇒ Boolean Also known as: path_exists?, file_exists?
112 113 114 115 116 |
# File 'lib/deplomat/node.rb', line 112 def path_exist?(path) execute("[ -e #{path} ]", @current_path, log_command: false, _raise_exceptions: true) && true rescue Deplomat::ExecutionError # returned 1, file doesn't exist false end |
#remove(what) ⇒ Object Also known as: rm
92 93 94 |
# File 'lib/deplomat/node.rb', line 92 def remove(what) execute("rm -rf #{what}") end |
#update_requisite_number!(n, counter_file_path: "#{@current_path}/.deployment_requisites_counter") ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/deplomat/node.rb', line 149 def update_requisite_number!(n, counter_file_path: "#{@current_path}/.deployment_requisites_counter") current_number = current_requisite_number(counter_file_path) if n <= current_number log "New requisite number (#{n}) is below or equals the current one (#{current_number}). " + "Something must have gone wrong.", color: "red" self.close if self.respond_to?(:close) exit 1 else execute("echo '#{n}' > #{counter_file_path}") end end |