func Rollback(rollback int, deploy_config setup.DeployConfiguration, commands setup.CommandConfig) { /* remove the current directory, and switch back to version current - number */ connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath) if err != nil { log.Panic(err) } sftp, err := sftp.NewClient(connection) if err != nil { log.Fatal(err) } defer sftp.Close() var build_list []string w, err := sftp.ReadDir(deploy_config.TargetDirectory) for _, folder := range w { if folder.Name() == "current" || !folder.IsDir() { continue } build_list = append(build_list, folder.Name()) } sort.Sort(ByDate(build_list)) rollback_version := build_list[len(build_list)-rollback] //list is ordered old to new, so we want to index in relation to the end of the list. //Create a symbolic link to the new directory. cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current") ExecuteCmd(connection, cmd, nil, nil) cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+rollback_version, deploy_config.TargetDirectory+"/current") ExecuteCmd(connection, cmd, nil, nil) }
/* Create a new version, remove oldest if there are more than *Rollback return the new version number so that the deploy knows where to place shit. */ func create_version(deploy_config setup.DeployConfiguration) string { connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath) if err != nil { log.Panic(err) } sftp, err := sftp.NewClient(connection) if err != nil { log.Fatal(err) } defer sftp.Close() new_version := time.Now().Format("20060102150405") var build_list []string w, err := sftp.ReadDir(deploy_config.TargetDirectory) for _, folder := range w { if folder.Name() == "current" || !folder.IsDir() { continue } build_list = append(build_list, folder.Name()) } sort.Sort(ByDate(build_list)) //Remove oldest in build_list if len(build_list)+1 > deploy_config.Rollbacks+1 { remove_list := build_list[:len(build_list)-deploy_config.Rollbacks] //remove any old that exceeds Rollbaks, which are in the start of the list for _, version := range remove_list { log.Printf("Removing version: %s\n", deploy_config.TargetDirectory+"/"+version) //err :=sftp.Remove(deploy_config.TargetDirectory + "/" + version) cmd := fmt.Sprintf("rm -r %s", deploy_config.TargetDirectory+"/"+version) err := ExecuteCmd(connection, cmd, nil, nil) if err != nil { log.Println(err) } } } log.Printf("Creating folder: %s\n", deploy_config.TargetDirectory+"/"+new_version) sftp.Mkdir(deploy_config.TargetDirectory + "/" + new_version) //Create a symbolic link to the new directory. cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current") //log.Printf("Removing old symlink: %s", cmd) ExecuteCmd(connection, cmd, nil, nil) cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+new_version, deploy_config.TargetDirectory+"/current") //log.Printf("Creating symlink: %s", cmd) ExecuteCmd(connection, cmd, nil, nil) return new_version }