func (m *BuildClient) UpdateCodeRevision(model_id string, request *CodeRevisionLong) (CodeRevisionLong, error) { var url bytes.Buffer resp := new(CodeRevisionResponse) url.WriteString(EI_URL) url.WriteString(MODELS_ENDPOINT) url.WriteString("/") url.WriteString(model_id) url.WriteString("/") url.WriteString(MODELS_REVISIONS_ENDPOINT) req_string, err := json.Marshal(request) logging.Debug("Request String for upload: %s", req_string) full_resp, err := m._complete_request("POST", url.String(), req_string) if err != nil { logging.Debug("Failed to update code revisions: %s", err.Error()) return resp.Revisions, err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from code revision update.. %s", err.Error()) return resp.Revisions, err } if resp.Success == false { return resp.Revisions, errors.New("Error When retriveing Code Revisions") } return resp.Revisions, nil }
func main() { logging_int := flag.Int("l", int(logging.LOG_INFO), "Levels 0-4 0 == None, 4 == Debug") log_colors_flag := flag.Bool("log_color", false, "-log_color enables log coloring(mingw/linux only)") settings_file := flag.String("settings", DEFAULT_PROJECT_FILE, "Set the settings file to a non standard file...") flag.Parse() err, log_value := logging.IntToLogLevel(*logging_int) if err == nil { logging.SetLoggingLevel(log_value) } else { PrintHelp() os.Exit(1) return } logging.SetColorEnabled(*log_colors_flag) args := flag.Args() if len(args) < 1 { logging.Info("Need a subcommand") PrintHelp() os.Exit(1) return } c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { for { select { case <-c: os.Exit(0) } } }() logging.Debug("Using settings file: %s", *settings_file) projectSettings := ProcessSettings(*settings_file) logging.Debug("Settings found: %s", projectSettings) for _, cmd := range commands { if cmd.Name() == args[0] && cmd.Runnable() { cmd.Flag.Usage = func() { cmd.Usage() } for i, s := range args { logging.Debug("Left Args: %d:%s", i, s) } if cmd.CustomFlags { args = args[0:] } else if len(args) > 2 { cmd.Flag.Parse(args[0:]) args = cmd.Flag.Args() } cmd.settings = projectSettings cmd.Run(cmd, args) return } } }
func StartDeviceLogging(client *ei.BuildClient, device_id string) { logs, poll_url, err := client.GetDeviceLogs(device_id) if err != nil { logging.Fatal("Failed to get device logs %s", err.Error()) return } logging.Debug("Poll Url %s", poll_url) for _, entry := range logs { fmt.Printf("%s: %s: %s\n", entry.Timestamp, entry.Type, entry.Message) } for { logs, err = client.ContinueDeviceLogs(poll_url) if err != nil { if _, ok := err.(*ei.Timeout); ok == false { logging.Fatal("Failed to get device logs %s", err.Error()) return } else { logging.Debug("Long poll timed out...") } } else { for _, entry := range logs { fmt.Printf("%s: %s: %s\n", entry.Timestamp, entry.Type, entry.Message) } } } }
func (m *BuildClient) RestartModelDevices(model_id string) error { var url bytes.Buffer resp := new(ModelResponse) url.WriteString(EI_URL) url.WriteString(MODELS_ENDPOINT) url.WriteString("/") url.WriteString(model_id) url.WriteString("/") url.WriteString(MODELS_DEVICE_RESTART_ENDPOINT) full_resp, err := m._complete_request("POST", url.String(), nil) if err != nil { logging.Debug("An error happened during model restart, %s", err.Error()) return err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from model response.. %s", err.Error()) return err } if resp.Success == false { return errors.New("Error When retriveing Code Revisions") } return nil }
func (m *BuildClient) GetModel(model_id string) (*Model, error) { var url bytes.Buffer resp := new(ModelResponse) url.WriteString(EI_URL) url.WriteString(MODELS_ENDPOINT) url.WriteString("/") url.WriteString(model_id) full_resp, err := m._complete_request("GET", url.String(), nil) if err != nil { logging.Debug("An error happened during model get, %s", err.Error()) return &resp.Model, err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from get model response.. %s", err.Error()) return &resp.Model, err } if resp.Success == false { return &resp.Model, errors.New("Error when attempting to get model: " + resp.Error.MessageShort) } return &resp.Model, nil }
func (m *BuildClient) _complete_request(method string, url string, data []byte) ([]byte, error) { var req *http.Request if data != nil { req, _ = http.NewRequest(method, url, bytes.NewBuffer(data)) } else { req, _ = http.NewRequest(method, url, nil) } m.SetAuthHeader(req) req.Header.Set("Content-Type", "application/json") resp, err := m.http_client.Do(req) if resp.StatusCode == http.StatusGatewayTimeout { return nil, new(Timeout) } if err == nil { dump, err := httputil.DumpResponse(resp, true) logging.Debug(string(dump)) full_response, err := ioutil.ReadAll(resp.Body) if err != nil { return full_response, err } return full_response, nil } else { return nil, err } }
func (m *BuildClient) GetDeviceLogs(device_id string) ([]DeviceLogEntry, string, error) { var url bytes.Buffer resp := new(DeviceLogResponse) url.WriteString(EI_URL) url.WriteString(DEVICES_ENDPOINT) url.WriteString("/") url.WriteString(device_id) url.WriteString("/") url.WriteString(DEVICES_LOG_ENDPOINT) full_resp, err := m._complete_request("GET", url.String(), nil) if err != nil { logging.Debug("Failed to get device logs: %s", err.Error()) return resp.Logs, "", err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from device logs.. %s", err.Error()) return resp.Logs, "", err } if resp.Success == false { return resp.Logs, "", errors.New("Error When retriveing device logs") } return resp.Logs, resp.PollUrl, nil }
func TestMe(cmd *Command, args []string) { logging.Info("Attempting to find test scripts...") test_files, err := FindTestFiles() if err != nil { os.Exit(1) } for _, input_file := range test_files { split_file := strings.Split(input_file, ".") // Remove test, and add .o split_file = append(split_file[:len(split_file)-2], split_file[len(split_file)-1], "o") output_file := strings.Join(split_file, ".") logging.Info("Processing target: %s as output: %s", input_file, output_file) err = PreProcessFile(output_file, input_file, cmd.settings.LibraryDirs) if err != nil { logging.Warn("Could not processes output file %s, got error: ", output_file, err.Error()) continue } logging.Info("Running target: %s", output_file) err = ExecuteSqrl(output_file) if err != nil { logging.Warn("Could not run output_file %s, got error: ", output_file, err.Error()) continue } } logging.Debug("All Files Found: %s", test_files) logging.Info("Script finished") }
func (m *BuildClient) RestartDevice(device_id string) error { var url bytes.Buffer resp := new(DeviceResponse) url.WriteString(EI_URL) url.WriteString(DEVICES_ENDPOINT) url.WriteString("/") url.WriteString(device_id) url.WriteString("/") url.WriteString(DEVICES_RESTART_ENDPOINT) full_resp, err := m._complete_request("POST", url.String(), nil) if err != nil { logging.Debug("Failed to get device list: %s", err.Error()) return err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from code revision update.. %s", err.Error()) return err } if resp.Success == false { return errors.New("Error When retriveing Code Revisions") } return nil }
func (m *BuildClient) UpdateDevice(new_device *Device, device_id string) (Device, error) { var url bytes.Buffer resp := new(DeviceResponse) url.WriteString(EI_URL) url.WriteString(DEVICES_ENDPOINT) url.WriteString("/") url.WriteString(device_id) req_bytes, err := json.Marshal(new_device) full_resp, err := m._complete_request("PUT", url.String(), req_bytes) if err != nil { logging.Debug("Failed to update device: %s", err.Error()) return resp.Device, err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from device update.. %s", err.Error()) return resp.Device, err } if resp.Success == false { return resp.Device, errors.New("Error when updating device") } return resp.Device, nil }
func (m *BuildClient) GetCodeRevision(model_id string, build_num string) (CodeRevisionLong, error) { var url bytes.Buffer resp := new(CodeRevisionResponse) url.WriteString(EI_URL) url.WriteString(MODELS_ENDPOINT) url.WriteString("/") url.WriteString(model_id) url.WriteString("/") url.WriteString(MODELS_REVISIONS_ENDPOINT) url.WriteString("/") url.WriteString(build_num) full_resp, err := m._complete_request("GET", url.String(), nil) if err != nil { logging.Debug("Failed to get code revisions: %s", err.Error()) return resp.Revisions, err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from code revision.. %s", err.Error()) return resp.Revisions, err } if resp.Success == false { return resp.Revisions, errors.New("Error When retriveing Code Revisions") } return resp.Revisions, nil }
func PreProcessFile(outputFile string, inputFile string, libraryDirs []string) error { if _, err := os.Stat(inputFile); err != nil { logging.Fatal("Did not find input file %s", inputFile) return errors.New("Input file not found") } gppCmdName := PREPROCESSOR_CMD_BIN gppArgs := []string{"-o", outputFile, inputFile} var s []string for _, dir := range libraryDirs { s = []string{"-I", dir} gppArgs = append(gppArgs, strings.Join(s, "")) } gppArgs = append(gppArgs, "-C") logging.Debug("gppArgs: %s", gppArgs) gppCmd := exec.Command(gppCmdName, gppArgs...) cmdReader, err := gppCmd.StdoutPipe() if err != nil { logging.Fatal("Failed to get stdout pipe Error: %s\n", err.Error()) os.Exit(1) } cmdErrorReader, err := gppCmd.StderrPipe() if err != nil { logging.Fatal("Failed to get stderr pipe Error: %s\n", err.Error()) os.Exit(1) } scanner := bufio.NewScanner(cmdReader) go func() { for scanner.Scan() { logging.Info("%s", scanner.Text()) } }() errScanner := bufio.NewScanner(cmdErrorReader) go func() { for errScanner.Scan() { logging.Warn("%s", errScanner.Text()) } }() err = gppCmd.Start() if err != nil { logging.Fatal("Error starting gpp", err) os.Exit(1) } err = gppCmd.Wait() if err != nil { logging.Fatal("Error waiting for gpp", err) os.Exit(1) } return nil }
func DeviceSubCommand(cmd *Command, args []string) { logging.Debug("In Device") for i, s := range args { logging.Debug("%d:%s", i, s) } client, err := CreateClient(cmd.settings.ApiKeyFile) if err != nil { os.Exit(1) return } if len(args) < 2 { PrintModelHelp() os.Exit(1) } if args[1] == "list" { ListDevices(client) } else if args[1] == "log" { if len(args) < 3 { fmt.Printf("Usage: device log <device_id>\n") PrintDeviceHelp() } else { logging.Debug("Attempting to start logging") StartDeviceLogging(client, args[2]) } } else if args[1] == "assign" { if len(args) < 4 { fmt.Printf("Usage: device assign <device_id> <model_id>\n") PrintDeviceHelp() } else { logging.Debug("Attemptting to assign device to model") AssignDeviceToModel(client, args[2], args[3]) } } else { logging.Debug("Showing help") PrintDeviceHelp() os.Exit(1) } }
func (m *BuildClient) CreateModel(new_model *Model) (*Model, error) { var url bytes.Buffer resp := new(ModelResponse) url.WriteString(EI_URL) url.WriteString(MODELS_ENDPOINT) req_string, err := json.Marshal(new_model) logging.Debug("Request String for upload: %s", req_string) full_resp, err := m._complete_request("POST", url.String(), req_string) if err != nil { logging.Debug("An error happened during model creation, %s", err.Error()) return &resp.Model, err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from model response.. %s", err.Error()) return &resp.Model, err } return &resp.Model, nil }
func (m *BuildClient) ListModels() (*ModelList, error) { list := new(ModelList) full_resp, err := m._complete_request("GET", Concat(EI_URL, "models"), nil) if err != nil { logging.Debug("An error happened during model get, %s", err.Error()) return list, err } if err := json.Unmarshal(full_resp, list); err != nil { logging.Warn("Failed to unmarshal data from models.. %s", err.Error()) return list, err } return list, nil }
func ModelSubCommand(cmd *Command, args []string) { logging.Debug("In model") // TODO(sean) Break this out to a helpers area for the ei stuff client, err := CreateClient(cmd.settings.ApiKeyFile) if err != nil { os.Exit(1) return } if len(args) < 2 { logging.Debug(cmd.settings.ModelKey) if cmd.settings.ModelKey != "" { model, err := client.GetModel(cmd.settings.ModelKey) if err != nil { logging.Fatal("Failed to get model id: %s, Got Error: %s", cmd.settings.ModelKey, err.Error()) os.Exit(1) return } fmt.Printf("Current Model: %s\n", model.Name) } else { fmt.Printf("No Model set in settings.wrench\n") } PrintModelHelp() os.Exit(1) } if args[1] == "list" { ListModels(client) } else { PrintModelHelp() os.Exit(1) } }
func AssignDeviceToModel(client *ei.BuildClient, device_id string, model_id string) { device, err := client.GetDevice(device_id) if err != nil { logging.Fatal("Failed to retrieve current model for model id: %s", model_id) return } // Don't do anything if its already the same if device.ModelId == model_id { return } device.ModelId = model_id updated_device, err := client.UpdateDevice(&device, device_id) logging.Debug("Model and new Devices: %s", updated_device) }
func (m *BuildClient) ContinueDeviceLogs(poll_url string) ([]DeviceLogEntry, error) { var url bytes.Buffer resp := new(DeviceLogResponse) url.WriteString(BASE_EI_URL) url.WriteString(poll_url) full_resp, err := m._complete_request("GET", url.String(), nil) if err != nil { logging.Debug("Failed to get device logs: %s", err.Error()) return resp.Logs, err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from device logs.. %s", err.Error()) return resp.Logs, err } if resp.Success == false { return resp.Logs, errors.New("Error when retriveing device logs") } return resp.Logs, nil }
func FindTestFiles() ([]string, error) { var files []string err := filepath.Walk("./", func(path string, info os.FileInfo, err error) error { if info.IsDir() { location := path + "\\" + TEST_FILE_PATTERN matches, err := filepath.Glob(location) if err != nil { return err } if len(matches) > 0 { logging.Debug("Found these files: %s", matches) files = append(files, matches...) } } return nil }) if err != nil { return nil, err } return files, nil }
func (m *BuildClient) DeleteDevice(device_id string) error { var url bytes.Buffer resp := new(DeviceResponse) url.WriteString(EI_URL) url.WriteString(DEVICES_ENDPOINT) url.WriteString("/") url.WriteString(device_id) full_resp, err := m._complete_request("DELETE", url.String(), nil) if err != nil { logging.Debug("Failed to delete device: %s", err.Error()) return err } if err := json.Unmarshal(full_resp, resp); err != nil { logging.Warn("Failed to unmarshal data from device deletion.. %s", err.Error()) return err } if resp.Success == false { return errors.New("Error when updating device") } return nil }
func main() { logging_int := flag.Int("l", int(logging.LOG_INFO), "Levels 0-4 0 == None, 4 == Debug") log_colors_flag := flag.Bool("log_color", false, "-log_color enables log coloring(mingw/linux only)") settings_file := flag.String("settings", DEFAULT_PROJECT_FILE, "Set the settings file to a non standard file...") gen_settings_flag := flag.Bool("g", false, "-g generates a default settings file if one is not found") flag.Parse() err, log_value := logging.IntToLogLevel(*logging_int) if err == nil { logging.SetLoggingLevel(log_value) } else { PrintHelp() os.Exit(1) return } logging.SetColorEnabled(*log_colors_flag) args := flag.Args() if len(args) < 1 { logging.Info("Need a subcommand") PrintHelp() os.Exit(1) return } c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { for { select { case <-c: os.Exit(0) } } }() var projectSettings ProjectSettings logging.Debug("Using settings file: %s", *settings_file) if _, err := os.Stat(*settings_file); err != nil { logging.Info("Did not find the settings file %s", *settings_file) if *settings_file != DEFAULT_PROJECT_FILE { logging.Fatal("Could not load non default settings file...") os.Exit(1) } if *gen_settings_flag { projectSettings, err = GenerateDefaultSettingsFile(*settings_file) if err != nil { os.Exit(1) } } else { os.Exit(1) } } else { projectSettings, err = LoadSettings(*settings_file) if err != nil { logging.Info("Failed to load settings file: %s", *settings_file) os.Exit(1) } } logging.Debug("Settings found: %s", projectSettings) for _, cmd := range commands { if cmd.Name() == args[0] && cmd.Runnable() { cmd.Flag.Usage = func() { cmd.Usage() } for i, s := range args { logging.Debug("Left Args: %d:%s", i, s) } if cmd.CustomFlags { args = args[0:] } else if len(args) > 2 { cmd.Flag.Parse(args[0:]) args = cmd.Flag.Args() } cmd.settings = projectSettings cmd.Run(cmd, args) return } } }