Exemple #1
0
func ipxeBootScriptServer(w http.ResponseWriter, r *http.Request) {
	log.Printf("creating boot script for %s", r.RemoteAddr)
	v := r.URL.Query()

	options := kernel.New()

	// Process the console parameter.
	console := v.Get("console")
	if console != "" {
		options.SetConsole(strings.Split(console, ","))
	}

	// Process the cloudconfig parameter.
	cloudConfigId := v.Get("cloudconfig")
	if cloudConfigId != "" {
		options.SetCloudConfigUrl(fmt.Sprintf("http://%s/configs/%s.yml", baseUrl, cloudConfigId))
	}

	// Process the sshkey paramter.
	sshKeyId := v.Get("sshkey")
	if sshKeyId != "" {
		sshKeyPath := filepath.Join(dataDir, fmt.Sprintf("sshkeys/%s.pub", sshKeyId))
		sshKey, err := sshKeyFromFile(sshKeyPath)
		if err != nil {
			log.Printf("Error reading ssh publickey from %s: %s", sshKeyPath, err)
			http.Error(w, err.Error(), 500)
			return
		}
		options.SSHKey = sshKey
	}

	// Process the version parameter.
	version := v.Get("version")
	if version == "" {
		version = "latest"
	}

	// Process the iPXE boot script template.
	t, err := template.New("ipxebootscript").Parse(ipxeBootScript)
	if err != nil {
		log.Print("Error generating iPXE boot script: " + err.Error())
		http.Error(w, "Error generating the iPXE boot script", 500)
		return
	}
	data := map[string]string{
		"BaseUrl": baseUrl,
		"Options": options.String(),
		"Version": version,
	}
	err = t.Execute(w, data)
	if err != nil {
		log.Print("Error generating iPXE boot script: " + err.Error())
		http.Error(w, "Error generating the iPXE boot script", 500)
		return
	}
	return
}
func ipxeBootScriptServer(w http.ResponseWriter, r *http.Request) {
	log.Printf("creating boot script for %s", r.RemoteAddr)

	baseUrl := config.BaseUrl
	if baseUrl == "" {
		baseUrl = r.Host
	}
	v := r.URL.Query()

	options := kernel.New()

	// Process the profile parameter.
	profile := v.Get("profile")
	if profile != "" {
		profilePath := filepath.Join(config.DataDir, fmt.Sprintf("profiles/%s.json", profile))
		err := kernalOptionsFromFile(profilePath, options)
		if err != nil {
			log.Printf("Error reading kernal options from %s: %s", profilePath, err)
			http.Error(w, err.Error(), 500)
			return
		}
	}

	if options.CloudConfig != "" {
		options.SetCloudConfigUrl(fmt.Sprintf("http://%s/configs/%s.yml", baseUrl, options.CloudConfig))
	}
	if options.SSHKey != "" {
		sshKeyPath := filepath.Join(config.DataDir, fmt.Sprintf("sshkeys/%s.pub", options.SSHKey))
		sshKey, err := sshKeyFromFile(sshKeyPath)
		if err != nil {
			log.Printf("Error reading ssh publickey from %s: %s", sshKeyPath, err)
			http.Error(w, err.Error(), 500)
			return
		}
		options.SSHKey = sshKey
	}

	// Process the iPXE boot script template.
	t, err := template.New("ipxebootscript").Parse(ipxeBootScript)
	if err != nil {
		log.Print("Error generating iPXE boot script: " + err.Error())
		http.Error(w, "Error generating the iPXE boot script", 500)
		return
	}
	data := map[string]string{
		"BaseUrl": baseUrl,
		"Options": options.String(),
		"Version": options.Version,
	}
	err = t.Execute(w, data)
	if err != nil {
		log.Print("Error generating iPXE boot script: " + err.Error())
		http.Error(w, "Error generating the iPXE boot script", 500)
		return
	}
	return
}