<commit_before>package digitalocean

import (
	"errors"
	"fmt"
	"koding/kites/kloud/klientprovisioner"
	"strconv"
	"sync"
	"time"

	do "github.com/koding/kloud/api/digitalocean"
	"github.com/koding/kloud/machinestate"
	"github.com/koding/kloud/packer"
	"github.com/koding/kloud/protocol"
	"github.com/koding/kloud/utils"

	"github.com/koding/logging"
	"github.com/koding/redis"
	"github.com/mitchellh/packer/builder/digitalocean"
)

type Client struct {
	*do.DigitalOcean
	Log  logging.Logger
	Push func(string, int, machinestate.State)

	Caching     bool
	CachePrefix string

	Redis       *redis.RedisSession
	RedisPrefix string

	Deploy *protocol.ProviderDeploy

	sync.Once
}

// Build is building an image and creates a droplet based on that image. If the
// given snapshot/image exist it directly skips to creating the droplet. It
// acceps two string arguments, first one is the snapshotname, second one is
// the dropletName.
func (c *Client) Build(snapshotName, dropletName string) (*protocol.Artifact, error) {
	// needed because this is passed as `data` to packer.Provider
	c.Builder.SnapshotName = snapshotName

	var image digitalocean.Image
	var err error

	// check if snapshot image does exist
	c.Push(fmt.Sprintf("Fetching image %s", snapshotName), 10, machinestate.Building)
	image, err = c.Image(snapshotName)
	if err != nil {
		return nil, err
	}

	c.Push(fmt.Sprintf("Getting the droplet '%s' image '%d'",
		dropletName, image.Id), 15, machinestate.Building)
	dropletId, err := c.NewDroplet(dropletName, image.Id)
	if err != nil {
		return nil, err
	}

	// our droplet has now an IP adress, get it
	c.Push(fmt.Sprintf("Getting info about droplet"), 60, machinestate.Building)
	droplet, err := c.ShowDroplet(dropletId)
	if err != nil {
		return nil, err
	}

	var privateKey string
	if c.Deploy != nil {
		privateKey = c.Deploy.PrivateKey
	}

	return &protocol.Artifact{
		IpAddress:     droplet.IpAddress,
		InstanceName:  dropletName,
		InstanceId:    strconv.Itoa(droplet.Id),
		SSHPrivateKey: privateKey,
	}, nil
}

// CreateImage creates an image using Packer. It uses digitalocean.Builder
// data. It returns the image info.
func (c *Client) CreateImage() (digitalocean.Image, error) {
	data, err := utils.TemplateData(c.Builder, klientprovisioner.RawData)
	if err != nil {
		return digitalocean.Image{}, err
	}

	provider := &packer.Provider{
		BuildName: "digitalocean",
		Data:      data,
	}

	// this is basically a "packer build template.json"
	if err := provider.Build(); err != nil {
		return digitalocean.Image{}, err
	}

	// return the image result
	return c.Image(c.Builder.SnapshotName)
}

func (c *Client) DropletId() (uint, error) {
	if c.Builder.DropletId == "" {
		return 0, errors.New("dropletId is not available")
	}

	dropletId := utils.ToUint(c.Builder.DropletId)
	if dropletId == 0 {
		return 0, fmt.Errorf("malformed data received %v. droplet Id must be an int.",
			c.Builder.DropletId)
	}

	return dropletId, nil<commit_msg>kloud/digitalocean: remove klientprovisione from digitalocean<commit_after>package digitalocean

import (
	"errors"
	"fmt"
	"strconv"
	"sync"
	"time"

	do "github.com/koding/kloud/api/digitalocean"
	"github.com/koding/kloud/machinestate"
	"github.com/koding/kloud/packer"
	"github.com/koding/kloud/protocol"
	"github.com/koding/kloud/utils"

	"github.com/koding/logging"
	"github.com/koding/redis"
	"github.com/mitchellh/packer/builder/digitalocean"
)

type Client struct {
	*do.DigitalOcean
	Log  logging.Logger
	Push func(string, int, machinestate.State)

	Caching     bool
	CachePrefix string

	Redis       *redis.RedisSession
	RedisPrefix string

	Deploy *protocol.ProviderDeploy

	sync.Once
}

// Build is building an image and creates a droplet based on that image. If the
// given snapshot/image exist it directly skips to creating the droplet. It
// acceps two string arguments, first one is the snapshotname, second one is
// the dropletName.
func (c *Client) Build(snapshotName, dropletName string) (*protocol.Artifact, error) {
	// needed because this is passed as `data` to packer.Provider
	c.Builder.SnapshotName = snapshotName

	var image digitalocean.Image
	var err error

	// check if snapshot image does exist
	c.Push(fmt.Sprintf("Fetching image %s", snapshotName), 10, machinestate.Building)
	image, err = c.Image(snapshotName)
	if err != nil {
		return nil, err
	}

	c.Push(fmt.Sprintf("Getting the droplet '%s' image '%d'",
		dropletName, image.Id), 15, machinestate.Building)
	dropletId, err := c.NewDroplet(dropletName, image.Id)
	if err != nil {
		return nil, err
	}

	// our droplet has now an IP adress, get it
	c.Push(fmt.Sprintf("Getting info about droplet"), 60, machinestate.Building)
	droplet, err := c.ShowDroplet(dropletId)
	if err != nil {
		return nil, err
	}

	var privateKey string
	if c.Deploy != nil {
		privateKey = c.Deploy.PrivateKey
	}

	return &protocol.Artifact{
		IpAddress:     droplet.IpAddress,
		InstanceName:  dropletName,
		InstanceId:    strconv.Itoa(droplet.Id),
		SSHPrivateKey: privateKey,
	}, nil
}

// CreateImage creates an image using Packer. It uses digitalocean.Builder
// data. It returns the image info.
func (c *Client) CreateImage(provisioner interface{}) (digitalocean.Image, error) {
	data, err := utils.TemplateData(c.Builder, provisioner)
	if err != nil {
		return digitalocean.Image{}, err
	}

	provider := &packer.Provider{
		BuildName: "digitalocean",
		Data:      data,
	}

	// this is basically a "packer build template.json"
	if err := provider.Build(); err != nil {
		return digitalocean.Image{}, err
	}

	// return the image result
	return c.Image(c.Builder.SnapshotName)
}

func (c *Client) DropletId() (uint, error) {
	if c.Builder.DropletId == "" {
		return 0, errors.New("dropletId is not available")
	}

	dropletId := utils.ToUint(c.Builder.DropletId)
	if dropletId == 0 {
		return 0, fmt.Errorf("malformed data received %v. droplet Id must be an int.",
			c.Builder.DropletId)
	}

	return dropletId, nil
{'fraction_non_alphanumeric': 0.11687657430730479, 'fraction_numerical': 0.003022670025188917, 'mean_word_length': 3.984100418410042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1086807', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2059, 'n_words': 565}