// Executes the "apply" command func execApplyCommand(c *cli.Context) error { if len(c.Args()) < 1 { return cli.NewExitError(errNoModuleName.Error(), 64) } L := lua.NewState() defer L.Close() config := &catalog.Config{ Module: c.Args()[0], DryRun: c.Bool("dry-run"), Logger: resource.DefaultLogger, SiteRepo: c.String("siterepo"), L: L, } katalog := catalog.New(config) if err := katalog.Load(); err != nil { if err != nil { return cli.NewExitError(err.Error(), 1) } } if err := katalog.Run(); err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
// ProjectDelete deletes services. func ProjectDelete(p project.APIProject, c *cli.Context) error { options := options.Delete{ RemoveVolume: c.Bool("v"), } if !c.Bool("force") { stoppedContainers, err := p.Containers(context.Background(), project.Filter{ State: project.Stopped, }, c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } if len(stoppedContainers) == 0 { fmt.Println("No stopped containers") return nil } fmt.Printf("Going to remove %v\nAre you sure? [yN]\n", strings.Join(stoppedContainers, ", ")) var answer string _, err = fmt.Scanln(&answer) if err != nil { return cli.NewExitError(err.Error(), 1) } if answer != "y" && answer != "Y" { return nil } } err := p.Delete(context.Background(), options, c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
func statusRequest() (*VMStatus, *cli.ExitError) { user := getUser() req, err := http.NewRequest("GET", "http://127.0.0.1:1050/status", nil) if err != nil { return nil, cli.NewExitError(err.Error(), 1) } req.Header.Add("X-Username", user.Name) client := &http.Client{} res, err := client.Do(req) if err != nil { return nil, cli.NewExitError(getRequestError(err), 1) } defer res.Body.Close() decoder := json.NewDecoder(res.Body) if res.StatusCode < 200 || res.StatusCode >= 400 { status := VMStatusError{} err := decoder.Decode(&status) if err != nil { return nil, cli.NewExitError(err.Error(), 1) } return nil, cli.NewExitError(status.Message, 1) } status := VMStatus{} err = decoder.Decode(&status) if err != nil { return nil, cli.NewExitError(err.Error(), 1) } return &status, nil }
func stringRequest(action string) *cli.ExitError { user := getUser() req, err := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:1050/%s", action), nil) if err != nil { return cli.NewExitError(err.Error(), 1) } req.Header.Add("X-Username", user.Name) client := &http.Client{} res, err := client.Do(req) if err != nil { return cli.NewExitError(err.Error(), 1) } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { return cli.NewExitError(err.Error(), 1) } code := 0 if res.StatusCode < 200 || res.StatusCode >= 400 { code = 1 } return cli.NewExitError(string(body), code) }
// Executes the "list" command func execListCommand(c *cli.Context) error { klient := newEtcdMinionClientFromFlags(c) cFlag := c.String("with-classifier") minions, err := parseClassifierPattern(klient, cFlag) // Ignore errors about missing minion directory if err != nil { if eerr, ok := err.(client.Error); !ok || eerr.Code != client.ErrorCodeKeyNotFound { return cli.NewExitError(err.Error(), 1) } } if len(minions) == 0 { return nil } table := uitable.New() table.MaxColWidth = 80 table.AddRow("MINION", "NAME") for _, minion := range minions { name, err := klient.MinionName(minion) if err != nil { return cli.NewExitError(err.Error(), 1) } table.AddRow(minion, name) } fmt.Println(table) return nil }
// Executes the "lastseen" command func execLastseenCommand(c *cli.Context) error { client := newEtcdMinionClientFromFlags(c) cFlag := c.String("with-classifier") minions, err := parseClassifierPattern(client, cFlag) if err != nil { return cli.NewExitError(err.Error(), 1) } table := uitable.New() table.MaxColWidth = 80 table.AddRow("MINION", "LASTSEEN") for _, minion := range minions { lastseen, err := client.MinionLastseen(minion) if err != nil { return cli.NewExitError(err.Error(), 1) } table.AddRow(minion, time.Unix(lastseen, 0)) } fmt.Println(table) return nil }
// Executes the "queue" command func execQueueCommand(c *cli.Context) error { if len(c.Args()) == 0 { return cli.NewExitError(errNoMinion.Error(), 64) } minion := uuid.Parse(c.Args()[0]) if minion == nil { return cli.NewExitError(errInvalidUUID.Error(), 64) } klient := newEtcdMinionClientFromFlags(c) // Ignore errors about missing queue directory queue, err := klient.MinionTaskQueue(minion) if err != nil { if eerr, ok := err.(client.Error); !ok || eerr.Code != client.ErrorCodeKeyNotFound { return cli.NewExitError(err.Error(), 1) } } if len(queue) == 0 { return nil } table := uitable.New() table.MaxColWidth = 40 table.AddRow("TASK", "STATE", "RECEIVED") for _, t := range queue { table.AddRow(t.ID, t.State, time.Unix(t.TimeReceived, 0)) } fmt.Println(table) return nil }
func TestProcess(t *testing.T) { passwd := "pass" url1, _ := soap.ParseURL("127.0.0.1") url2, _ := soap.ParseURL("root:@127.0.0.1") url3, _ := soap.ParseURL("line:[email protected]") url4, _ := soap.ParseURL("root:[email protected]") result, _ := url.Parse("https://*****:*****@127.0.0.1/sdk") passEmpty := "" result1, _ := url.Parse("https://root:@127.0.0.1/sdk") tests := []struct { URL *url.URL User string Password *string err error result *url.URL }{ {nil, "", nil, cli.NewExitError("--target argument must be specified", 1), nil}, {nil, "root", nil, cli.NewExitError("--target argument must be specified", 1), nil}, {nil, "root", &passwd, cli.NewExitError("--target argument must be specified", 1), nil}, {url1, "root", &passwd, nil, result}, {url4, "", nil, nil, result}, {url3, "root", &passwd, nil, result}, {url2, "", &passwd, nil, result}, {url1, "root", &passEmpty, nil, result1}, } for _, test := range tests { target := NewTarget() target.URL = test.URL target.User = test.User target.Password = test.Password if target.URL != nil { t.Logf("Before processing, url: %s", target.URL.String()) } e := target.HasCredentials() if test.err != nil { if e == nil { t.Errorf("Empty error") } if e.Error() != test.err.Error() { t.Errorf("Unexpected error message: %s", e.Error()) } } else if e != nil { t.Errorf("Unexpected error %s", e.Error()) } else { if target.URL != test.URL { t.Errorf("unexpected result url: %s", target.URL.String()) } else { t.Logf("result url: %s", target.URL.String()) } } } }
func ensureRoot() *cli.ExitError { if uid := os.Geteuid(); uid != 0 { return cli.NewExitError("This command requires sudo", 1) } if uid := os.Getenv("SUDO_UID"); uid == "" { return cli.NewExitError("This command requires sudo", 1) } return nil }
func validate(ip string, port int, c *cli.Context) *cli.ExitError { if strings.TrimSpace(ip) == "" { return cli.NewExitError("IP address is required. `hdp -h` for usage info.", 1) } if net.ParseIP(ip) == nil { return cli.NewExitError("Invalid IP address", 1) } if port < 0 || port > 65535 { return cli.NewExitError("Invalid port", 2) } return nil }
func runCleanup(hostname, home string) *cli.ExitError { exe, err := osext.Executable() if err != nil { return cli.NewExitError(err.Error(), 1) } output, err := exec.Command("sudo", exe, "cleanup", "--hostname", hostname, "--home", home).Output() code := 0 if err != nil { code = 1 } return cli.NewExitError(string(output), code) }
// Executes the "serve" command func execServeCommand(c *cli.Context) error { name, err := os.Hostname() if err != nil { return cli.NewExitError(err.Error(), 1) } concurrency := c.Int("concurrency") if concurrency < 0 { concurrency = runtime.NumCPU() } if c.String("siterepo") == "" { return cli.NewExitError(errNoSiteRepo.Error(), 64) } nameFlag := c.String("name") if nameFlag != "" { name = nameFlag } etcdCfg := etcdConfigFromFlags(c) minionCfg := &minion.EtcdMinionConfig{ Concurrency: concurrency, Name: name, SiteRepo: c.String("siterepo"), EtcdConfig: etcdCfg, } m, err := minion.NewEtcdMinion(minionCfg) if err != nil { return cli.NewExitError(err.Error(), 1) } // Channel on which the shutdown signal is sent quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) // Start minion err = m.Serve() if err != nil { return cli.NewExitError(err.Error(), 1) } // Block until a shutdown signal is received <-quit m.Stop() return nil }
// ProjectPull pulls images for services. func ProjectPull(p project.APIProject, c *cli.Context) error { err := p.Pull(context.Background(), c.Args()...) if err != nil && !c.Bool("ignore-pull-failures") { return cli.NewExitError(err.Error(), 1) } return nil }
// ProjectKill forces stop service containers. func ProjectKill(p project.APIProject, c *cli.Context) error { err := p.Kill(context.Background(), c.String("signal"), c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
// ProjectStop stops all services. func ProjectStop(p project.APIProject, c *cli.Context) error { err := p.Stop(context.Background(), c.Int("timeout"), c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
// ProjectUnpause unpauses service containers. func ProjectUnpause(p project.APIProject, c *cli.Context) error { err := p.Unpause(context.Background(), c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
// ProjectLog gets services logs. func ProjectLog(p project.APIProject, c *cli.Context) error { err := p.Log(context.Background(), c.Bool("follow"), c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
// ProjectPort prints the public port for a port binding. func ProjectPort(p project.APIProject, c *cli.Context) error { if len(c.Args()) != 2 { return cli.NewExitError("Please pass arguments in the form: SERVICE PORT", 1) } index := c.Int("index") protocol := c.String("protocol") serviceName := c.Args()[0] privatePort := c.Args()[1] port, err := p.Port(context.Background(), index, protocol, serviceName, privatePort) if err != nil { return cli.NewExitError(err.Error(), 1) } fmt.Println(port) return nil }
// Executes the "graph" command func execGraphCommand(c *cli.Context) error { if len(c.Args()) < 1 { return cli.NewExitError(errNoModuleName.Error(), 64) } L := lua.NewState() defer L.Close() module := c.Args()[0] config := &catalog.Config{ Module: module, DryRun: true, Logger: log.New(os.Stdout, "", log.LstdFlags), SiteRepo: c.String("siterepo"), L: L, } katalog := catalog.New(config) resource.LuaRegisterBuiltin(L) if err := L.DoFile(module); err != nil { return cli.NewExitError(err.Error(), 1) } collection, err := resource.CreateCollection(katalog.Unsorted) if err != nil { return cli.NewExitError(err.Error(), 1) } g, err := collection.DependencyGraph() if err != nil { return cli.NewExitError(err.Error(), 1) } g.AsDot("resources", os.Stdout) g.Reversed().AsDot("reversed", os.Stdout) sorted, err := g.Sort() if err == graph.ErrCircularDependency { circular := graph.New() circular.AddNode(sorted...) circular.AsDot("circular", os.Stdout) return cli.NewExitError(graph.ErrCircularDependency.Error(), 1) } return nil }
func (c *Create) processProxies() error { var err error if c.httpProxy != "" { c.HTTPProxy, err = url.Parse(c.httpProxy) if err != nil || c.HTTPProxy.Host == "" || c.HTTPProxy.Scheme != "http" { return cli.NewExitError(fmt.Sprintf("Could not parse HTTP proxy - expected format http://fqnd_or_ip:port: %s", c.httpProxy), 1) } } if c.httpsProxy != "" { c.HTTPSProxy, err = url.Parse(c.httpsProxy) if err != nil || c.HTTPSProxy.Host == "" || c.HTTPSProxy.Scheme != "https" { return cli.NewExitError(fmt.Sprintf("Could not parse HTTPS proxy - expected format https://fqnd_or_ip:port: %s", c.httpsProxy), 1) } } return nil }
// ProjectPs lists the containers. func ProjectPs(p project.APIProject, c *cli.Context) error { qFlag := c.Bool("q") allInfo, err := p.Ps(context.Background(), qFlag, c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } os.Stdout.WriteString(allInfo.String(!qFlag)) return nil }
// ProjectRun runs a given command within a service's container. func ProjectRun(p project.APIProject, c *cli.Context) error { if len(c.Args()) == 0 { logrus.Fatal("No service specified") } serviceName := c.Args()[0] commandParts := c.Args()[1:] options := options.Run{ Detached: c.Bool("d"), } exitCode, err := p.Run(context.Background(), serviceName, commandParts, options) if err != nil { return cli.NewExitError(err.Error(), 1) } return cli.NewExitError("", exitCode) }
// Executes the "info" command func execInfoCommand(c *cli.Context) error { if len(c.Args()) == 0 { return cli.NewExitError(errNoMinion.Error(), 64) } arg := c.Args()[0] minion := uuid.Parse(arg) if minion == nil { return cli.NewExitError(errInvalidUUID.Error(), 64) } klient := newEtcdMinionClientFromFlags(c) name, err := klient.MinionName(minion) if err != nil { return cli.NewExitError(err.Error(), 1) } lastseen, err := klient.MinionLastseen(minion) if err != nil { return cli.NewExitError(err.Error(), 1) } // Ignore errors about missing queue directory taskQueue, err := klient.MinionTaskQueue(minion) if err != nil { if eerr, ok := err.(client.Error); !ok || eerr.Code != client.ErrorCodeKeyNotFound { return cli.NewExitError(err.Error(), 1) } } // Ignore errors about missing log directory taskLog, err := klient.MinionTaskLog(minion) if err != nil { if eerr, ok := err.(client.Error); !ok || eerr.Code != client.ErrorCodeKeyNotFound { return cli.NewExitError(err.Error(), 1) } } // Ignore errors about missing classifier directory classifierKeys, err := klient.MinionClassifierKeys(minion) if err != nil { if eerr, ok := err.(client.Error); !ok || eerr.Code != client.ErrorCodeKeyNotFound { return cli.NewExitError(err.Error(), 1) } } table := uitable.New() table.MaxColWidth = 80 table.AddRow("Minion:", minion) table.AddRow("Name:", name) table.AddRow("Lastseen:", time.Unix(lastseen, 0)) table.AddRow("Queue:", len(taskQueue)) table.AddRow("Log:", len(taskLog)) table.AddRow("Classifiers:", len(classifierKeys)) fmt.Println(table) return nil }
func (c *Create) processBridgeNetwork() error { // bridge network params var err error _, c.Data.BridgeIPRange, err = net.ParseCIDR(c.BridgeIPRange) if err != nil { return cli.NewExitError(fmt.Sprintf("Error parsing bridge network ip range: %s. Range must be in CIDR format, e.g., 172.16.0.0/12", err), 1) } return nil }
// ProjectDelete deletes services. func ProjectDelete(p project.APIProject, c *cli.Context) error { options := options.Delete{ RemoveVolume: c.Bool("v"), } err := p.Delete(context.Background(), options, c.Args()...) if err != nil { return cli.NewExitError(err.Error(), 1) } return nil }
func (c *Create) processParams() error { defer trace.End(trace.Begin("")) if err := c.HasCredentials(); err != nil { return err } if c.cert != "" && c.key == "" { return cli.NewExitError("key cert should be specified at the same time", 1) } if c.cert == "" && c.key != "" { return cli.NewExitError("key cert should be specified at the same time", 1) } if c.ExternalNetworkName == "" { c.ExternalNetworkName = "VM Network" } if c.BridgeNetworkName == "" { c.BridgeNetworkName = c.DisplayName } if len(c.DisplayName) > MaxDisplayNameLen { return cli.NewExitError(fmt.Sprintf("Display name %s exceeds the permitted 31 characters limit. Please use a shorter -name parameter", c.DisplayName), 1) } if err := c.processContainerNetworks(); err != nil { return err } if err := c.processVolumeStores(); err != nil { return errors.Errorf("Error occurred while processing volume stores: %s", err) } // if err := c.processReservations(); err != nil { // return err // } // FIXME: add parameters for these configurations c.osType = "linux" c.Insecure = true return nil }
// ProjectConfig validates and print the compose file. func ProjectConfig(p project.APIProject, c *cli.Context) error { yaml, err := p.Config() if err != nil { return cli.NewExitError(err.Error(), 1) } if !c.Bool("quiet") { fmt.Println(yaml) } return nil }
func (c *Create) processInsecureRegistries() error { for _, registry := range c.insecureRegistries { url, err := url.Parse(registry) if err != nil { return cli.NewExitError(fmt.Sprintf("%s is an invalid format for registry url", registry), 1) } c.InsecureRegistries = append(c.InsecureRegistries, *url) } return nil }
func runAgent(c *cli.Context) error { a := agent.Agent{ DockerAddress: c.String("docker"), FusisAddress: c.String("fusis-addr"), LabelFilter: c.String("label-filter"), Interval: c.Duration("interval"), } if a.FusisAddress == "" { return cli.NewExitError("Parameter --fusis-addr is mandatory", 1) } err := a.Init() if err != nil { return cli.NewExitError(err.Error(), 1) } handleSignals(&a) log.Print("Running agent...") a.Start() a.Wait() return nil }
func Initialize(c *cli.Context) error { // read seed.json and send the data to the db reader, err := os.Open(c.String("seed")) if err != nil { return cli.NewExitError("Error opening seed.json: "+err.Error(), 1) } seedData, err := ioutil.ReadAll(reader) if err != nil { return cli.NewExitError("Error reading seed.json: "+err.Error(), 1) } var src = json.RawMessage(seedData) // generate a random string for the secret randomBytes := make([]byte, 48) _, err = rand.Read(randomBytes) if err != nil { return cli.NewExitError(err.Error(), 1) } newAppSecret := base64.RawURLEncoding.EncodeToString(randomBytes) conf := struct{ AuthSecret string }{newAppSecret} if err := patch(c, "config", &conf, nil); err != nil { return cli.NewExitError("Error from server: "+err.Error(), 1) } fmt.Println("Add this secret to your environment -- it won't be available again.") fmt.Printf("export ORI_APP_SECRET=%s\n", newAppSecret) c.GlobalSet("secret", newAppSecret) // send to admin if err = post(c, "load", &src, nil); err != nil { return err } return nil }