class Librarian::Puppet::Simple::CLI

Public Class Methods

bin!() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 24
def self.bin!
  start
end

Public Instance Methods

clean() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 66
def clean
  target_directory = options[:path] || File.expand_path("./modules")
  puts "Target Directory: #{target_directory}" if options[:verbose]
  FileUtils.rm_rf target_directory
end
compare_repos() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 150
def compare_repos

  repo_hash = {}
  @verbose            = options[:verbose]
  abort('path not supported by compare_repos command') if options[:path]
  if options[:downstream_only] and options[:upstream_only]
    abort('Cannot specify both downstream_only and upstream_only')
  end

  # create path where code will be stored
  if options[:existing_tmp_dir]
    path = options[:existing_tmp_dir]
  else
    path = File.join('.tmp', Time.now.strftime("%Y_%m_%d_%H_%S"))
  end

  FileUtils.mkdir_p(path)
  @custom_module_path = path

  # install the downstream modules in our tmp directory and build out a hash
  downstream = build_puppetfile_hash('downstream', !options[:existing_tmp_dir])
  # just build a hash of the downstream modules
  upstream   = build_puppetfile_hash('upstream', false)

  unless ( (downstream.keys - upstream.keys) == [] and
           (upstream.keys - downstream.keys)
         )
    abort('Your Puppetfile did not produce the same upstream and downstream repos, this is not yet supported')
  else

    upstream.each do |us_name, us_repo|
      # compare to see if the source of revisions are the same
      ds_repo = downstream[us_name]
      if ds_repo[:git] == us_repo[:git] and ds_repo[:ref] == us_repo[:ref]
        print_verbose("\nSources of #{us_name} are the same, nothing to compare.")
      else
        Dir.chdir(File.join(path, us_name)) do
          if us_repo[:git] =~ /(git|https?):\/\/(.+)\/(.+)?\/(.+)/
            remote_name = $3
            remotes = system_cmd('git remote')
            if remotes.include?(remote_name)
              puts "Did not have to add remote #{remote_name} to #{us_repo[:name]}, it was already there"
            else
              puts "Adding remote #{remote_name} #{us_repo[:git]}"
              system_cmd("git remote add #{remote_name} #{us_repo[:git]}")
            end
            system_cmd("git fetch #{remote_name}")
            if us_repo[:ref] =~ /^origin\/(\S+)$/
              compare_ref = "#{remote_name}/#{$1}"
            else
              compare_ref = "#{remote_name}/#{us_repo[:ref]}"
            end

            # set up parameters for git log call
            ignore_merges = options[:ignore_merges] ? '--no-merges' : ''
            show_diffs    = options[:show_diffs]    ? '-u' : ''
            oneline       = options[:oneline]       ? '--oneline' : ''
            # show the results, this assumes that HEAD is up-to-date (which it should be)

            if options[:downstream_only] and options[:upstream_only]
              abort('Cannot specify both downstream_only and upstream_only')
            end
            puts "########## Results for #{us_name} ##########"
            unless options[:upstream_only]
              puts "  ######## Commits only in downstream ########"
              results = system_cmd("git log --left-only HEAD...#{compare_ref} #{ignore_merges} #{show_diffs} #{oneline}", true)
              puts "  ######## End Downstream results ########"
            end
            unless options[:downstream_only]
              puts "  ######## Commits only in upstream ########"
              results = system_cmd("git log --right-only HEAD...#{compare_ref} #{ignore_merges} #{show_diffs} #{oneline}", true)
              puts "  ######## End upstream ########"
            end
            puts "########## End of Results for #{us_name} ##########"
          else
            abort("Unrecognizable upstream url #{us_repo[:git]}")
          end
        end
      end
    end
  end
end
dev_setup(remote_name) click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 91
def dev_setup(remote_name)
  @custom_module_path = options[:path]
  # populate @modules
  eval(File.read(File.expand_path(options[:puppetfile])))
  each_module_of_type(:git) do |repo|
    Dir.chdir(File.join((options[:path] || 'modules'), repo[:name])) do
      print_verbose "Adding development remote for git repo #{repo[:name]}"
      remotes = system_cmd('git remote')
      if remotes.include?(remote_name)
        puts "Did not have to add remote #{remote_name} to #{repo[:name]}"
      elsif ! remotes.include?('origin')
        raise(TestException, "Repo #{repo[:name]} has no remote called origin, failing")
      else
        remote_url = system_cmd('git remote show origin').detect {|x| x =~ /\s+Push\s+URL: / }
        if remote_url =~ /(git|https?):\/\/(.+)\/(.+)?\/(.+)/
          url = "git@#{$2}:#{remote_name}/#{$4}"
          puts "Adding remote #{remote_name} as #{url}"
          system_cmd("git remote add #{remote_name} #{url}")
        elsif remote_url =~ /^git@/
          puts "Origin is already a read/write remote, skipping b/c this is unexpected"
        else
          puts "remote_url #{remote_url} did not have the expected format. weird..."
        end
      end
    end
  end
end
generate_puppetfile() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 236
def generate_puppetfile
  eval(File.read(File.expand_path(options[:puppetfile])))
  if options[:out_file]
    File.open(options[:out_file], 'w') do |fh|
      print_puppet_file(fh)
    end
  else
    print_puppet_file(STDOUT)
  end
end
git_status() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 73
def git_status
  @custom_module_path = options[:path]
  # populate @modules
  eval(File.read(File.expand_path(options[:puppetfile])))
  each_module_of_type(:git) do |repo|
    Dir.chdir(File.join(module_path, repo[:name])) do
      status = system_cmd('git status')
      if status.include?('nothing to commit (working directory clean)')
        puts "Module #{repo[:name]} has not changed" if options[:verbose]
      else
        puts "Uncommitted changes for: #{repo[:name]}"
        puts "  #{status.join("\n  ")}"
      end
    end
  end
end
install() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 30
def install
  @verbose = options[:verbose]
  clean if options[:clean]
  @custom_module_path = options[:path]
  # evaluate the file to populate @modules
  eval(File.read(File.expand_path(options[:puppetfile])))
  install!
end
update() click to toggle source
# File lib/librarian/puppet/simple/cli.rb, line 41
def update
  @verbose = options[:verbose]
  @custom_module_path = options[:path]
  eval(File.read(File.expand_path(options[:puppetfile])))
  each_module_of_type(:git) do |repo|
    if Dir.exists?(File.join(module_path, repo[:name]))
      Dir.chdir(File.join(module_path, repo[:name])) do
        remote = repo[:git]
        # if no ref is given, assume master
        branch = repo[:ref] || 'master'
        if branch =~ /^origin\/(.*)$/
          branch = $1
        end
        co_cmd     = 'git checkout FETCH_HEAD'
        update_cmd = "git fetch #{repo[:git]} #{branch} && #{co_cmd}"
        print_verbose "\n\n#{repo[:name]} -- #{update_cmd}"
        git_pull_cmd = system_cmd(update_cmd)
      end
    else
      install_git module_path, repo[:name], repo[:git], repo[:ref]
    end
  end
end

Private Instance Methods

build_puppetfile_hash(name, perform_installation=false) click to toggle source

builds out a certain type of repo

# File lib/librarian/puppet/simple/cli.rb, line 261
def build_puppetfile_hash(name, perform_installation=false)
  repo_hash = {}
  # set environment variable to determine what version of modules to install
  # this assumes that the environment variable repos_to_use has been coded in
  # your Puppetfile to allow installation of different versions of modules
  ENV['repos_to_use'] = name
  # parse Puppetfile and install modules in our tmp directory.
  eval(File.read(File.expand_path(options[:puppetfile])))
  # install modules if desired
  install! if perform_installation

  # iterate through all git modules
  each_module_of_type(:git) do |git_repo|
    abort("Module git_repo[:name] was defined multiple times in same Puppetfile") if repo_hash[git_repo[:name]]
    repo_hash[git_repo[:name]] = git_repo
  end
  # clear out the modules once finished
  clear_modules
  repo_hash
end
print_puppet_file(stream) click to toggle source