This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
filesystem.go
142 lines (124 loc) · 4.56 KB
/
filesystem.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package docker
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"path/filepath"
"time"
"github.com/eclipse/codewind-installer/pkg/utils"
"gopkg.in/yaml.v2"
)
// WriteToComposeFile the contents of the docker compose yaml
func WriteToComposeFile(dockerComposeFile string, debug bool) *DockerError {
dockerComposeTempErr := utils.CreateTempFile(dockerComposeFile)
if dockerComposeTempErr != nil {
return &DockerError{errOpDockerComposeFileCreate, dockerComposeTempErr, dockerComposeTempErr.Error()}
}
dockerClient, dockerErr := NewDockerClient()
if dockerErr != nil {
return dockerErr
}
// If we are using a remote docker host it won't be able to read a local secrets file.
secretFileName := "/dev/null"
if UsingLocalDockerHost(dockerClient) {
var secretErr *DockerError
secretFileName, secretErr = writeDockerConfigSecretFile(path.Dir(dockerComposeFile))
if secretErr != nil {
return secretErr
}
}
dataStruct := Compose{}
data := fmt.Sprintf(composeTemplate, secretFileName)
unmarshDataErr := yaml.Unmarshal([]byte(data), &dataStruct)
if unmarshDataErr != nil {
return &DockerError{errOpDockerComposeFileCreate, unmarshDataErr, unmarshDataErr.Error()}
}
if debug == true && len(dataStruct.SERVICES.PFE.Ports) > 0 {
debugPort := DetermineDebugPortForPFE()
// Add the debug port to the docker compose data
dataStruct.SERVICES.PFE.Ports = append(dataStruct.SERVICES.PFE.Ports, "127.0.0.1:"+debugPort+":9777")
}
marshalledData, yamlErr := yaml.Marshal(&dataStruct)
if yamlErr != nil {
return &DockerError{errOpDockerComposeFileCreate, yamlErr, yamlErr.Error()}
}
if debug == true {
fmt.Printf("==> %s structure is: \n%s\n\n", dockerComposeFile, string(marshalledData))
} else {
fmt.Println("==> environment structure written to " + filepath.ToSlash(dockerComposeFile))
}
writeFileErr := ioutil.WriteFile(dockerComposeFile, marshalledData, 0644)
if writeFileErr != nil {
return &DockerError{errOpDockerComposeFileCreate, writeFileErr, writeFileErr.Error()}
}
return nil
}
func writeDockerConfigSecretFile(parentPath string) (string, *DockerError) {
dockerConfig, err := getDockerCredentials("local")
if err != nil {
return "", err
}
dockerConfigBytes, jsonErr := json.MarshalIndent(dockerConfig, "", " ")
if jsonErr != nil {
return "", &DockerError{errDockerCredential, jsonErr, jsonErr.Error()}
}
encoded := base64.StdEncoding.EncodeToString(dockerConfigBytes)
secretFile := path.Join(parentPath, dockerConfigSecretFile)
writeFileErr := ioutil.WriteFile(secretFile, []byte(encoded), 0600)
if writeFileErr != nil {
return "", &DockerError{errDockerCredential, writeFileErr, writeFileErr.Error()}
}
return secretFile, nil
}
// ClearDockerConfigSecret We erase the contents rather than deleting
// the file as the docker-compose file expects the secret to be present.
func ClearDockerConfigSecret(parentPath string) error {
// Most callers won't handle this error as this shouldn't block shutdown.
secretFile := path.Join(parentPath, dockerConfigSecretFile)
return ioutil.WriteFile(secretFile, []byte{}, 0600)
}
// PingHealth - pings environment api every 15 seconds to check if containers started
func PingHealth(healthEndpoint string) (bool, *DockerError) {
var started = false
fmt.Println("Waiting for Codewind to start")
dockerClient, err := NewDockerClient()
if err != nil {
return false, err
}
hostname, port, err := GetPFEHostAndPort(dockerClient)
if err != nil {
return false, err
}
for i := 0; i < 120; i++ {
resp, err := http.Get("http://" + hostname + ":" + port + healthEndpoint)
if err != nil {
fmt.Printf(".")
} else {
if resp.StatusCode == 200 {
fmt.Println("\nHTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))
fmt.Println("Codewind successfully started on http://" + hostname + ":" + port)
started = true
break
}
}
time.Sleep(1 * time.Second)
}
if started != true {
log.Fatal("Codewind containers are taking a while to start. Please check the container logs and/or restart Codewind")
}
return started, nil
}