github.com/robryk/drone@v0.2.1-0.20140602202253-40fe4305815d/pkg/build/util.go (about)

     1  package build
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/sha1"
     6  	"fmt"
     7  	"io"
     8  	"strings"
     9  )
    10  
    11  // createUID is a helper function that will
    12  // create a random, unique identifier.
    13  func createUID() string {
    14  	c := sha1.New()
    15  	r := createRandom()
    16  	io.WriteString(c, string(r))
    17  	s := fmt.Sprintf("%x", c.Sum(nil))
    18  	return "drone-" + s[0:10]
    19  }
    20  
    21  // createRandom creates a random block of bytes
    22  // that we can use to generate unique identifiers.
    23  func createRandom() []byte {
    24  	k := make([]byte, sha1.BlockSize)
    25  	if _, err := io.ReadFull(rand.Reader, k); err != nil {
    26  		return nil
    27  	}
    28  	return k
    29  }
    30  
    31  // list of service aliases and their full, canonical names
    32  var defaultServices = map[string]string{
    33  	"cassandra":     "relateiq/cassandra:latest",
    34  	"couchdb":       "bradrydzewski/couchdb:1.5",
    35  	"elasticsearch": "bradrydzewski/elasticsearch:0.90",
    36  	"memcached":     "bradrydzewski/memcached",
    37  	"mongodb":       "bradrydzewski/mongodb:2.4",
    38  	"mysql":         "bradrydzewski/mysql:5.5",
    39  	"neo4j":         "bradrydzewski/neo4j:1.9",
    40  	"postgres":      "bradrydzewski/postgres:9.1",
    41  	"redis":         "bradrydzewski/redis:2.8",
    42  	"rabbitmq":      "bradrydzewski/rabbitmq:3.2",
    43  	"riak":          "guillermo/riak:latest",
    44  	"zookeeper":     "jplock/zookeeper:3.4.5",
    45  }
    46  
    47  // parseImageName parses a Docker image name, in the format owner/name:tag,
    48  // and returns each segment.
    49  //
    50  // If the owner is blank, it is assumed to be an official drone image,
    51  // and will be prefixed with the appropriate owner name.
    52  //
    53  // If the tag is empty, it is assumed to be the latest version.
    54  func parseImageName(image string) (owner, name, tag string) {
    55  	owner = "bradrydzewski" // this will eventually change to drone
    56  	name = image
    57  	tag = "latest"
    58  
    59  	// first we check to see if the image name is an alias
    60  	// for a known service.
    61  	//
    62  	// TODO I'm not a huge fan of this code here. Maybe it
    63  	//      should get handled when the yaml is parsed, and
    64  	//      convert the image and service names in the yaml
    65  	//      to fully qualified names?
    66  	if cname, ok := defaultServices[image]; ok {
    67  		name = cname
    68  	}
    69  
    70  	parts := strings.Split(name, "/")
    71  	if len(parts) == 2 {
    72  		owner = parts[0]
    73  		name = parts[1]
    74  	}
    75  
    76  	parts = strings.Split(name, ":")
    77  	if len(parts) == 2 {
    78  		name = parts[0]
    79  		tag = parts[1]
    80  	}
    81  
    82  	return
    83  }