func generateTempSshConfig(conn apis.Connection) (string, error) { var buffer bytes.Buffer host, exists := conn.Config["Host"].(string) if !exists { return "", fmt.Errorf("Host name property does not exist in SSH config") } buffer.WriteString("Host ") buffer.WriteString(host) buffer.WriteString("\n") for key, value := range conn.Config { if key != "Host" { buffer.WriteString(fmt.Sprintf(" %s %s\n", key, value)) } } util.CreateDirectoryIfNotExists(path.Join(apis.DefaultAnvilFolder, DefaultTestinfraSubfolder)) tempSshFile, err := ioutil.TempFile(path.Join(apis.DefaultAnvilFolder, DefaultTestinfraSubfolder), sshConfigPrexix) if err != nil { return "", err } _, err = tempSshFile.Write(buffer.Bytes()) if err != nil { return "", err } tempSshFile.Close() return tempSshFile.Name(), nil }
func (t *TestCase) PersistState() { out, err := yaml.Marshal(t) if err != nil { log.Fatalf("Can't marshal state: %v", err) } err = util.CreateDirectoryIfNotExists(DefaultStateFolder) if err != nil { log.Panicf("Can't create state directory %s: %v", DefaultStateFolder, err) } statePath := path.Join(DefaultStateFolder, fmt.Sprintf("%s_%s.yml", DefaultStatePrefix, t.Name)) err = ioutil.WriteFile(statePath, out, 0655) if err != nil { log.Fatalf("Can' write state to %s: %v", statePath, err) } }
func generateInventory(inst apis.Instance) (string, error) { if inst.Connection.Type != apis.SSH { return "", fmt.Errorf("%s is not a supported connection type for the ansible provisioner", inst.Connection.Type) } var buffer bytes.Buffer hostname, exists := inst.Connection.Config["Host"].(string) if !exists { hostname = "default" } buffer.WriteString(hostname) buffer.WriteString(" ") for key, value := range inst.Connection.Config { ansibleKey, mapped := mappedSshParams[key] if mapped { buffer.WriteString(fmt.Sprintf("%s=%s ", ansibleKey, value)) } } buffer.WriteString("\n") err := util.CreateDirectoryIfNotExists(path.Join(apis.DefaultAnvilFolder, DefaultAnsibleSubfolder)) if err != nil { return "", err } tempFile, err := ioutil.TempFile(path.Join(apis.DefaultAnvilFolder, DefaultAnsibleSubfolder), "ansible_inventory") if err != nil { return "", err } tempFile.Write(buffer.Bytes()) if err := tempFile.Sync(); err != nil { return "", err } if err := tempFile.Close(); err != nil { return "", err } return tempFile.Name(), nil }
func getVagrant(instanceName string) *vagrant.Vagrant { workdir := path.Join(DefaultAnvilFolder, DefaultVargantSubfolder, instanceName) util.CreateDirectoryIfNotExists(workdir) return vagrant.NewVagrant(workdir) }