func (operation *InfoOperation) Run(logger log.Log) bool { logger.Message("RUNNING INFO OPERATION") logger.Debug(log.VERBOSITY_DEBUG, "Run:Targets", operation.targets.TargetOrder()) for _, targetID := range operation.targets.TargetOrder() { target, targetExists := operation.targets.Target(targetID) node, hasNode := target.Node() _, hasInstances := target.Instances() if !targetExists { // this is strange logger.Warning("Internal target error, was told to use a target that doesn't exist") continue } nodeLogger := logger.MakeChild(targetID) if hasNode { nodeLogger.Message(targetID + " Information") node.Client().NodeInfo(nodeLogger) } else { nodeLogger.Message("No node [" + node.MachineName() + "]") } if hasInstances { node.Instances().Client().InstancesInfo(nodeLogger) } else { nodeLogger.Message("|-- No instances [" + node.MachineName() + "]") } } return true }
func (operation *InitGenerateOperation) Run(logger log.Log) bool { logger.Info("running init operation:" + operation.output) var writer io.Writer switch operation.output { case "logger": fallthrough case "": writer = logger default: if strings.HasSuffix(operation.output, ".") { operation.output = operation.output + operation.handler } if fileWriter, err := os.Create(operation.output); err == nil { operation.skip = append(operation.skip, operation.output) writer = io.Writer(fileWriter) defer fileWriter.Close() logger.Message("Opening file for init generation output: " + operation.output) } else { logger.Error("Could not open output file to write init to:" + operation.output) } } initialize.Init_Generate(logger.MakeChild("init-generate"), operation.handler, operation.root, operation.skip, operation.sizeLimit, writer) return true }
func (client *FSouza_InstanceClient) Attach(logger log.Log) bool { id := client.instance.MachineName() // build options for the docker attach operation options := docker.AttachToContainerOptions{ Container: id, InputStream: os.Stdin, OutputStream: os.Stdout, ErrorStream: logger, Logs: true, // Get container logs, sending it to OutputStream. Stream: true, // Stream the response? Stdin: true, // Attach to stdin, and use InputStream. Stdout: true, // Attach to stdout, and use OutputStream. Stderr: true, //Success chan struct{} RawTerminal: client.settings.Config.Tty, // Use raw terminal? Usually true when the container contains a TTY. } logger.Message("Attaching to instance container [" + id + "]") err := client.backend.AttachToContainer(options) if err != nil { logger.Error("Failed to attach to instance container [" + id + "] =>" + err.Error()) return false } else { logger.Message("Disconnected from instance container [" + id + "]") return true } }
func (tasks *InitTasks) Init_User_Run(logger log.Log, template string) bool { if template == "" { logger.Error("You have not provided a template name $/> coach init user {template}") return false } templatePath, ok := tasks.conf.Path("user-templates") if !ok { logger.Error("COACH has no user template path for the current user") return false } sourcePath := path.Join(templatePath, template) if _, err := os.Stat(sourcePath); err != nil { logger.Error("Invalid template path suggested for new project init : [" + template + "] expected path [" + sourcePath + "] => " + err.Error()) return false } logger.Message("Perfoming init operation from user template [" + template + "] : " + sourcePath) tasks.AddFileCopy(tasks.root, sourcePath) tasks.AddMessage("Copied coach template [" + template + "] to init project") tasks.AddFile(".coach/CREATEDFROM.md", `THIS PROJECT WAS CREATED FROM A User Template :`+template) return true }
func (client *FSouza_NodeClient) Destroy(logger log.Log, force bool) bool { // Get the image name image, tag := client.GetImageName() if tag != "" { image += ":" + tag } if !client.HasImage() { logger.Warning("Node has no image to destroy [" + image + "]") return false } options := docker.RemoveImageOptions{ Force: force, } // ask the docker client to remove the image err := client.backend.RemoveImageExtended(image, options) if err != nil { logger.Error("Node image removal failed [" + image + "] => " + err.Error()) return false } else { client.backend.Refresh(true, false) logger.Message("Node image was removed [" + image + "]") return true } }
func (tasks *InitTasks) Init_Git_Run(logger log.Log, source string) bool { if source == "" { logger.Error("You have not provided a git target $/> coach init git https://github.com/aleksijohansson/docker-drupal-coach") return false } url := source path := tasks.root cmd := exec.Command("git", "clone", "--progress", url, path) cmd.Stdin = os.Stdin cmd.Stdout = logger cmd.Stderr = logger err := cmd.Start() if err != nil { logger.Error("Failed to clone the remote repository [" + url + "] => " + err.Error()) return false } logger.Message("Clone remote repository to local project folder [" + url + "]") err = cmd.Wait() if err != nil { logger.Error("Failed to clone the remote repository [" + url + "] => " + err.Error()) return false } tasks.AddMessage("Cloned remote repository [" + url + "] to local project folder") tasks.AddFile(".coach/CREATEDFROM.md", `THIS PROJECT WAS CREATED FROM GIT`) return true }
func (client *FSouza_InstanceClient) Commit(logger log.Log, tag string, message string) bool { id := client.instance.MachineName() config := client.settings.Config repo := client.settings.Repository author := client.settings.Author if repo == "" { repo, _ = client.GetImageName() } options := docker.CommitContainerOptions{ Container: id, Repository: repo, Tag: tag, Run: &config, } if message != "" { options.Message = message } if author != "" { author = client.conf.Author } _, err := client.backend.CommitContainer(options) if err != nil { logger.Warning("Failed to commit container changes to an image [" + client.instance.Id() + ":" + id + "] : " + tag) return false } else { client.backend.Refresh(true, false) logger.Message("Committed container changes to an image [" + client.instance.Id() + ":" + id + "] : " + tag) return true } }
func (operation *StatusOperation) Run(logger log.Log) bool { logger.Message("RUNNING Status OPERATION") logger.Debug(log.VERBOSITY_DEBUG, "Run:Targets", operation.targets.TargetOrder()) for _, targetID := range operation.targets.TargetOrder() { target, targetExists := operation.targets.Target(targetID) node, hasNode := target.Node() instances, hasInstances := target.Instances() if !targetExists { // this is strange logger.Warning("Internal target error, was told to use a target that doesn't exist") continue } nodeLogger := logger.MakeChild(targetID) status := []string{} if hasNode { status = append(status, operation.NodeStatus(nodeLogger, node)...) } else { status = append(status, "No node for target") } if hasInstances { status = append(status, operation.InstancesStatus(nodeLogger, instances)...) } else { status = append(status, "No instances for target") } nodeLogger.Message("[" + strings.Join(status, "][") + "]") } return true }
func Init_Generate(logger log.Log, handler string, path string, skip []string, sizeLimit int64, output io.Writer) bool { logger.Message("GENERATING INIT") var generator Generator switch handler { case "test": generator = Generator(&TestInitGenerator{logger: logger, output: output}) case "yaml": generator = Generator(&YMLInitGenerator{logger: logger, output: output}) default: logger.Error("Unknown init generator (handler) " + handler) return false } iterator := GenerateIterator{ logger: logger, output: output, skip: skip, sizeLimit: sizeLimit, generator: generator, } if iterator.Generate(path) { logger.Message("FINISHED GENERATING YML INIT") return true } else { logger.Error("ERROR OCCURRED GENERATING YML INIT") return false } }
func (task *InitTaskFile) RunTask(logger log.Log) bool { if task.path == "" { return false } if task.MakeFile(logger, task.path, task.contents) { logger.Message("--> Created file : " + task.path) return true } else { logger.Warning("--> Failed to create file : " + task.path) return false } }
func (task *InitTaskRemoteFile) RunTask(logger log.Log) bool { if task.path == "" || task.root == "" || task.url == "" { return false } if task.CopyRemoteFile(logger, task.path, task.url) { logger.Message("--> Copied remote file : " + task.url + " -> " + task.path) return true } else { logger.Warning("--> Failed to copy remote file : " + task.url) return false } }
func (client *FSouza_InstanceClient) Unpause(logger log.Log) bool { id := client.instance.MachineName() err := client.backend.UnpauseContainer(id) if err != nil { logger.Error("Failed to unpause Instance [" + client.instance.Id() + "] Container [" + id + "] =>" + err.Error()) return false } else { client.backend.Refresh(false, true) logger.Message("Unpaused Instance [" + client.instance.Id() + "] Container [" + id + "]") return true } }
func (task *InitTaskFileCopy) RunTask(logger log.Log) bool { if task.path == "" || task.root == "" || task.source == "" { return false } if task.CopyFileRecursive(logger, task.path, task.source) { logger.Message("--> Copied file : " + task.source + " -> " + task.path) return true } else { logger.Warning("--> Failed to copy file : " + task.source + " -> " + task.path) return false } }
func (client *FSouza_InstanceClient) Stop(logger log.Log, force bool, timeout uint) bool { id := client.instance.MachineName() err := client.backend.StopContainer(id, timeout) if err != nil { logger.Error("Failed to stop node container [" + id + "] => " + err.Error()) return false } else { client.backend.Refresh(false, true) logger.Message("Node instance stopped [" + id + "]") return true } }
func (client *FSouza_NodeClient) Build(logger log.Log, force bool) bool { image, tag := client.GetImageName() if client.settings.BuildPath == "" { logger.Warning("Node image [" + image + ":" + tag + "] not built as an empty path was provided. You must point Build: to a path inside .coach") return false } if !force && client.HasImage() { logger.Info("Node image [" + image + ":" + tag + "] not built as an image already exists. You can force this operation to build this image") return false } // determine an absolute buildPath to the build, for Docker to use. buildPath := "" for _, confBuildPath := range client.conf.Paths.GetConfSubPaths(client.settings.BuildPath) { logger.Debug(log.VERBOSITY_DEBUG_STAAAP, "Looking for Build: "+confBuildPath) if _, err := os.Stat(confBuildPath); !os.IsNotExist(err) { buildPath = confBuildPath break } } if buildPath == "" { logger.Error("No matching build path could be found [" + client.settings.BuildPath + "]") } options := docker.BuildImageOptions{ Name: image + ":" + tag, ContextDir: buildPath, RmTmpContainer: true, OutputStream: logger, } logger.Info("Building node image [" + image + ":" + tag + "] From build path [" + buildPath + "]") // ask the docker client to build the image err := client.backend.BuildImage(options) if err != nil { logger.Error("Node build failed [" + client.node.MachineName() + "] in build path [" + buildPath + "] => " + err.Error()) return false } else { client.backend.Refresh(true, false) logger.Message("Node succesfully built image [" + image + ":" + tag + "] From path [" + buildPath + "]") return true } }
func (task *InitTaskFileStringReplace) RunTask(logger log.Log) bool { if task.path == "" || task.root == "" || task.oldString == "" || task.newString == "" { return false } if task.replaceCount == 0 { task.replaceCount = -1 } if task.FileStringReplace(logger, task.path, task.oldString, task.newString, task.replaceCount) { logger.Message("--> performed string replace on file : " + task.path) return true } else { logger.Warning("--> Failed to perform string replace on file : " + task.path) return false } }
func (client *FSouza_InstanceClient) Start(logger log.Log, force bool) bool { // Convert the node data into docker data (transform node keys to container IDs for things like Links & VolumesFrom) id := client.instance.MachineName() Host := client.settings.Host // ask the docker client to start the instance container err := client.backend.StartContainer(id, &Host) if err != nil { logger.Error("Failed to start node container [" + id + "] => " + err.Error()) return false } else { logger.Message("Node instance started [" + id + "]") client.backend.Refresh(false, true) return true } }
func (client *FSouza_InstanceClient) Remove(logger log.Log, force bool) bool { name := client.instance.MachineName() options := docker.RemoveContainerOptions{ ID: name, } // ask the docker client to remove the instance container err := client.backend.RemoveContainer(options) if err != nil { logger.Error("Failed to remove instance container [" + name + "] =>" + err.Error()) return false } else { client.backend.Refresh(false, true) logger.Message("Removed instance container [" + name + "] ") return true } return false }
func (task *InitTaskGitClone) RunTask(logger log.Log) bool { if task.root == "" || task.url == "" { logger.Error("EMPTY ROOT PASSED TO GIT: " + task.root) return false } destinationPath := task.path url := task.url if !task.MakeDir(logger, destinationPath, false) { return false } destinationAbsPath, ok := task.absolutePath(destinationPath, true) if !ok { logger.Warning("Invalid copy destination path: " + destinationPath) return false } cmd := exec.Command("git", "clone", "--progress", url, destinationAbsPath) cmd.Stderr = logger err := cmd.Start() if err != nil { logger.Error("Failed to clone the remote repository [" + url + "] => " + err.Error()) return false } err = cmd.Wait() if err != nil { logger.Error("Failed to clone the remote repository [" + url + "] => " + err.Error()) return false } logger.Message("Cloned remote repository [" + url + "] to local path " + destinationPath) return true }
func (client *FSouza_NodeClient) Pull(logger log.Log, force bool) bool { image, tag := client.GetImageName() actionCacheTag := "pull:" + image + ":" + tag if _, ok := actionCache[actionCacheTag]; ok { logger.Message("Node image [" + image + ":" + tag + "] was just pulled, so not pulling it again.") return true } if !force && client.HasImage() { logger.Info("Node already has an image [" + image + ":" + tag + "], so not pulling it again. You can force this operation if you want to pull this image.") return false } options := docker.PullImageOptions{ Repository: image, OutputStream: logger, RawJSONStream: false, } if tag != "" { options.Tag = tag } var auth docker.AuthConfiguration // var ok bool //options.Registry = "https://index.docker.io/v1/" // auths, _ := docker.NewAuthConfigurationsFromDockerCfg() // if auth, ok = auths.Configs[registry]; ok { // options.Registry = registry // } else { // node.log.Warning("You have no local login credentials for any repo. Defaulting to no login.") auth = docker.AuthConfiguration{} options.Registry = "https://index.docker.io/v1/" // } logger.Message("Pulling node image [" + image + ":" + tag + "] from server [" + options.Registry + "] using auth [" + auth.Username + "] : " + image + ":" + tag) logger.Debug(log.VERBOSITY_DEBUG_LOTS, "AUTH USED: ", map[string]string{"Username": auth.Username, "Password": auth.Password, "Email": auth.Email, "ServerAdddress": auth.ServerAddress}) // ask the docker client to build the image err := client.backend.PullImage(options, auth) if err != nil { logger.Error("Node image not pulled : " + image + " => " + err.Error()) actionCache[actionCacheTag] = false return false } else { client.backend.Refresh(true, false) logger.Message("Node image pulled: " + image + ":" + tag) actionCache[actionCacheTag] = false return true } }
func (client *FSouza_InstanceClient) Create(logger log.Log, overrideCmd []string, force bool) bool { instance := client.instance if !force && client.HasContainer() { logger.Info("[" + instance.MachineName() + "]: Skipping node instance, which already has a container") return false } /** * Transform node data, into a format that can be used * for the actual Docker call. This involves transforming * the node keys into docker container ids, for things like * the name, Links, VolumesFrom etc */ name := instance.MachineName() Config := client.settings.Config Host := client.settings.Host image, tag := client.GetImageName() if tag != "" && tag != "latest" { image += ":" + tag } Config.Image = image if len(overrideCmd) > 0 { Config.Cmd = overrideCmd } // ask the docker client to create a container for this instance options := docker.CreateContainerOptions{ Name: name, Config: &Config, HostConfig: &Host, } container, err := client.backend.CreateContainer(options) client.backend.Refresh(false, true) if err != nil { logger.Debug(log.VERBOSITY_DEBUG, "CREATE FAIL CONTAINERS: ", err) /** * There is a weird bug with the library, where sometimes it * reports a missing image error, and yet it still creates the * container. It is not clear if this failure occurs in the * remote API, or in the dockerclient library. */ client.backend.Refresh(false, true) if err.Error() == "no such image" && client.HasContainer() { logger.Message("Created instance container [" + name + " FROM " + Config.Image + "] => " + container.ID[:12]) logger.Warning("Docker created the container, but reported an error due to a 'missing image'. This is a known bug, that can be ignored") return true } logger.Error("Failed to create instance container [" + name + " FROM " + Config.Image + "] => " + err.Error()) return false } else { client.backend.Refresh(false, true) logger.Message("Created instance container [" + name + "] => " + container.ID[:12]) return true } }
func (client *FSouza_InstancesClient) InstancesInfo(logger log.Log) { instances := client.instances if instances.MachineName() == INSTANCES_NULL_MACHINENAME { } else if len(instances.InstancesOrder()) == 0 { logger.Message("|-= no containers") } else { logger.Message("|-> instances (containers) MachineName:" + instances.MachineName()) w := new(tabwriter.Writer) w.Init(logger, 8, 12, 2, ' ', 0) row := []string{ "|=", "Name", "Container", "Default", "Created", "Running", "Status", "ID", "Created", "Names", } w.Write([]byte(strings.Join(row, "\t") + "\n")) for _, name := range instances.InstancesOrder() { instance, _ := instances.Instance(name) machineName := instance.MachineName() instanceClient := instance.Client() row := []string{ "|-", name, machineName, } if instance.IsDefault() { row = append(row, "yes") } else { row = append(row, "no") } if instanceClient.HasContainer() { row = append(row, "yes") } else { row = append(row, "no") } if instanceClient.IsRunning() { row = append(row, "yes") } else { row = append(row, "no") } containers, _ := client.backend.MatchContainers(machineName, false) for _, container := range containers { row = append(row, container.Status, container.ID[:12], strconv.FormatInt(int64(container.Created), 10), strings.Join(container.Names, ", "), ) break } w.Write([]byte(strings.Join(row, "\t") + "\n")) } w.Flush() } }
func (operation *InitOperation) Run(logger log.Log) bool { logger.Info("running init operation") var err error var ok bool var targetPath, coachPath string targetPath = operation.root if targetPath == "" { targetPath, ok = operation.conf.Path("project-root") if !ok || targetPath == "" { targetPath, err = os.Getwd() if err != nil { logger.Error("No path suggested for new project init") return false } } } _, err = os.Stat(targetPath) if err != nil { logger.Error("Invalid path suggested for new project init : [" + targetPath + "] => " + err.Error()) return false } coachPath, _ = operation.conf.Paths.Path("coach-root") logger.Message("Preparing INIT operation [" + operation.handler + ":" + operation.source + "] in path : " + targetPath) _, err = os.Stat(coachPath) if !operation.force && err == nil { logger.Error("cannot create new project folder, as one already exists") return false } logger = logger.MakeChild(strings.ToUpper(operation.handler)) tasks := initialize.InitTasks{} tasks.Init(logger.MakeChild("TASKS"), operation.conf, targetPath) ok = true switch operation.handler { case "user": ok = tasks.Init_User_Run(logger, operation.source) case "demo": ok = tasks.Init_Demo_Run(logger, operation.source) case "git": ok = tasks.Init_Git_Run(logger, operation.source) case "yaml": ok = tasks.Init_Yaml_Run(logger, operation.source) case "default": ok = tasks.Init_Default_Run(logger, operation.source) default: logger.Error("Unknown init handler " + operation.handler) ok = false } if ok { logger.Info("Running init tasks") tasks.RunTasks(logger) return true } else { logger.Warning("No init tasks were defined.") return false } }
func (task *InitTaskMessage) RunTask(logger log.Log) bool { logger.Message(task.message) return true }