Beispiel #1
0
func Remove(c *cli.Context) error {

	// Project
	st, err := discovery.GetStore(c)
	if err != nil {
		return err
	}
	p, err := getProject(c.Args(), st)
	if err != nil {
		return nil
	}

	// Controller
	o, err := swarm.New(st)
	if err != nil {
		return err
	}
	ctr := Controller{orch: o, project: p}

	// Stop
	if err = ctr.Stop(); err != nil {
		return nil
	}
	fmt.Println("Project deleted:", p.ID)

	return nil
}
Beispiel #2
0
func Query(c *cli.Context) error {

	// Project
	st, err := discovery.GetStore(c)
	if err != nil {
		return err
	}
	args := []string{}
	if c.String("name") != "" {
		args = append(args, c.String("name"))
	}
	p, err := getProject(args, st)
	if err != nil {
		return err
	}

	// Controller
	o, err := swarm.New(st)
	if err != nil {
		return err
	}
	ctr := Controller{orch: o, project: p}

	// Query
	query := strings.Join(c.Args(), " ")
	log.Infoln("Query for:", query)
	if err = ctr.project.Query(query); err != nil {
		return err
	}

	return nil
}
Beispiel #3
0
// Launch a docker build from a Dockerfile
func DockerBuild(c *cli.Context, tmp_dir_path, imageName string) (err error) {
	// Get store from cli
	st, err := discovery.GetStore(c)
	if err != nil {
		return err
	}

	// Get swarm from store
	sw, err := swarm.New(st)
	if err != nil {
		return err
	}

	fmt.Printf("Building Docker image named '%s' from Dockerfile located at %s/Dockerfile\n", imageName, tmp_dir_path)
	_, err = sw.BuildImg(imageName, tmp_dir_path)
	check(err)
	return
}
Beispiel #4
0
func Run(c *cli.Context) error {
	daemon := c.Bool("d")
	if daemon {
		log.Debugln("Running project in daemon")
	} else {
		log.Debugln("Running project")
	}

	// Services
	if len(c.Args()) == 0 {
		msg := fmt.Sprintf(
			"You need to provide a workflow, ie. %s",
			"\"service1 | service2 | service3\"",
		)
		return errors.New(msg)
	}
	services := []string{}
	var query string
	for i, service := range strings.Split(c.Args()[0], "|") {
		if i == 0 {
			serviceFull := strings.Split(service, " ")
			service = serviceFull[0]
			if len(serviceFull) > 1 {
				query = strings.Join(serviceFull[1:], " ")
			}
		}
		service = strings.TrimSpace(service)
		services = append(services, service)
	}
	// TODO: check if services exists in store
	log.Debugln("Services", services)

	// Project
	name := c.String("name")
	st, err := discovery.GetStore(c)
	if err != nil {
		return err
	}
	p, err := NewProject(name, st)
	if err != nil {
		return err
	}
	log.Debugln(p)
	if err = p.SetServices(services); err != nil {
		return err
	}
	log.Debugf("Project %s (%s)\n", p.ID, p.Name)
	if daemon {
		fmt.Printf("Project %s (%s)\n", p.ID, p.Name)
	}

	// Controller
	o, err := swarm.New(st)
	if err != nil {
		return err
	}
	ctr := Controller{orch: o, project: p}

	// Run
	api, err := ctr.LaunchAPI()
	if err != nil {
		return err
	}
	for _, service := range ctr.project.Services {
		if err = ctr.launchService(service); err != nil {
			return err
		}
	}
	log.Debugf("API listening on: http://%s\n", api.Addr())

	if daemon {
		fmt.Printf("API listening on: http://%s\n", api.Addr())
		return nil
	}

	// Query
	if query != "" {
		if err = ctr.project.Query(query); err != nil {
			return err
		}
	}

	// Stop
	if err = ctr.Stop(); err != nil {
		return nil
	}

	return nil
}
Beispiel #5
0
func Initialize(c *cli.Context) (err error) {

	// Conf
	name := c.String("name")
	cf, err := getConf(c)
	if err != nil {
		return
	}
	if _, err := cf.GetPool(name); err == nil {
		return errors.New("Pool already exist")
	}

	// Servers
	servers := c.StringSlice("servers")
	if len(servers) == 0 {
		dockerHost := os.Getenv("DOCKER_HOST")
		if dockerHost == "" {
			dockerHost = "unix:///var/run/docker.sock"
		}
		servers = append(servers, dockerHost)
	} else if len(servers) == 1 {
		serversArray := strings.SplitN(servers[0], ",", -1)
		servers = []string{}
		for _, server := range serversArray {
			serverFullAddr := fmt.Sprintf("tcp://%s:2375", server)
			servers = append(servers, serverFullAddr)
		}
	}

	// Store
	st, err := store.Get("etcd")
	if err != nil {
		return err
	}
	if err = st.Initialize(name, servers); err != nil {
		return err
	}

	// Orch
	var orchest orch.Orch
	orchest = swarm.Swarm{Store: st}
	if err = orchest.Initialize(servers); err != nil {
		return err
	}

	// Wamp Router
	swarm, err := swarm.New(st)
	if err != nil {
		return err
	}
	if _, err = wampRouter(swarm); err != nil {
		return err
	}

	cf.SetPool(name, st.Addr())
	cf.SetMainPool(name)
	if err = cf.Save(); err != nil {
		return err
	}
	fmt.Println("Cluster initialized")
	return nil
}