<commit_before>package service

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"strings"

	"github.com/intel-data/app-launching-service-broker/utils"
)

// CFClient object
type CFClient struct {
	config *ServiceConfig
}

// NewCFClient creates a new isntance of CFClient
func NewCFClient(c *ServiceConfig) *CFClient {
	return &CFClient{
		config: c,
	}
}

func (c *CFClient) initialize() (*utils.CommandLogger, error) {
	log.Println("initializing...")

	// yep, this is a royal hack, should get this from the env somehow
	pushID := genRandomString(8)
	appDir, err := ioutil.TempDir(c.config.CFEnv.TempDir, pushID)
	if err != nil {
		log.Fatalf("err creating a temp dir: %v", err)
		return nil, err
	}

	// api
	cf := utils.NewCommandLogger("cf")

	// TODO: remove the skip API validation part once real cert deployed
	cf.WithArgs("api", c.config.APIEndpoint, "--skip-ssl-validation").
		WithEnv("CF_HOME", appDir).Exec()
	if cf.Err() != nil {
		log.Fatalf("err cmd: %v", cf)
		return cf, cf.Err()
	}

	// auth
	cf.WithArgs("auth", c.config.APIUser, c.config.APIPassword).Exec()
	if cf.Err() != nil {
		log.Fatalf("err cmd: %v", cf)
		return cf, cf.Err()
	}

	return cf, nil
}

func (c *CFClient) provision(ctx *CFServiceContext) error {
	log.Printf("provisioning service: %v", ctx)

	// initialize
	cf, err := c.initialize()
	if err != nil {
		log.Fatalf("err initializing command: %v", err)
		return err
	}

	// target
	cf.WithArgs("target", "-o", ctx.OrgName, "-s", ctx.SpaceName).Exec()
	if cf.Err() != nil {
		log.Fatalf("err cmd: %v", cf)
		return cf.Err()
	}

	// push
	cf.WithArgs("push", ctx.AppName, "-p", c.config.AppSource, "--no-start").Exec()
	if cf.Err() != nil {
		log.Printf("push error: %s", cf.Err())
		c.deprovision(ctx)
		return cf.Err()
	}

	// TODO: Add cleanup of dependencies
	for i, dep := range c.config.Dependencies {
		depName := dep.Name + "-" + ctx.AppName
		cf.WithArgs("create-service", dep.Name, dep.Plan, depName).Exec()
		if cf.Err() != nil {
			log.Printf("err on dependency[%d]: %s - %v", i, depName, cf)
			return cf.Err()
		}

		// bind
		cf.WithArgs("bind-service", ctx.AppName, depName).Exec()
		if cf.Err() != nil {
			log.Printf("err on bind[%d]: %s > %s - %v", i, ctx.AppName, depName, cf)
			return cf.Err()
		}
	}

	// start
	cf.WithArgs("start", ctx.AppName).Exec()
	if cf.Err() != nil {
		log.Printf("err cmd: %v", cf)
		c.deprovision(ctx)
		return cf.Err()
	}

	return nil
}
<commit_msg>Added setting environment for app<commit_after>package service

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"strings"

	"github.com/intel-data/app-launching-service-broker/utils"
)

// CFClient object
type CFClient struct {
	config *ServiceConfig
}

// NewCFClient creates a new isntance of CFClient
func NewCFClient(c *ServiceConfig) *CFClient {
	return &CFClient{
		config: c,
	}
}

func (c *CFClient) initialize() (*utils.CommandLogger, error) {
	log.Println("initializing...")

	// yep, this is a royal hack, should get this from the env somehow
	pushID := genRandomString(8)
	appDir, err := ioutil.TempDir(c.config.CFEnv.TempDir, pushID)
	if err != nil {
		log.Fatalf("err creating a temp dir: %v", err)
		return nil, err
	}

	// api
	cf := utils.NewCommandLogger("cf")

	// TODO: remove the skip API validation part once real cert deployed
	cf.WithArgs("api", c.config.APIEndpoint, "--skip-ssl-validation").
		WithEnv("CF_HOME", appDir).Exec()
	if cf.Err() != nil {
		log.Fatalf("err cmd: %v", cf)
		return cf, cf.Err()
	}

	// auth
	cf.WithArgs("auth", c.config.APIUser, c.config.APIPassword).Exec()
	if cf.Err() != nil {
		log.Fatalf("err cmd: %v", cf)
		return cf, cf.Err()
	}

	return cf, nil
}

func (c *CFClient) provision(ctx *CFServiceContext) error {
	log.Printf("provisioning service: %v", ctx)

	// initialize
	cf, err := c.initialize()
	if err != nil {
		log.Fatalf("err initializing command: %v", err)
		return err
	}

	// target
	cf.WithArgs("target", "-o", ctx.OrgName, "-s", ctx.SpaceName).Exec()
	if cf.Err() != nil {
		log.Fatalf("err cmd: %v", cf)
		return cf.Err()
	}

	// push
	cf.WithArgs("push", ctx.AppName, "-p", c.config.AppSource, "--no-start").Exec()
	if cf.Err() != nil {
		log.Printf("push error: %s", cf.Err())
		c.deprovision(ctx)
		return cf.Err()
	}

	// set env to APP, change if more then one app launcher is needed
	cf.WithArgs("set-env", ctx.AppName, "APP_LAUNCHER_NAME", "APP_LAUNCHER")
	cf.WithArgs("set-env", ctx.AppName, "APP_LAUNCHER_STATE", "creating")

	// TODO: Add cleanup of dependencies
	cf.WithArgs("set-env", ctx.AppName, "APP_LAUNCHER_STATE", "create_bind_services")
	for i, dep := range c.config.Dependencies {
		depName := dep.Name + "-" + ctx.AppName
		cf.WithArgs("create-service", dep.Name, dep.Plan, depName).Exec()
		if cf.Err() != nil {
			log.Printf("err on dependency[%d]: %s - %v", i, depName, cf)
			return cf.Err()
		}

		// bind
		cf.WithArgs("bind-service", ctx.AppName, depName).Exec()
		if cf.Err() != nil {
			log.Printf("err on bind[%d]: %s > %s - %v", i, ctx.AppName, depName, cf)
			return cf.Err()
		}
	}

	// start
	cf.WithArgs("set-env", ctx.AppName, "APP_LAUNCHER_STATE", "start")
	cf.WithArgs("start", ctx.AppName).Exec()
	if cf.Err() != nil {
		log.Printf("err cmd: %v", cf)
		c.deprovision(ctx)
		return cf.Err()
	}
	cf.WithArgs("set-env", ctx.AppName, "APP_LAUNCHER_STATE", "finished")

	return nil
}

{'fraction_non_alphanumeric': 0.18074986010072747, 'fraction_numerical': 0.00037306472673008767, 'mean_word_length': 3.594687232219366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2184624', 'n_tokens_mistral': 2140, 'n_tokens_neox': 2059, 'n_words': 509}