forked from pivotal-cf/on-demand-service-broker
/
orphan_deployments.go
88 lines (71 loc) · 3.22 KB
/
orphan_deployments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2016-Present Pivotal Software, Inc. All rights reserved.
// This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package main
import (
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/amansingh066/on-demand-service-broker/authorizationheader"
"github.com/amansingh066/on-demand-service-broker/broker/services"
"github.com/amansingh066/on-demand-service-broker/config"
"github.com/amansingh066/on-demand-service-broker/loggerfactory"
"github.com/craigfurman/herottp"
yaml "gopkg.in/yaml.v2"
)
const (
OrphanBoshDeploymentsDetectedMessage = "Orphan BOSH deployments detected with no corresponding service instance in the platform. " +
"Before deleting any deployment it is recommended to verify the service instance no longer exists in the platform and any data is safe to delete."
OrphanDeploymentsDetectedExitCode = 10
)
func main() {
loggerFactory := loggerfactory.New(os.Stderr, "orphan-deployments", loggerfactory.Flags)
logger := loggerFactory.New()
var configPath string
flag.StringVar(&configPath, "configPath", "", "path to orphan-deployment errand config")
flag.Parse()
if configPath == "" {
logger.Fatalln("-configPath must be given as argument")
}
contents, err := ioutil.ReadFile(configPath)
if err != nil {
logger.Fatalln(err.Error())
}
var errandConfig config.OrphanDeploymentsErrandConfig
if err := yaml.Unmarshal(contents, &errandConfig); err != nil {
logger.Fatalf("failed to unmarshal errand config: %s\n", err.Error())
}
rootCAs, err := x509.SystemCertPool()
if err != nil {
logger.Fatalf("error retrieving root ca's: %s", err)
}
rootCAs.AppendCertsFromPEM([]byte(errandConfig.BrokerAPI.TLS.CACert))
httpClient := herottp.New(herottp.Config{
Timeout: 30 * time.Second,
RootCAs: rootCAs,
DisableTLSCertificateVerification: errandConfig.BrokerAPI.TLS.DisableSSLCertVerification,
})
brokerUsername := errandConfig.BrokerAPI.Authentication.Basic.Username
brokerPassword := errandConfig.BrokerAPI.Authentication.Basic.Password
authHeaderBuilder := authorizationheader.NewBasicAuthHeaderBuilder(brokerUsername, brokerPassword)
brokerServices := services.NewBrokerServices(httpClient, authHeaderBuilder, errandConfig.BrokerAPI.URL, logger)
orphans, err := brokerServices.OrphanDeployments()
if err != nil {
logger.Fatalf("error retrieving orphan deployments: %s", err)
}
rawJSON, err := json.Marshal(orphans)
if err != nil {
logger.Fatalf("error marshalling orphan deployments: %s", err)
}
fmt.Fprintln(os.Stdout, string(rawJSON))
if len(orphans) > 0 {
logger.Println(OrphanBoshDeploymentsDetectedMessage)
os.Exit(OrphanDeploymentsDetectedExitCode)
}
}