Esempio n. 1
0
func runDomaininit(cmd *Command, args []string) {
	// verify flags
	if len(*domaininitDomainId) == 0 {
		fmt.Fprintf(os.Stderr, "Invalid params.\n")
		os.Exit(1)
	}

	domainKey, err := swarm.NewRsaKey()
	if err != nil {
		fmt.Fprintf(os.Stderr, "GenerateKey failed: %s\n", err)
		os.Exit(1)
	}

	// create domainCert
	domainCert := swarm.DomainCert{
		DomainId:  *domaininitDomainId,
		DomainKey: *domainKey,
	}

	// encode
	encoded, err := swarm.EncodeB64Gob(domainCert)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Gob encode failed: %s\n", err)
		os.Exit(1)
	}

	// write to file
	filePath := "/tmp/domain.cert"
	f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed open file %s, error: %s\n", filePath, err)
		os.Exit(1)
	}
	defer f.Close()
	f.Write(encoded)

	fmt.Println("Domain cert /tmp/domain.cert created, please keep it safely.")
}
Esempio n. 2
0
func runNodeinit(cmd *Command, args []string) {
	// verify flags
	if len(*nodeinitNodebase) == 0 || len(*nodeinitDomaincert) == 0 || len(*nodeinitNodeaddress) == 0 || len(*nodeinitOSSaddress) == 0 {
		fmt.Fprintf(os.Stderr, "Invalid params.\n")
		os.Exit(1)
	}

	// parse cert
	domainCert, err := loadDomainCert()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Load key failed: %s\n", err)
		os.Exit(1)
	}

	// domain pub key
	domainPubKeyEncoded, err := swarm.EncodeB64Gob(domainCert.DomainKey.PublicKey)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Gob encode failed: %s\n", err)
		os.Exit(1)
	}

	// sig
	nodeCert := swarm.NodeCert{
		DomainId: domainCert.DomainId,
		NodeId:   *nodeinitNodeaddress,
	}

	nodeSigBase64, err := swarm.DoNodeSig(&nodeCert, &domainCert.DomainKey)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Sig node cert failed: %s\n", err)
		os.Exit(1)
	}

	// prepare dirs
	mkdir(swarm.CONF_DIR)
	mkdir(swarm.DEVICES_DIR)
	mkdir(swarm.LOGS_DIR)

	// prepare files
	targetPath := path.Join(*nodeinitNodebase, swarm.CONF_DIR, swarm.NODE_CONF_FILE)
	f, err := swarm.CreateFile(targetPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed create file %s, error: %s.\n", targetPath, err)
		os.Exit(1)
	}
	defer f.Close()

	// write config file
	defaultConfig := []string{}
	defaultConfig = append(defaultConfig, "[default]")
	defaultConfig = append(defaultConfig, fmt.Sprintf("LOG_FILE=%s", "logs/swarm.log"))
	defaultConfig = append(defaultConfig, fmt.Sprintf("NODE_ADDRESS=%s", *nodeinitNodeaddress))
	defaultConfig = append(defaultConfig, fmt.Sprintf("OSS_ADDRESS=%s", *nodeinitOSSaddress))
	defaultConfig = append(defaultConfig, fmt.Sprintf("DOMAIN_ID=%s", domainCert.DomainId))
	defaultConfig = append(defaultConfig, fmt.Sprintf("DOMAIN_PUB_KEY=%s", domainPubKeyEncoded))
	defaultConfig = append(defaultConfig, fmt.Sprintf("NODE_SIG=%s", nodeSigBase64))
	defaultConfig = append(defaultConfig, "")

	f.Write([]byte(strings.Join(defaultConfig, "\n")))

	fmt.Printf("Node %s initiated.\n", *nodeinitNodebase)
}