Esempio n. 1
0
File: get.go Progetto: runabove/sail
func cmdGet(cmd *cobra.Command, args []string) {
	// FIXME: duplicate
	internal.ReadConfig()
	var ns string

	// Check args
	if len(args) > 1 {
		internal.Exit("Invalid usage. sail compose get [--standard] [<application>]. Please see sail compose get -h\n")
	} else if len(args) > 1 {
		ns = args[0]
	} else {
		ns = internal.User
	}

	path := fmt.Sprintf("/applications/%s/fig?standard=%v", ns, getStandard)
	data, _, err := internal.Request("GET", path, nil)
	if err != nil {
		internal.Exit("Error: %s\n", err)
	}

	fmt.Printf("%s", data)
}
Esempio n. 2
0
func serviceAdd(args Add) {

	if args.ContainerEnvironment == nil {
		args.ContainerEnvironment = make([]string, 0)
	}

	// Parse volumes
	if len(cmdAddVolume) > 0 {
		args.Volumes = make(map[string]VolumeConfig)
	}
	for _, vol := range cmdAddVolume {
		t := strings.Split(vol, ":")
		if len(t) == 2 {
			args.Volumes[t[0]] = VolumeConfig{Size: t[1]}
		} else if len(t) == 1 {
			args.Volumes[t[0]] = VolumeConfig{Size: "10"}
		} else {
			fmt.Fprintf(os.Stderr, "Error: Volume parameter '%s' not formated correctly\n", vol)
			os.Exit(1)
		}
	}

	// Parse links
	if len(redeployLink) > 0 {
		args.Links = make(map[string]string)
	}

	for _, link := range cmdAddLink {
		t := strings.Split(link, ":")
		if len(t) == 1 {
			args.Links[t[0]] = t[0]
		} else {
			args.Links[t[0]] = t[1]
		}
	}

	// Parse ContainerNetworks arguments
	for _, network := range cmdAddNetwork {
		args.ContainerNetwork[network] = make(map[string][]string)
	}

	for _, gat := range cmdAddGateway {
		t := strings.Split(gat, ":")
		if len(t) != 2 {
			fmt.Fprintf(os.Stderr, "Invalid gateway parameter, should be \"input:output\". Typically, output will be one of 'predictor', 'public'")
			os.Exit(1)
		}
		if _, ok := args.ContainerNetwork[t[0]]; !ok {
			fmt.Fprintf(os.Stderr, "Automatically adding %s to network list\n", t[0])
			args.ContainerNetwork[t[0]] = make(map[string][]string)
		}
		if _, ok := args.ContainerNetwork[t[1]]; !ok {
			fmt.Fprintf(os.Stderr, "Automatically adding %s to network list\n", t[0])
			args.ContainerNetwork[t[1]] = make(map[string][]string)
		}
		args.ContainerNetwork[t[0]]["gateway_to"] = append(args.ContainerNetwork[t[0]]["gateway_to"], t[1])
	}

	// Parse ContainerPorts
	args.ContainerPorts = parsePublishedPort(addPublish)

	path := fmt.Sprintf("/applications/%s/services/%s", args.Application, args.Service)
	body, err := json.MarshalIndent(args, " ", " ")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fatal: %s\n", err)
		return
	}

	if addBatch {
		ret, code, err := internal.Request("POST", path, body)

		// http.Request failed for some reason
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s\n", err)
			return
		}

		//  If we are in ensure mode, fallback to redeploy
		if code == 409 && cmdAddRedeploy {
			ensureMode(args)
			return
		}

		// If API returned a json error
		e := internal.DecodeError(ret)
		if e != nil {
			fmt.Fprintf(os.Stderr, "%s\n", e)
			return
		}

		// Just print data
		internal.FormatOutputDef(ret)

		// Always start service
		if internal.Format == "pretty" {
			fmt.Fprintf(os.Stderr, "Starting service %s/%s...\n", args.Application, args.Service)
		}
		serviceStart(args.Application, args.Service, false)

		return
	}

	buffer, code, err := internal.Stream("POST", path+"?stream", body)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s\n", err)
		return
	}

	if code == 409 && cmdAddRedeploy {
		ensureMode(args)
		return
	}

	line, err := internal.DisplayStream(buffer)
	internal.Check(err)
	if line != nil {
		var data map[string]interface{}
		err = json.Unmarshal(line, &data)
		internal.Check(err)

		fmt.Printf("Hostname: %v\n", data["hostname"])
		fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
	}
}