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 ExampleCreateUpdateDeleteModel() { logging.SetLoggingLevel(logging.LOG_DEBUG) logging.Info("Starting Test") client := NewBuildClient(API_KEY) new_model := new(Model) new_model.Name = "Wrench Test Model" model, err := client.CreateModel(new_model) if err != nil { logging.Fatal("Example Failed %s", err.Error()) return } new_model.Name = "Wrench Test Model Again" update_model, err := client.UpdateModel(model.Id, new_model) if err != nil { logging.Fatal("Example Failed %s", err.Error()) return } if update_model.Id != model.Id { logging.Fatal("Example Failed, Model Ids don't match") return } err = client.DeleteModel(model.Id) if err != nil { logging.Fatal("Example Failed %s", err.Error()) return } logging.Info("Succesfully created model: %s with Id: %s", model.Name, model.Id) logging.Info("Ending Test") }
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 LoadSettings(settings_file string) (ProjectSettings, error) { var settings ProjectSettings settings_data, err := ioutil.ReadFile(settings_file) if err != nil { logging.Fatal("Could not open settings file %s", settings_file) return settings, err } err = json.Unmarshal(settings_data, &settings) if err != nil { logging.Fatal("Couldn't parse settings file: %s", settings_file) return settings, err } return settings, nil }
func LoadSettings(settings_file string) ProjectSettings { settings_data, err := ioutil.ReadFile(settings_file) if err != nil { logging.Fatal("Could not open settings file %s", settings_file) os.Exit(1) } var settings ProjectSettings err = json.Unmarshal(settings_data, &settings) if err != nil { logging.Fatal("Couldn't parse settings file: %s", settings_file) os.Exit(1) } return settings }
func ProcessSettings(settings_file string) ProjectSettings { 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) } logging.Info("Generating default settings file...") var settings ProjectSettings settings.AgentFileInPath = DEFAULT_AGENT_IN_FILE settings.AgentFileOutPath = DEFAULT_AGENT_OUT_FILE settings.DeviceFileInPath = DEFAULT_DEVICE_IN_FILE settings.DeviceFileOutPath = DEFAULT_DEVICE_OUT_FILE settings.LibraryDirs = append(settings.LibraryDirs, DEFAULT_LIB_DIR) settings_data, err := json.Marshal(settings) if err != nil { logging.Warn("Failed to generate default settings json data") } else { err = ioutil.WriteFile(settings_file, settings_data, 777) if err != nil { logging.Warn("Failed to write new defaults...") } } return settings } return LoadSettings(settings_file) }
func CreateClient(key_file_path string) (*ei.BuildClient, error) { keyfile_data, err := ioutil.ReadFile(key_file_path) if err != nil { logging.Fatal("Could not open the keyfile: %s", key_file_path) return nil, err } keyfile := new(ApiKeyFile) err = json.Unmarshal(keyfile_data, keyfile) if err != nil { logging.Fatal("Could not parse keyfile: %s", key_file_path) return nil, err } client := ei.NewBuildClient(keyfile.Key) return client, nil }
func RestartDevice(client *ei.BuildClient, device_id string) { err := client.RestartDevice(device_id) if err != nil { logging.Fatal("Failed to restart device %s with error %s", device_id, err.Error()) return } fmt.Printf("Successfully restarted device and agent") }
func RunMe(cmd *Command, args []string) { logging.Info("Attempting to run script...") if len(args) < 2 { logging.Fatal("run requires file name") os.Exit(1) } ExecuteSqrl(args[1]) logging.Info("Script finished") }
func UploadFiles(cmd *Command, args []string) { logging.Info("Attempting to upload...") keyfile_data, err := ioutil.ReadFile(cmd.settings.ApiKeyFile) if err != nil { logging.Fatal("Could not open the keyfile: %s", cmd.settings.ApiKeyFile) os.Exit(1) return } keyfile := new(ApiKeyFile) err = json.Unmarshal(keyfile_data, keyfile) if err != nil { logging.Fatal("Could not parse keyfile: %s", cmd.settings.ApiKeyFile) os.Exit(1) return } agent_code, err := ioutil.ReadFile(cmd.settings.AgentFileOutPath) if err != nil { logging.Fatal("Could not open the agent code: %s", cmd.settings.AgentFileOutPath) os.Exit(1) return } device_code, err := ioutil.ReadFile(cmd.settings.DeviceFileOutPath) if err != nil { logging.Fatal("Could not open the device code %s", cmd.settings.DeviceFileOutPath) os.Exit(1) return } request := new(ei.CodeRevisionLong) client := ei.NewBuildClient(keyfile.Key) request.AgentCode = string(agent_code) request.DeviceCode = string(device_code) response, err := client.UpdateCodeRevision(cmd.settings.ModelKey, request) if err != nil { logging.Fatal("Failed to upload code to model %s, Error: %s", cmd.settings.ModelKey, err.Error()) os.Exit(1) return } logging.Info("Succesfully uploaded version %d", response.Version) }
func RunScript(script_file string) int { file_string := C.CString(script_file) rval := C.device_run_script(file_string) C.free(unsafe.Pointer(file_string)) if rval != 0 { logging.Fatal("Squirrel script failed to run") return int(rval) } return 0 }
func ListModels(client *ei.BuildClient) { model_list, err := client.ListModels() if err != nil { logging.Fatal("Failed to get model list %s", err.Error()) return } for _, model := range model_list.Models { fmt.Printf("Id: %s, Name: %s\n", model.Id, model.Name) } }
func ListDevices(client *ei.BuildClient) { device_list, err := client.GetDeviceList() if err != nil { logging.Fatal("Failed to get device list %s", err.Error()) return } for _, model := range device_list { fmt.Printf("Id: %s, Name: %s\n", model.Id, model.Name) } }
func ExampleRestartDevice() { logging.SetLoggingLevel(logging.LOG_INFO) logging.Info("Starting Test") client := NewBuildClient(API_KEY) err := client.RestartDevice(TEST_DEVICE_ID) if err != nil { logging.Fatal("Failed to get device! %s", err.Error()) return } fmt.Printf("Device Reset") logging.Info("Ending Test") }
func ExampleGetDevice() { logging.SetLoggingLevel(logging.LOG_INFO) logging.Info("Starting Test") client := NewBuildClient(API_KEY) device, err := client.GetDevice(TEST_DEVICE_ID) if err != nil { logging.Fatal("Failed to get device! %s", err.Error()) return } fmt.Printf("Name: %s Id: %s\n", device.Name, device.Id) logging.Info("Ending Test") }
func ExampleUpdateDevice() { logging.SetLoggingLevel(logging.LOG_INFO) logging.Info("Starting Test") client := NewBuildClient(API_KEY) new_device := new(Device) new_device.Name = "Wiggy Whacky - hamburg-2" dev, err := client.UpdateDevice(new_device, TEST_DEVICE_ID) if err != nil { logging.Fatal("Failed to update device! %s", err.Error()) return } fmt.Printf("Device Updated: %s", dev.Name) logging.Info("Ending Test") }
func ExampleRestartModelDevices() { logging.SetLoggingLevel(logging.LOG_DEBUG) logging.Info("Starting Example") client := NewBuildClient(API_KEY) err := client.RestartModelDevices(MODEL_KEY) if err != nil { logging.Fatal("Example Failed %s", err.Error()) return } logging.Info("Succesfully restarted model: %s", MODEL_KEY) logging.Info("Ending Example") }
func ExampleDeviceList() { logging.SetLoggingLevel(logging.LOG_INFO) logging.Info("Starting Test") client := NewBuildClient(API_KEY) list, err := client.GetDeviceList() if err != nil { logging.Fatal("Failed to get device list! %s", err.Error()) return } for _, device := range list { logging.Info("Name: %s, Id: %s, Model: %s", device.Name, device.Id, device.ModelId) } logging.Info("Ending Test") }
func ExampleGetDeviceLogs() { logging.SetLoggingLevel(logging.LOG_INFO) logging.Info("Starting Test") client := NewBuildClient(API_KEY) logs, err := client.GetDeviceLogs(TEST_DEVICE_ID) if err != nil { logging.Fatal("Failed to get device list! %s", err.Error()) return } for _, log := range logs { fmt.Printf("%s %s:%s\n", log.Timestamp, log.Type, log.Message) } logging.Info("Ending Test") }
func ExampleGetModel() { logging.SetLoggingLevel(logging.LOG_DEBUG) logging.Info("Starting Test") client := NewBuildClient(API_KEY) model_list, err := client.ListModels() if err != nil { logging.Fatal("Test Failed %s", err.Error()) return } for _, model := range model_list.Models { logging.Info("Id: %s, Name: %s", model.Id, model.Name) } logging.Info("Ending Test") }
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 ExampleGetCodeRevisions() { logging.SetLoggingLevel(logging.LOG_DEBUG) logging.Info("Starting Test") client := NewBuildClient(API_KEY) revisions, err := client.GetCodeRevisionList(MODEL_KEY) if err != nil { logging.Fatal("Test Failed %s", err.Error()) return } for _, revision := range revisions { logging.Info(string(revision.CreatedAt)) logging.Info(`Version: %d CreatedAt: %s, ReleaseNotes: %s,`, revision.Version, revision.CreatedAt, revision.ReleaseNotes) } logging.Info("Ending Test") }
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 ExampleGetCodeRevision() { logging.SetLoggingLevel(logging.LOG_DEBUG) logging.Info("Starting Test") client := NewBuildClient(API_KEY) revision, err := client.GetCodeRevision(MODEL_KEY, "1") if err != nil { logging.Fatal("Test Failed %s", err.Error()) return } logging.Info(string(revision.CreatedAt)) logging.Info(`Version: %d CreatedAt: %s, ReleaseNotes: %s, AgentCode: %s, DeviceCode: %s,`, revision.Version, revision.CreatedAt, revision.ReleaseNotes, revision.AgentCode, revision.DeviceCode) logging.Info("Ending Test") }
func UploadFiles(cmd *Command, args []string) { // Create our flags flag_set := flag.NewFlagSet("UploadFlagSet", flag.ExitOnError) restart_device := flag_set.Bool("r", false, "-r restarts the server on a successful model upload") flag_set.Parse(args[1:]) logging.Info("Attempting to upload...") keyfile_data, err := ioutil.ReadFile(cmd.settings.ApiKeyFile) if err != nil { logging.Fatal("Could not open the keyfile: %s", cmd.settings.ApiKeyFile) os.Exit(1) return } keyfile := new(ApiKeyFile) err = json.Unmarshal(keyfile_data, keyfile) if err != nil { logging.Fatal("Could not parse keyfile: %s", cmd.settings.ApiKeyFile) os.Exit(1) return } agent_code, err := ioutil.ReadFile(cmd.settings.AgentFileOutPath) if err != nil { logging.Fatal("Could not open the agent code: %s", cmd.settings.AgentFileOutPath) os.Exit(1) return } device_code, err := ioutil.ReadFile(cmd.settings.DeviceFileOutPath) if err != nil { logging.Fatal("Could not open the device code %s", cmd.settings.DeviceFileOutPath) os.Exit(1) return } request := new(ei.CodeRevisionLong) client := ei.NewBuildClient(keyfile.Key) request.AgentCode = string(agent_code) request.DeviceCode = string(device_code) response, err := client.UpdateCodeRevision(cmd.settings.ModelKey, request) if err != nil { logging.Fatal("Failed to upload code to model %s, Error: %s", cmd.settings.ModelKey, err.Error()) os.Exit(1) return } if response.Success == false { fmt.Println("There were errors in the build:") fmt.Printf("Error Code: %s \n", response.Error.Code) fmt.Printf("Message: %s \n", response.Error.FullMessage) return } logging.Info("Succesfully uploaded version %d", response.Revisions.Version) if *restart_device == true { err := client.RestartModelDevices(cmd.settings.ModelKey) if err != nil { logging.Fatal("Failed to restart devices after upload, Error: %s", err.Error()) os.Exit(1) } model, err := client.GetModel(cmd.settings.ModelKey) if err != nil { fmt.Printf("Model: %s devices restarted.\n", cmd.settings.ModelKey) return } else { fmt.Printf("Model: %s restarted.\n", model.Name) } } }
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 } } }