Class: Gorgon::SourceTreeSyncer

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgon/source_tree_syncer.rb

Constant Summary collapse

RSYNC_TRANSPORT_SSH =
'ssh'
RSYNC_TRANSPORT_ANONYMOUS =
'anonymous'
SYS_COMMAND =
'rsync'
OPTS =
'-azr --timeout=5 --delete'
RSH_OPTS =
'ssh -o NumberOfPasswordPrompts=0 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i gorgon.pem'
EXCLUDE_OPT =
'--exclude'
BLANK_SOURCE_TREE_ERROR =
"Source tree path cannot be blank. Check your gorgon.json file."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sync_config) ⇒ SourceTreeSyncer

Returns a new instance of SourceTreeSyncer.



16
17
18
19
20
21
# File 'lib/gorgon/source_tree_syncer.rb', line 16

def initialize(sync_config)
  sync_config ||= {}
  @source_tree_path = sync_config[:source_tree_path].to_s
  @exclude          = sync_config[:exclude]
  @rsync_transport  = sync_config[:rsync_transport]
end

Instance Attribute Details

#excludeObject (readonly)

Returns the value of attribute exclude.



14
15
16
# File 'lib/gorgon/source_tree_syncer.rb', line 14

def exclude
  @exclude
end

#rsync_transportObject (readonly)

Returns the value of attribute rsync_transport.



14
15
16
# File 'lib/gorgon/source_tree_syncer.rb', line 14

def rsync_transport
  @rsync_transport
end

#source_tree_pathObject (readonly)

Returns the value of attribute source_tree_path.



14
15
16
# File 'lib/gorgon/source_tree_syncer.rb', line 14

def source_tree_path
  @source_tree_path
end

#tempdirObject (readonly)

Returns the value of attribute tempdir.



14
15
16
# File 'lib/gorgon/source_tree_syncer.rb', line 14

def tempdir
  @tempdir
end

Instance Method Details

#pullObject

Pulls the source code to ./gorgon/ temporary directory from source_tree_path and calls the passed block.

The temporary directory is removed after block is called even if an exception is raised by the block.

Returns and yields an object that represents executed command context for pulling the source code. It has following attributes:

  • execution_context.command: The command that was executed

  • execution_context.success: true if command execution returned exit status of 0, false otherwise.

  • execution_context.output: Output written on standard output by the command during execution.

  • execution_context.errors: Output written on standard error by the command during execution.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gorgon/source_tree_syncer.rb', line 39

def pull
  source = source_tree_path + "/"
  command = make_command(source, ".")
  if blank_source_tree_path?
    execution_context = prepare_execution_context(command, false, output: nil, errors: BLANK_SOURCE_TREE_ERROR)
    yield(execution_context) if block_given?
    return execution_context
  end

  @tempdir = Dir.mktmpdir("gorgon")
  Dir.chdir(@tempdir)

  execution_context = execute_command(command)
  begin
    yield(execution_context) if block_given?
  ensure
    cleanup
  end

  execution_context
end

#pushObject

Pushes the source code to source_tree_path.

Returns an object that represents executed command context for pushing the source code. It has following attributes:

  • execution_context.command: The command that was executed

  • execution_context.success: true if command execution returned exit status of 0, false otherwise.

  • execution_context.output: Output written on standard output by the command during execution.

  • execution_context.errors: Output written on standard error by the command during execution.



73
74
75
76
77
78
# File 'lib/gorgon/source_tree_syncer.rb', line 73

def push
  command = make_command('.', source_tree_path)
  return prepare_execution_context(command, false, output: nil, errors: BLANK_SOURCE_TREE_ERROR) if blank_source_tree_path?

  execute_command(command)
end