github.com/coreos/mantle@v0.13.0/sdk/repo.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sdk
    16  
    17  import (
    18  	"bytes"
    19  	"io/ioutil"
    20  	"log"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"github.com/coreos/mantle/system"
    26  	"github.com/coreos/mantle/system/exec"
    27  )
    28  
    29  const (
    30  	defaultGroup = "developer"
    31  
    32  	// In the SDK chroot the repo is always at this location
    33  	chrootRepoRoot = "/mnt/host/source"
    34  
    35  	// Assorted paths under the repo root
    36  	defaultCacheDir = ".cache"
    37  	defaultBuildDir = "src/build"
    38  	defaultBoardCfg = "src/scripts/.default_board"
    39  )
    40  
    41  func isDir(dir string) bool {
    42  	stat, err := os.Stat(dir)
    43  	return err == nil && stat.IsDir()
    44  }
    45  
    46  func envDir(env string) string {
    47  	dir := os.Getenv(env)
    48  	if dir == "" {
    49  		return ""
    50  	}
    51  	if !filepath.IsAbs(dir) {
    52  		log.Fatalf("%s is not an absolute path: %q", env, dir)
    53  	}
    54  	return dir
    55  }
    56  
    57  func RepoRoot() string {
    58  	if dir := envDir("REPO_ROOT"); dir != "" {
    59  		return dir
    60  	}
    61  
    62  	if isDir(chrootRepoRoot) {
    63  		return chrootRepoRoot
    64  	}
    65  
    66  	wd, err := os.Getwd()
    67  	if err != nil {
    68  		log.Fatalf("Invalid working directory: %v", err)
    69  	}
    70  
    71  	for dir := wd; ; dir = filepath.Dir(dir) {
    72  		if isDir(filepath.Join(dir, ".repo")) {
    73  			return dir
    74  		} else if filepath.IsAbs(dir) {
    75  			break
    76  		}
    77  	}
    78  
    79  	return wd
    80  }
    81  
    82  func RepoCache() string {
    83  	return filepath.Join(RepoRoot(), defaultCacheDir)
    84  }
    85  
    86  func DefaultBoard() string {
    87  	defaultBoard := system.PortageArch() + "-usr"
    88  	cfg := filepath.Join(RepoRoot(), defaultBoardCfg)
    89  	board, err := ioutil.ReadFile(cfg)
    90  	if err != nil {
    91  		return defaultBoard
    92  	}
    93  
    94  	board = bytes.TrimSpace(board)
    95  	if len(board) == 0 {
    96  		return defaultBoard
    97  	}
    98  
    99  	return string(board)
   100  }
   101  
   102  func BoardRoot(board string) string {
   103  	if board == "" {
   104  		board = DefaultBoard()
   105  	}
   106  	return filepath.Join("/build", board)
   107  }
   108  
   109  func BuildRoot() string {
   110  	if dir := envDir("BUILD_ROOT"); dir != "" {
   111  		return dir
   112  	}
   113  	return filepath.Join(RepoRoot(), defaultBuildDir)
   114  }
   115  
   116  // version may be "latest" or a full version like "752.1.0+2015-07-27-1656"
   117  func BuildImageDir(board, version string) string {
   118  	if board == "" {
   119  		board = DefaultBoard()
   120  	}
   121  	if version == "" {
   122  		version = "latest"
   123  	} else if version != "latest" {
   124  		// Assume all builds are "attempt" #1
   125  		version += "-a1"
   126  	}
   127  	dir := defaultGroup + "-" + version
   128  	return filepath.Join(BuildRoot(), "images", board, dir)
   129  }
   130  
   131  func RepoInit(chroot, url, branch, name string, useHostDNS bool) error {
   132  	return enterChroot(enter{
   133  		Chroot:     chroot,
   134  		CmdDir:     chrootRepoRoot,
   135  		UseHostDNS: useHostDNS,
   136  		Cmd: []string{"--",
   137  			"repo", "init",
   138  			"--manifest-url", url,
   139  			"--manifest-branch", branch,
   140  			"--manifest-name", name,
   141  		}})
   142  }
   143  
   144  func RepoVerifyTag(branch string) error {
   145  	manifestRepoDir := ".repo/manifests"
   146  	if strings.HasPrefix(branch, "refs/tags/") {
   147  		branch = strings.TrimPrefix(branch, "refs/tags/")
   148  	}
   149  
   150  	tag := exec.Command("git", "-C", manifestRepoDir, "tag", "-v", branch)
   151  	tag.Stderr = os.Stderr
   152  	return tag.Run()
   153  }
   154  
   155  func RepoSync(chroot string, force, useHostDNS bool) error {
   156  	args := []string{"--", "repo", "sync", "--no-clone-bundle"}
   157  	if force {
   158  		args = append(args, "--force-sync")
   159  	}
   160  	return enterChroot(enter{
   161  		Chroot:     chroot,
   162  		CmdDir:     chrootRepoRoot,
   163  		Cmd:        args,
   164  		UseHostDNS: useHostDNS,
   165  	})
   166  }