Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
141 lines (133 loc) · 4.4 KB
/
Copy pathutil.go
File metadata and controls
141 lines (133 loc) · 4.4 KB
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
/*
Onix Config Manager - Artisan
Copyright (c) 2018-Present by www.gatblau.org
Licensed under the Apache License, Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0
Contributors to this project, hereby assign copyright in this code to the project,
to be licensed under the same terms as the rest of the code.
*/
package app
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gatblau/onix/artisan/build"
"github.com/gatblau/onix/artisan/core"
"github.com/gatblau/onix/artisan/merge"
"github.com/gatblau/onix/oxlib/resx"
"github.com/google/uuid"
"gopkg.in/yaml.v2"
)
// loadSvcManFromImage extracts the service manifest from a docker image
func loadSvcManFromImage(svcRef SvcRef) (*SvcManifest, error) {
var cmd string
pathLabel := "artisan.svc.manifest"
containerName := fmt.Sprintf("%s-info", svcRef.Name)
// create a container instance in stopped state
cmd = fmt.Sprintf("docker create --name %s %s", containerName, svcRef.Image)
_, err := build.Exe(cmd, ".", merge.NewEnVarFromSlice([]string{}), false)
if err != nil {
return nil, fmt.Errorf("cannot create docker container from image '%s': %s\n", svcRef.Image, err)
}
// create a random tmp folder
tmp, err := tmpPath()
if err != nil {
return nil, fmt.Errorf("cannot create tmp working folder: %s\n", err)
}
// inspect docker manifest looking for artisan.svc.manifest label value
cmd = fmt.Sprintf("docker inspect --format '{{ index .Config.Labels \"%s\"}}' %s", pathLabel, containerName)
svcManPath, err := build.Exe(cmd, ".", merge.NewEnVarFromSlice([]string{}), false)
if err != nil {
return nil, fmt.Errorf("cannot extract service manifest path from container label: %s\n", err)
}
if len(svcManPath) == 0 {
return nil, fmt.Errorf("missing service manifest path label '%s' in image '%s': %s\n", pathLabel, svcRef.Image, err)
}
// extract service manifest
svcPath := filepath.Join(tmp, "svc.yaml")
cmd = fmt.Sprintf("docker cp %s:%s %s", containerName, svcManPath, svcPath)
_, err = build.Exe(cmd, ".", merge.NewEnVarFromSlice([]string{}), false)
if err != nil {
return nil, fmt.Errorf("cannot copy service manifest from container image: %s\n", err)
}
// remove the svc-info container
_, err = build.Exe(fmt.Sprintf("docker rm -f %s", containerName), ".", merge.NewEnVarFromSlice([]string{}), false)
if err != nil {
return nil, fmt.Errorf("cannot remove info container image: %s\n", err)
}
// load the service manifest
content, err := os.ReadFile(svcPath)
if err != nil {
return nil, fmt.Errorf("cannot load service manifest: %s\n", err)
}
// unmarshal manifest
svcMan := new(SvcManifest)
err = yaml.Unmarshal(content, svcMan)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal service manifest: %s\n", err)
}
// remove tmp folder
cmd = fmt.Sprintf("rm -rf %s", tmp)
_, err = build.Exe(cmd, ".", merge.NewEnVarFromSlice([]string{}), false)
if err != nil {
return nil, fmt.Errorf("cannot remove tmp working folder: %s\n", err)
}
return svcMan, nil
}
// loadSvcManFromURI extracts the service manifest from a remote URI
func loadSvcManFromURI(svc *SvcRef, credentials string) (*SvcManifest, error) {
var (
content []byte
err error
)
content, err = resx.ReadFile(svc.URI, credentials)
if err != nil {
return nil, err
}
m := new(SvcManifest)
err = yaml.Unmarshal(content, m)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal remotely fetched service manifest yaml file: %s\n", err)
}
return m, nil
}
func isURL(uri string) bool {
return strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://")
}
// return a temporary path and create the tmp folder
func tmpPath() (string, error) {
uuid.EnableRandPool()
id, err := uuid.NewUUID()
if err != nil {
return "", err
}
path := filepath.Join(core.TmpPath(), id.String())
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return "", err
}
return path, nil
}
// fetchFile fetches a file content from an url
// authentication is done via uri scheme (i.e. https://username:password@domain/.../...)
func fetchFile(uri string) ([]byte, error) {
client := http.Client{
Timeout: 60 * time.Second,
}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
if resp.StatusCode > 299 {
return nil, fmt.Errorf("cannot fetch '%s': %s\n", uri, resp.Status)
}
return ioutil.ReadAll(resp.Body)
}