// Init initializes a new swarm cluster. func (d *SwarmDaemon) Init(autoAccept map[string]bool, secret string) error { req := swarm.InitRequest{ ListenAddr: d.listenAddr, } for _, role := range []swarm.NodeRole{swarm.NodeRoleManager, swarm.NodeRoleWorker} { policy := swarm.Policy{ Role: role, Autoaccept: autoAccept[strings.ToLower(string(role))], } if secret != "" { policy.Secret = &secret } req.Spec.AcceptancePolicy.Policies = append(req.Spec.AcceptancePolicy.Policies, policy) } status, out, err := d.SockRequest("POST", "/swarm/init", req) if status != http.StatusOK { return fmt.Errorf("initializing swarm: invalid statuscode %v, %q", status, out) } if err != nil { return fmt.Errorf("initializing swarm: %v", err) } info, err := d.info() if err != nil { return err } d.Info = info return nil }
// SwarmFromGRPC converts a grpc Cluster to a Swarm. func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm { swarm := types.Swarm{ ID: c.ID, Spec: types.Spec{ Orchestration: types.OrchestrationConfig{ TaskHistoryRetentionLimit: c.Spec.Orchestration.TaskHistoryRetentionLimit, }, Raft: types.RaftConfig{ SnapshotInterval: c.Spec.Raft.SnapshotInterval, KeepOldSnapshots: c.Spec.Raft.KeepOldSnapshots, LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers, HeartbeatTick: c.Spec.Raft.HeartbeatTick, ElectionTick: c.Spec.Raft.ElectionTick, }, }, } heartbeatPeriod, _ := ptypes.Duration(c.Spec.Dispatcher.HeartbeatPeriod) swarm.Spec.Dispatcher.HeartbeatPeriod = uint64(heartbeatPeriod) swarm.Spec.CAConfig.NodeCertExpiry, _ = ptypes.Duration(c.Spec.CAConfig.NodeCertExpiry) for _, ca := range c.Spec.CAConfig.ExternalCAs { swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{ Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())), URL: ca.URL, Options: ca.Options, }) } // Meta swarm.Version.Index = c.Meta.Version.Index swarm.CreatedAt, _ = ptypes.Timestamp(c.Meta.CreatedAt) swarm.UpdatedAt, _ = ptypes.Timestamp(c.Meta.UpdatedAt) // Annotations swarm.Spec.Name = c.Spec.Annotations.Name swarm.Spec.Labels = c.Spec.Annotations.Labels for _, policy := range c.Spec.AcceptancePolicy.Policies { p := types.Policy{ Role: types.NodeRole(strings.ToLower(policy.Role.String())), Autoaccept: policy.Autoaccept, } if policy.Secret != nil { secret := string(policy.Secret.Data) p.Secret = &secret } swarm.Spec.AcceptancePolicy.Policies = append(swarm.Spec.AcceptancePolicy.Policies, p) } return swarm }