Module: Kitchen::Yansible::Tools::Dependencies

Included in:
Provisioner::Yansible
Defined in:
lib/kitchen-yansible/tools/dependencies.rb

Instance Method Summary collapse

Instance Method Details

#git_check_ref(path, ref) ⇒ Object



46
47
48
49
# File 'lib/kitchen-yansible/tools/dependencies.rb', line 46

def git_check_ref(path, ref)
  debug("Checking out #{ref} on #{path}.")
  execute_local_command("git show-ref #{ref}", opts: { :chdir => path })
end

#git_checkout(path, ref, force: false) ⇒ Object



41
42
43
44
# File 'lib/kitchen-yansible/tools/dependencies.rb', line 41

def git_checkout(path, ref, force: false)
  debug("Checking out #{ref} on #{path}.")
  execute_local_command("git checkout #{ref}#{force ? ' -f' : ''}", opts: { :chdir => path })
end

#git_clean(path) ⇒ Object



35
36
37
38
39
# File 'lib/kitchen-yansible/tools/dependencies.rb', line 35

def git_clean(path)
  debug("Cleaning up '#{path}' origin.")
  execute_local_command('git clean -fdx', opts: { :chdir => path })
  execute_local_command('git reset --hard', opts: { :chdir => path })
end

#git_clone(name, url, path) ⇒ Object



25
26
27
28
# File 'lib/kitchen-yansible/tools/dependencies.rb', line 25

def git_clone(name, url, path)
  info("Cloning '#{name}' Git repository.")
  execute_local_command("git clone --progress --verbose #{url} #{path}")
end

#git_get_origin(path) ⇒ Object



30
31
32
33
# File 'lib/kitchen-yansible/tools/dependencies.rb', line 30

def git_get_origin(path)
  debug("Get '#{path}' origin.")
  execute_local_command( 'git remote get-url origin', opts: { :chdir => path }, return_stdout: true )
end

#prepare_dependencies(dependencies) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kitchen-yansible/tools/dependencies.rb', line 51

def prepare_dependencies(dependencies)
  dependencies.each do |dependency|
    info("Processing '#{dependency[:name]}' dependency.")
    dependency_target_path = File.join(dependencies_tmp_dir, dependency[:name])
    if dependency.key?(:path)
      info('Processing as path type.')
      if File.exist?(dependency[:path])
        copy_dirs(dependency[:path], dependency_target_path)
      else
        warn("Dependency path '#{dependency[:path]}' doesn't exists. Omitting copy operation.")
      end
    end
    if dependency.key?(:repo)
      if dependency[:repo].downcase == 'git'
        info('Processing as Git repository.')
        if command_exists('git')
          if File.exist?(dependency_target_path)
            if execute_local_command('git status .', opts: { :chdir => dependency_target_path })
              if git_get_origin(dependency_target_path).chomp.eql?(dependency[:url])
                warn("Dependency downloaded already, resetting to HEAD.")
                git_clean(dependency_target_path)
              else
                warn("Removing directory #{dependency_target_path} due to repository origin difference.")
                FileUtils.remove_entry_secure(dependency_target_path)
                git_clone(dependency[:name], dependency[:url], dependency_target_path)
              end
            else
              warn("Dependency path '#{dependency_target_path}' is not a valid Git repository. Removing then.")
              FileUtils.remove_entry_secure(dependency_target_path)
              git_clone(dependency[:name], dependency[:url], dependency_target_path)
            end
          else
            git_clone(dependency[:name], dependency[:url], dependency_target_path)
          end

          raw_ref = dependency.key?(:ref) ? dependency[:ref] : 'master'
          begin
            git_check_ref(dependency_target_path, raw_ref)
          rescue
            message = unindent(<<-MSG)

              ===============================================================================
               Invalid Git reference - #{raw_ref}
               Please check '#{dependency[:name]}' dependency configuration.
              ===============================================================================
            MSG
            raise UserError, message
          end

          info("Resetting repository to '#{raw_ref}' reference.")
          git_checkout(dependency_target_path, raw_ref, force: true)
        else
          message = unindent(<<-MSG)

            ===============================================================================
             Couldn't find git binary.
             Please make sure execution host has Git binaries installed.
            ===============================================================================
          MSG
          raise UserError, message
        end
      else
        raise UserError, "Working with '#{dependency[:repo]}' repository is not implemented yet."
      end
    end
  end unless dependencies.nil?
end