ocket8888 commented on a change in pull request #4379: Rewrite isos to Golang URL: https://github.com/apache/trafficcontrol/pull/4379#discussion_r382754749
########## File path: traffic_ops/traffic_ops_golang/iso/iso.go ########## @@ -0,0 +1,368 @@ +// Package iso provides support for generating ISO images. +package iso + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "encoding/json" + "errors" + "fmt" + "net" + "net/http" + "os/exec" + "path/filepath" + "strings" + + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-rfc" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-util" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth" + "github.com/jmoiron/sqlx" +) + +// Various directories and filenames related to ISO generation. +const ( + cfgDefaultDir = "/var/www/files" // Default directory containing config file + cfgFilename = "osversions.json" // The JSON config file containing mapping of OS names to directories + cfgFilenamePerl = "osversions.cfg" // Config file name in the Perl version + + // This is the directory name inside each OS directory where + // configuration files for kickstart scripts are placed. + ksCfgDir = "ks_scripts" + + // Configuration files that are generated inside the ks_scripts directory. + ksCfgNetwork = "network.cfg" + ksCfgMgmtNetwork = "mgmt_network.cfg" + ksCfgPassword = "password.cfg" + ksCfgDisk = "disk.cfg" + ksStateOut = "state.out" + + ksAltCommand = "generate" // Optional executable that is invoked instead of mkisofs +) + +// Various database columns and values. +const ( + ksFilesParamName = "kickstart.files.location" + ksFilesParamConfigFile = "mkisofs" +) + +// Various HTTP-related values. +const ( + httpHeaderContentDisposition = "Content-Disposition" + httpHeaderContentType = "Content-Type" + httpHeaderContentDownload = "application/download" +) + +// ISOs handler is responsible for generating and returning an ISO image, +// as a streaming download. +// +// Response types: +// +// Error: +// HTTP 400 +// { +// "alerts": [ +// {"level":"error","text":"hostName is required"}, +// {"level":"error","text":"disk is required"}, +// ..., +// ] +// } +// +// Success: +// HTTP 200 +// Content-Disposition: attachment; filename="db.infra.ciab.test-centos72.iso" +// Content-Type: application/download +// +func ISOs(w http.ResponseWriter, req *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(req, nil, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, req, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + // Decode request body into isoRequest instance. + var ir isoRequest + if err := json.NewDecoder(req.Body).Decode(&ir); err != nil { + userErr := errors.New("unable to decode JSON request") + api.HandleErr(w, req, inf.Tx.Tx, http.StatusBadRequest, userErr, fmt.Errorf("%v: %v", userErr, err)) + return + } + + isos(w, req, inf.Tx, inf.User, ir) +} + +// cmdOverwriteCtxKey is used in an http.Request's context +// to set a cmd override value. +var cmdOverwriteCtxKey struct{} + +// isos performs the majority of work for the /isos endpoint handler. It is separated out from +// the exported handler for testability. +func isos(w http.ResponseWriter, req *http.Request, tx *sqlx.Tx, user *auth.CurrentUser, ir isoRequest) { + // Ensure isoRequest is valid. + if errMsgs := ir.validate(); len(errMsgs) > 0 { + writeRespErrorAlerts(w, req, errMsgs) + return + } + + // Ensure that the given OSVersionDir is defined in the osversions.json config + // file as a valid directory. This directory is later referenced for ISO creation + // and therefore must an allowed value. + if ok, err := ir.validateOSDir(tx); err != nil { + statusCode := http.StatusInternalServerError + userErr := errors.New("unable to read osversions configuration") Review comment: We're not supposed to say anything about the state of the server in responses ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
