func machineID() (string, error) { if fileutil.Exists(systemDMachineIDPath) { return fileutil.ReadAllText(systemDMachineIDPath) } else if fileutil.Exists(upstartMachineIDPath) { return fileutil.ReadAllText(upstartMachineIDPath) } else { return "", fmt.Errorf("unable to fetch machine-id") } }
// Execute executes a list of shell commands in the given working directory. // The orchestration directory specifies where to create the script file and where // to save stdout and stderr. The orchestration directory will be created if it doesn't exist. // Returns readers for the standard output and standard error streams and a set of errors. // The errors need not be fatal - the output streams may still have data // even though some errors are reported. For example, if the command got killed while executing, // the streams will have whatever data was printed up to the kill point, and the errors will // indicate that the process got terminated. func (sh ShellCommandExecuter) Execute( log log.T, workingDir string, stdoutFilePath string, stderrFilePath string, cancelFlag task.CancelFlag, executionTimeout int, commandName string, commandArguments []string, ) (stdout io.Reader, stderr io.Reader, exitCode int, errs []error) { var err error exitCode, err = runCommandOutputToFiles(log, cancelFlag, workingDir, stdoutFilePath, stderrFilePath, executionTimeout, commandName, commandArguments) if err != nil { errs = append(errs, err) } emptyReader := bytes.NewReader([]byte{}) // create reader from stdout, if it exist, otherwise use empty reader if fileutil.Exists(stdoutFilePath) { stdout, err = os.Open(stdoutFilePath) if err != nil { // some unexpected error (file should exist) errs = append(errs, err) } } else { stdout = emptyReader } // create reader from stderr, if it exist, otherwise use empty reader if fileutil.Exists(stderrFilePath) { stderr, err = os.Open(stderrFilePath) if err != nil { // some unexpected error (file should exist) errs = append(errs, err) } } else { stderr = emptyReader } return }
// setCmdState persists given commandState func setCmdState(log log.T, commandState message.CommandState, absoluteFileName, locationFolder string) { content, err := jsonutil.Marshal(commandState) if err != nil { log.Errorf("encountered error with message %v while marshalling %v to string", err, commandState) } else { if fileutil.Exists(absoluteFileName) { log.Debugf("overwriting contents of %v", absoluteFileName) } log.Tracef("persisting interim state %v in file %v", jsonutil.Indent(content), absoluteFileName) if s, err := fileutil.WriteIntoFileWithPermissions(absoluteFileName, jsonutil.Indent(content), os.FileMode(int(appconfig.ReadWriteAccess))); s && err == nil { log.Debugf("successfully persisted interim state in %v", locationFolder) } else { log.Debugf("persisting interim state in %v failed with error %v", locationFolder, err) } } }
// Store function updates the shared credentials with the specified values: // * If the shared credentials file does not exist, it will be created. Any parent directories will also be created. // * If the section to update does not exist, it will be created. func Store(accessKeyID, secretAccessKey, sessionToken, profile string) error { if profile == "" { profile = defaultProfile } credPath, err := filename() if err != nil { return err } // check if file exists, if not create it if !fileutil.Exists(credPath) { err := createFile(credPath) if err != nil { return awserr.New("SharedCredentialsStore", "failed to create shared credentials file", err) } } config, err := ini.Load(credPath) if err != nil { return awserr.New("SharedCredentialsStore", "failed to load shared credentials file", err) } iniProfile := config.Section(profile) if err != nil { return awserr.New("SharedCredentialsStore", "failed to get profile", err) } // Default to empty string if not found iniProfile.Key(awsAccessKeyID).SetValue(accessKeyID) iniProfile.Key(awsSecretAccessKey).SetValue(secretAccessKey) iniProfile.Key(awsSessionToken).SetValue(sessionToken) err = config.SaveTo(credPath) if err != nil { return awserr.New("SharedCredentialsStore", "failed to save profile", err) } return nil }
// PersistData stores the given object in the file-system in pretty Json indented format // This will override the contents of an already existing file func PersistData(log log.T, commandID, instanceID, locationFolder string, object interface{}) { lockDocument(commandID) defer unlockDocument(commandID) absoluteFileName := getCmdStateFileName(commandID, instanceID, locationFolder) content, err := jsonutil.Marshal(object) if err != nil { log.Errorf("encountered error with message %v while marshalling %v to string", err, object) } else { if fileutil.Exists(absoluteFileName) { log.Debugf("overwriting contents of %v", absoluteFileName) } log.Tracef("persisting interim state %v in file %v", jsonutil.Indent(content), absoluteFileName) if s, err := fileutil.WriteIntoFileWithPermissions(absoluteFileName, jsonutil.Indent(content), os.FileMode(int(appconfig.ReadWriteAccess))); s && err == nil { log.Debugf("successfully persisted interim state in %v", locationFolder) } else { log.Debugf("persisting interim state in %v failed with error %v", locationFolder, err) } } }
func (fsvFileSystem) Exists(path string) bool { return fileutil.Exists(path) }
// httpDownload attempts to download a file via http/s call func httpDownload(log log.T, fileURL string, destFile string) (output DownloadOutput, err error) { log.Debugf("attempting to download as http/https download %v", destFile) eTagFile := destFile + ".etag" var check http.Client var request *http.Request request, err = http.NewRequest("GET", fileURL, nil) if err != nil { return } if fileutil.Exists(destFile) == true && fileutil.Exists(eTagFile) == true { var existingETag string existingETag, err = fileutil.ReadAllText(eTagFile) request.Header.Add("If-None-Match", existingETag) } check = http.Client{ CheckRedirect: func(r *http.Request, via []*http.Request) error { r.URL.Opaque = r.URL.Path return nil }, } var resp *http.Response resp, err = check.Do(request) if err != nil { log.Debug("failed to download from http/https, ", err) fileutil.DeleteFile(destFile) fileutil.DeleteFile(eTagFile) return } if resp.StatusCode == http.StatusNotModified { log.Debugf("Unchanged file.") output.IsUpdated = false output.LocalFilePath = destFile return output, nil } else if resp.StatusCode != http.StatusOK { log.Debug("failed to download from http/https, ", err) fileutil.DeleteFile(destFile) fileutil.DeleteFile(eTagFile) err = fmt.Errorf("http request failed. status:%v statuscode:%v", resp.Status, resp.StatusCode) return } defer resp.Body.Close() eTagValue := resp.Header.Get("Etag") if eTagValue != "" { log.Debug("file eTagValue is ", eTagValue) err = fileutil.WriteAllText(eTagFile, eTagValue) if err != nil { log.Errorf("failed to write eTagfile %v, %v ", eTagFile, err) return } } _, err = FileCopy(log, destFile, resp.Body) if err == nil { output.LocalFilePath = destFile output.IsUpdated = true } else { log.Errorf("failed to write destFile %v, %v ", destFile, err) } return }
// s3Download attempts to download a file via the aws sdk. func s3Download(log log.T, amazonS3URL s3util.AmazonS3URL, destFile string) (output DownloadOutput, err error) { log.Debugf("attempting to download as s3 download %v", destFile) eTagFile := destFile + ".etag" config := &aws.Config{} var appConfig appconfig.SsmagentConfig appConfig, err = appconfig.Config(false) if err != nil { log.Error("failed to read appconfig.") } else { creds, err1 := appConfig.ProfileCredentials() if err1 != nil { config.Credentials = creds } } config.S3ForcePathStyle = aws.Bool(amazonS3URL.IsPathStyle) config.Region = aws.String(amazonS3URL.Region) params := &s3.GetObjectInput{ Bucket: aws.String(amazonS3URL.Bucket), Key: aws.String(amazonS3URL.Key), } if fileutil.Exists(destFile) == true && fileutil.Exists(eTagFile) == true { var existingETag string existingETag, err = fileutil.ReadAllText(eTagFile) if err != nil { log.Debugf("failed to read etag file %v, %v", eTagFile, err) return } params.IfNoneMatch = aws.String(existingETag) } s3client := s3.New(session.New(config)) req, resp := s3client.GetObjectRequest(params) err = req.Send() if err != nil { if req.HTTPResponse == nil || req.HTTPResponse.StatusCode != http.StatusNotModified { log.Debug("failed to download from s3, ", err) fileutil.DeleteFile(destFile) fileutil.DeleteFile(eTagFile) return } log.Debugf("Unchanged file.") output.IsUpdated = false output.LocalFilePath = destFile return output, nil } if *resp.ETag != "" { log.Debug("files etag is ", *resp.ETag) err = fileutil.WriteAllText(eTagFile, *resp.ETag) if err != nil { log.Errorf("failed to write eTagfile %v, %v ", eTagFile, err) return } } defer resp.Body.Close() _, err = FileCopy(log, destFile, resp.Body) if err == nil { output.LocalFilePath = destFile output.IsUpdated = true } else { log.Errorf("failed to write destFile %v, %v ", destFile, err) } return }
func setupTest(credPath string) { // check if file exists, if not create it if !fileutil.Exists(credPath) { fileutil.WriteAllText(credPath, "") } }
func getPlatformDetails(log log.T) (name string, version string, err error) { log.Debugf(gettingPlatformDetailsMessage) contents := "" var contentsBytes []byte name = notAvailableMessage version = notAvailableMessage if fileutil.Exists(systemReleaseCommand) { log.Debugf(fetchingDetailsMessage, systemReleaseCommand) contents, err = fileutil.ReadAllText(systemReleaseCommand) log.Debugf(commandOutputMessage, contents) if err != nil { log.Debugf(errorOccurredMessage, systemReleaseCommand, err) return } if strings.Contains(contents, "Amazon") { data := strings.Split(contents, "release") name = strings.TrimSpace(data[0]) version = strings.TrimSpace(data[1]) } else if strings.Contains(contents, "Red Hat") { data := strings.Split(contents, "release") name = strings.TrimSpace(data[0]) version = strings.TrimSpace(data[1]) } else if strings.Contains(contents, "CentOS") { data := strings.Split(contents, "release") name = strings.TrimSpace(data[0]) version = strings.TrimSpace(data[1]) } } else if fileutil.Exists(redhatReleaseCommand) { log.Debugf(fetchingDetailsMessage, redhatReleaseCommand) contents, err = fileutil.ReadAllText(redhatReleaseCommand) log.Debugf(commandOutputMessage, contents) if err != nil { log.Debugf(errorOccurredMessage, redhatReleaseCommand, err) return } if strings.Contains(contents, "Red Hat") { data := strings.Split(contents, "release") name = strings.TrimSpace(data[0]) versionData := strings.Split(data[1], "(") version = strings.TrimSpace(versionData[0]) } } else { log.Debugf(fetchingDetailsMessage, lsbReleaseCommand) // platform name if contentsBytes, err = exec.Command(lsbReleaseCommand, "-i").Output(); err != nil { log.Debugf(fetchingDetailsMessage, lsbReleaseCommand, err) return } name = strings.TrimSpace(string(contentsBytes)) log.Debugf(commandOutputMessage, name) name = strings.TrimSpace(string(contentsBytes)) name = strings.TrimLeft(name, "Distributor ID:") name = strings.TrimSpace(name) log.Debugf("platform name %v", name) // platform version if contentsBytes, err = exec.Command(lsbReleaseCommand, "-r").Output(); err != nil { log.Debugf(errorOccurredMessage, lsbReleaseCommand, err) return } version = strings.TrimSpace(string(contentsBytes)) log.Debugf(commandOutputMessage, version) version = strings.TrimLeft(version, "Release:") version = strings.TrimSpace(version) log.Debugf("platform version %v", version) } return }