func NewWebService() (*Webservice, error) { config, err := utils.GetConcertoConfig() if err != nil { return nil, err } if !config.IsConfigReady() { return nil, fmt.Errorf("Configuration is incomplete.") } client, err := httpClient(config) if err != nil { return nil, err } return &Webservice{config, client}, nil }
// WireUpEvent prepares common resources to send request to Concerto API func WireUpEvent(c *cli.Context) (ns *api.EventService, f format.Formatter) { f = format.GetFormatter() config, err := utils.GetConcertoConfig() if err != nil { f.PrintFatal("Couldn't wire up config", err) } hcs, err := utils.NewHTTPConcertoService(config) if err != nil { f.PrintFatal("Couldn't wire up concerto service", err) } ns, err = api.NewEventService(hcs) if err != nil { f.PrintFatal("Couldn't wire up event service", err) } return ns, f }
// WireUpDomain prepares common resources to send request to Concerto API func WireUpDomain(c *cli.Context) (ds *api.DomainService, f format.Formatter) { f = format.GetFormatter() config, err := utils.GetConcertoConfig() if err != nil { f.PrintFatal("Couldn't wire up config", err) } hcs, err := utils.NewHTTPConcertoService(config) if err != nil { f.PrintFatal("Couldn't wire up concerto service", err) } ds, err = api.NewDomainService(hcs) if err != nil { f.PrintFatal("Couldn't wire up domain service", err) } return ds, f }
// WireUpCluster prepares common resources to send request to Concerto API func WireUpCluster(c *cli.Context) (cs *api.ClusterService, f format.Formatter) { f = format.GetFormatter() config, err := utils.GetConcertoConfig() if err != nil { f.PrintFatal("Couldn't wire up config", err) } hcs, err := utils.NewHTTPConcertoService(config) if err != nil { f.PrintFatal("Couldn't wire up concerto service", err) } cs, err = api.NewClusterService(hcs) if err != nil { f.PrintFatal("Couldn't wire up cluster service", err) } return cs, f }
func (w *WebClient) getApiKeys() error { w.url.Path = "/settings/api_key.zip" response, err := w.client.Get(w.url.String()) defer response.Body.Close() if err != nil { return err } if response.StatusCode < 300 && response.Header.Get("Content-Type") == "application/zip" { config, err := utils.GetConcertoConfig() if err != nil { log.Debug(err.Error) return err } concertoFolderSSL := path.Join(config.ConfLocation, "ssl") if !config.IsHost { os.MkdirAll(path.Join(concertoFolderSSL, "private"), 0755) file, err := ioutil.TempFile(os.TempDir(), "api-key.zip") if err != nil { return err } defer file.Close() io.Copy(file, response.Body) err = utils.Unzip(file.Name(), concertoFolderSSL) defer os.Remove(file.Name()) if err != nil { return err } log.Debugf("Unziped Api Keys in %s.\n", concertoFolderSSL) return nil } return errors.New("You are trying to overwrite server configuration. Please contact your administrator") } return errors.New(fmt.Sprintf("We are not able to download your API keys. Please try by loging to %s/settings/api_key.zip in your web navigator ", w.url.String())) }
func cmdDockerHijack(c *cli.Context) { var nodes []Node var node Node discovered := false utils.FlagsRequired(c, []string{"node"}) var firstArgument string if c.Args().Present() { firstArgument = c.Args().First() } else { firstArgument = "help" } nodeName := c.String("node") webservice, err := webservice.NewWebService() utils.CheckError(err) err, data, res := webservice.Get("/v1/kaas/ships") utils.CheckError(err) utils.CheckReturnCode(res, data) err = json.Unmarshal(data, &nodes) utils.CheckError(err) // Validating if node exist for _, element := range nodes { if (element.Name == nodeName) || (element.Id == nodeName) { discovered = true node = element } } if discovered == true { dockerLocation, err := exec.LookPath("docker") if err != nil { log.Warn(fmt.Sprintf("We could not find docker in your enviroment. Please install it.")) os.Exit(1) } log.Debug(fmt.Sprintf("Found docker at %s", dockerLocation)) config, err := utils.GetConcertoConfig() utils.CheckError(err) nodeParameters := fmt.Sprintf("--host=tcp://%s:2376", node.Fqdn) tls := "--tls=true" clientCertificate := fmt.Sprintf("--tlscert=%s", config.Certificate.Cert) clientKey := fmt.Sprintf("--tlskey=%s", config.Certificate.Key) clientCA := fmt.Sprintf("--tlscacert=%s", config.Certificate.Ca) arguments := append([]string{nodeParameters, tls, clientCertificate, clientKey, clientCA, firstArgument}, c.Args().Tail()...) log.Debug(fmt.Sprintf("Going to execute %s %s", dockerLocation, arguments)) cmd := exec.Command(dockerLocation, arguments...) stdout, err := cmd.StdoutPipe() utils.CheckError(err) stderr, err := cmd.StderrPipe() utils.CheckError(err) // Start command err = cmd.Start() utils.CheckError(err) defer cmd.Wait() go io.Copy(os.Stderr, stderr) ls := bufio.NewReader(stdout) for { line, isPrefix, err := ls.ReadLine() if isPrefix { log.Errorf("%s", errors.New("isPrefix: true")) } if err != nil { if err != io.EOF { log.Errorf("%s", err.Error()) } break } fmt.Printf("%s\n", strings.Replace(string(line), "docker", fmt.Sprintf("concerto nodes docker --node %s", nodeName), -1)) } go func() { time.Sleep(30 * time.Second) log.Fatal(fmt.Sprintf("Timeout out. Check conectivity to %s", nodeParameters)) }() return } else { log.Warn(fmt.Sprintf("Node \"%s\" is not in your account please create it. Thank you.", nodeName)) os.Exit(1) } }
func cmdKubectlHijack(c *cli.Context) { var clusters []Cluster var cluster Cluster discovered := false operational := false utils.FlagsRequired(c, []string{"cluster"}) clusterName := c.String("cluster") var firstArgument string if c.Args().Present() { firstArgument = c.Args().First() } else { firstArgument = "help" } webservice, err := webservice.NewWebService() utils.CheckError(err) err, data, res := webservice.Get("/v1/kaas/fleets") utils.CheckError(err) utils.CheckReturnCode(res, data) err = json.Unmarshal(data, &clusters) utils.CheckError(err) // Validating if cluster exist for _, element := range clusters { if (element.Name == clusterName) || (element.Id == clusterName) { discovered = true cluster = element if cluster.State == "operational" || cluster.State == "partially_operational" { operational = true } } } if (discovered && operational) || firstArgument == "help" { kubeLocation, err := exec.LookPath("kubectl") if err != nil { log.Warn(fmt.Sprintf("We could not find kubectl in your enviroment. Please install it.")) os.Exit(1) } if discovered && !operational { log.Warn(fmt.Sprintf("Cluster \"%s\" is not operational. Wait till it gets operational.", clusterName)) } log.Debug(fmt.Sprintf("Found kubectl at %s", kubeLocation)) config, err := utils.GetConcertoConfig() utils.CheckError(err) var clusterParameters, clientCertificate, clientKey, clientCA string if len(cluster.Masters) > 0 { clusterParameters = fmt.Sprintf("--server=https://%s:6443", cluster.Masters[0]) } clientCertificate = fmt.Sprintf("--client-certificate=%s", config.Certificate.Cert) clientKey = fmt.Sprintf("--client-key=%s", config.Certificate.Key) clientCA = fmt.Sprintf("--certificate-authority=%s", config.Certificate.Ca) arguments := append([]string{clusterParameters, "--api-version=v1", clientCertificate, clientKey, clientCA, firstArgument}, c.Args().Tail()...) log.Debug(fmt.Sprintf("Going to execute %s %s", kubeLocation, arguments)) cmd := exec.Command(kubeLocation, arguments...) stdout, err := cmd.StdoutPipe() utils.CheckError(err) stderr, err := cmd.StderrPipe() utils.CheckError(err) // Start command err = cmd.Start() utils.CheckError(err) defer cmd.Wait() go io.Copy(os.Stderr, stderr) ls := bufio.NewReader(stdout) for { line, isPrefix, err := ls.ReadLine() if isPrefix { log.Errorf("%s", errors.New("isPrefix: true")) } if err != nil { if err != io.EOF { log.Errorf("%s", err.Error()) } break } fmt.Printf("%s\n", strings.Replace(string(line), "kubectl", fmt.Sprintf("concerto cluster kubectl --cluster %s", clusterName), -1)) } go func() { time.Sleep(30 * time.Second) log.Fatal(fmt.Sprintf("Timeout out. Check conectivity to %s", clusterParameters)) }() return } else { log.Warn(fmt.Sprintf("Cluster \"%s\" is not in your account please create it. Thank you.", clusterName)) os.Exit(1) } }
func cmdSetupApiKeys(c *cli.Context) { var emailUnClean string var passwordUnClean []byte reader := bufio.NewReader(os.Stdin) config, err := utils.GetConcertoConfig() if err != nil { log.Fatalf("Error getting current configuration: %s", err.Error) } loginURL := config.ConcertoURL if err != nil { log.Fatalf("Error getting Concerto URL: %s", err.Error) } fmt.Printf("Using Concerto endpoint %s \n", loginURL) if c.IsSet("email") { emailUnClean = c.String("email") } else { fmt.Printf("Email: ") emailUnClean, _ = reader.ReadString('\n') } if c.IsSet("password") { passwordUnClean = []byte(c.String("password")) } else { fmt.Printf("Password: "******"\n") if govalidator.IsEmail(email) { client, err := NewWebClient(loginURL) if err != nil { log.Fatal(err) } fmt.Printf("Logging into Concerto ...") err = client.login(email, password) if err != nil { log.Fatal(err) } fmt.Printf(" OK\n") fmt.Printf("Checking/Generating API keys ...") err = client.generateAPIKeys() if err != nil { log.Fatal(err) } fmt.Printf(" OK\n") fmt.Printf("Downloading API keys ...") err = client.getApiKeys() if err != nil { log.Fatal(err) } fmt.Printf(" OK\n") } else { log.Fatalf("Email address %s is not a valid email", email) } }