func extractGitSource(git git.Git, gitSource *api.GitBuildSource, revision *api.SourceRevision, dir string, timeout time.Duration) (bool, error) { if gitSource == nil { return false, nil } // Set the HTTP and HTTPS proxies to be used by git clone. originalProxies := setHTTPProxy(gitSource.HTTPProxy, gitSource.HTTPSProxy) defer resetHTTPProxy(originalProxies) // Check source URI, trying to connect to the server only if not using a proxy. if err := checkSourceURI(git, gitSource.URI, timeout); err != nil { return true, err } glog.V(2).Infof("Cloning source from %s", gitSource.URI) if err := git.Clone(gitSource.URI, dir, s2iapi.CloneConfig{Recursive: true, Quiet: true}); err != nil { return true, err } // if we specify a commit, ref, or branch to checkout, do so if len(gitSource.Ref) != 0 || (revision != nil && revision.Git != nil && len(revision.Git.Commit) != 0) { commit := gitSource.Ref if revision != nil && revision.Git != nil && revision.Git.Commit != "" { commit = revision.Git.Commit } if err := git.Checkout(dir, commit); err != nil { return true, err } } return true, nil }
// fetchSource retrieves the inputs defined by the build source into the // provided directory, or returns an error if retrieval is not possible. func fetchSource(dir string, build *api.Build, urlTimeout time.Duration, in io.Reader, git git.Git) (*s2iapi.SourceInfo, error) { hasGitSource := false // expect to receive input from STDIN if err := extractInputBinary(in, build.Spec.Source.Binary, dir); err != nil { return nil, err } // may retrieve source from Git hasGitSource, err := extractGitSource(git, build.Spec.Source.Git, build.Spec.Revision, dir, urlTimeout) if err != nil { return nil, err } var sourceInfo *s2iapi.SourceInfo if hasGitSource { sourceInfo = git.GetInfo(dir) } // a Dockerfile has been specified, create or overwrite into the destination if dockerfileSource := build.Spec.Source.Dockerfile; dockerfileSource != nil { baseDir := dir // if a context dir has been defined and we cloned source, overwrite the destination if hasGitSource && len(build.Spec.Source.ContextDir) != 0 { baseDir = filepath.Join(baseDir, build.Spec.Source.ContextDir) } return sourceInfo, ioutil.WriteFile(filepath.Join(baseDir, "Dockerfile"), []byte(*dockerfileSource), 0660) } return sourceInfo, nil }
// checkSourceURI performs a check on the URI associated with the build // to make sure that it is valid. It also optionally tests the connection // to the source uri. func checkSourceURI(git git.Git, rawurl string, testConnection bool, timeout time.Duration) error { if !git.ValidCloneSpec(rawurl) { return fmt.Errorf("Invalid git source url: %s", rawurl) } if strings.HasPrefix(rawurl, "git@") || strings.HasPrefix(rawurl, "git://") { return nil } srcURL, err := url.Parse(rawurl) if err != nil { return err } if !testConnection { return nil } host := srcURL.Host if strings.Index(host, ":") == -1 { switch srcURL.Scheme { case "http": host += ":80" case "https": host += ":443" } } dialer := net.Dialer{Timeout: timeout} conn, err := dialer.Dial("tcp", host) if err != nil { return err } return conn.Close() }
// checkSourceURI performs a check on the URI associated with the build // to make sure that it is valid. It also optionally tests the connection // to the source uri. func checkSourceURI(git git.Git, rawurl string, timeout time.Duration) error { if !git.ValidCloneSpec(rawurl) { return fmt.Errorf("Invalid git source url: %s", rawurl) } return checkRemoteGit(rawurl, timeout) }