ledo init
This commit is contained in:
40
app/cmd/init.go
Normal file
40
app/cmd/init.go
Normal file
@ -0,0 +1,40 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
"html/template"
|
||||
"ledo/app/modules/context"
|
||||
"ledo/app/modules/interact"
|
||||
"ledo/app/templates"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var CmdInit = cli.Command{
|
||||
Name: "init",
|
||||
Aliases: []string{"i"},
|
||||
Category: catSetup,
|
||||
Usage: "Init ledo in project",
|
||||
Description: `Initialize LeadDocker in current project`,
|
||||
Action: runInitLedo,
|
||||
}
|
||||
|
||||
func runInitLedo(cmd *cli.Context) error {
|
||||
config, _ := context.LoadConfigFile()
|
||||
data, err := interact.InitLedoProject(config.Docker)
|
||||
tpl, err := template.New("tpl").Parse(templates.LedoConfigurationFileTemplate)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
err = tpl.Execute(os.Stdout, data)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
advRun := interact.InitAdvancedConfigurationAsk()
|
||||
fmt.Printf("%v", advRun)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ func RunSelectMode(cmd *cli.Context) error {
|
||||
ctx.Mode.SetMode(cmd.Args().First())
|
||||
return nil
|
||||
}
|
||||
// interact.SelectDockerHubTag("paramah/php")
|
||||
interact.SelectMode(ctx, "")
|
||||
return nil
|
||||
}
|
||||
|
2
app/helper/popupar_images.go
Normal file
2
app/helper/popupar_images.go
Normal file
@ -0,0 +1,2 @@
|
||||
package helper
|
||||
|
@ -79,7 +79,8 @@ func MergeComposerFiles(filenames ...string) (string, error) {
|
||||
}
|
||||
|
||||
func ShowDockerImageFQN(ctx *context.LedoContext) string {
|
||||
return fmt.Sprintf("%s/%s/%s/master:latest", ctx.Config.Docker.Registry, ctx.Config.Docker.Namespace, ctx.Config.Docker.Name)
|
||||
fqn := fmt.Sprintf("%s/%s/%s/master", ctx.Config.Docker.Registry, ctx.Config.Docker.Namespace, ctx.Config.Docker.Name)
|
||||
return strings.ToLower(fqn)
|
||||
}
|
||||
|
||||
func ExecComposerUp(ctx *context.LedoContext) {
|
||||
|
@ -34,6 +34,11 @@ func InitCommand(ctx *cli.Context) *LedoContext {
|
||||
modeYml = ".jz-mode"
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configYml); err != nil {
|
||||
fmt.Printf("Config file not found. Please run ledo init\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
mode := mode.InitMode(modeYml, configYml)
|
||||
c.Mode = mode
|
||||
|
||||
@ -56,6 +61,18 @@ func InitCommand(ctx *cli.Context) *LedoContext {
|
||||
return &c
|
||||
}
|
||||
|
||||
func LoadConfigFile() (*config.LedoFile, error) {
|
||||
configYml := ".ledo.yml"
|
||||
|
||||
if _, err := os.Stat(configYml); err != nil {
|
||||
nilCfg := &config.LedoFile{}
|
||||
return nilCfg, err
|
||||
}
|
||||
|
||||
cfg, _ := config.NewLedoFile(configYml)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (lx *LedoContext) ExecCmd(cmd string, cmdArgs []string) error {
|
||||
fmt.Printf("Execute: %v %v\n", cmd, strings.Join(cmdArgs, " "))
|
||||
command := exec.Command(cmd, cmdArgs...)
|
||||
|
40
app/modules/docker_hub/docker_hub.go
Normal file
40
app/modules/docker_hub/docker_hub.go
Normal file
@ -0,0 +1,40 @@
|
||||
package docker_hub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DockerImageTag struct {
|
||||
Layer string `json:"layer"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
var url = "https://registry.hub.docker.com/v1/repositories"
|
||||
|
||||
func GetImageTags(image string) []DockerImageTag {
|
||||
url = url + "/" + image + "/tags"
|
||||
res, _ := http.Get(url)
|
||||
defer res.Body.Close()
|
||||
|
||||
bytes, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
var tags []DockerImageTag
|
||||
json.Unmarshal(bytes, &tags)
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
func ImageTagsToArray(tags []DockerImageTag) []string {
|
||||
n := len(tags)
|
||||
arrTags := make([]string, n)
|
||||
for idx, tag := range tags {
|
||||
arrTags[idx] = tag.Name
|
||||
}
|
||||
|
||||
return arrTags
|
||||
}
|
30
app/modules/interact/docker_hub_tags.go
Normal file
30
app/modules/interact/docker_hub_tags.go
Normal file
@ -0,0 +1,30 @@
|
||||
package interact
|
||||
|
||||
import (
|
||||
survey "github.com/AlecAivazis/survey/v2"
|
||||
"ledo/app/modules/docker_hub"
|
||||
)
|
||||
|
||||
func SelectDockerHubTag(dockerImage string) (string, error) {
|
||||
dockerImageTags := docker_hub.GetImageTags(dockerImage)
|
||||
selectedTag := "latest"
|
||||
|
||||
var qs = []*survey.Question{
|
||||
{
|
||||
Name: "tags",
|
||||
Prompt: &survey.Select{
|
||||
Message: "Select available docker image tag",
|
||||
PageSize: 10,
|
||||
Options: docker_hub.ImageTagsToArray(dockerImageTags),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := survey.Ask(qs, &selectedTag)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return selectedTag, err
|
||||
}
|
76
app/modules/interact/init.go
Normal file
76
app/modules/interact/init.go
Normal file
@ -0,0 +1,76 @@
|
||||
|
||||
package interact
|
||||
|
||||
import (
|
||||
survey "github.com/AlecAivazis/survey/v2"
|
||||
"ledo/app/modules/config"
|
||||
)
|
||||
|
||||
func InitLedoProject(dockerConfig config.DockerMap) (config.DockerMap, error) {
|
||||
if dockerConfig.Registry == "" {
|
||||
dockerConfig.Registry = "hub.docker.com"
|
||||
}
|
||||
|
||||
if dockerConfig.Shell == "" {
|
||||
dockerConfig.Shell = "/bin/bash"
|
||||
}
|
||||
|
||||
if dockerConfig.Username == "" {
|
||||
dockerConfig.Username = "root"
|
||||
}
|
||||
|
||||
var qs = []*survey.Question{
|
||||
{
|
||||
Name: "Registry",
|
||||
Prompt: &survey.Input{Message: "Enter docker registry address: ", Default: dockerConfig.Registry, Help: "Docker registry for main service image"},
|
||||
Validate: survey.Required,
|
||||
Transform: survey.ToLower,
|
||||
},
|
||||
{
|
||||
Name: "Namespace",
|
||||
Prompt: &survey.Input{Message: "Enter project namespace: ",Default: dockerConfig.Namespace, Help: "Project namespace (eq. GitLab project group)"},
|
||||
Validate: survey.Required,
|
||||
Transform: survey.ToLower,
|
||||
},
|
||||
{
|
||||
Name: "Name",
|
||||
Prompt: &survey.Input{Message: "Enter project name: ", Default: dockerConfig.Name},
|
||||
Validate: survey.Required,
|
||||
Transform: survey.ToLower,
|
||||
},
|
||||
{
|
||||
Name: "MainService",
|
||||
Prompt: &survey.Input{Message: "Enter docker-compose main service name: ",Default: dockerConfig.MainService, Help: "Main service, important if you want use ledo shell command or ledo run command"},
|
||||
Validate: survey.Required,
|
||||
Transform: survey.ToLower,
|
||||
},
|
||||
{
|
||||
Name: "Shell",
|
||||
Prompt: &survey.Input{Message: "Enter default shell: ", Default: dockerConfig.Shell},
|
||||
Validate: survey.Required,
|
||||
Transform: survey.ToLower,
|
||||
},
|
||||
{
|
||||
Name: "Username",
|
||||
Prompt: &survey.Input{Message: "Enter docker main service username: ", Default: dockerConfig.Username, Help: "Default user, if set ledo run command was execute with sudo user"},
|
||||
Validate: survey.Required,
|
||||
Transform: survey.ToLower,
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
err := survey.Ask(qs, &dockerConfig)
|
||||
|
||||
if err != nil {
|
||||
return config.DockerMap{}, err
|
||||
}
|
||||
|
||||
return dockerConfig, err
|
||||
}
|
||||
|
||||
func InitAdvancedConfigurationAsk() (bool) {
|
||||
runAdv := false
|
||||
prompt := &survey.Confirm{Message: "Run advanced docker mode configuration?"}
|
||||
survey.AskOne(prompt, &runAdv)
|
||||
return runAdv
|
||||
}
|
5
app/templates/compose_template.go
Normal file
5
app/templates/compose_template.go
Normal file
@ -0,0 +1,5 @@
|
||||
package templates
|
||||
|
||||
var LedoDockerComposeBaseFileTemplate = `
|
||||
|
||||
`
|
52
app/templates/dockerfile_template.go
Normal file
52
app/templates/dockerfile_template.go
Normal file
@ -0,0 +1,52 @@
|
||||
package templates
|
||||
|
||||
var default_DockerFileTemplate = `
|
||||
FROM {{.Image}}/{{.Tag}}
|
||||
|
||||
ENV DIR /usr/local
|
||||
WORKDIR ${DIR}
|
||||
|
||||
# Copy entrypoint
|
||||
COPY docker/docker-entrypoint.sh /bin/docker-entrypoint.sh
|
||||
|
||||
# Copy project content
|
||||
COPY {{.ContainerContent}} $DIR
|
||||
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
CMD [""]
|
||||
`
|
||||
var php_DockerFileTemplate = `
|
||||
FROM paramah/{{.Tag}}
|
||||
ARG ENVIRONMENT=production
|
||||
|
||||
RUN ngxconfig sf.conf
|
||||
|
||||
ENV DIR /var/www
|
||||
WORKDIR ${DIR}
|
||||
|
||||
# Copy entrypoint
|
||||
COPY docker/docker-entrypoint.sh /bin/docker-entrypoint.sh
|
||||
RUN chmod +x /bin/docker-entrypoint.sh
|
||||
|
||||
# Develop packages
|
||||
RUN xdebug_enable
|
||||
|
||||
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
|
||||
RUN chown www-data:www-data ${DIR} && /bin/composer self-update --2
|
||||
USER www-data
|
||||
|
||||
# For Docker build cache
|
||||
COPY ./composer.* $DIR/
|
||||
RUN /bin/composer install --no-scripts --no-interaction --no-autoloader && composer clear-cache
|
||||
|
||||
# Copy application
|
||||
COPY --chown=www-data:www-data ./ $DIR
|
||||
|
||||
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
EXPOSE 80
|
||||
# done
|
||||
|
||||
USER root
|
||||
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
|
||||
`
|
18
app/templates/ledofile_template.go
Normal file
18
app/templates/ledofile_template.go
Normal file
@ -0,0 +1,18 @@
|
||||
package templates
|
||||
|
||||
var LedoConfigurationFileTemplate = `
|
||||
docker:
|
||||
registry: {{.Registry}}
|
||||
namespace: {{.Namespace}}
|
||||
name: {{.Name}}
|
||||
main_service: {{.MainService}}
|
||||
shell: {{.Shell}}
|
||||
{{- if ne .Username "root" }}
|
||||
username: {{.Username}}
|
||||
{{end}}
|
||||
modes:
|
||||
normal: docker/docker-compose.yml
|
||||
dev: docker/docker-compose.yml docker/docker-compose.dev.yml
|
||||
traefik: docker/docker-compose.yml docker/docker-compose.traefik.yml
|
||||
`
|
||||
|
Reference in New Issue
Block a user