Skip to content

Commit

Permalink
Merge pull request #6 from starkandwayne/check-port-issues
Browse files Browse the repository at this point in the history
Check port issues
  • Loading branch information
TheDigitalEagle authored Nov 15, 2022
2 parents 911b0fd + 47203e2 commit 6d5c369
Show file tree
Hide file tree
Showing 90 changed files with 19,035 additions and 148 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ $cf create-service config-server default test-service -c "whatever json configur

## History ##

* v1.0.0 - Full release with support for SR Actuators
* v0.0.5 - Services as configurable objects
* v0.0.4 - Now with configurable package names
* v0.0.3 - Now without package fetching
Expand Down
12 changes: 12 additions & 0 deletions broker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package broker
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
clients "github.com/cloudfoundry-community/go-cf-clients-helper"
cf "github.com/cloudfoundry-community/go-cfclient"
"github.com/cloudfoundry-community/go-uaa"
)

Expand All @@ -22,6 +23,17 @@ func (broker *SCSBroker) GetClient() (*ccv3.Client, error) {
return session.V3(), err
}

func (broker *SCSBroker) GetCommunity() (*cf.Client, error) {
config := &cf.Config{
ApiAddress: broker.Config.CfConfig.ApiUrl,
SkipSslValidation: broker.Config.CfConfig.SkipSslValidation,
Username: broker.Config.CfConfig.CfUsername,
Password: broker.Config.CfConfig.CfPassword,
}

return cf.NewClient(config)
}

func (broker *SCSBroker) GetUaaClient() (*uaa.API, error) {

cf, err := broker.GetClient()
Expand Down
189 changes: 117 additions & 72 deletions broker/create_registry_server_instance.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package broker

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path"
"strings"
"strconv"
"time"

"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
Expand All @@ -19,6 +22,18 @@ func (broker *SCSBroker) createRegistryServerInstance(serviceId string, instance
return "", err
}

rc := utilities.NewRegistryConfig()
broker.Logger.Info("jsonparams == " + jsonparams)
rp, err := utilities.ExtractRegistryParams(jsonparams)
if err != nil {
return "", err
}

count, err := rp.Count()
if err != nil {
return "", err
}

cfClient, err := broker.GetClient()
if err != nil {
return "", errors.New("Couldn't start session: " + err.Error())
Expand Down Expand Up @@ -121,109 +136,139 @@ func (broker *SCSBroker) createRegistryServerInstance(serviceId string, instance
return "", errors.New("no domains found for this instance")
}

route, _, err := cfClient.CreateRoute(ccv3.Route{
SpaceGUID: spaceGUID,
DomainGUID: domains[0].GUID,
Host: appName,
})
if err != nil {
return "", err
}
_, err = cfClient.MapRoute(route.GUID, app.GUID)
broker.Logger.Info("Starting Application")
app, _, err = cfClient.UpdateApplicationStart(app.GUID)
if err != nil {
return "", err
broker.Logger.Info("Application Start Failed, Trying restart")
app, _, err = cfClient.UpdateApplicationRestart(app.GUID)
if err != nil {
broker.Logger.Info("Application Start failed")
return "", err
}
}

broker.Logger.Info("handle node count")
// handle the node count
rc := &registryConfig{}
broker.Logger.Info("jsonparams == " + jsonparams)
rp, err := utilities.ExtractRegistryParams(jsonparams)
if err != nil {
return "", err
}
if count > 1 {
rc.Clustered()
broker.Logger.Info(fmt.Sprintf("scaling to %d", count))
err = broker.scaleRegistryServer(cfClient, &app, count)
if err != nil {
return "", err
}

if count, found := rp["count"]; found {
if c, ok := count.(int); ok {
if c > 1 {
rc.Clustered()
broker.Logger.Info(fmt.Sprintf("scaling to %d", c))
err = broker.scaleRegistryServer(cfClient, &app, c, rc)
if err != nil {
return "", err
}
} else {
rc.Standalone()
}
} else {
rc.Standalone()
community, err := broker.GetCommunity()
if err != nil {
return "", err
}

stats, err := getProcessStatsByAppAndType(cfClient, community, broker.Logger, app.GUID, "web")
if err != nil {
return "", nil
}

for _, stat := range stats {

rc.AddPeer(stat.Index, fmt.Sprintf("http://%s:%d/eureka", stat.Host, stat.InstancePorts[0].External), serviceId)
}
} else {
rc.Standalone()
}

broker.Logger.Info("Updating Environment")
err = broker.UpdateAppEnvironment(cfClient, &app, &info, serviceId, instanceId, rc.String(), params)
err = broker.UpdateRegistryEnvironment(cfClient, &app, &info, serviceId, instanceId, rc, params)

if err != nil {
return "", err
}

app, _, err = cfClient.UpdateApplicationRestart(app.GUID)
route, _, err := cfClient.CreateRoute(ccv3.Route{
SpaceGUID: spaceGUID,
DomainGUID: domains[0].GUID,
Host: appName,
})
if err != nil {
return "", err
}

broker.Logger.Info(route.URL)
_, err = cfClient.MapRoute(route.GUID, app.GUID)

return route.URL, nil
}
if err != nil {
return "", err
}

type registryConfig struct {
Mode string
Peers []string
}
broker.Logger.Info("Starting Application")
app, _, err = cfClient.UpdateApplicationStart(app.GUID)
if err != nil {
broker.Logger.Info("Application Start Failed, Trying restart")
app, _, err = cfClient.UpdateApplicationRestart(app.GUID)
if err != nil {
broker.Logger.Info("Application Start failed")
return "", err
}
}

func (rc *registryConfig) AddPeer(peer string) {
rc.Peers = append(rc.Peers, peer)
}
community, err := broker.GetCommunity()
if err != nil {
return "", err
}

func (rc *registryConfig) Standalone() {
rc.Mode = "standalone"
}
if count > 1 {
stats, err := getProcessStatsByAppAndType(cfClient, community, broker.Logger, app.GUID, "web")
if err != nil {
return "", err
}

func (rc *registryConfig) Clustered() {
rc.Mode = "clustered"
}
for _, stat := range stats {
rc.AddPeer(stat.Index, fmt.Sprintf("http://%s:%d/eureka", stat.Host, stat.InstancePorts[0].External), serviceId)
}
}

func (rc *registryConfig) String() string {
return string(rc.Bytes())
}
peers, err := json.Marshal(rc.Peers)
if err != nil {
return "", err
}
x := 0
for _, peer := range rc.Peers {
req, err := http.NewRequest(http.MethodPost, "https://"+route.URL+"/config/peers", bytes.NewBuffer(peers))
if err != nil {
fmt.Printf("client: could not create request: %s\n", err)

func (rc *registryConfig) Bytes() []byte {
client := make(map[string]interface{})
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Cf-App-Instance", app.GUID+":"+strconv.Itoa(peer.Index))

if rc.Mode == "standalone" {
client["registerWithEureka"] = false
client["fetchRegistry"] = false
}
refreshreq, err := http.NewRequest(http.MethodPost, "https://"+route.URL+"/actuator/refresh", nil)
if err != nil {
fmt.Printf("client: could not create request: %s\n", err)

if len(rc.Peers) > 0 {
serviceUrl := make(map[string]interface{})
defaultZone := strings.Join(rc.Peers, ",")
serviceUrl["defaultZone"] = defaultZone
client["serviceUrl"] = serviceUrl
}
}
refreshreq.Header.Set("Content-Type", "application/json")
refreshreq.Header.Set("X-Cf-App-Instance", app.GUID+":"+strconv.Itoa(peer.Index))

eureka := make(map[string]interface{})
eureka["client"] = client
client := http.Client{
Timeout: 30 * time.Second,
}

data := make(map[string]interface{})
data["eureka"] = eureka
res, err := client.Do(req)
if err != nil {
fmt.Printf("client: error making http request: %s\n", err)
}
broker.Logger.Info(res.Request.RequestURI)
broker.Logger.Info(string(peers))
broker.Logger.Info(res.Status)

output, err := json.Marshal(data)
if err != nil {
return []byte("{}")
refreshres, err := client.Do(refreshreq)
if err != nil {
fmt.Printf("client: error making http request: %s\n", err)
}
broker.Logger.Info(refreshres.Request.RequestURI)
broker.Logger.Info(string(peers))
broker.Logger.Info(refreshres.Status)
x++
}

return output
broker.Logger.Info(route.URL)

return route.URL, nil
}
46 changes: 46 additions & 0 deletions broker/create_service_instances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package broker

import (
"context"
"fmt"

brokerapi "github.com/pivotal-cf/brokerapi/domain"
scsccparser "github.com/starkandwayne/spring-cloud-services-cli-config-parser"
)

func (broker *SCSBroker) CreateServiceInstances(ctx context.Context, instanceID string, details brokerapi.ProvisionDetails, asyncAllowed bool) error {
broker.Logger.Info(fmt.Sprintf("Starting thread for creating service application these details: %s", details))

go broker.startInstances(instanceID, details)

return nil
}

func (broker *SCSBroker) startInstances(instanceID string, details brokerapi.ProvisionDetails) (string, error) {
var provisioner func(string, string, string, map[string]string) (string, error)

envsetup := scsccparser.EnvironmentSetup{}
raw := details.RawParameters
if len(raw) == 0 {
raw = []byte("{}")
}
mapparams, err := envsetup.ParseEnvironmentFromRaw(raw)
if err != nil {
return "", err
}

switch details.ServiceID {
case "service-registry":
provisioner = broker.createRegistryServerInstance
case "config-server":
provisioner = broker.createConfigServerInstance

}

url, err := provisioner(details.ServiceID, instanceID, string(details.RawParameters), mapparams)
if err != nil {
return "", err
}

return url, nil
}
36 changes: 36 additions & 0 deletions broker/get_process_stats_by_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package broker

import (
"errors"

"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/lager"
cf "github.com/cloudfoundry-community/go-cfclient"
)

func getProcessStatsByAppAndType(cfClient *ccv3.Client, community *cf.Client, logger lager.Logger, appGUID string, procType string) ([]cf.Stats, error) {
stats := make([]cf.Stats, 0)

procs, err := getApplicationProcessesByType(cfClient, logger, appGUID, procType)
if err != nil {
return stats, err
}

for _, proc := range procs {
candidates, err := community.GetProcessStats(proc.GUID)
if err != nil {
continue
}

for _, stat := range candidates {
stats = append(stats, stat)
}
}

if len(stats) == 0 {
return stats, errors.New("no stats found")
}

return stats, nil

}
6 changes: 6 additions & 0 deletions broker/poll_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/lager"
)

func (broker *SCSBroker) pollBuild(buildGUID string, appName string) (ccv3.Droplet, ccv3.Warnings, error) {
Expand All @@ -29,6 +30,11 @@ func (broker *SCSBroker) pollBuild(buildGUID string, appName string) (ccv3.Dropl
return ccv3.Droplet{}, allWarnings, err
}

broker.Logger.Info("polling build final state:", lager.Data{
"package_guid": build.GUID,
"state": build.State,
})

switch build.State {
case constant.BuildFailed:
return ccv3.Droplet{}, allWarnings, errors.New(build.Error)
Expand Down
Loading

0 comments on commit 6d5c369

Please sign in to comment.