-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_docker.go
More file actions
161 lines (131 loc) · 5.45 KB
/
generate_docker.go
File metadata and controls
161 lines (131 loc) · 5.45 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"errors"
"fmt"
"log"
"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/codesphere-cloud/cs-go/pkg/exporter"
"github.com/codesphere-cloud/cs-go/pkg/git"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)
type GenerateDockerCmd struct {
cmd *cobra.Command
Opts *GenerateDockerOpts
}
type GenerateDockerOpts struct {
*GenerateOpts
BaseImage string
Envs []string
}
func (c *GenerateDockerCmd) RunE(cc *cobra.Command, args []string) error {
fs := cs.NewOSFileSystem(c.Opts.RepoRoot)
gitSvc := git.NewGitService(fs)
exporter := exporter.NewExporterService(fs, c.Opts.Output, c.Opts.BaseImage, c.Opts.Envs, c.Opts.RepoRoot, c.Opts.Force)
clientFactory := func() (Client, error) {
return NewClient(*c.Opts.GlobalOptions)
}
if err := c.GenerateDocker(fs, exporter, gitSvc, clientFactory); err != nil {
return fmt.Errorf("failed to generate docker: %w", err)
}
log.Println("docker artifacts created:")
log.Printf("Input: %s\n", c.Opts.Input)
log.Printf("Output: %s\n", c.Opts.Output)
log.Println("To start with docker-compose, run:")
log.Printf("cd %s && docker compose up\n\n", c.Opts.Output)
log.Println("To build and push images, run:")
log.Println("export REGISTRY=<your-registry>")
log.Println("export IMAGE_PREFIX=<some-prefix>")
log.Printf("%s generate images --reporoot %s -r $REGISTRY -p $IMAGE_PREFIX -i %s -o %s\n\n", io.BinName(), c.Opts.RepoRoot, c.Opts.Input, c.Opts.Output)
return nil
}
func AddGenerateDockerCmd(generate *cobra.Command, opts *GenerateOpts) {
docker := GenerateDockerCmd{
cmd: &cobra.Command{
Use: "docker",
Short: "Generates docker artifacts based on a ci.yml of a workspace",
Long: io.Long(`The generated artifacts will be saved in the output folder (default is ./export).
It then generates following artifacts inside the output folder:
./<service-n> Each service is exported to a separate folder.
./<service-n>/dockerfile docker to build the container of the service.
./<service-n>/entrypoint.sh Entrypoint of the container (run stage of Codesphere workspace).
./docker-compose.yml Environment to allow running the services with docker-compose.
./nginx.conf Configuration for NGINX, which is used by as router between services.
Codesphere recommends adding the generated artifacts to the source code repository.
Limitations:
- Environment variables have to be set explicitly as the Codesphere environment has its own way to provide env variables.
- The workspace ID, team ID etc. are not automatically available and have to be set explicitly.
- Hardcoded workspace urls don't work outside of the Codesphere environment.
- Each dockerfile of your services contain all prepare steps. To have the smallest image possible you would have to delete all unused steps in each service.
`),
Example: io.FormatExampleCommands("generate docker", []io.Example{
{Cmd: "-w 1234", Desc: "Generate docker for workspace 1234"},
{Cmd: "-w 1234 -i ci.prod.yml", Desc: "Generate docker for workspace 1234 based on ci profile ci.prod.yml"},
}),
},
Opts: &GenerateDockerOpts{
GenerateOpts: opts,
},
}
docker.cmd.Flags().StringVarP(&docker.Opts.BaseImage, "baseimage", "b", "", "Base image for the docker")
docker.cmd.Flags().StringArrayVarP(&docker.Opts.Envs, "env", "e", []string{}, "Env vars to put into generated artifacts")
generate.AddCommand(docker.cmd)
docker.cmd.RunE = docker.RunE
}
func (c *GenerateDockerCmd) GenerateDocker(fs *cs.FileSystem, exp exporter.Exporter, git git.Git, clientFactory func() (Client, error)) error {
if c.Opts.BaseImage == "" {
return errors.New("baseimage is required")
}
ciInput := c.Opts.Input
if !fs.FileExists(ciInput) {
log.Printf("Input file %s not found attempting to clone workspace repository...\n", c.Opts.Input)
client, err := clientFactory()
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %w", err)
}
if err := c.CloneRepository(client, fs, git, c.Opts.RepoRoot); err != nil {
return fmt.Errorf("failed to clone repository: %w", err)
}
if !fs.FileExists(ciInput) {
return fmt.Errorf("input file %s not found after cloning repository", c.Opts.Input)
}
}
_, err := exp.ReadYmlFile(ciInput)
if err != nil {
return fmt.Errorf("failed to export docker artifacts: %w", err)
}
err = exp.ExportDockerArtifacts()
if err != nil {
return fmt.Errorf("failed to export docker artifacts: %w", err)
}
return nil
}
func (c *GenerateDockerCmd) CloneRepository(client Client, fs *cs.FileSystem, git git.Git, clonedir string) error {
log.Printf("Cloning repository into %s...\n", clonedir)
wsId, err := c.Opts.GetWorkspaceId()
if err != nil {
return fmt.Errorf("failed to get workspace ID: %w", err)
}
ws, err := client.GetWorkspace(wsId)
if err != nil {
return fmt.Errorf("failed to get workspace: %w", err)
}
if ws.GitUrl.Get() == nil || *ws.GitUrl.Get() == "" {
return errors.New("workspace does not have a git repository")
}
repoUrl := *ws.GitUrl.Get()
repoBranch := "main"
if c.Opts.Branch != "" {
repoBranch = c.Opts.Branch
} else if ws.InitialBranch.Get() != nil {
repoBranch = *ws.InitialBranch.Get()
}
_, err = git.CloneRepository(fs, repoUrl, repoBranch, clonedir)
if err != nil {
return fmt.Errorf("failed to clone repository %s branch %s: %w", repoUrl, repoBranch, err)
}
log.Printf("Repository %s, branch %s cloned.\n", repoUrl, repoBranch)
return nil
}