Beispiel #1
0
// Attempts to execute an API request
func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
	parseStorage := func() string {
		if len(flag.Args()) != 2 {
			log.Fatal("usage: cloudcfg [OPTIONS] get|list|create|update|delete <url>")
		}
		return strings.Trim(flag.Arg(1), "/")
	}

	readUrl := func(storage string) string {
		return *httpServer + path.Join("/api/v1beta1", storage)
	}

	var request *http.Request
	var err error
	switch method {
	case "get", "list":
		url := readUrl(parseStorage())
		if len(*labelQuery) > 0 && method == "list" {
			url = url + "?labels=" + *labelQuery
		}
		request, err = http.NewRequest("GET", url, nil)
	case "delete":
		request, err = http.NewRequest("DELETE", readUrl(parseStorage()), nil)
	case "create":
		storage := parseStorage()
		request, err = cloudcfg.RequestWithBodyData(readConfig(storage), readUrl(storage), "POST")
	case "update":
		storage := parseStorage()
		request, err = cloudcfg.RequestWithBodyData(readConfig(storage), readUrl(storage), "PUT")
	default:
		return false
	}

	var printer cloudcfg.ResourcePrinter
	if *json {
		printer = &cloudcfg.IdentityPrinter{}
	} else if *yaml {
		printer = &cloudcfg.YAMLPrinter{}
	} else {
		printer = &cloudcfg.HumanReadablePrinter{}
	}

	var body string
	if body, err = cloudcfg.DoRequest(request, auth); err == nil {
		if err = printer.Print(body, os.Stdout); err != nil {
			log.Fatalf("Failed to print: %#v\nRaw received text:\n%v\n", err, string(body))
		}
		fmt.Print("\n")
	} else {
		log.Fatalf("Error: %#v %s", err, body)
	}

	return true
}
Beispiel #2
0
// CloudCfg command line tool.
func main() {
	flag.Parse() // Scan the arguments list

	if *versionFlag {
		fmt.Println("Version:", APP_VERSION)
		os.Exit(0)
	}

	if len(flag.Args()) < 2 {
		usage()
	}
	method := flag.Arg(0)
	secure := true
	parsedUrl, err := url.Parse(*httpServer)
	if err != nil {
		log.Fatalf("Unable to parse %v as a URL\n", err)
	}
	if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" {
		secure = false
	}
	storage := strings.Trim(flag.Arg(1), "/")
	url := *httpServer + path.Join("/api/v1beta1", storage)
	var request *http.Request

	var printer cloudcfg.ResourcePrinter
	if *json {
		printer = &cloudcfg.IdentityPrinter{}
	} else if *yaml {
		printer = &cloudcfg.YAMLPrinter{}
	} else {
		printer = &cloudcfg.HumanReadablePrinter{}
	}

	var auth *kube_client.AuthInfo
	if secure {
		auth, err = cloudcfg.LoadAuthInfo(*authConfig)
		if err != nil {
			log.Fatalf("Error loading auth: %#v", err)
		}
	}

	switch method {
	case "get", "list":
		if len(*labelQuery) > 0 && method == "list" {
			url = url + "?labels=" + *labelQuery
		}
		request, err = http.NewRequest("GET", url, nil)
	case "delete":
		request, err = http.NewRequest("DELETE", url, nil)
	case "create":
		request, err = cloudcfg.RequestWithBodyData(readConfig(storage), url, "POST")
	case "update":
		request, err = cloudcfg.RequestWithBodyData(readConfig(storage), url, "PUT")
	case "rollingupdate":
		client := &kube_client.Client{
			Host: *httpServer,
			Auth: auth,
		}
		cloudcfg.Update(flag.Arg(1), client, *updatePeriod)
	case "run":
		args := flag.Args()
		if len(args) < 4 {
			log.Fatal("usage: cloudcfg -h <host> run <image> <replicas> <name>")
		}
		image := args[1]
		replicas, err := strconv.Atoi(args[2])
		name := args[3]
		if err != nil {
			log.Fatalf("Error parsing replicas: %#v", err)
		}
		err = cloudcfg.RunController(image, name, replicas, kube_client.Client{Host: *httpServer, Auth: auth}, *portSpec, *servicePort)
		if err != nil {
			log.Fatalf("Error: %#v", err)
		}
		return
	case "stop":
		err = cloudcfg.StopController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: auth})
		if err != nil {
			log.Fatalf("Error: %#v", err)
		}
		return
	case "rm":
		err = cloudcfg.DeleteController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: auth})
		if err != nil {
			log.Fatalf("Error: %#v", err)
		}
		return
	default:
		log.Fatalf("Unknown command: %s", method)
	}
	if err != nil {
		log.Fatalf("Error: %#v", err)
	}
	body, err := cloudcfg.DoRequest(request, auth)
	if err != nil {
		log.Fatalf("Error: %#v", err)
	}
	err = printer.Print(body, os.Stdout)
	if err != nil {
		log.Fatalf("Failed to print: %#v\nRaw received text:\n%v\n", err, string(body))
	}
	fmt.Print("\n")
}