Esempio n. 1
0
File: main.go Progetto: sysbot/mayu
func main() {
	log.SetFlags(0)
	log.SetPrefix("mayu: ")
	flag.Set("logtostderr", "true")
	flag.Parse()

	if *showVersion {
		printVersion()
		os.Exit(0)
	}

	glog.V(8).Infoln(fmt.Sprintf("starting mayu version %s", projectVersion))

	hostmgr.DisableGit = *noGit

	var err error

	conf, err = loadConfig(*confFile)
	if err != nil {
		glog.Fatalln(err)
	}

	if ok, err := conf.Validate(); !ok {
		glog.Fatalln(err)
	}

	var cluster *hostmgr.Cluster

	if fileExists(*clusterDir) {
		cluster, err = hostmgr.OpenCluster(*clusterDir)
	} else {
		cluster, err = hostmgr.NewCluster(*clusterDir, true)
	}

	if err != nil {
		glog.Fatalf("unable to get a cluster: %s\n", err)
	}

	pxeManager, err := defaultPXEManager(cluster)
	if err != nil {
		glog.Fatalf("unable to create a pxe manager: %s\n", err)
	}

	if *showTemplates {
		placeholderHost := hostmgr.Host{}

		os.Stdout.WriteString("last stage cloud config:\n")
		pxeManager.writeLastStageCC(placeholderHost, os.Stdout)

		b := bytes.NewBuffer(nil)
		pxeManager.writeLastStageCC(placeholderHost, b)
		yamlErr := validateCC(b.Bytes())
		if yamlErr != nil {
			fmt.Errorf("error found while checking generated cloud-config: %+v", yamlErr)
			os.Exit(1)
		}

		os.Exit(0)
	}

	err = pxeManager.Start()
	if err != nil {
		glog.Errorln(err)
	}
}
Esempio n. 2
0
func mainRun(cmd *cobra.Command, args []string) {
	glog.V(8).Infof("starting mayu version %s", projectVersion)

	var err error

	// hack to make some dnsmasq versions happy
	globalFlags.tFTPRoot, err = filepath.Abs(globalFlags.tFTPRoot)

	if ok, err := globalFlags.Validate(); !ok {
		glog.Fatalln(err)
	}

	hostmgr.DisableGit = globalFlags.noGit

	var cluster *hostmgr.Cluster

	if fileExists(fmt.Sprintf("%s/cluster.json", globalFlags.clusterDir)) {
		cluster, err = hostmgr.OpenCluster(globalFlags.clusterDir)
	} else {
		cluster, err = hostmgr.NewCluster(globalFlags.clusterDir, true)
	}

	if err != nil {
		glog.Fatalf("unable to get a cluster: %s\n", err)
	}
	if globalFlags.useIgnition && globalFlags.templateSnippets == DefaultTemplateSnippets {
		globalFlags.templateSnippets = DefaultIgnitionTemplateSnippets
	}

	pxeManager, err := pxemgr.PXEManager(pxemgr.PXEManagerConfiguration{
		ConfigFile:               globalFlags.configFile,
		UseInternalEtcdDiscovery: globalFlags.useInternalEtcdDiscovery,
		EtcdQuorumSize:           globalFlags.etcdQuorumSize,
		EtcdDiscoveryUrl:         globalFlags.etcdDiscoveryUrl,
		EtcdEndpoint:             globalFlags.etcdEndpoint,
		DNSmasqExecutable:        globalFlags.dnsmasq,
		DNSmasqTemplate:          globalFlags.dnsmasqTemplate,
		TFTPRoot:                 globalFlags.tFTPRoot,
		NoTLS:                    globalFlags.noTLS,
		HTTPPort:                 globalFlags.httpPort,
		HTTPBindAddress:          globalFlags.httpBindAddress,
		TLSCertFile:              globalFlags.tlsCertFile,
		TLSKeyFile:               globalFlags.tlsKeyFile,
		YochuPath:                globalFlags.yochuPath,
		StaticHTMLPath:           globalFlags.staticHTMLPath,
		TemplateSnippets:         globalFlags.templateSnippets,
		LastStageCloudconfig:     globalFlags.lastStageCloudconfig,
		IgnitionConfig:           globalFlags.ignitionConfig,
		UseIgnition:              globalFlags.useIgnition,
		FirstStageScript:         globalFlags.firstStageScript,
		ImagesCacheDir:           globalFlags.imagesCacheDir,
		Version:                  projectVersion,
	}, cluster)
	if err != nil {
		glog.Fatalf("unable to create a pxe manager: %s\n", err)
	}

	if globalFlags.showTemplates {
		placeholderHost := hostmgr.Host{}

		if globalFlags.useIgnition {
			b := bytes.NewBuffer(nil)
			if err := pxeManager.WriteIgnitionConfig(placeholderHost, b); err != nil {
				fmt.Errorf("error found while checking generated ignition config: %+v", err)
				os.Exit(1)
			}
			os.Stdout.WriteString("ignition config:\n")
			os.Stdout.WriteString(b.String())
		} else {
			os.Stdout.WriteString("last stage cloud config:\n")
			pxeManager.WriteLastStageCC(placeholderHost, os.Stdout)

			b := bytes.NewBuffer(nil)
			pxeManager.WriteLastStageCC(placeholderHost, b)
			yamlErr := validateYAML(b.Bytes())
			if yamlErr != nil {
				fmt.Errorf("error found while checking generated cloud-config: %+v", yamlErr)
				os.Exit(1)
			}
		}

		os.Exit(0)
	}

	err = pxeManager.Start()
	if err != nil {
		glog.Errorln(err)
	}
}