github.com/coreos/mantle@v0.13.0/cmd/ore/esx/create-base.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package esx
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"github.com/coreos/mantle/sdk"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  var (
    27  	cmdCreateBase = &cobra.Command{
    28  		Use:   "create-base",
    29  		Short: "Create base vm on ESX",
    30  		Long: `Upload an OVF and create a base VM.
    31  
    32  After a successful run, the final line of output will be the name of the VM created.
    33  `,
    34  		RunE: runBaseCreate,
    35  	}
    36  
    37  	ovaPath    string
    38  	baseVMName string
    39  )
    40  
    41  func init() {
    42  	ESX.AddCommand(cmdCreateBase)
    43  	cmdCreateBase.Flags().StringVar(&ovaPath, "file",
    44  		sdk.BuildRoot()+"/images/amd64-usr/latest/coreos_production_vmware_ova.ova",
    45  		"path to CoreOS image (build with: ./image_to_vm.sh --format=vmware_ova ...)")
    46  	cmdCreateBase.Flags().StringVar(&baseVMName, "name", "", "name of base VM")
    47  }
    48  
    49  func runBaseCreate(cmd *cobra.Command, args []string) error {
    50  	vmName := baseVMName
    51  	if vmName == "" {
    52  		ver, err := sdk.VersionsFromDir(filepath.Dir(ovaPath))
    53  		if err != nil {
    54  			fmt.Fprintf(os.Stderr, "Unable to get version from image directory, provide a -name flag or include a version.txt in the image directory: %v\n", err)
    55  			os.Exit(1)
    56  		}
    57  		vmName = ver.Version
    58  	}
    59  
    60  	err := API.CreateBaseDevice(vmName, ovaPath)
    61  	if err != nil {
    62  		fmt.Fprintf(os.Stderr, "Couldn't create base VM: %v\n", err)
    63  		os.Exit(1)
    64  	}
    65  	return nil
    66  }