class Environmate::EnvironmentManager

Public Class Methods

cleanup() click to toggle source

cleanup old links and environments

# File lib/environmate/environment_manager.rb, line 49
def self.cleanup
  cleanup_links
  cleanup_environments
  cleanup_orphan_dirs
end
env_from_branch(branch) click to toggle source

puppet environment name from branch name

# File lib/environmate/environment_manager.rb, line 56
def self.env_from_branch(branch)
  prefix = Environmate.configuration['dynamic_environments_prefix']
  if branch.start_with?(prefix)
    branch.gsub(prefix, '').gsub(/(\/|\-)/,'_')
  else
    nil
  end
end
environments() click to toggle source
# File lib/environmate/environment_manager.rb, line 12
def self.environments
  env_dir_entries('directory').map do |dir|
    env = Environment.new(dir)
    env.valid? ? env : nil
  end.compact
end
find(env_name) click to toggle source
# File lib/environmate/environment_manager.rb, line 5
def self.find(env_name)
  environments.find do |env|
    env.name == env_name ||
    env.links.any?{|link| File.basename(link) == env_name}
  end
end
master() click to toggle source

get the master environment and make sure it is created and updated.

# File lib/environmate/environment_manager.rb, line 35
def self.master
  master_path       = Environmate.configuration['master_path']
  master_repository = Environmate.configuration['master_repository']
  master_branch     = Environmate.configuration['master_branch']
  unless File.exists?(File.join(master_path,'.git'))
    git = GitRepository.new(master_path)
    git.clone(master_repository)
  end
  master_env = Environment.new(master_path)
  master_env.update("origin/#{master_branch}")
  master_env
end
orphan_dirs() click to toggle source
# File lib/environmate/environment_manager.rb, line 19
def self.orphan_dirs
  env_dir_entries('directory').map do |dir|
    env = Environment.new(dir)
    env.valid? ? nil : env
  end.compact
end

Private Class Methods

cleanup_environments() click to toggle source

cleanup all the old environments

# File lib/environmate/environment_manager.rb, line 98
def self.cleanup_environments
  used_envs = links.values.uniq
  environments.each do |env|
    unless used_envs.include?(env.name)
      Environmate.log.debug("Cleanup: Removing old environment #{env.name}")
      env.delete
    end
  end
end
cleanup_orphan_dirs() click to toggle source

cleanup all orphan directories

# File lib/environmate/environment_manager.rb, line 109
def self.cleanup_orphan_dirs
  orphan_dirs.each do |env|
    Environmate.log.debug("Cleanup: Removing orphan dir #{env.name}")
    env.delete
  end
end
env_dir_entries(ftype) click to toggle source

returns an array of direcories in the environement path

# File lib/environmate/environment_manager.rb, line 77
def self.env_dir_entries(ftype)
  env_path = Environmate.configuration['environment_path']
  Dir[env_path + '/*'].map do |dir|
    File.ftype(dir) == ftype ? dir : nil
  end.compact
end
envs_with_branches() click to toggle source

returns an array of environments which has remote branches

# File lib/environmate/environment_manager.rb, line 68
def self.envs_with_branches
  env_path = Environmate.configuration['master_path']
  git = GitRepository.new(env_path)
  git.remote_branches.map do |branch|
    env_from_branch(branch)
  end.compact
end