<commit_before>package cmd

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"path"
	"strconv"
	"strings"

	"github.com/spf13/cobra"

	"github.com/quantum/castle/pkg/castled"
	"github.com/quantum/castle/pkg/util"
	"github.com/quantum/clusterd/pkg/orchestrator"
	"github.com/quantum/clusterd/pkg/store"
)

var (
	discoveryURL string
	etcdURLs     string
	nodeID       string
	privateIPv4  string
	devices      string
	forceFormat  bool
)

var rootCmd = &cobra.Command{
	Use:   "castled",
	Short: "castled tool for bootstrapping and running castle storage.",
	Long:  `https://github.com/quantum/castle`,
}

func init() {
	rootCmd.Flags().StringVar(&nodeID, "node-id", "12345", "unique ID for the node (required)")
	rootCmd.Flags().StringVar(&discoveryURL, "discovery-url", "http://discovery.castle.com/26bd83c92e7145e6b103f623263f61df",
		"etcd discovery URL")
	rootCmd.Flags().StringVar(&etcdURLs, "etcd-urls", "http://127.0.0.1:4001",
		"comma separated list of etcd listen URLs (required), ignored if the discovery URL is specified")
	rootCmd.Flags().StringVar(&privateIPv4, "private-ipv4", "", "private IPv4 address for this machine (required)")
	rootCmd.Flags().StringVar(&devices, "devices", "", "comma separated list of devices to use")
	rootCmd.Flags().BoolVar(&forceFormat, "force-format", false,
		"true to force the format of any specified devices, even if they already have a filesystem.  BE CAREFUL!")

	rootCmd.MarkFlagRequired("node-id")
	rootCmd.MarkFlagRequired("private-ipv4")

	rootCmd.RunE = joinCluster
}

func Execute() error {
	addCommands()
	return rootCmd.Execute()
}

func addCommands() {
	rootCmd.AddCommand(versionCmd)
	rootCmd.AddCommand(daemonCmd)
}

func joinCluster(cmd *cobra.Command, args []string) error {
	if err := util.verifyRequiredFlags(cmd, []string{"node-id", "private-ipv4"}); err != nil {
		return err
	}

	// TODO: Get the etcd client with the discovery token rather than the etcd endpoints
	// get an etcd client to coordinate with the rest of the cluster and load/save config
	etcdClient, err := store.GetEtcdClient(strings.Split(etcdURLs, ","))
	if err != nil {
		return err
	}

	// Write desired state for this machine to etcd
	baseKey := path.Join(orchestrator.DesiredNodesKey, nodeID)
	properties := map[string]string{
		"privateIPv4": privateIPv4,
		"devices":     devices,
		"forceFormat": strconv.FormatBool(forceFormat),
	}
	if err := orchestrator.StoreEtcdProperties(etcdClient, baseKey, properties); err != nil {
		return err<commit_msg>Fix merge conflict issue<commit_after>package cmd

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"path"
	"strconv"
	"strings"

	"github.com/spf13/cobra"

	"github.com/quantum/castle/pkg/castled"
	"github.com/quantum/castle/pkg/util"
	"github.com/quantum/clusterd/pkg/orchestrator"
	"github.com/quantum/clusterd/pkg/store"
)

var (
	discoveryURL string
	etcdURLs     string
	nodeID       string
	privateIPv4  string
	devices      string
	forceFormat  bool
)

var rootCmd = &cobra.Command{
	Use:   "castled",
	Short: "castled tool for bootstrapping and running castle storage.",
	Long:  `https://github.com/quantum/castle`,
}

func init() {
	rootCmd.Flags().StringVar(&nodeID, "node-id", "12345", "unique ID for the node (required)")
	rootCmd.Flags().StringVar(&discoveryURL, "discovery-url", "http://discovery.castle.com/26bd83c92e7145e6b103f623263f61df",
		"etcd discovery URL")
	rootCmd.Flags().StringVar(&etcdURLs, "etcd-urls", "http://127.0.0.1:4001",
		"comma separated list of etcd listen URLs (required), ignored if the discovery URL is specified")
	rootCmd.Flags().StringVar(&privateIPv4, "private-ipv4", "", "private IPv4 address for this machine (required)")
	rootCmd.Flags().StringVar(&devices, "devices", "", "comma separated list of devices to use")
	rootCmd.Flags().BoolVar(&forceFormat, "force-format", false,
		"true to force the format of any specified devices, even if they already have a filesystem.  BE CAREFUL!")

	rootCmd.MarkFlagRequired("node-id")
	rootCmd.MarkFlagRequired("private-ipv4")

	rootCmd.RunE = joinCluster
}

func Execute() error {
	addCommands()
	return rootCmd.Execute()
}

func addCommands() {
	rootCmd.AddCommand(versionCmd)
	rootCmd.AddCommand(daemonCmd)
}

func joinCluster(cmd *cobra.Command, args []string) error {
	if err := util.VerifyRequiredFlags(cmd, []string{"node-id", "private-ipv4"}); err != nil {
		return err
	}

	// TODO: Get the etcd client with the discovery token rather than the etcd endpoints
	// get an etcd client to coordinate with the rest of the cluster and load/save config
	etcdClient, err := store.GetEtcdClient(strings.Split(etcdURLs, ","))
	if err != nil {
		return err
	}

	// Write desired state for this machine to etcd
	baseKey := path.Join(orchestrator.DesiredNodesKey, nodeID)
	properties := map[string]string{
		"privateIPv4": privateIPv4,
		"devices":     devices,
		"forceFormat": strconv.FormatBool(forceFormat),
	}
	if err := orchestrator.StoreEtcdProperties(etcdClient, baseKey, properties); err != nil {
		return err
{'fraction_non_alphanumeric': 0.1332530604053783, 'fraction_numerical': 0.018864138069436084, 'mean_word_length': 4.755196304849885, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1176543', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1696, 'n_words': 426}