Class: Cumuli::ProjectManager::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/cumuli/project_manager/project.rb

Constant Summary collapse

DEFAULT_WAIT_TIME =
10
LOCALHOST =
'127.0.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts) ⇒ Project

Returns a new instance of Project.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cumuli/project_manager/project.rb', line 10

def initialize(name, opts)
  @name = name

  @repository = opts['repository']
  @path = opts['path'] || "./#{name}"
  @port = opts['port']
  @type = opts['type'] || 'app'
  @wait_time = opts['wait_time'] || DEFAULT_WAIT_TIME
  @domain = opts['domain'] || '127.0.0.1'

  @database_config = opts['database_config'] || []
  @database_sample_config = opts['database_sample_config'] || []
  @setup_scripts = opts['setup_scripts'] || []

  @database_config = [@database_config] unless @database_config.is_a?(Array)
  @database_sample_config = [@database_sample_config] unless @database_sample_config.is_a?(Array)
end

Instance Attribute Details

#database_configObject (readonly)

Returns the value of attribute database_config.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def database_config
  @database_config
end

#database_sample_configObject (readonly)

Returns the value of attribute database_sample_config.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def database_sample_config
  @database_sample_config
end

#domainObject (readonly)

Returns the value of attribute domain.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def port
  @port
end

#repositoryObject (readonly)

Returns the value of attribute repository.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def repository
  @repository
end

#setup_scriptsObject (readonly)

Returns the value of attribute setup_scripts.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def setup_scripts
  @setup_scripts
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def type
  @type
end

#wait_timeObject (readonly)

Returns the value of attribute wait_time.



7
8
9
# File 'lib/cumuli/project_manager/project.rb', line 7

def wait_time
  @wait_time
end

Instance Method Details

#app_to_procfileObject



86
87
88
# File 'lib/cumuli/project_manager/project.rb', line 86

def app_to_procfile
  "#{name}: cumuli #{path} -p #{port}\n"
end

#database_config_pathsObject



62
63
64
# File 'lib/cumuli/project_manager/project.rb', line 62

def database_config_paths
  database_config.map { |config_path| full_path("#{path}/#{config_path}") }
end

#database_configured?(path) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/cumuli/project_manager/project.rb', line 66

def database_configured?(path)
  File.exist?(path)
end

#database_sample_pathsObject



58
59
60
# File 'lib/cumuli/project_manager/project.rb', line 58

def database_sample_paths
  database_sample_config.map { |config_path| full_path("#{path}/#{config_path}") }
end

#full_path(partial_path) ⇒ Object



53
54
55
56
# File 'lib/cumuli/project_manager/project.rb', line 53

def full_path(partial_path)
  partial_path.gsub!(/^\.\//, '')
  "#{Dir.pwd}/#{partial_path}"
end

#run_command(command) ⇒ Object



98
99
100
101
102
# File 'lib/cumuli/project_manager/project.rb', line 98

def run_command(command)
  fork do
    CLI::RemoteCommand.new([command, "DIR=#{path}"]).perform
  end
end

#service_to_procfileObject



90
91
92
# File 'lib/cumuli/project_manager/project.rb', line 90

def service_to_procfile
  "#{name}: cumuli #{path}\n"
end

#setupObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cumuli/project_manager/project.rb', line 104

def setup
  # checkout master, because sometimes on setup no branch is
  # checked out
  run_command 'git checkout master'

  write_database_config

  setup_scripts.each do |script|
    run_command script
  end
end

#socket_available?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/cumuli/project_manager/project.rb', line 42

def socket_available?
  TCPSocket.new(domain, port)
  true
rescue Errno::ECONNREFUSED
  false
end

#submodule_initObject



94
95
96
# File 'lib/cumuli/project_manager/project.rb', line 94

def submodule_init
  system "git submodule add #{repository} #{path}" if repository
end

#to_procfileObject



82
83
84
# File 'lib/cumuli/project_manager/project.rb', line 82

def to_procfile
  send("#{type}_to_procfile") rescue ''
end

#urlObject



49
50
51
# File 'lib/cumuli/project_manager/project.rb', line 49

def url
  "http://#{domain}#{port ? ":#{port}" : ''}"
end

#wait?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/cumuli/project_manager/project.rb', line 28

def wait?
  type == 'app' && (wait_time && wait_time > 0) && (port || domain != LOCALHOST)
end

#wait_for_startObject



32
33
34
35
# File 'lib/cumuli/project_manager/project.rb', line 32

def wait_for_start
  return unless wait?
  Waiter.new("Unable to start #{name}").wait_until(wait_time) { socket_available? }
end

#wait_for_stopObject



37
38
39
40
# File 'lib/cumuli/project_manager/project.rb', line 37

def wait_for_stop
  return unless wait?
  Waiter.new("Unable to stop #{name}").wait_until(wait_time) { !socket_available? }
end

#write_database_configObject



76
77
78
79
80
# File 'lib/cumuli/project_manager/project.rb', line 76

def write_database_config
  database_config_paths.each_with_index do |config_path, i|
    FileUtils.cp(database_sample_paths[i], config_path) unless database_configured?(config_path)
  end
end

#write_database_config!Object



70
71
72
73
74
# File 'lib/cumuli/project_manager/project.rb', line 70

def write_database_config!
  database_config_paths.each_with_index do |config_path, i|
    FileUtils.cp(database_sample_paths[i], config_path)
  end
end