// RestoreServices reverts service data
327845
func (f *Facade) RestoreServices(ctx datastore.Context, tenantID string, svcs []service.Service) error {
	defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.RestoreServices"))
	// get pools
	pools, err := f.GetResourcePools(ctx)
	if err != nil {
		glog.Errorf("Could not look up resource pools: %s", err)
		return err
	}
	poolsmap := make(map[string]struct{})
	for _, pool := range pools {
		poolsmap[pool.ID] = struct{}{}
	}
	// remove services for tenant
	if err := f.removeService(ctx, tenantID); err != nil {
		if !datastore.IsErrNoSuchEntity(err) {
			return err
		}
	}
	// get service tree
	svcsmap := make(map[string][]service.Service)
	for _, svc := range svcs {
		svcsmap[svc.ParentServiceID] = append(svcsmap[svc.ParentServiceID], svc)
	}
	// add the services
	var traverse func(parentID string) error
	traverse = func(parentID string) error {
		for _, svc := range svcsmap[parentID] {
			alog := f.auditLogger.Message(ctx, "Restore Service").WithField("servicename", svc.Name).Action(audit.Restore).Entity(&svc)
			svc.DatabaseVersion = 0
			svc.DesiredState = int(service.SVCStop)
			if _, ok := poolsmap[svc.PoolID]; !ok {
				glog.Warningf("Could not find pool %s for service %s (%s).  Setting pool to default.", svc.PoolID, svc.Name, svc.ID)
				svc.PoolID = "default"
			}
			if err := f.addService(ctx, tenantID, svc, true); err != nil {
				glog.Errorf("Could not restore service %s (%s): %s", svc.Name, svc.ID, err)
				return alog.Error(err)
			}
			if err := f.restoreIPs(ctx, &svc); err != nil {
				glog.Warningf("Could not restore address assignments for service %s (%s): %s", svc.Name, svc.ID, err)
			}
			if err := traverse(svc.ID); err != nil {
				return alog.Error(err)
			}
			alog.Succeeded()
			glog.Infof("Restored service %s (%s)", svc.Name, svc.ID)
		}
		return nil
	}
	if err := traverse(""); err != nil {
		glog.Errorf("Error while rolling back services: %s", err)
		return err
	}
	return nil
}
1290561
func Restore(config *api.Config, cmd *cobra.Command) {
	data, err := ioutil.ReadFile(DefaultConfigPath)
	if err != nil {
		data, err = ioutil.ReadFile(".stifile")
		if err != nil {
			glog.V(1).Infof("Unable to restore %s: %v", DefaultConfigPath, err)
			return
		}
		glog.Infof("DEPRECATED: Use %s instead of .stifile", DefaultConfigPath)
	}

	c := Config{}
	if err := json.Unmarshal(data, &c); err != nil {
		glog.V(1).Infof("Unable to parse %s: %v", DefaultConfigPath, err)
		return
	}

	source, err := git.Parse(c.Source)
	if err != nil {
		glog.V(1).Infof("Unable to parse %s: %v", c.Source, err)
		return
	}

	config.BuilderImage = c.BuilderImage
	config.Source = source
	config.Tag = c.Tag

	envOverride := false
	if cmd.Flag("env").Changed {
		envOverride = true
	}

	for name, value := range c.Flags {
		// Do not change flags that user sets. Allow overriding of stored flags.
		if name == "env" {
			if envOverride {
				continue
			}
			for _, v := range strings.Split(value, ",") {
				cmd.Flags().Set(name, v)
			}
		} else if savedEnvMatcher.MatchString(name) {
			if envOverride {
				continue
			}
			cmd.Flags().Set("env", value)
		} else {
			if cmd.Flag(name).Changed {
				continue
			}
			cmd.Flags().Set(name, value)
		}
	}
}
1218074
func (c *client) Restore(ctx context.Context, id string, attachStdio libcontainerdtypes.StdioCallback) (bool, int, libcontainerdtypes.Process, error) {
	c.logger.WithField("container", id).Debug("restore()")

	// TODO Windows: On RS1, a re-attach isn't possible.
	// However, there is a scenario in which there is an issue.
	// Consider a background container. The daemon dies unexpectedly.
	// HCS will still have the compute service alive and running.
	// For consistence, we call in to shoot it regardless if HCS knows about it
	// We explicitly just log a warning if the terminate fails.
	// Then we tell the backend the container exited.
	if hc, err := hcsshim.OpenContainer(id); err == nil {
		const terminateTimeout = time.Minute * 2
		err := hc.Terminate()

		if hcsshim.IsPending(err) {
			err = hc.WaitTimeout(terminateTimeout)
		} else if hcsshim.IsAlreadyStopped(err) {
			err = nil
		}

		if err != nil {
			c.logger.WithField("container", id).WithError(err).Debug("terminate failed on restore")
			return false, -1, nil, err
		}
	}
	return false, -1, &restoredProcess{
		c:  c,
		id: id,
	}, nil
}
757467
func (s *ListRestoreJobsOutput) SetRestoreJobs(v []*RestoreJobsListMember) *ListRestoreJobsOutput {
	s.RestoreJobs = v
	return s
}
1270309
func LockServices(conn client.Connection, svcs []ServiceLockNode) error {
	tx := conn.NewTransaction()
	for _, svc := range svcs {
		node, err := NewServiceNodeFromService(&service.Service{})
		if err != nil {
			return NewLockServiceFailure(svc.ServiceID, svc.PoolID, err)
		}
		if err := conn.Get(svc.Path(), node); err != nil {
			return NewLockServiceFailure(svc.ServiceID, svc.PoolID, err)
		}
		node.Locked = true
		tx.Set(svc.Path(), node)
	}
	return tx.Commit()
}
1069750
func (s *StanServer) postRecoveryProcessing(recoveredClients []*stores.Client, recoveredSubs []*subState) {
	for _, sub := range recoveredSubs {
		sub.Lock()
		// Consider this subscription initialized. Note that it may
		// still have newOnHold == true, which would prevent incoming
		// messages to be delivered before we attempt to redeliver
		// unacknowledged messages in performRedeliveryOnStartup.
		sub.initialized = true
		sub.Unlock()
	}
	// Go through the list of clients and ensure their Hb timer is set. Only do
	// this for standalone mode. If clustered, timers will be setup on leader
	// election.
	if !s.isClustered {
		for _, sc := range recoveredClients {
			// Because of the loop, we need to make copy for the closure
			cID := sc.ID
			s.clients.setClientHB(cID, s.opts.ClientHBInterval, func() {
				s.checkClientHealth(cID)
			})
		}
	}
}
429507
func (c *ServicedCli) buildServicePaths(svcs []service.ServiceDetails) (map[string]string, error) {
	svcMap := make(map[string]service.ServiceDetails)
	for _, svc := range svcs {
		svcMap[svc.ID] = svc
	}

	// likely that svcs contains all services since it was likely populated with getServices()
	// however, ensure that parent services are in svcMap
	for _, svc := range svcs {
		parentID := svc.ParentServiceID
		for parentID != "" {
			if _, ok := svcMap[parentID]; ok {
				break // break from inner for loop
			}
			svc, err := c.driver.GetServiceDetails(parentID)
			if err != nil || svc == nil {
				return nil, fmt.Errorf("unable to retrieve service for id:%s %s", parentID, err)
			}
			svcMap[parentID] = *svc

			parentID = svc.ParentServiceID
		}
	}

	// recursively build full path for all services
	pathmap := make(map[string]string)
	for _, svc := range svcs {
		fullpath := svc.Name
		parentServiceID := svc.ParentServiceID

		for parentServiceID != "" {
			fullpath = path.Join(svcMap[parentServiceID].Name, fullpath)
			parentServiceID = svcMap[parentServiceID].ParentServiceID
		}

		pathmap[svc.ID] = strings.ToLower(fullpath)
	}

	return pathmap, nil
}
672683
func (g *Cloud) ComputeServices() *Services {
	return &Services{g.service, g.serviceAlpha, g.serviceBeta}
}
990839
func (o *BackendServiceConfig) SetBackendServices(v []*BackendService) *BackendServiceConfig {
	if o.BackendServices = v; o.BackendServices == nil {
		o.nullFields = append(o.nullFields, "BackendServices")
	}
	return o
}
126416
func (d *Device) SetServices(svcs []*ble.Service) error {
	return d.Server.SetServices(svcs)
}
2228
func (s *ErrorRootCause) SetServices(v []*ErrorRootCauseService) *ErrorRootCause {
	s.Services = v
	return s
}
1036020
func (_m *DFS) Restore(r io.Reader, version int) error {
	ret := _m.Called(r, version)

	var r0 error
	if rf, ok := ret.Get(0).(func(io.Reader, int) error); ok {
		r0 = rf(r, version)
	} else {
		r0 = ret.Error(0)
	}

	return r0
}
114566
func (shim backendShim) RescaleService(name string) error {
	service, err := shim.st.Application(name)
	if err != nil {
		return errors.Trace(err)
	}
	return service.EnsureMinUnits()
}
1324819
func (w *upgradeSeriesWorker) unitServices() (map[string]string, error) {
	services, err := w.service.ListServices()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return service.FindUnitServiceNames(services), nil
}
885237
func (s *Store) Restore(rc io.ReadCloser) error {
	s.restoreMu.Lock()
	defer s.restoreMu.Unlock()

	// Get size of database.
	var sz uint64
	if err := binary.Read(rc, binary.LittleEndian, &sz); err != nil {
		return err
	}

	// Now read in the database file data and restore.
	database := make([]byte, sz)
	if _, err := io.ReadFull(rc, database); err != nil {
		return err
	}

	// Create temp file and write incoming database to there.
	temp, err := tempfile()
	if err != nil {
		return err
	}
	defer os.Remove(temp.Name())
	defer temp.Close()

	if _, err := temp.Write(database); err != nil {
		return err
	}

	// Create new database from file, connect, and load
	// existing database from that.
	db, err := sdb.New(temp.Name(), "", false)
	if err != nil {
		return err
	}
	conn, err := db.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()
	if err := s.dbConn.Load(conn); err != nil {
		return err
	}

	// Get size of meta, read those bytes, and set to meta.
	if err := binary.Read(rc, binary.LittleEndian, &sz); err != nil {
		return err
	}
	meta := make([]byte, sz)
	if _, err := io.ReadFull(rc, meta); err != nil {
		return err
	}
	err = func() error {
		s.metaMu.Lock()
		defer s.metaMu.Unlock()
		return json.Unmarshal(meta, &s.meta)
	}()
	if err != nil {
		return err
	}

	// Get size of connections, read those bytes, and set to connections.
	if err := binary.Read(rc, binary.LittleEndian, &sz); err != nil {
		return err
	}
	conns := make([]byte, sz)
	if _, err := io.ReadFull(rc, conns); err != nil {
		return err
	}
	err = func() error {
		s.connsMu.Lock()
		defer s.connsMu.Unlock()
		if err := json.Unmarshal(conns, &s.conns); err != nil {
			return err
		}
		for _, c := range s.conns {
			dbConn, err := s.db.Connect()
			if err != nil {
				return err
			}
			c.Restore(dbConn, s)
		}
		return nil
	}()
	if err != nil {
		return err
	}

	stats.Add(numRestores, 1)
	return nil
}
326014
func (w *restoreWorker) RestoreStatus() state.RestoreStatus {
	w.mu.Lock()
	defer w.mu.Unlock()
	return w.status
}
1320616
func (s *EventFilter) SetServices(v []*string) *EventFilter {
	s.Services = v
	return s
}