github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cloudinit/sshinit/apt_mirror.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package sshinit
     5  
     6  const (
     7  	// aptSourcesList is the location of the APT sources list
     8  	// configuration file.
     9  	aptSourcesList = "/etc/apt/sources.list"
    10  
    11  	// aptListsDirectory is the location of the APT lists directory.
    12  	aptListsDirectory = "/var/lib/apt/lists"
    13  
    14  	// extractAptSource is a shell command that will extract the
    15  	// currently configured APT source location. We assume that
    16  	// the first source for "main" in the file is the one that
    17  	// should be replaced throughout the file.
    18  	extractAptSource = `awk "/^deb .* $(lsb_release -sc) .*main.*\$/{print \$2;exit}" ` + aptSourcesList
    19  
    20  	// aptSourceListPrefix is a shell program that translates an
    21  	// APT source (piped from stdin) to a file prefix. The algorithm
    22  	// involves stripping up to one trailing slash, stripping the
    23  	// URL scheme prefix, and finally translating slashes to
    24  	// underscores.
    25  	aptSourceListPrefix = `sed 's,.*://,,' | sed 's,/$,,' | tr / _`
    26  )
    27  
    28  // renameAptListFilesCommands takes a new and old mirror string,
    29  // and returns a sequence of commands that will rename the files
    30  // in aptListsDirectory.
    31  func renameAptListFilesCommands(newMirror, oldMirror string) []string {
    32  	oldPrefix := "old_prefix=" + aptListsDirectory + "/$(echo " + oldMirror + " | " + aptSourceListPrefix + ")"
    33  	newPrefix := "new_prefix=" + aptListsDirectory + "/$(echo " + newMirror + " | " + aptSourceListPrefix + ")"
    34  	renameFiles := `
    35  for old in ${old_prefix}_*; do
    36      new=$(echo $old | sed s,^$old_prefix,$new_prefix,)
    37      mv $old $new
    38  done`
    39  
    40  	return []string{
    41  		oldPrefix,
    42  		newPrefix,
    43  		// Don't do anything unless the mirror/source has changed.
    44  		`[ "$old_prefix" != "$new_prefix" ] &&` + renameFiles,
    45  	}
    46  }