func getRouterForBox(box *provision.Box) (router.Router, error) { routerName, err := box.GetRouter() if err != nil { return nil, err } return router.Get(routerName) }
func (p *dockerProvisioner) deployPipeline(box *provision.Box, imageId string, w io.Writer) (string, error) { fmt.Fprintf(w, "\n--- deploy box (%s, image:%s)\n", box.GetFullName(), imageId) actions := []*action.Action{ &updateStatusInRiak, &createContainer, &startContainer, &updateStatusInRiak, &setNetworkInfo, &followLogsAndCommit, } pipeline := action.NewPipeline(actions...) args := runContainerActionsArgs{ box: box, imageId: imageId, writer: w, isDeploy: true, buildingImage: imageId, containerStatus: provision.StatusLaunching, provisioner: p, } err := pipeline.Execute(args) if err != nil { fmt.Fprintf(w, "deploy pipeline for box (%s)\n --> %s", box.GetFullName(), err) return "", err } return imageId, nil }
func (p *oneProvisioner) UnsetCName(box *provision.Box, cname string) error { r, err := getRouterForBox(box) if err != nil { return err } return r.UnsetCName(cname, box.GetFullName()) }
func (p *oneProvisioner) Stop(box *provision.Box, process string, w io.Writer) error { fmt.Fprintf(w, "\n--- stoping box (%s)\n", box.GetFullName()) args := runMachineActionsArgs{ box: box, writer: w, isDeploy: false, machineStatus: provision.StatusStopping, provisioner: p, } actions := []*action.Action{ &updateStatusInRiak, &stopMachine, &updateStatusInRiak, } pipeline := action.NewPipeline(actions...) err := pipeline.Execute(args) if err != nil { fmt.Fprintf(w, "--- stoping box (%s)\n --> %s", box.GetFullName(), err) return err } return nil }
func (p *oneProvisioner) Destroy(box *provision.Box, w io.Writer) error { fmt.Fprintf(w, "\n--- destroying box (%s)\n", box.GetFullName()) args := runMachineActionsArgs{ box: box, writer: w, isDeploy: false, machineStatus: provision.StatusDestroying, provisioner: p, } actions := []*action.Action{ &updateStatusInRiak, &destroyOldMachine, &destroyOldRoute, } pipeline := action.NewPipeline(actions...) err := pipeline.Execute(args) if err != nil { fmt.Fprintf(w, "--- destroying box (%s)\n --> %s", box.GetFullName(), err) return err } err = doneNotify(box, w, alerts.DESTROYED) return nil }
//start by validating the image. //1. &updateStatus in Riak - Deploying.. //2. &create an inmemory machine type from a Box. //3. &updateStatus in Riak - Creating.. //4. &followLogs by posting it in the queue. func (p *oneProvisioner) deployPipeline(box *provision.Box, imageId string, w io.Writer) (string, error) { fmt.Fprintf(w, "--- deploy box (%s, image:%s)\n", box.GetFullName(), imageId) actions := []*action.Action{ &updateStatusInRiak, &createMachine, &updateStatusInRiak, &followLogs, } pipeline := action.NewPipeline(actions...) args := runMachineActionsArgs{ box: box, imageId: imageId, writer: w, isDeploy: true, machineStatus: provision.StatusLaunching, provisioner: p, } err := pipeline.Execute(args) if err != nil { fmt.Fprintf(w, "--- deploy pipeline for box (%s, image:%s)\n --> %s", box.GetFullName(), imageId, err) return "", err } return imageId, nil }
func (p *dockerProvisioner) ImageDeploy(box *provision.Box, imageId string, w io.Writer) (string, error) { isValid, err := isValidBoxImage(box.GetFullName(), imageId) if err != nil { return "", err } if !isValid { return "", fmt.Errorf("invalid image for box %s: %s", box.GetFullName(), imageId) } return p.deployPipeline(box, imageId, w) }
func (p *oneProvisioner) ImageDeploy(box *provision.Box, imageId string, w io.Writer) (string, error) { isValid, err := isValidBoxImage(box.GetFullName(), imageId) if err != nil { return "", err } if !isValid { imageId = p.getBuildImage(box.Repo, box.ImageVersion) } return p.deployPipeline(box, imageId, w) }
//this is essentially converting box to a container. func (p *dockerProvisioner) GetContainerByBox(box *provision.Box) (*container.Container, error) { return &container.Container{ BoxId: box.Id, CartonId: box.CartonId, Name: box.Name, BoxName: box.GetFullName(), Level: box.Level, Status: box.Status, }, nil }
func (*dockerProvisioner) Addr(box *provision.Box) (string, error) { r, err := getRouterForBox(box) if err != nil { log.Errorf("Failed to get router: %s", err) return "", err } addr, err := r.Addr(box.GetFullName()) if err != nil { log.Errorf("Failed to obtain box %s address: %s", box.GetFullName(), err) return "", err } return addr, nil }
func (p *dockerProvisioner) Stop(box *provision.Box, process string, w io.Writer) error { containers, err := p.listContainersByBox(box) if err != nil { fmt.Fprintf(w, "Failed to list box containers (%s)\n --> %s", box.GetFullName(), err) } return runInContainers(containers, func(c *container.Container, _ chan *container.Container) error { err := c.Stop(p) if err != nil { log.Errorf("Failed to stop %q: %s", box.GetFullName(), err) } return err }, nil, true) }
func doneNotify(box *provision.Box, w io.Writer, evtAction alerts.EventAction) error { fmt.Fprintf(w, "\n--- done %s box \n", box.GetFullName()) mi := make(map[string]string) mi[alerts.VERTNAME] = box.GetFullName() mi[alerts.VERTTYPE] = box.Tosca newEvent := events.NewMulti( []*events.Event{ &events.Event{ AccountsId: box.AccountsId, EventAction: evtAction, EventType: events.EventUser, EventData: events.EventData{M: mi}, Timestamp: time.Now().Local(), }, }) return newEvent.Write() }
func (p *dockerProvisioner) Start(box *provision.Box, process string, w io.Writer) error { containers, err := p.listContainersByBox(box) if err != nil { fmt.Fprintf(w, "Failed to list box containers (%s)\n --> %s", box.GetFullName(), err) } return runInContainers(containers, func(c *container.Container, _ chan *container.Container) error { err := c.Start(&container.StartArgs{ Provisioner: p, Box: box, }) if err != nil { return err } c.SetStatus(provision.StatusStarting) if info, err := c.NetworkInfo(p); err == nil { p.fixContainer(c, info) } return nil }, nil, true) }
func (p *dockerProvisioner) SetBoxStatus(box *provision.Box, w io.Writer, status provision.Status) error { fmt.Fprintf(w, "\n---- status %s box %s ----\n", box.GetFullName(), status.String()) actions := []*action.Action{ &updateStatusInRiak, } pipeline := action.NewPipeline(actions...) args := runContainerActionsArgs{ box: box, writer: w, containerStatus: status, provisioner: p, } err := pipeline.Execute(args) if err != nil { log.Errorf("error on execute status pipeline for box %s - %s", box.GetFullName(), err) return err } return nil }
func (p *oneProvisioner) SetState(box *provision.Box, w io.Writer, changeto provision.Status) error { fmt.Fprintf(w, "\n--- stateto %s\n", box.GetFullName()) args := runMachineActionsArgs{ box: box, writer: w, machineStatus: changeto, provisioner: p, } actions := []*action.Action{ &changeStateofMachine, &addNewRoute, } pipeline := action.NewPipeline(actions...) err := pipeline.Execute(args) if err != nil { return err } err = doneNotify(box, w, alerts.LAUNCHED) return err }
func (p *dockerProvisioner) Destroy(box *provision.Box, w io.Writer) error { fmt.Fprintf(w, "\n--- destroying box (%s) ----\n", box.GetFullName()) containers, err := p.listContainersByBox(box) if err != nil { fmt.Fprintf(w, "Failed to list box containers (%s)\n --> %s", box.GetFullName(), err) return err } args := changeUnitsPipelineArgs{ box: box, toRemove: containers, writer: ioutil.Discard, provisioner: p, boxDestroy: true, } pipeline := action.NewPipeline( &destroyOldContainers, &removeOldRoutes, ) err = pipeline.Execute(args) if err != nil { return err } return nil }