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

	if len(*deviceinitDevice) == 0 {
		fmt.Fprintf(os.Stderr, "Invalid params.\n")
		os.Exit(1)
	}

	// verify deviceinitNodebase
	devicePath := path.Join(*deviceinitNodebase, swarm.DEVICES_DIR, *deviceinitDevice)
	if !swarm.ExistPath(devicePath) {
		fmt.Fprintf(os.Stderr, "Error: device path %s not exists\n", devicePath)
		os.Exit(1)
	}
	uuidPath := path.Join(devicePath, swarm.UUID_FILE)
	if swarm.ExistPath(uuidPath) {
		fmt.Fprintf(os.Stderr, "Error: uuid file %s exists already\n", uuidPath)
		os.Exit(1)
	}

	// prepare dirs
	blocksPath := path.Join(devicePath, swarm.BLOCKS_DIR)
	err := os.MkdirAll(blocksPath, 0755)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed make dir %s, error: %s.\n", blocksPath, err)
		os.Exit(1)
	}

	// prepare uuid
	f, err := swarm.CreateFile(uuidPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed create file %s, error: %s.\n", uuidPath, err)
		os.Exit(1)
	}
	defer f.Close()
	f.Write([]byte(swarm.Uuid()))
}
Ejemplo 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)
}