Ejemplo n.º 1
0
func main() {
	v := struct {
		Path  string `json:"path"`
		Depth int    `json:"depth"`
	}{}

	c := new(plugin.Clone)
	plugin.Param("clone", c)
	plugin.Param("vargs", &v)
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	if v.Depth == 0 {
		v.Depth = 50
	}
	if len(v.Path) != 0 {
		c.Dir = filepath.Join("/drone/src", v.Path)
	}

	err := os.MkdirAll(c.Dir, 0777)
	if err != nil {
		fmt.Printf("Error creating directory %s. %s\n", c.Dir, err)
		os.Exit(2)
	}

	// generate the .netrc file
	if err := writeNetrc(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	}

	// write the rsa private key if provided
	if err := writeKey(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(4)
	}

	var cmds []*exec.Cmd
	if isPR(c) {
		cmds = append(cmds, clone(c))
		cmds = append(cmds, fetch(c))
		cmds = append(cmds, checkoutHead(c))
	} else {
		cmds = append(cmds, cloneBranch(c))
		cmds = append(cmds, checkoutSha(c))
	}

	for _, cmd := range cmds {
		cmd.Dir = c.Dir
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		trace(cmd)
		err := cmd.Run()
		if err != nil {
			os.Exit(1)
		}
	}
}
Ejemplo n.º 2
0
func main() {
	var repo = plugin.Repo{}
	var build = plugin.Build{}
	var vargs = struct {
		Urls []string `json:"urls"`
	}{}

	plugin.Param("repo", &repo)
	plugin.Param("build", &build)
	plugin.Param("vargs", &vargs)
	plugin.Parse()

	// data structure
	data := struct {
		Repo  plugin.Repo  `json:"repo"`
		Build plugin.Build `json:"build"`
	}{repo, build}

	// json payload that will be posted
	payload, err := json.Marshal(&data)
	if err != nil {
		os.Exit(1)
	}

	// post payload to each url
	for _, url := range vargs.Urls {
		resp, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
		if err != nil {
			os.Exit(1)
		}
		resp.Body.Close()
	}
}
Ejemplo n.º 3
0
func main() {
	c := new(plugin.Clone)
	v := new(Rsync)
	plugin.Param("clone", c)
	plugin.Param("vargs", v)
	plugin.Parse()

	// write the rsa private key if provided
	if err := writeKey(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	// create the rsync command
	rs := buildRsync(v)
	rs.Dir = c.Dir
	rs.Stderr = os.Stderr
	rs.Stdout = os.Stdout
	trace(rs)
	err := rs.Run()
	if err != nil {
		os.Exit(1)
		return
	}

	// and execute

	// create remote command script

	// and execute
}
Ejemplo n.º 4
0
func main() {
	var repo = plugin.Repo{}
	var build = plugin.Build{}
	var vargs = struct {
		Urls []string `json:"urls"`
	}{}

	plugin.Param("repo", &repo)
	plugin.Param("build", &build)
	plugin.Param("vargs", &vargs)
	plugin.Parse()

	// post build and repo data to webhook urls
	data := struct {
		Repo  plugin.Repo  `json:"repo"`
		Build plugin.Build `json:"build"`
	}{repo, build}

	payload, _ := json.Marshal(&data)

	for _, url := range vargs.Urls {
		resp, _ := http.Post(url, "application/json", bytes.NewBuffer(payload))
		resp.Body.Close()
	}
}
Ejemplo n.º 5
0
func main() {
	var vargs = struct {
		ReplicationControllers []string `json:replicationcontrollers`
		Services               []string `json:services`
		ApiServer              string   `json:apiserver`
		Token                  string   `json:token`
		Namespace              string   `json:namespace`
		Debug                  string   `json:debug`
		Webhook                string   `json:webhook`
		Source                 string   `json:source`
		WebHookToken           string   `json:webhook_token`
	}{}

	workspace := plugin.Workspace{}
	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)
	plugin.Parse()

	// Iterate over rcs and svcs
	for _, rc := range vargs.ReplicationControllers {
		artifact, err := readArtifactFromFile(workspace.Path, rc, vargs.ApiServer, vargs.Namespace)
		if err != nil {
			log.Panic(err)
			return
		}
		if b, _ := existsArtifact(artifact, vargs.Token); b {
			deleteArtifact(artifact, vargs.Token)
			time.Sleep(time.Second * 5)
		}
		createArtifact(artifact, vargs.Token)
	}
	for _, rc := range vargs.Services {
		artifact, err := readArtifactFromFile(workspace.Path, rc, vargs.ApiServer, vargs.Namespace)
		if err != nil {
			log.Panic(err)
			return
		}
		createArtifact(artifact, vargs.Token)
	}
	wh := &WebHook{
		Timestamp: makeTimestamp(),
		Images:    deployments,
		Namespace: vargs.Namespace,
		Source:    vargs.Source,
		Target:    vargs.ApiServer,
		Url:       vargs.Webhook,
		Token:     vargs.WebHookToken,
	}
	sendWebhook(wh)
}
Ejemplo n.º 6
0
func main() {
	fmt.Printf("Drone DockerHub Plugin built at %s\n", buildDate)

	vargs := DockerHub{}
	build := plugin.Build{}

	plugin.Param("vargs", &vargs)
	plugin.Param("build", &build)

	if err := plugin.Parse(); err != nil {
		println(err.Error())
		os.Exit(1)
	}

	endpoint := fmt.Sprintf("https://registry.hub.docker.com/u/%s/trigger/%s/", vargs.Repo, vargs.Token)
	values := DockerHubValues{SourceType: "Branch", SourceName: build.Branch}
	values_json, err := json.Marshal(values)

	req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(values_json))

	if err != nil {
		fmt.Println(re.ReplaceAllString(err.Error(), "${1}HIDDEN"))

		os.Exit(1)
	}

	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}

	var resp *http.Response
	err = try.Do(func(attempt int) (bool, error) {
		var err error

		resp, err = client.Do(req)
		return attempt < 5, err
	})

	if err != nil {
		fmt.Println(re.ReplaceAllString(err.Error(), "${1}HIDDEN"))

		os.Exit(1)
	}
	resp.Body.Close()
}
Ejemplo n.º 7
0
func main() {
	repo := plugin.Repo{}
	build := plugin.Build{}
	system := plugin.System{}
	vargs := Slack{}

	plugin.Param("build", &build)
	plugin.Param("repo", &repo)
	plugin.Param("system", &system)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	client.SetUrl(vargs.Webhook)

	// generate the Slack message
	msg := Message{}
	msg.Username = vargs.Username
	msg.Channel = vargs.Recipient

	if len(vargs.Recipient) != 0 {
		msg.Channel = Prepend("@", vargs.Recipient)
	} else {
		msg.Channel = Prepend("#", vargs.Channel)
	}

	attach := msg.NewAttachment()
	attach.Text = GetMessage(repo, build, system, vargs)
	attach.Fallback = GetFallback(&repo, &build)
	attach.Color = GetColor(&build)
	attach.MrkdwnIn = []string{"text", "fallback"}

	// sends the message
	if err := client.SendMessage(&msg); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
Ejemplo n.º 8
0
func main() {
	repo := plugin.Repo{}
	build := plugin.Build{}
	vargs := Slack{}

	plugin.Param("build", &build)
	plugin.Param("repo", &repo)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	// create the Slack client
	client := Client{}
	client.Url = vargs.Webhook

	// generate the Slack message
	msg := Message{}
	msg.Channel = vargs.Channel
	msg.Username = vargs.Username

	attach := msg.NewAttachment()
	attach.Text = GetMessage(&repo, &build)
	attach.Fallback = GetFallback(&repo, &build)
	attach.Color = GetColor(&build)
	attach.MrkdwnIn = []string{"text", "fallback"}

	// sends the message
	if err := client.SendMessage(&msg); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
Ejemplo n.º 9
0
func main() {
	workspace := plugin.Workspace{}
	vargs := Fabric{}

	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	if err := writeKey(&workspace); err != nil {
		log.Println("Unable to write private key")
		log.Println(err)
		os.Exit(1)
	}

	if err := os.Chdir(workspace.Path); err != nil {
		log.Println("Unable to dc into workspace.Path")
		os.Exit(1)
	}

	for _, c := range vargs.Commands {
		fabArgs := strings.Split(c, " ")
		c := exec.Command("fab", fabArgs...)
		c.Stdout = os.Stdout
		c.Stderr = os.Stderr
		err := c.Run()
		if err != nil {
			log.Println(err)
			os.Exit(101)
		}
	}
}
Ejemplo n.º 10
0
func main() {
	vargs := Marathon{}
	plugin.Parse()

}
Ejemplo n.º 11
0
func main() {
	clone := plugin.Clone{}
	vargs := Docker{}

	plugin.Param("clone", &clone)
	plugin.Param("vargs", &vargs)
	if err := plugin.Parse(); err != nil {
		println(err.Error())
		os.Exit(1)
	}

	// Set the storage driver
	if len(vargs.Storage) == 0 {
		vargs.Storage = "aufs"
	}

	stop := func() {
		cmd := exec.Command("start-stop-daemon", "--stop", "--pidfile", "/var/run/docker.pid")
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		trace(cmd)
		cmd.Run()
	}
	defer stop()

	// Starts the Docker daemon
	go func() {
		cmd := exec.Command("/bin/bash", "/bin/wrapdocker")
		cmd.Stdout = ioutil.Discard
		cmd.Stderr = ioutil.Discard
		cmd.Run()

		cmd = exec.Command("docker", "-d", "-s", vargs.Storage)
		cmd.Stdout = ioutil.Discard
		cmd.Stderr = ioutil.Discard
		trace(cmd)
		cmd.Run()
	}()

	// Sleep for a few seconds
	time.Sleep(5 * time.Second)

	// Set the Registry value
	if len(vargs.Registry) == 0 {
		vargs.Registry = "https://index.docker.io/v1/"
	}
	// Set the Dockerfile path
	if len(vargs.File) == 0 {
		vargs.File = "."
	}
	// Set the Tag value
	switch vargs.Tag {
	case "$DRONE_BRANCH":
		vargs.Tag = clone.Branch
	case "$DRONE_COMMIT":
		vargs.Tag = clone.Sha
	case "":
		vargs.Tag = "latest"
	}
	vargs.Repo = fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag)

	// Login to Docker
	cmd := exec.Command("docker", "login", "-u", vargs.Username, "-p", vargs.Password, "-e", vargs.Email, vargs.Registry)
	cmd.Dir = clone.Dir
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if err != nil {
		stop()
		os.Exit(1)
	}

	// Docker environment info
	cmd = exec.Command("docker", "version")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	trace(cmd)
	cmd.Run()
	cmd = exec.Command("docker", "info")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	trace(cmd)
	cmd.Run()

	// Build the container
	cmd = exec.Command("docker", "build", "--pull=true", "--rm=true", "-t", vargs.Repo, vargs.File)
	cmd.Dir = clone.Dir
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	trace(cmd)
	err = cmd.Run()
	if err != nil {
		stop()
		os.Exit(1)
	}

	// Push the container
	cmd = exec.Command("docker", "push", vargs.Repo)
	cmd.Dir = clone.Dir
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	trace(cmd)
	err = cmd.Run()
	if err != nil {
		stop()
		os.Exit(1)
	}
}
Ejemplo n.º 12
0
func main() {
	repo := plugin.Repo{}
	build := plugin.Build{}
	workspace := plugin.Workspace{}
	vargs := Npm{}

	plugin.Param("build", &build)
	plugin.Param("repo", &repo)
	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	// check for required parameters
	if len(vargs.Username) == 0 {
		fmt.Println("Username not provided")
		os.Exit(1)
	}

	if len(vargs.Password) == 0 {
		fmt.Println("Password not provided")
		os.Exit(1)
	}

	if len(vargs.Email) == 0 {
		fmt.Println("Email not provided")
		os.Exit(1)
	}

	// set defaults
	var globalRegistry bool

	if len(vargs.Registry) == 0 {
		vargs.Registry = "https://registry.npmjs.org"
		globalRegistry = true
	} else {
		globalRegistry = false
	}

	// get the package info
	var packagePath string

	if len(vargs.Folder) == 0 {
		packagePath = path.Join(workspace.Path)
	} else {
		packagePath = path.Join(workspace.Path, vargs.Folder)
	}

	packageFile := path.Join(packagePath, "package.json")

	npmPackage, err := readPackageFile(packageFile)

	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	// see if the package should be published
	publish, err := shouldPublishPackage(vargs, npmPackage)

	if publish {
		fmt.Println("Attempting to publish package")

		// write the npmrc file
		npmrcPath := path.Join(packagePath, ".npmrc")
		err := writeNpmrcFile(vargs, npmrcPath)

		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}

		var cmds []*exec.Cmd

		// write registry command
		if !globalRegistry {
			cmds = append(cmds, registryCommand(vargs))
		}

		// write auth command
		if vargs.AlwaysAuth {
			cmds = append(cmds, alwaysAuthCommand())
		}

		// write the publish command
		cmds = append(cmds, publishCommand())

		// run the commands
		for _, cmd := range cmds {
			cmd.Dir = packagePath
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			trace(cmd)
			err := cmd.Run()
			if err != nil {
				os.Exit(1)
			}
		}
	} else {
		fmt.Println("Package already published")
	}
}
Ejemplo n.º 13
0
func main() {
	var err error
	var path, wd string

	repo := plugin.Repo{}
	build := plugin.Build{}
	workspace := plugin.Workspace{}
	cmds := ComposerCommands{}

	plugin.Param("repo", &repo)
	plugin.Param("build", &build)
	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &cmds)
	err = plugin.Parse()

	if err != nil {
		fmt.Printf("Error while attempting to parse input: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("Got commands list:\n")
	for _, c := range cmds.Commands {
		fmt.Printf("- %s\n", c)
	}

	// let's try to find the Rancher Compose executable
	path, err = exec.LookPath("rancher-compose")
	if err != nil {
		fmt.Printf("Could not find rancher-compose executable on path: %s\n", err)
		os.Exit(1)
	}

	// chdir to the repo path
	if workspace.Path != "" {
		fmt.Printf("Using working directory: %s\n", workspace.Path)
		os.Chdir(workspace.Path)
	} else {
		wd, err = os.Getwd()
		if err != nil {
			fmt.Printf("Using working directory: %s\n", wd)
		} else {
			fmt.Printf("WARNING: Error while attempting to get working dir: %s\n", err)
		}
	}

	// execute each of our Rancher Compose commands in sequence
	for _, c := range cmds.Commands {
		args := CleanSlice(strings.Split(c, " "))
		fmt.Printf("Executing Rancher Compose: %s %s\n", path, strings.Join(args[:], " "))

		cmd := exec.Command(path, args...)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		err := cmd.Run()

		// exit if any command fails
		if err != nil {
			fmt.Printf("Error while executing command: %s\n", err)
			os.Exit(1)
		}
	}

	fmt.Printf("Successfully executed commands\n")

}
Ejemplo n.º 14
0
func main() {
	fmt.Printf("Drone Slack Blame Plugin built at %s\n", buildDate)

	repo := plugin.Repo{}
	build := plugin.Build{}
	system := plugin.System{}
	vargs := Slack{}

	plugin.Param("build", &build)
	plugin.Param("system", &system)
	plugin.Param("repo", &repo)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	// setup the message
	buildLink := fmt.Sprintf("%s/%s/%d", system.Link, repo.FullName, build.Number)
	var messageOptions MessageOptions
	var color string
	var messageText string
	var channelText string

	// Determine if the build was a success
	if build.Status == "success" {
		messageOptions = vargs.Success
		color = "good"
		messageText = fmt.Sprintf("Build succeeded at %s", buildLink)
		channelText = "Thanks"
	} else {
		messageOptions = vargs.Failure
		color = "danger"
		messageText = fmt.Sprintf("Build failed at %s", buildLink)
		channelText = "Blame"
	}

	// set default values
	if len(messageOptions.Username) == 0 {
		messageOptions.Username = "******"
	}

	if len(messageOptions.Icon) == 0 {
		messageOptions.Icon = ":drone:"
	}

	if len(messageOptions.ImageAttachments) == 0 {
		messageOptions.ImageAttachments = []string{""}
	}

	// setup the message
	messageParams := slack.PostMessageParameters{
		Username:  messageOptions.Username,
		IconEmoji: messageOptions.Icon,
	}

	imageCount := len(messageOptions.ImageAttachments)
	rand.Seed(time.Now().UTC().UnixNano())

	attachment := slack.Attachment{
		Color:    color,
		Text:     messageText,
		ImageURL: messageOptions.ImageAttachments[rand.Intn(imageCount)],
	}

	messageParams.Attachments = []slack.Attachment{attachment}

	// get the commit author
	commitAuthor := build.Email

	// create the slack api
	api := slack.New(vargs.Token)

	// get the users
	//
	// Slack doesn't let you search by email so just need to get
	// everything and find the user in question
	var blameUser *slack.User

	users, _ := api.GetUsers()

	for _, user := range users {
		if user.Profile.Email == commitAuthor {
			fmt.Printf("%s\n", user.Name)
			fmt.Printf("%s\n", user.Profile.Email)
			blameUser = &user
			break
		}
	}

	// notify the user if possible
	var userAt string

	if blameUser != nil {
		userAt = fmt.Sprintf("@%s", blameUser.Name)

		// send the message to the user's channel
		//
		// this will appear through slackbot
		_, _, err := api.PostMessage(userAt, messageOptions.Message, messageParams)

		if err == nil {
			fmt.Printf("User %s notified\n", userAt)
		} else {
			fmt.Printf("Could not notify user %s!\n", userAt)
		}
	} else {
		userAt = build.Author
		fmt.Print("User could not be found")
	}

	// notify the channel if requested
	if len(vargs.Channel) != 0 {
		if !strings.HasPrefix(vargs.Channel, "#") {
			vargs.Channel = "#" + vargs.Channel
		}

		_, _, err := api.PostMessage(vargs.Channel, fmt.Sprintf("%s %s %s", messageOptions.Message, channelText, userAt), messageParams)

		if err == nil {
			fmt.Printf("Channel notified\n")
		} else {
			fmt.Printf("Could not notify channel!\n")
		}
	}
}