<commit_before>// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package integration

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"math/rand"
	"os"
	"time"

	"google.golang.org/api/container/v1"
	"google.golang.org/api/option"

	"golang.org/x/net/context"
)

const gceClusterStatusRunning = "RUNNING"

var zone = os.Getenv("GCE_ZONE")
var projectID = os.Getenv("GCE_PROJECT_ID")
var serviceAccount = os.Getenv("GCE_SERVICE_ACCOUNT")

// GceClusterManager represents a Google Compute Engine cluster (Container Engine)
type GceClusterManager struct {
	client      *gceClient
	clusterName string
	cluster     *container.Cluster
}

func newClusterName() string {
	return fmt.Sprintf("integration-test-%d", randInt())
}

func randInt() int {
	rand.Seed(time.Now().UnixNano())
	return rand.Int()
}

func createTempFile(data []byte, prefix string) (string, error) {
	tmpfile, err := ioutil.TempFile("", prefix)
	if err != nil {
		return "", err
	}
	if _, err := tmpfile.Write(data); err != nil {
		return "", err
	}
	if err := tmpfile.Close(); err != nil {
		return "", err
	}
	return tmpfile.Name(), nil
}

func (g *GceClusterManager) Name() string {
	return "gce"
}

func (g *GceClusterManager) Provisioner() string {
	return "kubernetes"
}

func (g *GceClusterManager) IP(env *Environment) string {
	g.fetchClusterData(env)
	if g.cluster != nil {
		return g.cluster.Endpoint
	}
	return ""
}

func (g *GceClusterManager) Start(env *Environment) *Result {
	ctx := context.Background()
	serviceAccountFile, err := createTempFile([]byte(serviceAccount), "gce-sa-")
	if err != nil {
		return nil
	}
	client, err := newClient(ctx, projectID, option.WithServiceAccountFile(serviceAccountFile))
	if err != nil {
		return nil
	}
	g.client = client
	g.clusterName = env.Get("clustername")
	if g.clusterName == "" {
		g.clusterName = newClusterName()
		if env.VerboseLevel() > 0 {
			fmt.Fprintf(safeStdout, "[gce] starting cluster %s in zone %s\n", g.clusterName, zone)
		}
		g.client.createCluster(g.clusterName, zone, 1)
	} else {
		g.fetchClusterData(env)
		if g.cluster == nil || g.cluster.Status != gceClusterStatusRunning {
			return &Result{ExitCode: 1, Error: fmt.Errorf("[gce] cluster %s is not running", g.clusterName)}
		}
	}
	return &Result{ExitCode: 0}
}

func (g *GceClusterManager) Delete(env *Environment) *Result {
	if g.clusterName == "" {
		return nil
	}
	if env.VerboseLevel() > 0 {
		fmt.Fprintf(safeStdout, "[gce] deleting cluster %s in zone %s\n", g.clusterName, zone)
	}
	g.client.deleteCluster(g.cluster.Name, zone)
	return &Result{ExitCode: 0}
}

func (g *GceClusterManager) fetchClusterData(env *Environment) {<commit_msg>integration: fix GCE cluster removal<commit_after>// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package integration

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"math/rand"
	"os"
	"time"

	"google.golang.org/api/container/v1"
	"google.golang.org/api/option"

	"golang.org/x/net/context"
)

const gceClusterStatusRunning = "RUNNING"

var zone = os.Getenv("GCE_ZONE")
var projectID = os.Getenv("GCE_PROJECT_ID")
var serviceAccount = os.Getenv("GCE_SERVICE_ACCOUNT")

// GceClusterManager represents a Google Compute Engine cluster (Container Engine)
type GceClusterManager struct {
	client      *gceClient
	clusterName string
	cluster     *container.Cluster
}

func newClusterName() string {
	return fmt.Sprintf("integration-test-%d", randInt())
}

func randInt() int {
	rand.Seed(time.Now().UnixNano())
	return rand.Int()
}

func createTempFile(data []byte, prefix string) (string, error) {
	tmpfile, err := ioutil.TempFile("", prefix)
	if err != nil {
		return "", err
	}
	if _, err := tmpfile.Write(data); err != nil {
		return "", err
	}
	if err := tmpfile.Close(); err != nil {
		return "", err
	}
	return tmpfile.Name(), nil
}

func (g *GceClusterManager) Name() string {
	return "gce"
}

func (g *GceClusterManager) Provisioner() string {
	return "kubernetes"
}

func (g *GceClusterManager) IP(env *Environment) string {
	g.fetchClusterData(env)
	if g.cluster != nil {
		return g.cluster.Endpoint
	}
	return ""
}

func (g *GceClusterManager) Start(env *Environment) *Result {
	ctx := context.Background()
	serviceAccountFile, err := createTempFile([]byte(serviceAccount), "gce-sa-")
	if err != nil {
		return nil
	}
	client, err := newClient(ctx, projectID, option.WithServiceAccountFile(serviceAccountFile))
	if err != nil {
		return nil
	}
	g.client = client
	g.clusterName = env.Get("clustername")
	if g.clusterName == "" {
		g.clusterName = newClusterName()
		env.Set("clustername", g.clusterName)
		if env.VerboseLevel() > 0 {
			fmt.Fprintf(safeStdout, "[gce] starting cluster %s in zone %s\n", g.clusterName, zone)
		}
		g.client.createCluster(g.clusterName, zone, 1)
	} else {
		g.fetchClusterData(env)
		if g.cluster == nil || g.cluster.Status != gceClusterStatusRunning {
			return &Result{ExitCode: 1, Error: fmt.Errorf("[gce] cluster %s is not running", g.clusterName)}
		}
	}
	return &Result{ExitCode: 0}
}

func (g *GceClusterManager) Delete(env *Environment) *Result {
	if g.clusterName == "" {
		return nil
	}
	if env.VerboseLevel() > 0 {
		fmt.Fprintf(safeStdout, "[gce] deleting cluster %s in zone %s\n", g.clusterName, zone)
	}
	g.client.deleteCluster(g.cluster.Name, zone)
	return &Result{ExitCode: 0}
}

func (g *GceClusterManager) fetchClusterData(env *Environment) {
{'fraction_non_alphanumeric': 0.140981837798957, 'fraction_numerical': 0.004675418090271534, 'mean_word_length': 4.363548698167792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7105404', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1948, 'n_words': 498}